mirror of
				https://github.com/Alfresco/alfresco-community-repo.git
				synced 2025-10-29 15:21:53 +00:00 
			
		
		
		
	125484 slanglois: MNT-16155 Update source headers - remove old Copyrights from Java and JSP dource files git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.1.N/root@125603 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
		
			
				
	
	
		
			196 lines
		
	
	
		
			7.0 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			196 lines
		
	
	
		
			7.0 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| package org.alfresco.repo.module.tool;
 | |
| 
 | |
| import java.io.FileNotFoundException;
 | |
| import java.io.IOException;
 | |
| import java.io.InputStream;
 | |
| import java.io.OutputStream;
 | |
| import java.util.Properties;
 | |
| 
 | |
| import org.alfresco.repo.module.ModuleDetailsImpl;
 | |
| import org.alfresco.service.cmr.module.ModuleDetails;
 | |
| 
 | |
| import de.schlichtherle.truezip.file.TFile;
 | |
| import de.schlichtherle.truezip.file.TFileInputStream;
 | |
| import de.schlichtherle.truezip.file.TFileOutputStream;
 | |
| 
 | |
| /**
 | |
|  * Module details helper used by the module mangement tool
 | |
|  * 
 | |
|  * @author Roy Wetherall
 | |
|  * @author Derek Hulley
 | |
|  */
 | |
| public class ModuleDetailsHelper
 | |
