Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2003-2010 eXo Platform SAS.
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Affero General Public License
   * as published by the Free Software Foundation; either version 3
   * of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, see<http://www.gnu.org/licenses/>.
  */
 package org.exoplatform.services.jcr.ext.backup;
 
 import  org.jibx.runtime.BindingDirectory;
 import  org.jibx.runtime.IBindingFactory;
 import  org.jibx.runtime.IMarshallingContext;
 import  org.jibx.runtime.IUnmarshallingContext;
 import  org.jibx.runtime.JiBXException;
 
 import java.io.File;
 import java.util.List;
 
Created by The eXo Platform SAS.
Date: 2010

Author(s):
Alex Reshetnyak
Version:
$Id$
 
 {
    private class LogWriter
    {
 
       protected Log logger = ExoLogger.getLogger("exo.jcr.component.ext.LogWriter");
 
       private File logFile;
 
       XMLStreamWriter writer;
 
       {
          this. = file;
 
          try
          {
              = SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<XMLStreamWriter>()
             {
                public XMLStreamWriter run() throws Exception
                {
                   return XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream(),
                            .);
                }
             });
          }
          catch (PrivilegedActionException pae)
          {
             Throwable cause = pae.getCause();
             if (cause instanceof FileNotFoundException)
             {
                throw (FileNotFoundException)cause;
             }
             else if (cause instanceof XMLStreamException)
             {
               throw (XMLStreamException)cause;
            }
            else if (cause instanceof FactoryConfigurationError)
            {
               throw (FactoryConfigurationError)cause;
            }
            else if (cause instanceof RuntimeException)
            {
               throw (RuntimeException)cause;
            }
            else
            {
               throw new RuntimeException(cause);
            }
         };
         .writeStartDocument();
         .writeStartElement("repository-backup-chain-log");
         .writeStartElement("version-log");
         .writeEndElement();
         .writeStartElement("start-time");
         .writeCharacters(JCRDateFormat.format());
         .writeEndElement();
         .flush();
      }
      public void writeSystemWorkspaceName(String wsNamethrows XMLStreamException
      {
         .writeStartElement("system-workspace");
         .writeCharacters(wsName);
         .writeEndElement();
         .flush();
      }
      public void writeBackupsPath(List<StringwsLogFilePathListRepositoryBackupConfig config)
               throws XMLStreamException,
               IOException
      {
         .writeStartElement("workspaces-backup-info");
         for (String path : wsLogFilePathList)
         {
            .writeStartElement("url");
            .writeCharacters(RepositoryChainLogPathHelper.getRelativePath(path, PrivilegedFileHelper.getCanonicalPath(config
                     .getBackupDir())));
            .writeEndElement();
         }
         .writeEndElement();
         .flush();
      }
      public synchronized void write(RepositoryBackupConfig configString fullBackupType,
               String incrementalBackupTypeFile serviceBackupDir)
               throws XMLStreamExceptionIOException
      {
         .writeStartElement("repository-backup-config");
         .writeStartElement("backup-type");
         .writeCharacters(String.valueOf(config.getBackupType()));
         .writeEndElement();
         .writeStartElement("full-backup-type");
         .writeCharacters(fullBackupType);
         .writeEndElement();
         .writeStartElement("incremental-backup-type");
         .writeCharacters(incrementalBackupType);
         .writeEndElement();
         if (config.getBackupDir() != null)
         {
            String backupDir = PrivilegedFileHelper.getCanonicalPath(config.getBackupDir());
            String serviceBackupDirPath = PrivilegedFileHelper.getCanonicalPath(serviceBackupDir);
            if (backupDir.startsWith(serviceBackupDirPath))
            {
               backupDir = "." + backupDir.replace(serviceBackupDirPath"");
               if (..equals("\\"))
               {
                  backupDir = backupDir.replaceAll("\\\\""/");
               }
            }
            
            .writeStartElement("backup-dir");
            .writeCharacters(backupDir);
            .writeEndElement();
         }
         if (config.getRepository() != null)
         {
            .writeStartElement("repository");
            .writeCharacters(config.getRepository());
            .writeEndElement();
         }
         .writeStartElement("incremental-job-period");
         .writeCharacters(Long.toString(config.getIncrementalJobPeriod()));
         .writeEndElement();
         .writeStartElement("incremental-job-number");
         .writeCharacters(Integer.toString(config.getIncrementalJobNumber()));
         .writeEndElement();
         .writeEndElement();
         .flush();
      }
      public synchronized void writeEndLog()
      {
         try
         {
            .writeStartElement("finish-time");
            .writeCharacters(JCRDateFormat.format());
            .writeEndElement();
            .writeEndElement();
            .writeEndDocument();
            .flush();
         }
         catch (XMLStreamException e)
         {
            .error("Can't write end log"e);
         }
      }
      public synchronized void writeRepositoryEntry(RepositoryEntry rEntry,
               RepositoryServiceConfiguration serviceConfigurationthrows XMLStreamExceptionIOException,
               JiBXException
      {
         File config =
                  new File(PrivilegedFileHelper.getCanonicalPath(RepositoryBackupChainLog.this..getBackupDir())
                     + . + "original-repository-config.xml");
         PrivilegedFileHelper.createNewFile(config);
         OutputStream saveStream = PrivilegedFileHelper.fileOutputStream(config);
      
         ArrayList<RepositoryEntryrepositoryEntries = new ArrayList<RepositoryEntry>();
         repositoryEntries.add(rEntry);
      
         RepositoryServiceConfiguration newRepositoryServiceConfiguration =
                  new RepositoryServiceConfiguration(serviceConfiguration.getDefaultRepositoryName(), repositoryEntries);
      
         IBindingFactory bfact;
         try
         {
            bfact = SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<IBindingFactory>()
            {
               public IBindingFactory run() throws Exception
               {
                  return BindingDirectory.getFactory(RepositoryServiceConfiguration.class);
               }
            });
         }
         catch (PrivilegedActionException pae)
         {
            Throwable cause = pae.getCause();
            if (cause instanceof JiBXException)
            {
               throw (JiBXException) cause;
            }
            else if (cause instanceof RuntimeException)
            {
               throw (RuntimeExceptioncause;
            }
            else
            {
               throw new RuntimeException(cause);
            }
         }
         IMarshallingContext mctx = bfact.createMarshallingContext();
      
         mctx.marshalDocument(newRepositoryServiceConfiguration"ISO-8859-1"nullsaveStream);
         saveStream.close();
      
         .writeStartElement("original-repository-config");
         .writeCharacters(config.getName());
         .writeEndElement();
      }
   }
   private class LogReader
   {
      protected Log logger = ExoLogger.getLogger("exo.jcr.component.ext.LogReader");
      private File logFile;
      private XMLStreamReader reader;
      private String version;
      {
         this. = logFile;
          =
                  XMLInputFactory.newInstance().createXMLStreamReader(PrivilegedFileHelper.fileInputStream(logFile),
                           .);
      }
      public void readLogFile() throws UnsupportedEncodingExceptionException
      {
         boolean endDocument = false;
         while (!endDocument)
         {
            int eventCode = .next();
            switch (eventCode)
            {
               case . :
                  String name = .getLocalName();
                  if (name.equals("repository-backup-config"))
                      = readBackupConfig();
                  if (name.equals("system-workspace"))
                      = readContent();
                  if (name.equals("backup-config"))
                      = readBackupConfig();
                  if (name.equals("workspaces-backup-info"))
                      = readWorkspaceBackupInfo();
                  if (name.equals("start-time"))
                      = JCRDateFormat.parse(readContent());
                  if (name.equals("finish-time"))
                      = JCRDateFormat.parse(readContent());
                  if (name.equals("original-repository-config"))
                      = readRepositoryEntry();
                  if (name.equals("version-log"))
                  {
                     this. = readContent();
                  }
                  break;
               case . :
                  endDocument = true;
                  break;
            }
         }
      }
      {
         String configName = readContent();
         File configFile =
                  new File(PrivilegedFileHelper.getCanonicalPath(getBackupConfig().getBackupDir()) + .
                           + configName);
         if (!PrivilegedFileHelper.exists(configFile))
         {
            throw new RepositoryRestoreExeption("The backup set is not contains original repository configuration : "
                     + PrivilegedFileHelper.getCanonicalPath(getBackupConfig().getBackupDir()));
         }
         IBindingFactory factory;
         try
         {
            factory = SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<IBindingFactory>()
            {
               public IBindingFactory run() throws Exception
               {
                  return BindingDirectory.getFactory(RepositoryServiceConfiguration.class);
               }
            });
         }
         catch (PrivilegedActionException pae)
         {
            Throwable cause = pae.getCause();
            if (cause instanceof JiBXException)
            {
               throw (JiBXException)cause;
            }
            else if (cause instanceof RuntimeException)
            {
               throw (RuntimeException)cause;
            }
            else
            {
               throw new RuntimeException(cause);
            }
         }
         IUnmarshallingContext uctx = factory.createUnmarshallingContext();
         RepositoryServiceConfiguration conf =
                  (RepositoryServiceConfigurationuctx.unmarshalDocument(PrivilegedFileHelper
                           .fileInputStream(configFile), null);
         if (conf.getRepositoryConfigurations().size() != 1)
         {
            throw new RepositoryRestoreExeption(
                     "The oririginal configuration should be contains only one repository entry :"
                     + PrivilegedFileHelper.getCanonicalPath(configFile));
         }
                  getBackupConfig().getRepository()))
         {
            throw new RepositoryRestoreExeption(
                     "The oririginal configuration should be contains only one repository entry with name \""
                              + getBackupConfig().getRepository() + "\" :"
                              + PrivilegedFileHelper.getCanonicalPath(configFile));
         }
         
         
         return conf.getRepositoryConfiguration(getBackupConfig().getRepository());
      }
      {
         List<StringwsBackupInfo = new ArrayList<String>();
         boolean endWorkspaceBackupInfo = false;
         while (!endWorkspaceBackupInfo)
         {
            int eventCode = .next();
            switch (eventCode)
            {
               case . :
                  String name = .getLocalName();
                  if (name.equals("url"))
                  {
                     if ( != null && .equals())
                     {
                        String path = readContent();
                        wsBackupInfo.add(RepositoryChainLogPathHelper.getPath(path, PrivilegedFileHelper.getCanonicalPath(
                                 .getBackupDir())));
                     }
                     else
                     {
                        wsBackupInfo.add(readContent());
                     }
                  }
                  break;
               case . :
                  String tagName = .getLocalName();
                  if (tagName.equals("workspaces-backup-info"))
                     endWorkspaceBackupInfo = true;
                  break;
            }
         }
         return wsBackupInfo;
      }
      {
         BackupConfig conf = new BackupConfig();
         boolean endBackupConfig = false;
         while (!endBackupConfig)
         {
            int eventCode = .next();
            switch (eventCode)
            {
               case . :
                  String name = .getLocalName();
                  if (name.equals("backup-dir"))
                  {
                     if ( != null && .equals())
                     {
                        String dir = readContent();
                        if (dir.equals("."))
                        {
                           String path = PrivilegedFileHelper.getCanonicalPath(.getParentFile());
                           conf.setBackupDir(new File(path));
                        }
                        else if (dir.startsWith("./"))
                        {
                           String path = PrivilegedFileHelper.getCanonicalPath(.getParentFile());
                           dir = dir.replace("./""/");
                           
                           if (..equals("\\"))
                           {
                              dir = dir.replaceAll("/""\\\\");
                           }
                           conf.setBackupDir(new File(path + dir));
                        }
                        else
                        {
                           conf.setBackupDir(new File(Deserializer.resolveVariables(dir)));
                        }
                     }
                     else
                     {
                        conf.setBackupDir(new File(readContent()));
                     }
                  }
                  if (name.equals("backup-type"))
                     conf.setBackupType(Integer.valueOf(readContent()));
                  if (name.equals("repository"))
                     conf.setRepository(readContent());
                  if (name.equals("workspace"))
                     conf.setWorkspace(readContent());
                  if (name.equals("incremental-job-period"))
                     conf.setIncrementalJobPeriod(Long.valueOf(readContent()).longValue());
                  if (name.equals("incremental-job-number"))
                     conf.setIncrementalJobNumber(Integer.valueOf(readContent()).intValue());
                  if (name.equals("full-backup-type"))
                      = readContent();
                  if (name.equals("incremental-backup-type"))
                      = readContent();
                  break;
               case . :
                  String tagName = .getLocalName();
                  if (tagName.equals("repository-backup-config"))
                     endBackupConfig = true;
                  break;
            }
         }
         return conf;
      }
      private String readContent() throws XMLStreamException
      {
         String content = null;
         int eventCode = .next();
         if (eventCode == .)
            content = .getText();
         return content;
      }
      public String getVersionLog()
      {
         return ;
      }
   }
   protected static Log logger = ExoLogger.getLogger("exo.jcr.component.ext.BackupChainLog");

   
