1 package org.apache.fulcrum.yaafi.framework.factory; 2 3 19 20 import org.apache.avalon.framework.configuration.DefaultConfiguration; 21 import org.apache.avalon.framework.context.Context; 22 import org.apache.avalon.framework.logger.ConsoleLogger; 23 import org.apache.avalon.framework.logger.Logger; 24 25 import org.apache.fulcrum.yaafi.framework.container.ServiceContainer; 26 27 28 34 35 public class ServiceManagerFactory 36 { 37 38 private static String serviceContainerClazzName = "org.apache.fulcrum.yaafi.framework.container.ServiceContainerImpl"; 39 40 41 private static Logger logger; 42 43 static 44 { 45 logger = new ConsoleLogger(); 47 } 48 49 52 public static ServiceContainer create( 53 Logger logger, 54 String componentRolesLocation, 55 String componentConfigurationLocation, 56 String parametersLocation) 57 throws Exception 58 { 59 return create( 60 logger, 61 componentRolesLocation, 62 componentConfigurationLocation, 63 parametersLocation, 64 null 65 ); 66 } 67 68 71 public static ServiceContainer create( 72 Logger logger, 73 String componentRolesLocation, 74 String componentConfigurationLocation, 75 String parametersLocation, 76 Context context) 77 throws Exception 78 { 79 Class clazz = null; 80 ServiceContainer result = null; 81 82 84 if( logger == null ) 85 { 86 String msg = "An instance of a logger is required"; 87 ServiceManagerFactory.logger.error(msg); 88 throw new IllegalArgumentException (msg); 89 } 90 91 try 92 { 93 95 ServiceManagerFactory.logger = logger.getChildLogger( ServiceManagerFactory.class.getName() ); 96 ServiceManagerFactory.logger.debug( "Loading the service container class " + serviceContainerClazzName ); 97 98 100 clazz = ServiceManagerFactory.class.getClassLoader().loadClass(serviceContainerClazzName); 101 ServiceManagerFactory.logger.debug( "Instantiating the service container class " + serviceContainerClazzName ); 102 result = (ServiceContainer) clazz.newInstance(); 103 } 104 catch( Exception e ) 105 { 106 String msg = "Creating the ServiceContainer failed"; 107 ServiceManagerFactory.logger.error( msg, e ); 108 throw e; 109 } 110 111 113 Logger serviceContainerLogger = ServiceManagerFactory.logger.getChildLogger( 114 ServiceContainer.class.getName() 115 ); 116 117 result.enableLogging( serviceContainerLogger ); 118 119 121 if( context != null ) 122 { 123 result.contextualize(context); 124 } 125 126 128 DefaultConfiguration configuration = new DefaultConfiguration( 129 ServiceContainer.ROLE_NAME 130 ); 131 132 configuration = createConfiguration( 133 componentRolesLocation, 134 componentConfigurationLocation, 135 parametersLocation ); 136 137 result.configure( configuration ); 138 139 141 result.initialize(); 142 143 return result; 144 } 145 146 149 private static DefaultConfiguration createConfiguration( 150 String componentRolesLocation, 151 String componentConfigurationLocation, 152 String parametersLocation ) 153 { 154 DefaultConfiguration result = new DefaultConfiguration( 155 ServiceContainer.ROLE_NAME 156 ); 157 158 160 DefaultConfiguration componentRolesLocationConfig = new DefaultConfiguration( 161 ServiceContainer.COMPONENT_ROLE_KEYS 162 ); 163 164 componentRolesLocationConfig.setValue( 165 componentRolesLocation 166 ); 167 168 result.addChild( componentRolesLocationConfig ); 169 170 172 DefaultConfiguration componentConfigurationLocationConfig = new DefaultConfiguration( 173 ServiceContainer.COMPONENT_CONFIG_KEY 174 ); 175 176 componentConfigurationLocationConfig.setValue( 177 componentConfigurationLocation 178 ); 179 180 result.addChild( componentConfigurationLocationConfig ); 181 182 184 DefaultConfiguration parametersLocationConfig = new DefaultConfiguration( 185 ServiceContainer.COMPONENT_PARAMETERS_KEY 186 ); 187 188 parametersLocationConfig.setValue( 189 parametersLocation 190 ); 191 192 result.addChild( parametersLocationConfig ); 193 194 return result; 195 } 196 } 197 | Popular Tags |