Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 eXo Platform SAS.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software 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
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.exoplatform.services.jcr.ext.initializer;
 
 
 import java.io.File;
 
Created by The eXo Platform SAS.
Date: 16.03.2009

Author(s):
Alex Reshetnyak
Version:
$Id: RemoteWorkspaceInitializerService.java 111 2008-11-11 11:11:11Z rainf0x $
 
 
 @Path("/jcr-remote-workspace-initializer/")
 @Produces("text/plain")
 {

   
The template for ip-address in configuration.
 
    private static final String IP_ADRESS_TEMPLATE = "[$]bind-ip-address";

   
Definition the constants to ReplicationTestService.
 
    public static final class Constants
    {
      
The base path to this service.
 
       public static final String BASE_URL = "/rest/jcr-remote-workspace-initializer";

      
Definition the operation types.
 
       public static final class OperationType
       {
         
Full backup only operation.
 
          public static final String GET_WORKSPACE = "getWorkspaceData";

         
OperationType constructor.
 
          private OperationType()
          {
          }
       }

      
Constants constructor.
      private Constants()
      {
      }
   }

   
The apache logger.
   private static Log log = ExoLogger.getLogger("exo.jcr.component.ext.RemoteWorkspaceInitializationService");

   
The repository service.
   private final RepositoryService repositoryService;

   
The backup manager.
   private final BackupManager backupManager;

   
Will be get session over base authenticate.
Url to remote data source.
   private final String dataSourceUrl;

   
Bind to IP address.
   private final String bindIpAddress;

   
The channel configuration.
   private final String channelConfig;

   
The channel name prefix.
   private final String channelName;

   
The folder for temporary files.
   private final File tempDir;

   
ReplicationTestService constructor.

Parameters:
repoService the RepositoryService
backupManager the BackupManager
sessionProviderService the ThreadLocalSessionProviderService
params the InitParams from configuration
   public RemoteWorkspaceInitializationService(RepositoryService repoServiceBackupManager backupManager,
      ThreadLocalSessionProviderService sessionProviderServiceInitParams params)
   {
      this. = repoService;
      this. = backupManager;
      this. = sessionProviderService;
      PropertiesParam pps = params.getPropertiesParam("remote-initializer-properties");
      if (pps == null)
         throw new RuntimeException("remote-initializer-properties not specified");
      if (pps.getProperty("remote-source-url") == null)
         throw new RuntimeException("remote-source-url not specified");
       = pps.getProperty("remote-source-url");
      if (pps.getProperty("bind-ip-address") == null)
         throw new RuntimeException("bind-ip-address not specified");
       = pps.getProperty("bind-ip-address");
      if (pps.getProperty("channel-config") == null)
         throw new RuntimeException("channel-config not specified");
       = pps.getProperty("channel-config");
      if (pps.getProperty("channel-name") == null)
         throw new RuntimeException("channel-name not specified");
       = pps.getProperty("channel-name");
      if (pps.getProperty("temp-dir") == null)
         throw new RuntimeException("temp-dir not specified");
      String tempD = pps.getProperty("temp-dir");
       = new File(tempD);
      if (!PrivilegedFileHelper.exists())
      {
         PrivilegedFileHelper.mkdirs();
      }
      .info("RemoteWorkspaceInitializerService");
   }

   
getWorkspaceData.

Parameters:
repository the repository name
workspace the workspace name
Returns:
File with workspace data
Throws:
RemoteWorkspaceInitializationException will be generated the RemoteWorkspaceInitializerException
   public File getWorkspaceData(String repositoryString workspacethrows RemoteWorkspaceInitializationException
   {
      String id = IdGenerator.generate();
      ChannelManager channelManager =
         new ChannelManager(.replaceAll(),  + "_" + repository
            + "_" + workspace + "_" + id, 2);
      RemoteTransport remoteTransport = new RemoteTransportImpl(channelManager);
      try
      {
         remoteTransport.init();
         return remoteTransport.getWorkspaceData(repositoryworkspaceid);
      }
      catch (Throwable t)
      {
         throw new RemoteWorkspaceInitializationException("Can't get workspace data"t);
      }
      finally
      {
         remoteTransport.close();
      }
   }

   
startFullBackup.

Parameters:
repositoryName the repository name
workspaceName the workspace name
id the unique identifier for channel
Returns:
Response return the response
   @GET
   @Path("/{repositoryName}/{workspaceName}/{id}/getWorkspaceData")
   public Response startFullBackup(@PathParam("repositoryName"String repositoryName,
      @PathParam("workspaceName"String workspaceName, @PathParam("id"String id)
   {
      String result = "OK";
      // init transport
      ChannelManager channelManager =
            + repositoryName + "_" + workspaceName + "_" + id, 2);
      RemoteTransport remoteTransport = new RemoteTransportImpl(channelManager);
      try
      {
         remoteTransport.init();
         // start backup
         BackupConfig config = new BackupConfig();
         config.setRepository(repositoryName);
         config.setWorkspace(workspaceName);
         config.setBackupDir(.getBackupDirectory());
         try
         {
            validateRepositoryName(repositoryName);
            validateWorkspaceName(repositoryNameworkspaceName);
            BackupChain backupChain = .startBackup(config);
            WorkspaceDataPublisher publisher = new WorkspaceDataPublisher(backupChainremoteTransport);
            publisher.start();
         }
         catch (Exception e)
         {
            result = "FAIL\n" + e.getMessage();
            .error("Can't start backup"e);
            remoteTransport.close();
         }
      }
      {
         result = "FAIL\n" + e1.getMessage();
         .error("Can't initialization transport"e1);
      }
      return Response.ok(result).build();
   }

   
validateRepositoryName.

Parameters:
repositoryName the repository name
   private void validateRepositoryName(String repositoryName)
   {
      try
      {
         .getRepository(repositoryName);
      }
      catch (RepositoryException e)
      {
         throw new RuntimeException("Can not get repository '" + repositoryName + "'"e);
      }
      catch (RepositoryConfigurationException e)
      {
         throw new RuntimeException("Can not get repository '" + repositoryName + "'"e);
      }
   }

   
validateWorkspaceName.

Parameters:
repositoryName the repository name
workspaceName the workspace name
   private void validateWorkspaceName(String repositoryNameString workspaceName)
   {
      try
      {
         Session ses =
            .getSessionProvider(null).getSession(workspaceName,
               .getRepository(repositoryName));
         ses.logout();
      }
      catch (LoginException e)
      {
         throw new RuntimeException("Can not loogin to workspace '" + workspaceName + "'"e);
      }
      catch (NoSuchWorkspaceException e)
      {
         throw new RuntimeException("Can not get workspace '" + workspaceName + "'"e);
      }
      catch (RepositoryException e)
      {
         throw new RuntimeException("Can not get workspace '" + workspaceName + "'"e);
      }
      catch (RepositoryConfigurationException e)
      {
         throw new RuntimeException("Can not get workspace '" + workspaceName + "'"e);
      }
   }

   
The WorkspaceDataPublisher will be published the workspace data.
   class WorkspaceDataPublisher extends Thread
   {

      
BACKUP_WAIT_INTERVAL. the constants for backup wait interval.
      private static final int BACKUP_WAIT_INTERVAL = 50;

      
The BackupChain for current backup.
      private BackupChain backupChain;

      
The RemoteTransport will be send workspace data .
      private RemoteTransport transport;

      
WorkspaceDataPublisher constructor.

Parameters:
chain the BackupChain for current backup
transport the RemoteTransport
      public WorkspaceDataPublisher(BackupChain chainRemoteTransport transport)
      {
         super("WorkspaceDataPublisher");
         this. = chain;
         this. = transport;
      }

      
      @Override
      public void run()
      {
         try
         {
            try
            {
               // wait till full backup will be stopped
               while (.getFullBackupState() != .)
               {
                  Thread.yield();
                  Thread.sleep();
               }
               // get path to file with full backup
               String path = .getBackupJobs().get(0).getStorageURL().getPath();
               // stop backup
               .stopBackup();
               // send data
               .sendWorkspaceData(new File(path));
               Thread.sleep( * 20 * 30);
            }
            catch (RemoteWorkspaceInitializationException e)
            {
               try
               {
                  .sendError("Can not send the workspace data : " + e.getMessage());
               }
               catch (RemoteWorkspaceInitializationException e1)
               {
                  .error("Can not send error message : " + e.getMessage(), e);
               }
            }
            catch (BackupOperationException e)
            {
               try
               {
                  .sendError("Can not send the workspace data : " + e.getMessage());
               }
               catch (RemoteWorkspaceInitializationException e1)
               {
                  .error("Can not send error message : " + e.getMessage(), e);
               }
            }
            catch (InterruptedException e)
            {
               try
               {
                  .sendError("Can not send the workspace data : " + e.getMessage());
               }
               catch (RemoteWorkspaceInitializationException e1)
               {
                  .error("Can not send error message : " + e.getMessage(), e);
               }
            }
         }
         catch (NoMemberToSendException e)
         {
            .error("Can not send the data  : " + e.getMessage(), e);
         }
         finally
         {
            try
            {
               .close();
            }
            catch (RemoteWorkspaceInitializationException e)
            {
               .error("Can not close the transport : " + e.getMessage(), e);
            }
         }
      }
   }
New to GrepCode? Check out our FAQ X