Start for 1.1 version log will be stored relative paths.
   protected static String VERSION_LOG_1_1 = "1.1";
   public static final String PREFIX = "repository-backup-";
   private static final String SUFFIX = ".xml";
   private File log;
   private LogWriter logWriter;
   private LogReader logReader;
   private String backupId;
   private Calendar startedTime;
   private Calendar finishedTime;
   private boolean finalized;
   private String workspaceSystem;
   private String fullBackupType;
   private final String versionLog;

   

Parameters:
logDirectory
config
systemWorkspace
wsLogFilePathList
backupId
startTime
rEntry
Throws:
BackupOperationException
   public RepositoryBackupChainLog(File logDirectoryRepositoryBackupConfig configString fullBackupType,
            String incrementalBackupTypeString systemWorkspaceList<StringwsLogFilePathListString backupId,
            Calendar startTimeRepositoryEntry rEntryRepositoryServiceConfiguration repositoryServiceConfiguration)
            throws BackupOperationException
   {
      try
      {
         this. = false;
         this. = ;
         this. =
                  new File(PrivilegedFileHelper.getCanonicalPath(logDirectory) + .
                           + ( + backupId + ));
         PrivilegedFileHelper.createNewFile(this.);
         this. = backupId;
         this. = config;
         this. = Calendar.getInstance();
         this. = fullBackupType;
         this. = incrementalBackupType;
         this. = rEntry;
          = new LogWriter();
         .write(configfullBackupTypeincrementalBackupTypelogDirectory);
         .writeSystemWorkspaceName(systemWorkspace);
         .writeBackupsPath(wsLogFilePathListconfig);
         .writeRepositoryEntry(rEntryrepositoryServiceConfiguration);
         this. = wsLogFilePathList;
         this. = systemWorkspace;
      }
      catch (IOException e)
      {
         throw new BackupOperationException("Can not create backup log ..."e);
      }
      catch (XMLStreamException e)
      {
         throw new BackupOperationException("Can not create backup log ..."e);
      }
      catch (FactoryConfigurationError e)
      {
         throw new BackupOperationException("Can not create backup log ..."e);
      }
      catch (JiBXException e)
      {
         throw new BackupOperationException("Can not create backup log ..."e);
      }
   }

   