| {
 | |
|     /**
 | |
|      * Factory method to create module details from a stream of a properties file
 | |
|      * @param is                the properties input stream, which will be closed during the call
 | |
|      * @return                  Returns the initialized module details
 | |
|      */
 | |
|     public static ModuleDetails createModuleDetailsFromPropertiesStream(InputStream is) throws IOException
 | |
|     {
 | |
|         return createModuleDetailsFromPropertiesStream(is, null);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Factory method to create module details from a stream of a properties file
 | |
|      * @param is                the properties input stream, which will be closed during the call
 | |
|      * @param log               logger
 | |
|      * @return                  Returns the initialized module details
 | |
|      */
 | |
|     public static ModuleDetails createModuleDetailsFromPropertiesStream(InputStream is, LogOutput log) throws IOException
 | |
|     {
 | |
|         try
 | |
|         {
 | |
|             Properties properties = new Properties();
 | |
|             properties.load(is);
 | |
|             return new ModuleDetailsImpl(properties, log);
 | |
|         }
 | |
|         finally
 | |
|         {
 | |
|             try { is.close(); } catch (Throwable e) {}
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Creates a module details helper object based on a file location.
 | |
|      * 
 | |
|      * @param location  file location
 | |
|      * @return          Returns the module details or null if the location points to nothing
 | |
|      * @throws IOException 
 | |
|      */
 | |
|     public static ModuleDetails createModuleDetailsFromPropertyLocation(String location) throws IOException
 | |
|     {
 | |
|         return createModuleDetailsFromPropertyLocation(location, null);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Creates a module details helper object based on a file location.
 | |
|      * 
 | |
|      * @param location  file location
 | |
|      * @param log       logger
 | |
|      * @return          Returns the module details or null if the location points to nothing
 | |
|      * @throws IOException 
 | |
|      */
 | |
|     public static ModuleDetails createModuleDetailsFromPropertyLocation(String location, LogOutput log) throws IOException
 | |
|     {
 | |
|         ModuleDetails result = null;
 | |
|         TFileInputStream is;
 | |
|         try
 | |
|         {
 | |
|             is = new TFileInputStream(location);
 | |
|         }
 | |
|         catch (FileNotFoundException error)
 | |
|         {
 | |
|             error.printStackTrace(System.out);
 | |
|             throw new ModuleManagementToolException("Unable to load module details from property file. File Not Found, " + error.getMessage(), error);
 | |
|             
 | |
|         }
 | |
| 
 | |
|         try
 | |
|         {
 | |
|             result = createModuleDetailsFromPropertiesStream(is, log);
 | |
|         }
 | |
|         catch (IOException exception)
 | |
|         {
 | |
|             throw new ModuleManagementToolException(
 | |
|                         "Unable to load module details from property file." + exception.getMessage(), exception);
 | |
|         }
 | |
|         finally
 | |
|         {
 | |
|             is.close(); // ALWAYS close the stream!
 | |
|         }
 | |
| 
 | |
|         return result;
 | |
|     }
 | |
|     
 | |
|     /**
 | |
|      * Creates a module details instance based on a war location and the module id
 | |
|      * 
 | |
|      * @param warLocation   the war location
 | |
|      * @param moduleId      the module id
 | |
|      * @return              Returns the module details for the given module ID as it occurs in the WAR, or <tt>null</tt>
 | |
|      *                      if there are no module details available.
 | |
|      * @throws IOException 
 | |
|      */
 | |
|     public static ModuleDetails createModuleDetailsFromWarAndId(String warLocation, String moduleId) throws IOException
 | |
|     {
 | |
|         String modulePropertiesFileLocation = ModuleDetailsHelper.getModulePropertiesFileLocation(warLocation, moduleId);
 | |
|         return ModuleDetailsHelper.createModuleDetailsFromPropertyLocation(modulePropertiesFileLocation);
 | |
|     }
 | |
|     
 | |
|     /**
 | |
|      * @param warLocation   the location of the WAR file
 | |
|      * @param moduleId      the module ID within the WAR
 | |
|      * @return              Returns a file handle to the module properties file within the given WAR.
 | |
|      *                      The file may or may not exist.
 | |
|      */
 | |
|     public static TFile getModuleDetailsFileFromWarAndId(String warLocation, String moduleId)
 | |
|     {
 | |
|         String location = ModuleDetailsHelper.getModulePropertiesFileLocation(warLocation, moduleId);
 | |
|         TFile file = new TFile(location);
 | |
|         return file;
 | |
|     }
 | |
|     
 | |
|     /**
 | |
|      * Gets the file location
 | |
|      * 
 | |
|      * @param warLocation   the war location
 | |
|      * @param moduleId      the module id
 | |
|      * @return              the file location
 | |
|      */
 | |
|     public static String getModulePropertiesFileLocation(String warLocation, String moduleId)
 | |
|     {
 | |
|         return warLocation + getModulePropertiesFilePathInWar(moduleId);
 | |
|     }
 | |
|     
 | |
|     /**
 | |
|      * @param moduleId      the module ID
 | |
|      * @return              Returns the path of the module file within a WAR
 | |
|      */
 | |
|     public static String getModulePropertiesFilePathInWar(String moduleId)
 | |
|     {
 | |
|         return WarHelper.MODULE_NAMESPACE_DIR + "/" + moduleId + WarHelper.MODULE_CONFIG_IN_WAR;
 | |
|     }
 | |
|     
 | |
|     /**
 | |
|      * Saves the module details to the war in the correct location based on the module id
 | |
|      * 
 | |
|      * @param warLocation   the war location
 | |
|      * @param moduleDetails      the module id
 | |
|      */
 | |
|     public static void saveModuleDetails(String warLocation, ModuleDetails moduleDetails)
 | |
|     {
 | |
|         // Ensure that it is a valid set of properties
 | |
|         String moduleId = moduleDetails.getId();
 | |
|         try
 | |
|         {
 | |
|             String modulePropertiesFileLocation = getModulePropertiesFileLocation(warLocation, moduleId);
 | |
|             TFile file = new TFile(modulePropertiesFileLocation);
 | |
|             if (file.exists() == false)
 | |
|             {
 | |
|                 file.createNewFile();
 | |
|             }  
 | |
|             
 | |
|             // Get all the module properties
 | |
|             Properties moduleProperties = moduleDetails.getProperties();
 | |
|             OutputStream os = new TFileOutputStream(file);
 | |
|             try
 | |
|             {
 | |
|                 moduleProperties.store(os, null);
 | |
|             }
 | |
|             finally
 | |
|             {
 | |
|                 os.close();
 | |
|             }
 | |
|         }
 | |
|         catch (IOException exception)
 | |
|         {
 | |
|             throw new ModuleManagementToolException(
 | |
|                     "Unable to save module details into WAR file: \n" +
 | |
|                     "   Module: " + moduleDetails.getId() + "\n" +
 | |
|                     "   Properties: " + moduleDetails.getProperties(),
 | |
|                     exception);
 | |
|         }
 | |
|     }
 | |
| }
 |