1 package org.apache.fulcrum.yaafi.testcontainer; 2 55 import java.io.File ; 56 57 import org.apache.avalon.framework.activity.Disposable; 58 import org.apache.avalon.framework.activity.Initializable; 59 import org.apache.avalon.framework.component.Component; 60 import org.apache.avalon.framework.component.ComponentException; 61 import org.apache.avalon.framework.context.DefaultContext; 62 import org.apache.avalon.framework.logger.AbstractLogEnabled; 63 import org.apache.avalon.framework.logger.ConsoleLogger; 64 import org.apache.avalon.framework.logger.Logger; 65 import org.apache.fulcrum.yaafi.framework.container.ServiceContainer; 66 import org.apache.fulcrum.yaafi.framework.container.ServiceContainerImpl; 67 import org.apache.fulcrum.yaafi.framework.factory.ServiceManagerFactory; 68 import org.apache.fulcrum.yaafi.service.servicemanager.ServiceManagerService; 69 70 71 77 public class Container extends AbstractLogEnabled implements Initializable, Disposable 78 { 79 80 public static String COMPONENT_APP_ROOT = "componentAppRoot"; 81 82 83 public static String URN_AVALON_HOME = "urn:avalon:home"; 84 85 86 public static String URN_AVALON_TEMP = "urn:avalon:temp"; 87 88 89 private ServiceContainer manager; 90 91 92 private String configFileName; 93 94 95 private String roleFileName; 96 97 98 private String parametersFileName; 99 100 101 104 public Container() 105 { 106 this.manager = new ServiceContainerImpl(); 108 this.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) ); 109 } 110 111 117 public void startup(String configFileName, String roleFileName, String parametersFileName ) 118 { 119 getLogger().debug("Starting container..."); 120 121 this.configFileName = configFileName; 122 this.roleFileName = roleFileName; 123 this.parametersFileName = parametersFileName; 124 125 File configFile = new File (configFileName); 126 127 if (!configFile.exists()) 128 { 129 throw new RuntimeException ( 130 "Could not initialize the container because the config file could not be found:" + configFile); 131 } 132 133 try 134 { 135 initialize(); 136 getLogger().info("YaffiContainer ready."); 137 } 138 catch (Exception e) 139 { 140 getLogger().error("Could not initialize the container", e); 141 throw new RuntimeException ("Could not initialize the container"); 142 } 143 } 144 145 153 public void initialize() throws Exception 154 { 155 DefaultContext context = new DefaultContext(); 156 String absolutePath = new File ("").getAbsolutePath(); 157 context.put(COMPONENT_APP_ROOT, absolutePath); 158 context.put(URN_AVALON_HOME, new File ( new File ("").getAbsolutePath() ) ); 159 160 Logger logger = new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ); 161 162 this.manager = ServiceManagerFactory.create( 163 logger, 164 this.roleFileName, 165 this.configFileName, 166 this.parametersFileName, 167 context 168 ); 169 } 170 171 174 public void dispose() 175 { 176 getLogger().debug("Disposing of container..."); 177 this.manager.dispose(); 178 getLogger().info("YaffiContainer has been disposed."); 179 } 180 186 public Object lookup(String roleName) throws ComponentException 187 { 188 try 189 { 190 return ServiceManagerService.getServiceManager().lookup(roleName); 191 } 192 catch( Exception e ) 193 { 194 String msg = "Failed to lookup role " + roleName; 195 throw new ComponentException(roleName,msg,e); 196 } 197 } 198 204 public void release(Component component) 205 { 206 ServiceManagerService.getServiceManager().release(component); 207 } 208 213 public void release(Object component) 214 { 215 ServiceManagerService.getServiceManager().release(component); 216 } 217 } 218 | Popular Tags |