Parameters:
log
Throws:
BackupOperationException
   {
      this. = log;
      this. = log.getName().replaceAll("").replaceAll("");
      try
      {
          = new LogReader(log);
         .readLogFile();
         this. = .getVersionLog();
      }
      catch (FileNotFoundException e)
      {
         throw new BackupOperationException("Can not read RepositoryBackupChainLog from file :"
                  + PrivilegedFileHelper.getAbsolutePath(log), e);
      }
      catch (XMLStreamException e)
      {
         throw new BackupOperationException("Can not read RepositoryBackupChainLog from file :"
                  + PrivilegedFileHelper.getAbsolutePath(log), e);
      }
      catch (UnsupportedEncodingException e)
      {
         throw new BackupOperationException("Can not read RepositoryBackupChainLog from file :"
                  + PrivilegedFileHelper.getAbsolutePath(log), e);
      }
      catch (Exception e)
      {
         throw new BackupOperationException("Can not read RepositoryBackupChainLog from file :"
                  + PrivilegedFileHelper.getAbsolutePath(log), e);
      }
   }

   
Getting log file path.

Returns:
String return the path to backup log
   public String getLogFilePath()
   {
      return PrivilegedFileHelper.getAbsolutePath();
   }

   
Getting repository backup configuration.

Returns:
ReposiotoryBackupConfig return the repository backup configuration
   {
      return ;
   }

   
