Property changes on: /home/ntoper/workspace/backup ___________________________________________________________________ Name: svn:ignore - target + target repository *.settings *.classpath .project repository.xml dir_conflicts.prej myzip.zip Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/AllWorkspacesBackup.java =================================================================== --- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/AllWorkspacesBackup.java (revision 429495) +++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/AllWorkspacesBackup.java (working copy) @@ -17,6 +17,9 @@ package org.apache.jackrabbit.backup; import java.io.IOException; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipException; import javax.jcr.LoginException; import javax.jcr.RepositoryException; @@ -26,7 +29,7 @@ import org.apache.jackrabbit.core.RepositoryImpl; /** - * @author ntoper + * This class allows the backup and restore of all the worskspaces. * */ public class AllWorkspacesBackup extends Backup { @@ -32,19 +35,26 @@ public class AllWorkspacesBackup extends Backup { /** - * @param repo - * @param conf - * @throws RepositoryException - * @throws LoginException + * @param repo the repository + * @param conf the BackupConfig object holding all informations about the backup/restore operations + * @param login + * @param password + * @throws RepositoryException + * @throws LoginException */ - public AllWorkspacesBackup(RepositoryImpl repo, BackupConfig conf) throws LoginException, RepositoryException { - super(repo, conf); + public AllWorkspacesBackup(RepositoryImpl repo, BackupConfig conf, String login, String password) + throws LoginException, RepositoryException { + super(repo, conf, login, password); } - - public AllWorkspacesBackup() { + + /** + * Constructor used by BackupManager. + * + */ + protected AllWorkspacesBackup() { super(); } - + /* (non-Javadoc) * @see org.apache.jackrabbit.backup.Backup#backup(org.apache.jackrabbit.backup.BackupIOHandler) @@ -54,12 +64,13 @@ Session s = this.getSession(); Workspace wsp = s.getWorkspace(); String[] allWsp = wsp.getAccessibleWorkspaceNames(); - + String login = this.getCredentials().getUserID(); + String password = this.getCredentials().getPassword().toString(); + for (int i = 0; i < allWsp.length; i++) { - WorkspaceBackup wspb = new WorkspaceBackup(this.repo, this.conf, allWsp[i]); - wspb.backup(h); - - WorkspaceConfigBackup wspConfb = new WorkspaceConfigBackup(this.repo, this.conf, allWsp[i]); + WorkspaceBackup wspb = new WorkspaceBackup(this.getRepo(), this.getConf(), allWsp[i], login, password); + wspb.backup(h); + WorkspaceConfigBackup wspConfb = new WorkspaceConfigBackup(this.getRepo(), this.getConf(), allWsp[i], login, password); wspConfb.backup(h); } } @@ -67,9 +78,29 @@ /* (non-Javadoc) * @see org.apache.jackrabbit.backup.Backup#restore(org.apache.jackrabbit.backup.BackupIOHandler) */ - public void restore(BackupIOHandler h) { - // TODO Auto-generated method stub + public void restore(BackupIOHandler h) throws ZipException, IOException, LoginException, RepositoryException { + //Get All workspaces name in the zip + Enumeration entries = h.getEntries(); + while (entries.hasMoreElements()) { + String s = ((ZipEntry) entries.nextElement()).getName(); + if (s.contains("export_") && s.endsWith(".xml")) { + int begin = "export_".length(); + //Allow to manage is we backup 110 workspaces for instance + int end = s.length() - ".xml".length(); + String name = s.substring(begin, end); + String login = this.getCredentials().getUserID(); + String password = this.getCredentials().getPassword().toString(); + //No need to check if the config file is there: if not, we will throw an exception later. + //Restore the config + WorkspaceConfigBackup wspConfb = new WorkspaceConfigBackup(this.getRepo(), this.getConf(), name, login, password); + wspConfb.restore(h); + + //Restore the content + WorkspaceBackup wsb = new WorkspaceBackup(this.getRepo(), this.getConf(), name, login, password); + wsb.restore(h); + } + } } } Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/Backup.java =================================================================== --- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/Backup.java (revision 429495) +++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/Backup.java (working copy) @@ -16,6 +16,7 @@ */ package org.apache.jackrabbit.backup; +import java.io.FileNotFoundException; import java.io.IOException; import javax.jcr.LoginException; @@ -33,36 +34,58 @@ */ public abstract class Backup { - RepositoryImpl repo; - BackupConfig conf; - Session session; + private RepositoryImpl repo; + private BackupConfig conf; + private Session session;; + private SimpleCredentials credentials; /** - * + * @param login + * @param password * @param repo The repository to backup * @param conf The specific BackupConfig object (usually a subset of backup.xml) - * @param name Name of the resource to backup. Unique. Useful? - * @throws RepositoryException - * @throws LoginException + * @throws RepositoryException + * @throws LoginException */ - //TODO Useful? - public Backup(RepositoryImpl repo, BackupConfig conf) throws LoginException, RepositoryException { + public Backup(RepositoryImpl repo, BackupConfig conf, String login, String password) + throws LoginException, RepositoryException { this.repo = repo; this.conf = conf; - this.session = this.repo.login( - new SimpleCredentials(this.conf.getLogin(), this.conf.getPassword().toCharArray())); - + this.credentials = new SimpleCredentials(login, password.toCharArray()); + this.session = this.repo.login(this.credentials); + } + + /** + * Used only by BackupManager. No attributes are initialized. + */ + protected Backup() { } - public Backup() { - + /** + * This constructor is used explicitly for restore operations + * + * @param login + * @param password + */ + protected Backup(String login, String password) { + this.credentials = new SimpleCredentials(login, password.toCharArray()); } - - public void init(RepositoryImpl repo, BackupConfig conf) throws LoginException, RepositoryException { + /** + * Used by BackupManager with the empty constructor. + * + * @param repo + * @param conf + * @param login + * @param password + * @throws LoginException + * @throws RepositoryException + */ + + protected void init(RepositoryImpl repo, BackupConfig conf, String login, String password) throws LoginException, RepositoryException { this.repo = repo; this.conf = conf; - this.session = this.repo.login( - new SimpleCredentials(this.conf.getLogin(), this.conf.getPassword().toCharArray())); + this.credentials = new SimpleCredentials(login, password.toCharArray()); + this.session = this.repo.login(this.credentials); } public RepositoryImpl getRepo() { @@ -68,15 +91,17 @@ public RepositoryImpl getRepo() { return this.repo; } - + + protected void setRepo(RepositoryImpl repo) { + this.repo = repo; + } + /* * Each ResourceBackup is responsible to handle the backup. - * + * * We use file when we cannot assume anything on the size of the data or we know it's big. When * we know the data is small we store it in RAM. - * - * - * + * * For each resource * Test maxFileSize * Zip the whole workingFolder @@ -81,16 +106,48 @@ * Test maxFileSize * Zip the whole workingFolder * check the checksum - * Send it to out + * Send it to out */ + /** + * Backup the resource designated by this class to h from the current repository + * @param h + * @throws FileNotFoundException + * @throws RepositoryException + * @throws IOException + * + */ public abstract void backup(BackupIOHandler h) throws RepositoryException, IOException; - public abstract void restore(BackupIOHandler h); - public Session getSession() { + /** + * Restore the resource designated by this class from h to the current repository + * @param h + * @throws FileNotFoundException + * @throws RepositoryException + * @throws IOException + */ + public abstract void restore(BackupIOHandler h) throws FileNotFoundException, RepositoryException, IOException; + + protected Session getSession() { return this.session; } - //TODO call sesssion.logout or useless? - + protected BackupConfig getConf() { + return conf; + } + + protected void setConf(BackupConfig conf2) { + this.conf = conf2; + } + + protected SimpleCredentials getCredentials() { + return credentials; + } + + protected void setCredentials(SimpleCredentials cred) { + this.credentials = cred; + } + public void finalize() { + this.session.logout(); + } } Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfig.java =================================================================== --- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfig.java (revision 429495) +++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfig.java (working copy) @@ -1,9 +1,9 @@ /* * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with + * contributor license agreements. See the NOTICE backupFile distributed with * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with + * The ASF licenses this backupFile to You under the Apache License, Version 2.0 + * (the "License"); you may not use this backupFile except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 @@ -25,6 +25,7 @@ import org.apache.jackrabbit.core.config.ConfigurationException; import org.apache.jackrabbit.core.config.PersistenceManagerConfig; +import org.apache.jackrabbit.core.config.RepositoryConfig; import org.xml.sax.InputSource; @@ -37,17 +38,15 @@ * */ public class BackupConfig { - - //TODO Useful? - private PersistenceManagerConfig pmc; - //Tused to backup a workspace first in a file - private File workFolder; + + //used to backup a workspace first in a backupFile + private final File workFolder; + //Not final since BackupManager adds some resources private Collection allResources; - private File file; - private File repoConfFile; - private String login; - private String password; - + private final File backupFile; + private final File repoConfFile; + + /** * Parses the given repository configuration document and returns the * parsed and initialized repository configuration. The given repository @@ -57,47 +56,38 @@ * method also initializes the configuration (creates the configured * directories, etc.). The {@link RepositoryConfigurationParser} class should be * used directly to just parse the configuration. - * @param repoConfFile + * @param repoConfFile * - * @param xml repository configuration document - * @param home repository home directory + * @param myFile repository configuration document + * @param repoConfFile repository file configuration * @return repository configuration * @throws ConfigurationException on configuration errors - * @throws IllegalAccessException - * @throws InstantiationException - * @throws ClassNotFoundException - * @throws IOException + * @throws IllegalAccessException + * @throws InstantiationException + * @throws ClassNotFoundException + * @throws IOException */ - public static BackupConfig create(String myFile, String repoConfFile, String login, String password) + public static BackupConfig create(String myFile, String repoConfFile) throws ConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException { - URI uri = new File(myFile).toURI(); InputSource is = new InputSource(uri.toString()); - BackupConfigurationParser parser = new BackupConfigurationParser(new Properties()); - - BackupConfig config = parser.parseBackupConfig(is, myFile, repoConfFile, login, password); - + BackupConfig config = parser.parseBackupConfig(is, myFile, repoConfFile); return config; } - - + + public BackupConfig(File path, Collection allResources, String myFile, String repoConfFile) throws IOException { - //TODO see if path is really useful? - public BackupConfig(PersistenceManagerConfig pmc, File path, Collection allResources, String myFile, String repoConfFile, String login, String password) throws IOException { - //Logic application: not in the parser: this code has to be here if (!(path.isDirectory() && path.canWrite())) { - throw new IOException(); - } - - this.pmc = pmc; + //if path not set in the conf file then create one as the current dir + path = new File("."); + } + this.workFolder = path; this.allResources = allResources; - this.file = new File(myFile); + this.backupFile = new File(myFile); this.repoConfFile = new File(repoConfFile); - this.password = password; - this.login = login; } public Collection getAllResources() { @@ -104,6 +94,10 @@ return allResources; } + public void addResource(Backup b) { + this.allResources.add(b); + } + public File getWorkFolder() { return workFolder; } @@ -108,32 +102,12 @@ return workFolder; } - public PersistenceManagerConfig getPmc() { - return pmc; - } public File getFile() { - return this.file; + return this.backupFile; } - - public File getRepoConfFile() { return repoConfFile; } - - - - public String getPassword() { - return this.password; - } - - - - public String getLogin() { - return this.login; - } - - - } Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationBackup.java =================================================================== --- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationBackup.java (revision 429495) +++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationBackup.java (working copy) @@ -18,6 +18,7 @@ import java.io.File; import java.io.IOException; +import java.util.zip.ZipException; import javax.jcr.LoginException; import javax.jcr.RepositoryException; @@ -26,7 +27,7 @@ /** * Backup/Restore the XML file used to configure this backup. - * + * * @author ntoper * */ @@ -35,19 +36,21 @@ /** * @param repo * @param conf - * @throws RepositoryException - * @throws LoginException + * @param login + * @param password + * @throws RepositoryException + * @throws LoginException */ - public BackupConfigurationBackup(RepositoryImpl repo, BackupConfig conf) throws LoginException, RepositoryException { - super(repo, conf); - + public BackupConfigurationBackup(RepositoryImpl repo, BackupConfig conf, String login, String password) + throws LoginException, RepositoryException { + super(repo, conf, login, password); + } - - public BackupConfigurationBackup() { + + protected BackupConfigurationBackup() { super(); } - - + /* (non-Javadoc) * @see org.apache.jackrabbit.backup.Backup#backup(org.apache.jackrabbit.backup.BackupIOHandler) */ @@ -53,7 +56,7 @@ */ public void backup(BackupIOHandler h) throws RepositoryException, IOException { - File file = conf.getFile(); + File file = this.getConf().getFile(); h.write("backup.xml", file); } @@ -58,11 +61,13 @@ } /* (non-Javadoc) + * This method is quite special. It is used to restore content from scratch. To break cyclic reference, we restore the file + * in the current directory (we don't have yet the temporary one). + * * @see org.apache.jackrabbit.backup.Backup#restore(org.apache.jackrabbit.backup.BackupIOHandler) */ - public void restore(BackupIOHandler h) { - // TODO Auto-generated method stub - + public void restore(BackupIOHandler h) throws ZipException, IOException { + File conf = new File("backup.xml"); + h.read("backup.xml", conf); } - } Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationParser.java =================================================================== --- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationParser.java (revision 429495) +++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationParser.java (working copy) @@ -26,8 +26,6 @@ import org.apache.jackrabbit.core.config.ConfigurationException; import org.apache.jackrabbit.core.config.ConfigurationParser; -import org.apache.jackrabbit.core.config.PersistenceManagerConfig; -import org.apache.jackrabbit.core.config.RepositoryConfigurationParser; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -34,7 +32,8 @@ import org.xml.sax.InputSource; /** - * @author ntoper + * BackupConfigurationParser. Used to parse the Backup configuration XML file. + * Please look at the documentation for the format: http://wiki.apache.org/jackrabbit/BackupTool * */ public class BackupConfigurationParser extends ConfigurationParser { @@ -44,7 +43,10 @@ private static final String RESOURCES = "Resources"; private static final String RESOURCE = "Resource"; private static final String SAVING_CLASS = "savingClass"; - + //TODO Add parse to get the name if a specific wsp has to be backupped/restored. + //TODO Add UUID choice + + /** * @param variables */ @@ -51,48 +53,42 @@ public BackupConfigurationParser(Properties variables) { super(variables); } - - + /** - * Parses backup? configuration. Backup configuration uses the - * following format: - *
- * TODO comment. See wiki for format
+ * Parses backup/restore configuration file.
+ *
+ * Please look at the documentation for the format: http://wiki.apache.org/jackrabbit/BackupTool
+ *
* @param xml repository configuration document
- * @param myFile
- * @param repoConfFile
+ * @param myFile path and name of the XML configuration file (TODO delete XML argument and build it with myFile)
+ * @param repoConfFile: path and name of the repository configuration file
* @return repository configuration
* @throws ConfigurationException if the configuration is broken
- * @throws IllegalAccessException
- * @throws InstantiationException
- * @throws ClassNotFoundException
- * @throws IOException
+ * @throws IllegalAccessException
+ * @throws InstantiationException
+ * @throws ClassNotFoundException
+ * @throws IOException
* @see #parseBeanConfig(Element, String)
* @see #parseVersioningConfig(Element)
*/
- public BackupConfig parseBackupConfig(InputSource xml, String myFile, String repoConfFile, String login, String password)
+ public BackupConfig parseBackupConfig(InputSource xml, String myFile, String repoConfFile)
throws ConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
- //TODO refactor dependency between this method and BackupConfig
+
Element root = parseXML(xml);
-
+
//Working Folder
Element workingFolder = getElement(root, WORKING_FOLDER);
File path = new File(workingFolder.getAttribute(WORKING_FOLDER_ATTRIBUTE));
-
- //Persistence Manager
- PersistenceManagerConfig pmc = this.parsePersistenceManagerConfig(root);
-
- //Management of resources tag
+
+ //Management of resources tag
Element resources = this.getElement(root, RESOURCES);
- Collection allResources = this.parseResourcesConfig(resources);
-
- return new BackupConfig(pmc, path, allResources, myFile, repoConfFile, login, password);
+ Collection allResources = this.parseResourcesConfig(resources);
+
+ return new BackupConfig(path, allResources, myFile, repoConfFile);
}
-
+
/**
- * TODO: to put in ConfigurationParser?
- *
* Returns the named children of the given parent element.
*
* @param parent parent element
@@ -97,10 +93,9 @@
*
* @param parent parent element
* @param name name of the child element
- * @param required indicates if the child element is required
- * @return named children elements, or null if not found
+ * @return named children elements, or null if not found
*/
- protected List getElements(Element parent, String name) {
+ private List getElements(Element parent, String name) {
NodeList children = parent.getChildNodes();
Vector selected = new Vector(10, 10);
for (int i = 0; i < children.getLength(); i++) {
@@ -107,15 +102,14 @@
Node child = children.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE
&& name.equals(child.getNodeName())) {
-
+
selected.addElement((Element) child);
}
}
- if (selected.size() == 0){
+ if (selected.size() == 0){
return null;
}
- else
- {
+ else {
selected.trimToSize();
return selected;
}
@@ -120,19 +114,25 @@
return selected;
}
}
-
-
- /*
- * For now only support of all workspace backup. I think it is actually simpler to manage on the end-user side. Be careful the objects aren't usable yet
- *
+
+ /**
+ * For now only support of all workspace backup. I think it is actually simpler to manage on the end-user side.
+ * Be careful the objects aren't properly initialized yet. You need to call init (in BackupManager).
+ *
* Pre-condition: there are resource tags in the conf file (otherwise there is a problem in the backup)
+ * @root root Element of the XML
+ * @throws ConfigurationException
+ * @throws ClassNotFoundException
+ * @throws InstantiationException
+ * @throws IllegalAccessException
+ * @return Collection of all resources to backup found in the file
*/
- private Collection parseResourcesConfig(Element root) throws ConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException {
+ private Collection parseResourcesConfig(Element root)
+ throws ConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException {
/*
* For each resource
- * get class and instantiate
- * addResource to BackupManager
+ * get class and instantiate
*/
Vector objects = new Vector();
Vector resources = (Vector) this.getElements(root, RESOURCE);
@@ -137,7 +137,7 @@
Vector objects = new Vector();
Vector resources = (Vector) this.getElements(root, RESOURCE);
Iterator it = resources.iterator();
-
+
while (it.hasNext()) {
//We don't care about the name. It is here only for humans: only the savingClass is important
//Instantiate it and put it in the collection.
@@ -142,24 +142,17 @@
//We don't care about the name. It is here only for humans: only the savingClass is important
//Instantiate it and put it in the collection.
Element resource = (Element) it.next();
- String savingClass = resource.getAttribute(SAVING_CLASS);
+ String savingClass = resource.getAttribute(SAVING_CLASS);
+
+ //Check we are not backupping/restoring a resource already backuped by BackupManager
+ if (savingClass.equals("org.apache.jackrabbit.backup.RepositoryBackup") ||
+ savingClass.equals("org.apache.jackrabbit.backup.BackupConfigBackup")) {
+ throw new IllegalAccessException();
+ }
+
Class c = Class.forName(savingClass);
- objects.addElement( (Backup) c.newInstance());
+ objects.addElement( (Backup) c.newInstance());
}
return objects;
-
- }
-
- /**
- * Parses the PersistenceManager config.
- *
- * @param parent parent of the PersistenceManager element
- * @return persistence manager configuration
- * @throws ConfigurationException if the configuration is broken
- */
- protected PersistenceManagerConfig parsePersistenceManagerConfig(
- Element parent) throws ConfigurationException {
- return new PersistenceManagerConfig(
- parseBeanConfig(parent, RepositoryConfigurationParser.PERSISTENCE_MANAGER_ELEMENT));
}
}
Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupIOHandler.java
===================================================================
--- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupIOHandler.java (revision 429495)
+++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupIOHandler.java (working copy)
@@ -18,16 +18,20 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;;
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.zip.ZipException;
+/**
+ * Represent a backup/restore file. This is where
+ * the content should be sent to/fetched from.
+ *
+ */
public interface BackupIOHandler {
-
- //Add reference to the file
- // How to precise if in or out... Maybe not needed?
void close() throws IOException;
- void initBackup() throws FileNotFoundException, IOException;
- void initRestore() throws FileNotFoundException;
void write(String name, File f) throws IOException;
void write(String name, ByteArrayOutputStream fos) throws IOException;
+ byte[] read(String zipEntry) throws ZipException, IOException;
+ public void read(String zipEntry, File myFile) throws ZipException, IOException;
+ Enumeration getEntries() throws ZipException, IOException;
}
Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupManager.java
===================================================================
--- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupManager.java (revision 429495)
+++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/BackupManager.java (working copy)
@@ -27,13 +27,14 @@
import org.apache.jackrabbit.core.RepositoryImpl;
/**
- * This class manages the backup/restore process. It is responsible to transmit it to the BackupIOHandler and to add the repository to the
+ * This class manages the backup/restore process. It is responsible to send to/fetch from the BackupIOHandler and to add the repository to the
* BackupConfig.
- *
- * It extends Backup since it is based on the same semantics. However it is not at the same type as a ResourceBackup (indicated by different names)
- *
+ *
+ * It extends Backup since it is based on the same semantics. However it is not the same type as a ResourceBackup
+ * (the different semantics are indicated by different names)
+ *
* It uses a work folder to get first all backup/restore information, zip them and send them to the handler.
- *
+ *
* @author ntoper
*
*/
@@ -38,22 +39,27 @@
*
*/
public class BackupManager extends Backup {
-
- public BackupManager(RepositoryImpl repo, BackupConfig conf) throws LoginException, RepositoryException {
- super(repo, conf);
-
+
+
+ public BackupManager(RepositoryImpl repo, BackupConfig conf, String login, String password) throws LoginException, RepositoryException {
+ super(repo, conf, login , password);
+
+
//Initiate correctly all objects in allResources
- Iterator it = this.conf.getAllResources().iterator();
-
- while(it.hasNext()) {
+ Iterator it = this.getConf().getAllResources().iterator();
+ while ( it.hasNext() ) {
Backup b = (Backup) it.next();
- b.init(repo, conf);
+ b.init(repo, conf, login, password);
}
}
-
-
- public static BackupManager create(RepositoryImpl impl, BackupConfig conf2) throws LoginException, RepositoryException {
- return new BackupManager(impl, conf2);
+
+ public BackupManager() {
+ super();
+ }
+
+
+ public static BackupManager create(RepositoryImpl impl, BackupConfig conf2, String login, String password) throws LoginException, RepositoryException {
+ return new BackupManager(impl, conf2, login, password);
}
/**
* Used to backup the repository and all subclasses. Call all classes when needed.
@@ -59,7 +65,6 @@
* Used to backup the repository and all subclasses. Call all classes when needed.
* This class stores the backup config file also. (simplify its fetching and logical since it's not a configurable resource)
*
- * TODO visibility of the conf is huge: each ResourceBackup can get and set others resources modifiers. Is it really bad?
*
* @param The BackupIOHandler where the backup will be saved
* @throws RepositoryException
@@ -71,16 +76,16 @@
* It is responsible to initiate and close the zipFile.
* Each backup method, use the BackupIOHandler to write the file directly.
*/
-
- h.initBackup();
+
+ //We need to put those two Backup resources here for backup since they are handled differently
+ //for restore
+ this.addResource(new RepositoryBackup());
+ this.addResource(new BackupConfigurationBackup());
+
try {
-
-
- Collection resources = this.conf.getAllResources();
-
-
+ Collection resources = this.getConf().getAllResources();
Iterator it = resources.iterator();
-
+
while (it.hasNext()) {
Backup b = (Backup) it.next();
b.backup(h);
@@ -90,13 +95,51 @@
h.close();
}
}
-
- public void restore(BackupIOHandler h) {
- // TODO Auto-generated method stub
-
+ /**
+ * TODO commment
+ * @param backup
+ * @throws RepositoryException
+ * @throws LoginException
+ */
+ private void addResource(Backup backup) throws LoginException, RepositoryException {
+ String login = this.getCredentials().getUserID();
+ String password = this.getCredentials().getPassword().toString();
+ backup.init(this.getRepo(), this.getConf(), login, password);
+ this.getConf().addResource(backup);
}
-
-
+ /**
+ * Same method as backup but for restore.
+ * TODO Comment
+ * @throws IllegalAccessException
+ * @throws InstantiationException
+ * @throws ClassNotFoundException
+ *
+ */
+ public void restore(BackupIOHandler h) throws RepositoryException, IOException {
+ /*
+ * There is a dissimetry in the restore operation compared to the backup one.
+ * It is because of the need to first restore the repository and launch it where during the backup we can
+ * backup the repository and the configuration file the same way as the other.
+ *
+ * (to make repository + backup file mandatory, they are added automatically in BAckupManager)
+ *
+ *
+ * Ignore any repository or backupConfig restore orders...
+ *
+ */
+ try {
+ Collection resources = this.getConf().getAllResources();
+ Iterator it = resources.iterator();
+
+ while (it.hasNext()) {
+ Backup b = (Backup) it.next();
+ b.restore(h);
+ }
+ }
+ finally {
+ h.close();
+ }
+ }
}
Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/LaunchBackup.java
===================================================================
--- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/LaunchBackup.java (revision 429495)
+++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/LaunchBackup.java (working copy)
@@ -16,6 +16,7 @@
*/
package org.apache.jackrabbit.backup;
+import java.io.File;
import java.io.IOException;
import javax.jcr.AccessDeniedException;
@@ -23,6 +24,7 @@
import javax.jcr.RepositoryException;
import org.apache.jackrabbit.core.RepositoryImpl;
+import org.apache.jackrabbit.core.config.ConfigurationException;
import org.apache.jackrabbit.core.config.RepositoryConfig;
/**
@@ -34,14 +36,38 @@
* Date: 23-jun-06
*/
public class LaunchBackup {
+ /**
+ * TODO where can I find the generic repository.xml?
+ * Path to the generic repository.xml
+ */
- static BackupIOHandler h;
- RepositoryImpl repo;
- BackupConfig conf;
- RepositoryConfig repoConf;
- BackupManager backup;
-
-
+ private static final String REPOSITORY_XML = "/home/ntoper/workspace/backup/";
+
+ /**
+ * The backupIOHandler used to handle IO
+ */
+ private static BackupIOHandler h;
+
+ /**
+ * Used to get a reference to the repository.
+ */
+ private RepositoryImpl repo;
+
+ /**
+ * The backupconfig object.
+ */
+ private BackupConfig conf;
+
+ /**
+ * The repositoryConfig object.
+ */
+ private RepositoryConfig repoConf;
+
+ /**
+ * The backupManager is used to launch all backup/restore operations.
+ */
+ private BackupManager backup;
+
/**
* The command line tool.
@@ -46,29 +72,31 @@
/**
* The command line tool.
*
- * LaunchBackup --zip myzip.zip --size 2 --conf backup.xml --login nico --password mlypass backup repository.xml repository/
- * LaunchBackup --zip ./myzip.zip --size 2 --conf backup.xml --login nico --password restore repository.xml repository/
+ * LaunchBackup --zip myzip.zip --conf backup.xml --login nico --password mlypass backup repository.xml repository/
+ * LaunchBackup --zip ./myzip.zip --login nico --password p repository.xml restore
+ * LaunchBackup --zip ./myzip.zip -- conf restore.xml --login nico --password p restore repository.xml repository/
+ *
+ * If backup.xml for restore, no repository + backupConfig restore Only partial restore
*
* --zip: where is the zip file (only implemented way to backup for now)
- * --size in Go
- *
* --conf: path to the config file for the backup tool
- *
+ *
* backup/restore: whether you want a backup or a restore
- *
+ *
* repository.xml: path to the config file of the repository
- *
+ *
* repository/ is the name of the repository
- *
+ *
*
* --help for help option
- * @throws RepositoryException
- * @throws IOException
- * @throws IOException
- * @throws IllegalAccessException
- * @throws InstantiationException
- * @throws ClassNotFoundException
*
+ * @throws RepositoryException
+ * @throws IOException
+ * @throws IllegalAccessException
+ * @throws InstantiationException
+ * @throws ClassNotFoundException
+ * @throws AccessDeniedException
+ * @throws IOException
*/
public static void main(String[] args) throws RepositoryException, AccessDeniedException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
// I have to declare all var here so they are not resetted out of the for.
@@ -82,52 +110,43 @@
//2 booleans in case the user specified nothing
boolean isBackup = false;
boolean isRestore = false;
-
+
//Parse the command line.
for (int i = 0; i < args.length; i++) {
-
+
if ( args[i].equals("--help") || args.length == 0) {
usage();
}
-
- if (args[i].equals("--zip")){
+
+ if (args[i].equals("--zip")) {
zipFile = args[i + 1];
- //We put it here because later we might offer other possibilities than only zip
- LaunchBackup.h = new ZipFileBackupIOHandler(zipFile);
}
-
- if (args[i].equals("--conf")){
-
+
+ if (args[i].equals("--conf")) {
confFile = args[i + 1];
-
}
-
- if (args[i].equals("--login")){
-
+
+ if (args[i].equals("--login")) {
login = args[i + 1];
-
}
-
- if (args[i].equals("--password")){
-
+
+ if (args[i].equals("--password")) {
password = args[i + 1];
-
}
-
- if (args[i].equals("backup") && isRestore == false ){
+
+ if (args[i].equals("backup") && !isRestore) {
isBackup = true;
repoConfFile = args[i + 1];
home = args[i + 2];
-
}
-
- if (args[i].equals("restore") && isBackup == false ){
+
+ if (args[i].equals("restore") && !isBackup ) {
isRestore = true;
repoConfFile = args[i + 1];
home = args[i + 2];
- }
+ }
}
-
+
//Check if login and password are provided otherwise weird thing will happen
if (login == null || password == null) {
throw new LoginException();
@@ -132,19 +151,26 @@
if (login == null || password == null) {
throw new LoginException();
}
-
+
LaunchBackup launch = null;
-
+
//We need to shutdown properly the repository whatever happens
- try {
+ try {
//Launch backup
if (isBackup) {
- launch = new LaunchBackup(repoConfFile, home, confFile, login, password);
+ launch = new LaunchBackup(repoConfFile, home, confFile, login, password);
+ LaunchBackup.h = new ZipBackupIOHandler(zipFile, true);
launch.backup(h);
- }
+ }
//Launch restore
+ else if (isRestore && confFile == null) {
+ LaunchBackup.h = new ZipBackupIOHandler(zipFile, false);
+ launch = new LaunchBackup(repoConfFile, home, login, password);
+ launch.restore(h);
+ }
else if (isRestore) {
- launch = new LaunchBackup();
+ LaunchBackup.h = new ZipBackupIOHandler(zipFile, false);
+ launch = new LaunchBackup(repoConfFile, home, confFile, login, password);
launch.restore(h);
}
//Launch nothing (if nothing specified
@@ -152,14 +178,14 @@
usage();
}
}
- finally
- {
- if (launch !=null)
+ finally {
+ if (launch != null) {
launch.shutdown();
+ }
}
}
-
+
/**
* Auxiliary method for main
@@ -165,7 +191,7 @@
* Auxiliary method for main
*
*/
- private static void usage(){
+ private static void usage() {
System.out.println("todo: cut and paste of the comment when the project is over");
System.exit(0);
}
@@ -173,14 +199,16 @@
/**
* Constructor of LaunchBackup. Initiate the repository.
*
- * @param String filename: name of the configuration file
- * @throws RepositoryException
- * @throws IllegalAccessException
- * @throws InstantiationException
- * @throws ClassNotFoundException
- * @throws IOException
+ * @param String repoConfFile: name of the configuration file
+ * @throws RepositoryException
+ * @throws IllegalAccessException
+ * @throws InstantiationException
+ * @throws ClassNotFoundException
+ * @throws IOException
+ *
*/
public LaunchBackup(String repoConfFile, String home, String backupConfFile, String login, String password) throws RepositoryException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
+
//Launch first the repository
this.repoConf = RepositoryConfig.create(repoConfFile, home);
this.repo = RepositoryImpl.create(this.repoConf);
@@ -186,21 +214,68 @@
this.repo = RepositoryImpl.create(this.repoConf);
//Create the backupConfig object
- this.conf = BackupConfig.create(backupConfFile, repoConfFile, login, password);
- this.backup = BackupManager.create(this.repo, this.conf);
-
+ this.conf = BackupConfig.create(backupConfFile, repoConfFile);
+ this.backup = BackupManager.create(this.repo, this.conf, login, password);
}
-
+
/**
- * Used for restore operations only
+ * Used for restore operations only.
+ *
+ * This constructor restores the repository! I don't see any other options since to restore we
+ * need the repository and what is inside.
+ *
+ *
+ * @param password
+ * @param login
+ * @param home
+ * @throws RepositoryException
+ * @throws IOException
+ * @throws IllegalAccessException
+ * @throws InstantiationException
+ * @throws ClassNotFoundException
+ * @throws
*
*/
+ public LaunchBackup(String repoConfFile, String home, String login, String password) throws RepositoryException, InstantiationException, IllegalAccessException, IOException, ClassNotFoundException {
+ /*
+ * There is a dissymetry there (due to design constraint: we wanted to be close of JR's way of working).
+ * We need to restore BackupConfiguration and the Repository and we need each other to create them.
+ */
- public LaunchBackup() {
- // TODO Auto-generated constructor stub
- }
+ //Extract BackupConfig
+ BackupConfigurationBackup b = new BackupConfigurationBackup();
+ b.restore(h);
+ //RepoConfFile isn't the right one. We know it
+ BackupConfig bc;
+ try {
+ //We know we have restored it to backup.xml
+ //There is no other way, except to break the abstract class and create
+ //another restore methods. This seems fine and this way is unique/
+ // If we have the issue again, we will evolve the design.
+ bc = BackupConfig.create("backup.xml", repoConfFile);
+ } catch (ConfigurationException e) {
+ throw new RepositoryException();
+ } catch (ClassNotFoundException e) {
+ throw new RepositoryException();
+ } catch (InstantiationException e) {
+ throw new RepositoryException();
+ }
+ finally {
+ //We need to delete it anyway
+ File f = new File("backup.xml");
+ f.delete();
+ }
+ //Restore repository
+ RepositoryBackup br = new RepositoryBackup(repoConfFile, home);;
+ br.setConf(bc);
+ br.restore(h);
+ RepositoryImpl repo = br.getRepo();
+ this.backup = BackupManager.create(repo, bc, login, password);
+ this.repo = this.backup.getRepo();
+ this.conf = this.backup.getConf();
+ }
/**
* Backup a repository
@@ -215,13 +290,16 @@
*Restore a repository
*
* @param BackupIOHandler h a reference to the backup to restore
+ * @throws IOException
+ * @throws RepositoryException
+ * @throws IllegalAccessException
*/
- public void restore(BackupIOHandler h) {
+ public void restore(BackupIOHandler h) throws RepositoryException, IOException, IllegalAccessException {
this.backup.restore(h);
}
-
+
private void shutdown() {
- this.repo.shutdown();
+ this.repo.shutdown();
}
-
+
}
Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/NamespaceBackup.java
===================================================================
--- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/NamespaceBackup.java (revision 429495)
+++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/NamespaceBackup.java (working copy)
@@ -16,8 +16,10 @@
*/
package org.apache.jackrabbit.backup;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
@@ -23,6 +25,7 @@
import java.util.HashMap;
import javax.jcr.LoginException;
+import javax.jcr.NamespaceException;
import javax.jcr.NamespaceRegistry;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
@@ -28,6 +31,7 @@
import javax.jcr.Session;
import javax.jcr.Workspace;
+import org.apache.jackrabbit.core.NamespaceRegistryImpl;
import org.apache.jackrabbit.core.RepositoryImpl;
@@ -41,7 +45,7 @@
*
*/
public class NamespaceBackup extends Backup implements Serializable {
-
+
/**
*
*/
@@ -46,7 +50,7 @@
*
*/
private static final long serialVersionUID = 4703796138774238005L;
-
+
/**
* This class holds all namespaces in a serializable way. We only put the relevant information.
* (Do not change this class or you might lose backward compatibility; instead use another version)
@@ -53,35 +57,38 @@
*
*/
private class Namespaces implements Serializable {
-
+
private static final long serialVersionUID = 8384076353482950602L;
-
- HashMap h;
+ private HashMap h;
+ protected Namespaces() {
+ h = new HashMap();
+ }
- public Namespaces() {
- h = new HashMap();
+ protected void addNamespace(String uri, String prefix) {
+ h.put(uri, prefix);
}
-
- public void addNamespace(String prefix, String uri) {
- h.put(prefix, uri);
+
+ protected String[] getAllUri() {
+ String[] s = new String[1];
+ return (String[]) h.keySet().toArray(s);
}
+ public String getPrefix(String uri) {
+ return (String) h.get(uri);
+ }
}
- /**
+ /**
* @param repo
* @param conf
- * @throws RepositoryException
- * @throws LoginException
+ * @throws RepositoryException
+ * @throws LoginException
*/
- public NamespaceBackup(RepositoryImpl repo, BackupConfig conf) throws LoginException, RepositoryException {
- super(repo, conf);
-
-
-
+ public NamespaceBackup(RepositoryImpl repo, BackupConfig conf, String login, String password) throws LoginException, RepositoryException {
+ super(repo, conf, login, password);
}
-
+
public NamespaceBackup() {
super();
}
@@ -86,7 +93,6 @@
super();
}
-
/* (non-Javadoc)
* TODO where do I find the local ns?
* TODO use a ByteArrayOutputStream?
@@ -94,35 +100,49 @@
*/
public void backup(BackupIOHandler h) throws RepositoryException, IOException {
- Session s = this.getSession();
- Workspace wsp = s.getWorkspace();
- NamespaceRegistry ns = wsp.getNamespaceRegistry();
-
- Namespaces myNs = new Namespaces();
+ Session s = this.getSession();
+ Workspace wsp = s.getWorkspace();
+ NamespaceRegistry ns = wsp.getNamespaceRegistry();
+
+ Namespaces myNs = new Namespaces();
+
+ String[] allPrefixes = ns.getPrefixes();
- String[] allPrefixes = ns.getPrefixes();
-
- for (int i = 0; i < allPrefixes.length; i++) {
- String prefix = allPrefixes[i];
- myNs.addNamespace(prefix, ns.getURI(prefix));
- }
-
- String name = this.getClass().toString();
-
- ByteArrayOutputStream fos = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(myNs);
- h.write(name, fos);
+ for (int i = 0; i < allPrefixes.length; i++) {
+ String prefix = allPrefixes[i];
+ myNs.addNamespace(ns.getURI(prefix),prefix);
+ }
+
+ String name = this.getClass().toString();
+
+ ByteArrayOutputStream fos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(fos);
+ oos.writeObject(myNs);
+ h.write(name, fos);
}
-
+
/* (non-Javadoc)
* @see org.apache.jackrabbit.backup.Backup#restore(org.apache.jackrabbit.backup.BackupIOHandler)
*/
- public void restore(BackupIOHandler h) {
- // TODO Auto-generated method stub
-
+ public void restore(BackupIOHandler h) throws RepositoryException, IOException {
+ String name = this.getClass().toString();
+ byte[] ns = h.read(name);
+ ByteArrayInputStream bis = new ByteArrayInputStream(ns);
+ ObjectInputStream ois = new ObjectInputStream(bis);
+
+ try {
+ Namespaces allNs = (Namespaces) ois.readObject();
+ String[] allUri = allNs.getAllUri();
+
+ Session s = this.getSession();
+ Workspace wsp = s.getWorkspace();
+ NamespaceRegistryImpl nsr = (NamespaceRegistryImpl) wsp.getNamespaceRegistry();
+
+ for (int i = 0; i < allUri.length; i++) {
+ nsr.safeRegisterNamespace(allNs.getPrefix(allUri[i]), allUri[i]);
+ }
+ } catch (ClassNotFoundException e) {
+ throw new RepositoryException();
+ }
}
-
-
-
}
Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/NodeTypeBackup.java
===================================================================
--- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/NodeTypeBackup.java (revision 429495)
+++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/NodeTypeBackup.java (working copy)
@@ -16,8 +16,12 @@
*/
package org.apache.jackrabbit.backup;
+import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
+import java.util.zip.ZipException;
import javax.jcr.LoginException;
import javax.jcr.NamespaceRegistry;
@@ -27,11 +31,15 @@
import javax.jcr.nodetype.NoSuchNodeTypeException;
import org.apache.jackrabbit.core.RepositoryImpl;
+import org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException;
import org.apache.jackrabbit.core.nodetype.NodeTypeDef;
import org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl;
import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;
+import org.apache.jackrabbit.core.nodetype.xml.NodeTypeReader;
import org.apache.jackrabbit.core.nodetype.xml.NodeTypeWriter;
+import org.apache.jackrabbit.name.IllegalNameException;
import org.apache.jackrabbit.name.QName;
+import org.apache.jackrabbit.name.UnknownPrefixException;
/**
* @author ntoper
@@ -45,14 +53,13 @@
* @throws RepositoryException
* @throws LoginException
*/
- public NodeTypeBackup(RepositoryImpl repo, BackupConfig conf) throws LoginException, RepositoryException {
- super(repo, conf);
- }
-
+ public NodeTypeBackup(RepositoryImpl repo, BackupConfig conf, String login, String password) throws LoginException, RepositoryException {
+ super(repo, conf, login, password);
+ }
+
public NodeTypeBackup() {
super();
- }
-
+ }
/* (non-Javadoc)
* @see org.apache.jackrabbit.backup.Backup#backup(org.apache.jackrabbit.backup.BackupIOHandler)
@@ -58,10 +65,9 @@
* @see org.apache.jackrabbit.backup.Backup#backup(org.apache.jackrabbit.backup.BackupIOHandler)
*/
public void backup(BackupIOHandler h) throws IOException, RepositoryException {
- //Can we assume the default wsp always exist?
Session s = this.getSession();
Workspace wsp = s.getWorkspace();
-
+
NodeTypeManagerImpl ntm = (NodeTypeManagerImpl) wsp.getNodeTypeManager();
NodeTypeRegistry ntreg = ntm.getNodeTypeRegistry();
NamespaceRegistry ns = wsp.getNamespaceRegistry();
@@ -68,10 +74,9 @@
NodeTypeDef[] ntd = getRegisteredNodesTypesDefs(ntreg);
ByteArrayOutputStream out = new ByteArrayOutputStream();
NodeTypeWriter.write(out, ntd, ns);
- h.write("NodeType", out);
+ h.write("NodeType", out);
}
-
-
+
/**
* Returns the nodes types definitions of all registered node types.
*
@@ -76,18 +81,18 @@
* Returns the nodes types definitions of all registered node types.
*
* @return the node type definition of all registered node types.
- * @throws NoSuchNodeTypeException
+ * @throws NoSuchNodeTypeException
*/
private static NodeTypeDef[] getRegisteredNodesTypesDefs(NodeTypeRegistry ntreg) throws NoSuchNodeTypeException {
- QName[] qn = ntreg.getRegisteredNodeTypes();
- NodeTypeDef[] ntd = new NodeTypeDef[qn.length];
-
- for (int i=0; i < qn.length; i++) {
- ntd[i] = ntreg.getNodeTypeDef(qn[i]);
- }
- return ntd;
+ QName[] qn = ntreg.getRegisteredNodeTypes();
+ NodeTypeDef[] ntd = new NodeTypeDef[qn.length];
+
+ for (int i=0; i < qn.length; i++) {
+ ntd[i] = ntreg.getNodeTypeDef(qn[i]);
+ }
+
+ return ntd;
}
-
/* (non-Javadoc)
* @see org.apache.jackrabbit.backup.Backup#restore(org.apache.jackrabbit.backup.BackupIOHandler)
@@ -92,9 +97,37 @@
/* (non-Javadoc)
* @see org.apache.jackrabbit.backup.Backup#restore(org.apache.jackrabbit.backup.BackupIOHandler)
*/
- public void restore(BackupIOHandler h) {
- // TODO Auto-generated method stub
+ public void restore(BackupIOHandler h) throws ZipException, IOException, RepositoryException {
+
+ File f = new File(this.getConf().getWorkFolder() + "Node.xml");
+ try {
+ h.read("NodeType", f);
+ FileInputStream is = new FileInputStream(f);
+ NodeTypeReader r = new NodeTypeReader(is);
+ NodeTypeDef[] ntds = r.getNodeTypeDefs();
+ Session s = this.getSession();
+ Workspace wsp = s.getWorkspace();
+
+ NodeTypeManagerImpl ntm = (NodeTypeManagerImpl) wsp.getNodeTypeManager();
+ NodeTypeRegistry ntreg = ntm.getNodeTypeRegistry();
+
+ for (int i = 0; i < ntds.length; i++) {
+ if (!ntreg.isRegistered(ntds[i].getName())) {
+ ntreg.registerNodeType(ntds[i]);
+ }
+ }
+ NamespaceRegistry ns = wsp.getNamespaceRegistry();
+
+ } catch (IllegalNameException e) {
+ new RepositoryException();
+ } catch (UnknownPrefixException e) {
+ new RepositoryException();
+ } catch (InvalidNodeTypeDefException e) {
+ new RepositoryException();
+ }
+ finally {
+ f.delete();
+ }
}
-
}
Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/NodeVersionHistoriesBackup.java
===================================================================
--- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/NodeVersionHistoriesBackup.java (revision 429495)
+++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/NodeVersionHistoriesBackup.java (working copy)
@@ -17,14 +17,45 @@
package org.apache.jackrabbit.backup;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.ZipException;
+import javax.jcr.AccessDeniedException;
+import javax.jcr.InvalidSerializedDataException;
+import javax.jcr.ItemExistsException;
import javax.jcr.LoginException;
+import javax.jcr.NodeIterator;
+import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.nodetype.NodeDefinition;
+import javax.jcr.version.VersionException;
+import org.apache.jackrabbit.core.ItemImpl;
+import org.apache.jackrabbit.core.ItemManager;
+import org.apache.jackrabbit.core.NodeImpl;
import org.apache.jackrabbit.core.RepositoryImpl;
+import org.apache.jackrabbit.core.SessionImpl;
+import org.apache.jackrabbit.core.nodetype.NodeDef;
+import org.apache.jackrabbit.core.nodetype.NodeDefImpl;
+import org.apache.jackrabbit.core.nodetype.NodeDefinitionImpl;
+import org.apache.jackrabbit.core.nodetype.NodeTypeDef;
+import org.apache.jackrabbit.core.version.VersionManager;
+import org.apache.jackrabbit.core.version.VersionManagerImpl;
+import org.apache.jackrabbit.core.xml.ImportHandler;
+import org.apache.jackrabbit.core.xml.SessionImporter;
+import org.apache.jackrabbit.name.MalformedPathException;
+import org.apache.jackrabbit.name.Path;
+import org.apache.jackrabbit.name.PathFormat;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
/**
* @author ntoper
@@ -31,7 +62,7 @@
*
*/
public class NodeVersionHistoriesBackup extends Backup {
-
+
/**
* @param repo
* @param conf
@@ -38,8 +69,8 @@
* @throws RepositoryException
* @throws LoginException
*/
- public NodeVersionHistoriesBackup(RepositoryImpl repo, BackupConfig conf) throws LoginException, RepositoryException {
- super(repo, conf);
+ public NodeVersionHistoriesBackup(RepositoryImpl repo, BackupConfig conf, String login, String password) throws LoginException, RepositoryException {
+ super(repo, conf, login, password);
// TODO Auto-generated constructor stub
}
@@ -47,7 +78,7 @@
super();
// TODO Auto-generated constructor stub
}
-
+
/* (non-Javadoc)
* @see org.apache.jackrabbit.backup.Backup#backup(org.apache.jackrabbit.backup.BackupIOHandler)
*/
@@ -52,10 +83,10 @@
* @see org.apache.jackrabbit.backup.Backup#backup(org.apache.jackrabbit.backup.BackupIOHandler)
*/
public void backup(BackupIOHandler h) throws RepositoryException,
- IOException {
+ IOException {
Session s = this.getSession();
- File temp = new File(this.conf.getWorkFolder() + "history.xml");
+ File temp = new File(this.getConf().getWorkFolder() + "history.xml");
try {
FileOutputStream out = new FileOutputStream(temp);
@@ -65,15 +96,208 @@
finally {
temp.delete();
}
- }
-
+ }
+
/* (non-Javadoc)
* @see org.apache.jackrabbit.backup.Backup#restore(org.apache.jackrabbit.backup.BackupIOHandler)
*/
- public void restore(BackupIOHandler h) {
+ /* public void restore(BackupIOHandler h) throws ZipException, IOException, RepositoryException {
+ SessionImpl s = (SessionImpl) this.getSession();
+ VersionManagerImpl versionMgr = (VersionManager) s.getVersionManager();
+ File temp = new File(this.getConf().getWorkFolder() + "history.xml");
+ try {
+ h.read("history.xml", temp);
+ FileInputStream in = new FileInputStream(temp);
+
+ // this.getRepo().importXML("/jcr:system/jcr:versionStorage", in, 0 );
+ }
+ finally {
+ temp.delete();
+ }
+
+
+
+ public void importXML(String parentAbsPath, InputStream in, int uuidBehavior) throws RepositoryException, IOException {
+
+ Path p = VersionManagerImpl.getVersionStoragePath();
+ SessionImpl s = (SessionImpl) this.getSystemSession("default");
+
+ //this.getItem()
+ ItemImpl item = s.getItemManager().getItem(p);
+ NodeImpl parent = (NodeImpl) item;
+ SessionImporter importer = new SessionImporter(parent, s, 3);
+ ImportHandler handler = new ImportHandler(importer, s.getNamespaceResolver(), this.getNamespaceRegistry());
+
+ try {
+ XMLReader parser =
+ XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
+ parser.setContentHandler(handler);
+ parser.setErrorHandler(handler);
+ // being paranoid...
+ parser.setFeature("http://xml.org/sax/features/namespaces", true);
+ parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
+ false);
+
+ parser.parse(new InputSource(in));
+ } catch (SAXException se) {
+ // check for wrapped repository exception
+ Exception e = se.getException();
+ if (e != null && e instanceof RepositoryException) {
+ throw (RepositoryException) e;
+ } else {
+ String msg = "failed to parse XML stream";
+ throw new InvalidSerializedDataException(msg, se);
+ }
+ }
+
+ }
+ }
+
+ SessionImpl s = (SessionImpl) this.getSession();
+ Path p;
+ try {
+ p = PathFormat.parse("/jcr:system/jcr:versionStorage", s.getNamespaceResolver()).getNormalizedPath();
+ } catch (MalformedPathException e) {
+ //Shouldn't happen or bug in the source code
+ throw new RepositoryException();
+ }
+ ItemImpl item = s.getItemManager().getItem(p);
+ NodeImpl parent = (NodeImpl) item;
+
+ //TODO Add a parameter to specify the UUIDBehavior?
+ SessionImporter importer = new SessionImporter(parent, s, 3);
+ ImportHandler handler = new ImportHandler(importer, s.getNamespaceResolver(), this.getRepo().getNamespaceRegistry());
+ File temp = new File(this.getConf().getWorkFolder() + "history.xml");
+ h.read("history.xml", temp);
+ FileInputStream in = new FileInputStream(temp);
+
+ try {
+ XMLReader parser =
+ XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
+ parser.setContentHandler(handler);
+ parser.setErrorHandler(handler);
+ // being paranoid...
+ parser.setFeature("http://xml.org/sax/features/namespaces", true);
+ parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
+ false);
+
+ parser.parse(new InputSource(in));
+ } catch (SAXException se) {
+ // check for wrapped repository exception
+ Exception e = se.getException();
+ if (e != null && e instanceof RepositoryException) {
+ throw (RepositoryException) e;
+ } else {
+ String msg = "failed to parse XML stream";
+ throw new InvalidSerializedDataException(msg, se);
+ }
+ } finally {
+ temp.delete();
+ }
+
//TODO find a way to put /jcr:system/jcr:versionStorage probably by instanciating as a repo/wsp the versioning pm
+ File temp = new File(this.getConf().getWorkFolder() + "history.xml");
+ SessionImpl s = (SessionImpl) this.getSession();
+
+
+ Path p = null;
+ try {
+ p = PathFormat.parse("/jcr:system/jcr:versionStorage", s.getNamespaceResolver()).getNormalizedPath();
+ } catch (MalformedPathException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ //Unprotect the tree...
+
+ ItemImpl item = s.getItemManager().getItem(p);
+
+ NodeImpl parent = (NodeImpl) item;
+ unprotect(parent);
+
+
+
+
+ try {
+
+ h.read("history.xml", temp);
+ FileInputStream iTemp = new FileInputStream(temp);
+ s.importXML("/", iTemp, 0);
+ } catch (PathNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (ItemExistsException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (ConstraintViolationException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (VersionException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (InvalidSerializedDataException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (LockException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+
+ }
+ finally {
+ temp.delete();
+ }
+
+
+
+
+ }*/
+ public void restore(BackupIOHandler h) throws RepositoryException, IOException {
+ File temp = new File(this.getConf().getWorkFolder() + "history.xml");
+ SessionImpl s = (SessionImpl) this.getSession();
+
+ Path p = null;
+ try {
+ p = PathFormat.parse("/jcr:system/jcr:versionStorage", s.getNamespaceResolver()).getNormalizedPath();
+ } catch (MalformedPathException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ //Unprotect the tree...
+
+ ItemImpl item = s.getItemManager().getItem(p);
+ NodeImpl parent = (NodeImpl) item;
+ unprotect(parent);
+ try {
+ h.read("history.xml", temp);
+ FileInputStream iTemp = new FileInputStream(temp);
+ s.importXML("/", iTemp, 0);
+
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+
+ }
+ finally {
+ temp.delete();
+ }
}
+
+
+ private static void unprotect(NodeImpl parent) throws RepositoryException {
+ NodeDefinitionImpl def = (NodeDefinitionImpl) parent.getDefinition();
+ NodeDefImpl nd = (NodeDefImpl) def.unwrap();
+ //TODO After restore should we W protect the node?
+ nd.setProtected(false);
+ if (!def.isProtected())
+ System.out.println(def.getName());
+
+ NodeIterator it = parent.getNodes();
+
+ while (it.hasNext()) {
+ unprotect((NodeImpl) it.nextNode());
+ }
+
+ }
}
Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/RepositoryBackup.java
===================================================================
--- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/RepositoryBackup.java (revision 429495)
+++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/RepositoryBackup.java (working copy)
@@ -16,13 +16,21 @@
*/
package org.apache.jackrabbit.backup;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
+import java.util.HashMap;
import java.util.Properties;
+import java.util.zip.ZipException;
import javax.jcr.LoginException;
+import javax.jcr.NamespaceRegistry;
import javax.jcr.RepositoryException;
@@ -29,6 +37,14 @@
import org.apache.jackrabbit.core.NodeId;
import org.apache.jackrabbit.core.NodeImpl;
import org.apache.jackrabbit.core.RepositoryImpl;
+import org.apache.jackrabbit.core.config.RepositoryConfig;
+import org.apache.jackrabbit.core.fs.BasedFileSystem;
+import org.apache.jackrabbit.core.fs.FileSystem;
+import org.apache.jackrabbit.core.fs.FileSystemException;
+import org.apache.jackrabbit.core.fs.FileSystemResource;
+import org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException;
+import org.apache.jackrabbit.core.nodetype.NodeTypeDefStore;
+import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;
/**
* @author ntoper
@@ -36,17 +52,19 @@
*/
public class RepositoryBackup extends Backup {
-
+ private String repoConfFile;
+ private String home;
+
/**
* @param repo
* @param conf
- * @throws RepositoryException
- * @throws LoginException
+ * @throws RepositoryException
+ * @throws LoginException
*/
- public RepositoryBackup(RepositoryImpl repo, BackupConfig conf) throws LoginException, RepositoryException {
- super(repo, conf);
+ public RepositoryBackup(RepositoryImpl repo, BackupConfig conf, String login, String password) throws LoginException, RepositoryException {
+ super(repo, conf, login, password);
}
-
+
public RepositoryBackup() {
super();
}
@@ -51,48 +69,48 @@
super();
}
+ public RepositoryBackup(String repoConfFile, String home) {
+ super();
+ this.repoConfFile = repoConfFile;
+ this.home = home;
+ }
/**
* Backup the repository config file
- *
+ *
* TODO Backup properties? Metadata store? Other ressources?
- * @throws IOException
- * @throws RepositoryException
- *
- *
+ * @throws IOException
+ * @throws RepositoryException
+ *
+ *
*/
public void backup(BackupIOHandler h) throws IOException, RepositoryException {
-
- File file = this.conf.getRepoConfFile();
+
+ File file = this.getConf().getRepoConfFile();
//Backup repository.xml
h.write("repository_xml", file);
-
- //Properties
- Properties p = new Properties();
- String[] keys = repo.getDescriptorKeys();
- for (int i = 0; i < keys.length; i++) {
- p.setProperty(keys[i], repo.getDescriptor(keys[i]));
- }
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- p.store(bos,"");
- h.write("repository_properties", bos);
-
- // Root node ID
- NodeImpl nod = (NodeImpl) this.getSession().getRootNode();
- NodeId n = nod.getNodeId();
-
- //We persist the string as a serialized object to avoid compatibility issue
- String s = n.toString();
- ByteArrayOutputStream fos = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(s);
- h.write("repository_rootNode", fos);
}
- public void restore(BackupIOHandler h) {
- // TODO Auto-generated method stub
-
+ public void restore(BackupIOHandler h) throws ZipException, IOException, RepositoryException {
+
+ //Restore repository.xml
+ File f = new File(this.repoConfFile);
+ h.read("repository_xml", f);
+
+ // Launch the repository and launch it.
+ RepositoryConfig repoConf = RepositoryConfig.create(this.repoConfFile, this.home);
+ this.setRepo(RepositoryImpl.create(repoConf));
+
+
+// this.getRepo().setNodeTypeRegistry(createNodeTypeRegistry(nsReg, new BasedFileSystem(this.getRepo().getStore()), "/nodetypes"));
+ /*
+ * 1. Create a NodeTypeRegistry specific for the restore (redefines only the load built in types path)
+ * 2. Update the NodeTypeRegistry in repo
+ */
+
+
}
-}
+
+}
\ No newline at end of file
Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceBackup.java
===================================================================
--- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceBackup.java (revision 429495)
+++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceBackup.java (working copy)
@@ -17,15 +17,24 @@
package org.apache.jackrabbit.backup;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.util.zip.ZipException;
-import javax.jcr.Item;
+import javax.jcr.InvalidSerializedDataException;
+import javax.jcr.ItemExistsException;
import javax.jcr.LoginException;
+import javax.jcr.NoSuchWorkspaceException;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
+import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
+import javax.jcr.Workspace;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.version.VersionException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerException;
import javax.xml.transform.sax.SAXTransformerFactory;
@@ -34,7 +43,10 @@
import org.apache.jackrabbit.core.RepositoryImpl;
import org.apache.jackrabbit.core.SessionImpl;
+import org.apache.jackrabbit.core.WorkspaceImpl;
import org.apache.jackrabbit.core.xml.SysViewSAXEventGenerator;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
//TODO Wiki doc to update
@@ -43,10 +55,9 @@
*
*/
public class WorkspaceBackup extends Backup {
-
- private static int called = 0;
+
private String wspName;
-
+
/**
* @param repo
* @param conf
@@ -53,59 +64,98 @@
* @throws RepositoryException
* @throws LoginException
*/
- public WorkspaceBackup(RepositoryImpl repo, BackupConfig conf, String name) throws LoginException, RepositoryException {
- super(repo, conf);
+ public WorkspaceBackup(RepositoryImpl repo, BackupConfig conf, String name, String login, String password) throws LoginException, RepositoryException {
+ super(repo, conf, login, password);
this.wspName = name;
}
- public void init(RepositoryImpl repo, BackupConfig conf, String name) throws LoginException, RepositoryException {
- super.init(repo, conf);
+ public void init(RepositoryImpl repo, BackupConfig conf, String name, String login, String password) throws LoginException, RepositoryException {
+ super.init(repo, conf, login, password);
this.wspName = name;
}
-
+
/* (non-Javadoc)
- * @see org.apache.jackrabbit.backup.Backup#backup(org.apache.jackrabbit.backup.BackupIOHandler)
+ * @see org.apache.jackrabbijcr:root/t.backup.Backup#backup(org.apache.jackrabbit.backup.BackupIOHandler)
*/
public void backup(BackupIOHandler h) throws RepositoryException,
- IOException {
- SessionImpl s = (SessionImpl) repo.login(new SimpleCredentials(this.conf.getLogin(), this.conf.getPassword().toCharArray()), this.wspName);
-
- SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
- File temp = new File(this.conf.getWorkFolder() + "wsp.xml");
- try {
- TransformerHandler th = stf.newTransformerHandler();
- th.setResult(new StreamResult(new FileOutputStream(temp)));
- th.getTransformer().setParameter(OutputKeys.METHOD, "xml");
- th.getTransformer().setParameter(OutputKeys.ENCODING, "UTF-8");
- th.getTransformer().setParameter(OutputKeys.INDENT, "no");
-
- new SysViewSAXEventGenerator(
- s.getRootNode(), false, false, th) {
- protected void process(Node node, int level)
- throws RepositoryException, SAXException {
- if (!"/jcr:system".equals(node.getPath())) {
- super.process(node, level);
- }
- }
- }.serialize();
- h.write("export"+ called +".xml", temp);
- } catch (TransformerException te) {
- throw new RepositoryException(te);
- } catch (SAXException se) {
- throw new RepositoryException(se);
- } finally {
- temp.delete();
- called += 1;
- }
-
+ IOException {
+ SessionImpl s = (SessionImpl) this.getRepo().login(this.getCredentials(), this.wspName);
+
+ SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
+ File temp = new File(this.getConf().getWorkFolder() + "wsp.xml");
+ try {
+ TransformerHandler th = stf.newTransformerHandler();
+ th.setResult(new StreamResult(new FileOutputStream(temp)));
+ th.getTransformer().setParameter(OutputKeys.METHOD, "xml");
+ th.getTransformer().setParameter(OutputKeys.ENCODING, "UTF-8");
+ th.getTransformer().setParameter(OutputKeys.INDENT, "no");
+
+ new SysViewSAXEventGenerator(
+ s.getRootNode(), false, false, th) {
+ protected void process(Node node, int level)
+ throws RepositoryException, SAXException {
+ if (!"/jcr:system".equals(node.getPath())) {
+ super.process(node, level);
+ }
+ }
+ }.serialize();
+ h.write("export_"+ this.wspName +".xml", temp);
+ } catch (TransformerException te) {
+ throw new RepositoryException(te);
+ } catch (SAXException se) {
+ throw new RepositoryException(se);
+ } finally {
+ temp.delete();
+ }
+
}
-
+
/* (non-Javadoc)
* @see org.apache.jackrabbit.backup.Backup#restore(org.apache.jackrabbit.backup.BackupIOHandler)
*/
- public void restore(BackupIOHandler h) {
- // TODO Auto-generated method stub
+ public void restore(BackupIOHandler h) throws ZipException, IOException, LoginException, NoSuchWorkspaceException, RepositoryException {
+ //TODO put temp and constant in object's attribute.
+
+ //Restore the SysView in a temp file
+ File wspXml = new File(this.getConf().getWorkFolder() + "/workspace.xml");
+ File temp = new File(this.getConf().getWorkFolder() + "wsp.xml");
+
+ try {
+ FileInputStream fis = new FileInputStream(wspXml);
+ InputSource xml = new InputSource(fis);
+
+ //Launch & register the wsp
+ //There is at least the default wsp.
+ SessionImpl s1 = (SessionImpl) this.getSession();
+ Workspace wsp_def = s1.getWorkspace();
+
+ //Check if the workspace already exist (UC: partial restore)
+ String[] allWsp = wsp_def.getAccessibleWorkspaceNames();
+ boolean isCreated = false;
+
+ for (int i = 0; i < allWsp.length; i++) {
+ if (this.wspName.equals(allWsp[i])) {
+ isCreated = true;
+ break;
+ }
+ }
- }
+ if (!isCreated) {
+ ((WorkspaceImpl) wsp_def).createWorkspace(this.wspName, xml);
+ }
+
+ h.read("export_"+ this.wspName +".xml", temp);
+
+ SessionImpl s2 = (SessionImpl) this.getRepo().login(this.getCredentials(), this.wspName);
+ FileInputStream iTemp = new FileInputStream(temp);
+ //TODO add a parameter in the conf file to manage UUIDBehavior
+ s2.importXML(s2.getRootNode().getPath(), iTemp, 3);
+ }
+ finally {
+ wspXml.delete();
+ temp.delete();
+ }
+ }
+
}
Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceConfigBackup.java
===================================================================
--- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceConfigBackup.java (revision 429495)
+++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceConfigBackup.java (working copy)
@@ -18,6 +18,7 @@
import java.io.File;
import java.io.IOException;
+import java.util.zip.ZipException;
import javax.jcr.LoginException;
import javax.jcr.RepositoryException;
@@ -22,12 +23,13 @@
import javax.jcr.LoginException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
-import javax.jcr.SimpleCredentials;
+
import org.apache.jackrabbit.core.RepositoryImpl;
import org.apache.jackrabbit.core.WorkspaceImpl;
+import org.apache.jackrabbit.core.config.ConfigurationException;
+import org.apache.jackrabbit.core.config.RepositoryConfig;
import org.apache.jackrabbit.core.config.WorkspaceConfig;
-import org.apache.jackrabbit.backup.Backup;
/**
* @author ntoper
@@ -35,9 +37,8 @@
*/
public class WorkspaceConfigBackup extends Backup {
- private static int called = 0;
private String wspName;
-
+
/**
* @param repo
* @param conf
@@ -44,13 +45,13 @@
* @throws RepositoryException
* @throws LoginException
*/
- public WorkspaceConfigBackup(RepositoryImpl repo, BackupConfig conf, String name) throws LoginException, RepositoryException {
- super(repo, conf);
+ public WorkspaceConfigBackup(RepositoryImpl repo, BackupConfig conf, String name, String login, String password) throws LoginException, RepositoryException {
+ super(repo, conf, login, password);
this.wspName = name;
}
- public void init(RepositoryImpl repo, BackupConfig conf, String name) throws LoginException, RepositoryException {
- super.init(repo, conf);
+ public void init(RepositoryImpl repo, BackupConfig conf, String name, String login, String password) throws LoginException, RepositoryException {
+ super.init(repo, conf, login, password);
this.wspName = name;
}
@@ -63,15 +64,14 @@
*/
public void backup(BackupIOHandler h) throws RepositoryException,
IOException {
- Session s = repo.login(new SimpleCredentials(this.conf.getLogin(), this.conf.getPassword().toCharArray()), this.wspName);
-
+ Session s = this.getRepo().login(this.getCredentials(), this.wspName);
+
WorkspaceImpl wsp = (WorkspaceImpl) s.getWorkspace();
WorkspaceConfig c = wsp.getConfig();
-
+
String home = c.getHomeDir();
File wspXml = new File (home + "/workspace.xml");
- h.write("WspConf" + called , wspXml);
- called += 1;
+ h.write("WspConf_" + this.wspName , wspXml);
}
/* (non-Javadoc)
@@ -77,9 +77,10 @@
/* (non-Javadoc)
* @see org.apache.jackrabbit.backup.Backup#restore(org.apache.jackrabbit.backup.BackupIOHandler)
*/
- public void restore(BackupIOHandler h) {
- // TODO Auto-generated method stub
+ public void restore(BackupIOHandler h) throws ConfigurationException, ZipException, IOException {
+ //Replace workspace.xml if needed
+ File wspXml = new File(this.getConf().getWorkFolder() + "/workspace.xml");
+ h.read("WspConf_" + this.wspName, wspXml);
}
-
}
Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/ZipBackupIOHandler.java
===================================================================
--- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/ZipBackupIOHandler.java (revision 0)
+++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/ZipBackupIOHandler.java (revision 0)
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.backup;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Enumeration;
+import java.util.zip.CRC32;
+import java.util.zip.CheckedInputStream;
+import java.util.zip.Checksum;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipException;
+import java.util.zip.ZipFile;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
+
+/**
+ * Question: ZipFile
+ *
+ * @author ntoper
+ *
+ */
+public class ZipBackupIOHandler implements BackupIOHandler {
+
+ private static int BUFFER_SIZE = 1024;
+
+ private File zip;
+ private FileOutputStream fout;
+ private ZipOutputStream zipOut;
+
+ private FileInputStream fin;
+ private ZipInputStream zipIn;
+
+ private boolean isBackup = false;
+
+
+ public ZipBackupIOHandler(String zipFile, boolean isBackup) throws IOException {
+ this.zip = new File(zipFile);
+ this.isBackup = isBackup;
+
+ if (isBackup) {
+ this.fout = new FileOutputStream(this.zip);
+ this.zipOut = new ZipOutputStream(this.fout);
+ }
+ else {
+ this.fin = new FileInputStream(this.zip);
+ this.zipIn = new ZipInputStream(this.fin);
+ }
+ }
+
+ public void close() throws IOException {
+ if (isBackup) {
+ zipOut.finish();
+ zipOut.close();
+ }
+ else {
+ zipIn.close();
+ }
+ }
+
+ /**
+ * Create a directory per resources
+ * Backup the resource and zip it
+ * @param string
+ * @param content
+ */
+ public void write(String name, File f) throws IOException {
+ zipOut.flush();
+ ZipEntry e = new ZipEntry(name);
+ zipOut.putNextEntry(e);
+
+ Checksum crc = new CRC32();
+ CheckedInputStream i = new CheckedInputStream(new FileInputStream(f), crc);
+
+ byte[] buffer = new byte[BUFFER_SIZE];
+ int len; while ( (len = i.read(buffer, 0, BUFFER_SIZE)) != -1) {
+ zipOut.write(buffer, 0, len);
+ }
+
+ //Checksum management
+ // TODO Is crc up to date? To be checked...
+ long check = crc.getValue();
+ e.setCrc(check);
+ zipOut.closeEntry();
+ }
+
+ /**
+ *
+ * TODO: refactor this method with the one upper.
+ *
+ *
+ * Used for small I/O operations (no NIO used there). Take a file and zip it.
+ *
+ * Most I/O operations are operated on RAM.
+ *
+ */
+ public void write(String name, ByteArrayOutputStream fos) throws IOException {
+ zipOut.flush();
+ ZipEntry e = new ZipEntry(name);
+ zipOut.putNextEntry(e);
+
+ Checksum crc = new CRC32();
+
+ InputStream io = new ByteArrayInputStream(fos.toByteArray());
+
+ CheckedInputStream i = new CheckedInputStream(io, crc);
+
+ byte[] buffer = new byte[BUFFER_SIZE];
+ int len;
+ while ( (len = i.read(buffer, 0, BUFFER_SIZE)) != -1) {
+ zipOut.write(buffer, 0, len);
+ }
+
+ //Checksum management
+ // TODO Is crc up to date? To be checked...
+ long check = crc.getValue();
+ e.setCrc(check);
+ zipOut.closeEntry();
+ }
+
+ public byte[] read(String zipEntry) throws ZipException, IOException {
+ ZipFile zf = new ZipFile(this.zip);
+ ZipEntry ze = new ZipEntry(zipEntry);
+ long crc1 = ze.getCrc();
+
+ Checksum chkCrc2 = new CRC32();
+ InputStream is = zf.getInputStream(ze);
+ CheckedInputStream cis = new CheckedInputStream(is, chkCrc2);
+
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+
+ byte[] buffer = new byte[BUFFER_SIZE];
+ int len;
+ while ( (len = cis.read(buffer, 0, BUFFER_SIZE)) != -1) {
+ os.write(buffer, 0, len);
+ }
+ //TODO check CRC
+ /* if (crc1 == chkCrc2.getValue()) {*/
+ return os.toByteArray();
+/* }
+ else {
+ throw new ZipException();
+ }*/
+ }
+
+ //TODO Refactor: the two upper are the same!! + quite similar to Backup
+ public void read(String zipEntry, File myFile) throws ZipException, IOException {
+ ZipFile zf = new ZipFile(this.zip);
+ ZipEntry ze = new ZipEntry(zipEntry);
+ //TODO check CRC
+
+ Checksum chkCrc2 = new CRC32();
+ InputStream is = zf.getInputStream(ze);
+ CheckedInputStream cis = new CheckedInputStream(is, chkCrc2);
+
+ OutputStream os = new FileOutputStream(myFile);
+
+
+ byte[] buffer = new byte[BUFFER_SIZE];
+ int len;
+ while ( (len = cis.read(buffer, 0, BUFFER_SIZE)) != -1) {
+ os.write(buffer, 0, len);
+ }
+
+ /* if (!(crc1 == chkCrc2.getValue())) {
+ System.out.println("crc1:" + crc1 + "a crc2:"+ chkCrc2.getValue() );
+ throw new ZipException();
+ }*/
+ }
+
+ public Enumeration getEntries() throws ZipException, IOException {
+ ZipFile zf = new ZipFile(this.zip);
+ return zf.entries();
+ }
+}
Index: /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/ZipFileBackupIOHandler.java
===================================================================
--- /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/ZipFileBackupIOHandler.java (revision 429495)
+++ /home/ntoper/workspace/backup/src/main/java/org/apache/jackrabbit/backup/ZipFileBackupIOHandler.java (working copy)
@@ -1,161 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.jackrabbit.backup;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.reflect.Array;
-import java.util.zip.CRC32;
-import java.util.zip.CheckedInputStream;
-import java.util.zip.Checksum;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipOutputStream;
-
-/**
- * Question: ZipFile
- *
- * @author ntoper
- *
- */
-public class ZipFileBackupIOHandler implements BackupIOHandler {
-
- private static int BUFFER_SIZE = 1024;
-
- private File zip;
- // private FileInputStream fin;
- // private ByteBuffer buffer;
- private FileOutputStream fout;
- private ZipOutputStream zipOut;
-
-
- public ZipFileBackupIOHandler(String zipFile) throws FileNotFoundException {
- this.zip = new File(zipFile);
- // this.buffer = ByteBuffer.allocateDirect(2048);
- }
-
- public void close() throws IOException {
- zipOut.finish();
- zipOut.close();
- }
-
- /// private void init() {
- //Useful?
- // this.buffer.clear();
- //}
-
- public void initBackup() throws IOException {
- boolean a = this.zip.createNewFile();
-
- if (!a) {
- throw new IOException();
- }
-
- this.fout = new FileOutputStream(this.zip);
- this.zipOut = new ZipOutputStream(this.fout);
- }
-
- public void initRestore() throws FileNotFoundException {
- // this.fin = new FileInputStream(this.zip);
- // this.fcin = this.fin.getChannel();
- //Restore zipFile
- }
-
- /**
- * Create a directory per resources
- * Backup the resource and zip it
- * @param string
- * @param content
- */
- /*private void writeFile(String string, String content) {
- File conf = new File();
- FileWriter fw = new FileWriter(cheminAbstraitSortie);
- BufferedWriter tamponEcriture = new BufferedWriter(fluxEcritureTexte);
- tamponEcriture.write(xml);
- tamponEcriture.flush();
- tamponEcriture.close();
-
- } */
-
- public void read() {
- }
-
-
- public void write(String name, File f) throws IOException {
- zipOut.flush();
- ZipEntry e = new ZipEntry(name);
- zipOut.putNextEntry(e);
-
- Checksum crc = new CRC32();
- CheckedInputStream i = new CheckedInputStream(new FileInputStream(f), crc);
-
- byte[] buffer = new byte[BUFFER_SIZE];
-
- int len;
- while ( (len = i.read(buffer, 0, BUFFER_SIZE)) != -1) {
- zipOut.write(buffer,0, len);
- }
-
- //Checksum management
- // TODO Is crc up to date? To be checked...
- long check = crc.getValue();
- e.setCrc(check);
- zipOut.closeEntry();
- }
-
-
- /**
- *
- * TODO: refactor this method with the one upper.
- *
- *
- * Used for small I/O operations (no NIO used there). Take a file and zip it.
- *
- * Most I/O operations are operated on RAM.
- *
- */
- public void write(String name, ByteArrayOutputStream fos) throws IOException {
- zipOut.flush();
- ZipEntry e = new ZipEntry(name);
- zipOut.putNextEntry(e);
-
- Checksum crc = new CRC32();
-
- InputStream io = new ByteArrayInputStream(fos.toByteArray());
-
- CheckedInputStream i = new CheckedInputStream(io, crc);
-
- byte[] buffer = new byte[BUFFER_SIZE];
- int len;
- while ( (len = i.read(buffer, 0, BUFFER_SIZE)) != -1) {
- zipOut.write(buffer,0, len);
- }
-
- //Checksum management
- // TODO Is crc up to date? To be checked...
- long check = crc.getValue();
- e.setCrc(check);
- zipOut.closeEntry();
- }
-
-
-}
Index: /home/ntoper/workspace/backup/src/test/backup.xml
===================================================================
--- /home/ntoper/workspace/backup/src/test/backup.xml (revision 429495)
+++ /home/ntoper/workspace/backup/src/test/backup.xml (working copy)
@@ -1,16 +1,12 @@