You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2006/08/08 09:18:06 UTC
svn commit: r429606 [1/2] - in /jackrabbit/trunk/contrib/backup/src:
main/java/org/apache/jackrabbit/backup/ test/
test/java/org/apache/jackrabbit/backup/
Author: jukka
Date: Tue Aug 8 00:18:05 2006
New Revision: 429606
URL: http://svn.apache.org/viewvc?rev=429606&view=rev
Log:
JCR-442: Committed patch-060808-backup.txt from Nicolas.
Added:
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/ZipBackupIOHandler.java (with props)
Removed:
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/ZipFileBackupIOHandler.java
Modified:
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/AllWorkspacesBackup.java
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/Backup.java
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfig.java
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationBackup.java
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationParser.java
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupIOHandler.java
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupManager.java
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/LaunchBackup.java
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NamespaceBackup.java
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NodeTypeBackup.java
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NodeVersionHistoriesBackup.java
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/RepositoryBackup.java
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceBackup.java
jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceConfigBackup.java
jackrabbit/trunk/contrib/backup/src/test/backup.xml
jackrabbit/trunk/contrib/backup/src/test/java/org/apache/jackrabbit/backup/BackupTest.java
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/AllWorkspacesBackup.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/AllWorkspacesBackup.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/AllWorkspacesBackup.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/AllWorkspacesBackup.java Tue Aug 8 00:18:05 2006
@@ -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,25 +29,32 @@
import org.apache.jackrabbit.core.RepositoryImpl;
/**
- * @author ntoper
+ * This class allows the backup and restore of all the worskspaces.
*
*/
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.indexOf("export_") != -1 && 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);
+ }
+ }
}
}
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/Backup.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/Backup.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/Backup.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/Backup.java Tue Aug 8 00:18:05 2006
@@ -16,6 +16,7 @@
*/
package org.apache.jackrabbit.backup;
+import java.io.FileNotFoundException;
import java.io.IOException;
import javax.jcr.LoginException;
@@ -33,64 +34,120 @@
*/
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);
}
-
- public Backup() {
-
+
+ /**
+ * Used only by BackupManager. No attributes are initialized.
+ */
+ protected Backup() {
}
- public void init(RepositoryImpl repo, BackupConfig conf) throws LoginException, RepositoryException {
+ /**
+ * 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());
+ }
+ /**
+ * 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() {
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
* 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();
+ }
}
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfig.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfig.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfig.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfig.java Tue Aug 8 00:18:05 2006
@@ -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,83 +56,58 @@
* 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;
}
-
-
- //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 {
-
+ public BackupConfig(File path, Collection allResources, String myFile, String repoConfFile) 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() {
return allResources;
}
+ public void addResource(Backup b) {
+ this.allResources.add(b);
+ }
+
public File getWorkFolder() {
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;
- }
-
-
-
}
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationBackup.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationBackup.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationBackup.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationBackup.java Tue Aug 8 00:18:05 2006
@@ -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,34 +36,38 @@
/**
* @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)
*/
public void backup(BackupIOHandler h) throws RepositoryException,
IOException {
- File file = conf.getFile();
+ File file = this.getConf().getFile();
h.write("backup.xml", file);
}
/* (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);
}
-
}
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationParser.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationParser.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationParser.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupConfigurationParser.java Tue Aug 8 00:18:05 2006
@@ -26,15 +26,14 @@
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;
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,122 +43,116 @@
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
*/
public BackupConfigurationParser(Properties variables) {
super(variables);
}
-
-
+
/**
- * Parses backup? configuration. Backup configuration uses the
- * following format:
- * <p>
- * 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
* @param name name of the child element
- * @param required indicates if the child element is required
- * @return named children elements, or <code>null</code> if not found
+ * @return named children elements, or <code>null</code> 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++) {
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;
}
}
-
-
- /*
- * 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);
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.
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 <code>PersistenceManager</code> 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));
}
}
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupIOHandler.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupIOHandler.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupIOHandler.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupIOHandler.java Tue Aug 8 00:18:05 2006
@@ -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;
}
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupManager.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupManager.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/BackupManager.java Tue Aug 8 00:18:05 2006
@@ -27,39 +27,44 @@
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
*
*/
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.
* 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();
+ }
+ }
}
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/LaunchBackup.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/LaunchBackup.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/LaunchBackup.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/LaunchBackup.java Tue Aug 8 00:18:05 2006
@@ -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,41 +36,67 @@
* Date: 23-jun-06
*/
public class LaunchBackup {
+ /**
+ * TODO where can I find the generic repository.xml?
+ * Path to the generic repository.xml
+ */
+
+ 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;
- static BackupIOHandler h;
- RepositoryImpl repo;
- BackupConfig conf;
- RepositoryConfig repoConf;
- BackupManager backup;
-
-
/**
* 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,69 +110,67 @@
//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();
}
-
+
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,20 +178,20 @@
usage();
}
}
- finally
- {
- if (launch !=null)
+ finally {
+ if (launch != null) {
launch.shutdown();
+ }
}
}
-
+
/**
* 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,35 +199,84 @@
/**
* 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);
//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() {
- // TODO Auto-generated constructor stub
+ 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.
+ */
+
+ //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();
}
-
+
}
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NamespaceBackup.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NamespaceBackup.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NamespaceBackup.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NamespaceBackup.java Tue Aug 8 00:18:05 2006
@@ -16,18 +16,22 @@
*/
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;
import javax.jcr.LoginException;
+import javax.jcr.NamespaceException;
import javax.jcr.NamespaceRegistry;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
+import org.apache.jackrabbit.core.NamespaceRegistryImpl;
import org.apache.jackrabbit.core.RepositoryImpl;
@@ -41,52 +45,54 @@
*
*/
public class NamespaceBackup extends Backup implements Serializable {
-
+
/**
*
*/
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)
*
*/
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();
}
-
/* (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();
-
- 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);
+ Session s = this.getSession();
+ Workspace wsp = s.getWorkspace();
+ NamespaceRegistry ns = wsp.getNamespaceRegistry();
+
+ Namespaces myNs = new Namespaces();
+
+ String[] allPrefixes = ns.getPrefixes();
+
+ 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();
+ }
}
-
-
-
}
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NodeTypeBackup.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NodeTypeBackup.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NodeTypeBackup.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NodeTypeBackup.java Tue Aug 8 00:18:05 2006
@@ -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,56 +53,81 @@
* @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)
*/
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();
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.
*
* @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)
*/
- 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();
+ }
}
-
}
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NodeVersionHistoriesBackup.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NodeVersionHistoriesBackup.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NodeVersionHistoriesBackup.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/NodeVersionHistoriesBackup.java Tue Aug 8 00:18:05 2006
@@ -17,29 +17,60 @@
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
*
*/
public class NodeVersionHistoriesBackup extends Backup {
-
+
/**
* @param repo
* @param conf
* @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,15 +78,15 @@
super();
// TODO Auto-generated constructor stub
}
-
+
/* (non-Javadoc)
* @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());
+ }
+
+ }
}
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/RepositoryBackup.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/RepositoryBackup.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/RepositoryBackup.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/RepositoryBackup.java Tue Aug 8 00:18:05 2006
@@ -16,19 +16,35 @@
*/
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;
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,63 +52,65 @@
*/
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();
}
+ 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
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceBackup.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceBackup.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceBackup.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceBackup.java Tue Aug 8 00:18:05 2006
@@ -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,69 +55,107 @@
*
*/
public class WorkspaceBackup extends Backup {
-
- private static int called = 0;
+
private String wspName;
-
+
/**
* @param repo
* @param conf
* @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();
+ }
}
-
+
}
Modified: jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceConfigBackup.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceConfigBackup.java?rev=429606&r1=429605&r2=429606&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceConfigBackup.java (original)
+++ jackrabbit/trunk/contrib/backup/src/main/java/org/apache/jackrabbit/backup/WorkspaceConfigBackup.java Tue Aug 8 00:18:05 2006
@@ -18,16 +18,18 @@
import java.io.File;
import java.io.IOException;
+import java.util.zip.ZipException;
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,22 +37,21 @@
*/
public class WorkspaceConfigBackup extends Backup {
- private static int called = 0;
private String wspName;
-
+
/**
* @param repo
* @param conf
* @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,23 +64,23 @@
*/
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)
* @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);
}
-
}