Getting the started time.

Returns:
Calendar return the started time
   public Calendar getStartedTime()
   {
      return ;
   }

   
Getting the finished time.

Returns:
Calendar return the finished time
   public Calendar getFinishedTime()
   {
      return ;
   }
   public boolean isFinilized()
   {
      return ;
   }

   
Finalize log.
   public synchronized void endLog()
   {
      if (!)
      {
          = Calendar.getInstance();
          = true;
         .writeEndLog();
         //copy backup chain log file in into Backupset files itself for portability (e.g. on another server)
         try
         {
            InputStream in = PrivilegedFileHelper.fileInputStream();
            File dest = new File(.getBackupDir() + . + .getName());
            if (!PrivilegedFileHelper.exists(dest))
            {
               OutputStream out = PrivilegedFileHelper.fileOutputStream(dest);
               byte[] buf = new byte[(int) (PrivilegedFileHelper.length())];
               in.read(buf);
               String sConfig = new String(buf.);
               sConfig = sConfig.replaceAll("<backup-dir>.+</backup-dir>""<backup-dir>.</backup-dir>");
               out.write(sConfig.getBytes(.));
               in.close();
               out.close();
            }
         }
         catch (PatternSyntaxException e)
         {
            .error("Can't write log"e);
         }
         catch (FileNotFoundException e)
         {
            .error("Can't write log"e);
         }
         catch (IOException e)
         {
            .error("Can't write log"e);
         }
      }
   }

   
Getting the system workspace name.

Returns:
String return the system workspace name.
   public String getSystemWorkspace()
   {
      return ;
   }

   
Getting the workspace backups info.

Returns:
Collection return the list with path to backups.
   {
      return ;
   }

   
Getting the backup id.

Returns:
int return the backup id
   public String getBackupId()
   {
      return ;
   }

   
Getting original repository configuration

Returns:
RepositoryEntry return the original repository configuration
   {
      return ;
   }
New to GrepCode? Check out our FAQ X