1 package org.apache.turbine.services.yaaficomponent; 2 3 56 57 import java.io.File ; 58 import java.util.ArrayList ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 62 import org.apache.avalon.framework.context.DefaultContext; 63 import org.apache.avalon.framework.logger.Log4JLogger; 64 import org.apache.commons.logging.Log; 65 import org.apache.commons.logging.LogFactory; 66 import org.apache.fulcrum.BaseService; 67 import org.apache.fulcrum.InitializationException; 68 import org.apache.fulcrum.yaafi.framework.container.ServiceContainer; 69 import org.apache.fulcrum.yaafi.framework.factory.ServiceManagerFactory; 70 import org.apache.turbine.Turbine; 71 72 73 100 public class TurbineYaafiComponentService 101 extends BaseService 102 implements YaafiComponentService 103 { 104 105 private static Log log = LogFactory.getLog(TurbineYaafiComponentService.class); 106 107 108 private ServiceContainer container = null; 109 110 114 120 public void init( Object data ) 121 throws InitializationException 122 { 123 try 124 { 125 init(); 126 setInit(true); 127 } 128 catch (Exception e) 129 { 130 log.error("Exception caught initialising service: ", e); 131 throw new InitializationException("init failed", e); 132 } 133 } 134 135 140 public void shutdown() 141 { 142 dispose(); 143 setInit(false); 144 } 145 146 150 155 public void init() throws InitializationException 156 { 157 org.apache.commons.configuration.Configuration conf = getConfiguration(); 158 159 161 String homePath = Turbine.getApplicationRoot(); 162 File home = new File (homePath); 163 164 166 String roleConfigurationFileName = conf.getString( 167 YaafiComponentService.COMPONENT_ROLE_KEYS, 168 YaafiComponentService.COMPONENT_ROLE_VALUE 169 ); 170 171 173 String componentConfigurationFileName = conf.getString( 174 YaafiComponentService.COMPONENT_CONFIG_KEY, 175 YaafiComponentService.COMPONENT_CONFIG_VALUE 176 ); 177 178 180 String parametersFileName = conf.getString( 181 YaafiComponentService.COMPONENT_PARAMETERS_KEY, 182 YaafiComponentService.COMPONENT_PARAMETERS_VALUE 183 ); 184 185 187 DefaultContext context = new DefaultContext(); 188 context.put(COMPONENT_APP_ROOT, homePath); 189 context.put(URN_AVALON_HOME, new File ( homePath ) ); 190 context.put(URN_AVALON_TEMP, new File ( homePath ) ); 191 192 try 193 { 194 this.container = ServiceManagerFactory.create( 195 new Log4JLogger( org.apache.log4j.Logger.getLogger( TurbineYaafiComponentService.class ) ), 196 roleConfigurationFileName, 197 componentConfigurationFileName, 198 parametersFileName, 199 context 200 ); 201 202 } 203 catch (Throwable t) 204 { 205 throw new InitializationException( 206 "Failed to initialize YaafiComponentService",t); } 208 209 List lookupComponents = conf.getList(COMPONENT_LOOKUP_KEY, 210 new ArrayList ()); 211 212 for (Iterator it = lookupComponents.iterator(); it.hasNext();) 213 { 214 String component = (String ) it.next(); 215 try 216 { 217 Object c = lookup(component); 218 log.info("Lookup for Component " + c + " successful"); 219 release(c); 220 } 221 catch (Exception e) 222 { 223 log.error("Lookup for Component " + component + " failed!"); 224 } 225 } 226 setInit(true); 227 } 228 229 232 public void dispose() 233 { 234 if (this.container != null) 235 { 236 this.container.dispose(); 237 this.container = null; 238 } 239 } 240 241 248 public Object lookup(String path) throws Exception 249 { 250 return this.container.lookup(path); 251 } 252 253 261 public void release(Object component) 262 { 263 this.container.release( component ); 264 } 265 } 266 | Popular Tags |