1 17 18 package org.apache.avalon.fortress.tools; 19 20 import java.lang.reflect.Method ; 21 22 import org.apache.avalon.fortress.impl.DefaultContainer; 23 import org.apache.avalon.fortress.impl.DefaultContainerManager; 24 import org.apache.avalon.fortress.util.FortressConfig; 25 import org.apache.avalon.framework.activity.Disposable; 26 import org.apache.avalon.framework.activity.Initializable; 27 import org.apache.avalon.framework.container.ContainerUtil; 28 import org.apache.avalon.framework.logger.ConsoleLogger; 29 import org.apache.avalon.framework.logger.LogEnabled; 30 import org.apache.avalon.framework.logger.Logger; 31 import org.apache.avalon.framework.service.ServiceException; 32 import org.apache.avalon.framework.service.ServiceManager; 33 import org.apache.avalon.framework.service.Serviceable; 34 35 41 public class FortressBean implements Initializable, LogEnabled, Serviceable, Disposable { 42 43 private final FortressConfig config = new FortressConfig(); 44 private Logger logger = null; 45 private DefaultContainerManager cm; 46 private DefaultContainer container; 47 private ServiceManager sm; 48 49 private String lookupComponentRole = null; 50 private String invokeMethod = null; 51 52 private boolean systemExitOnDispose = true; 53 54 57 public void enableLogging(Logger logger) { 58 this.logger = logger; 59 } 60 61 protected final Logger getLogger() { 62 if (this.logger == null) this.logger = new ConsoleLogger(); 63 return this.logger; 64 } 65 66 69 public void initialize() throws Exception { 70 if (this.sm == null) { 72 if (Thread.currentThread().getContextClassLoader() == null) { 73 if (this.getClass().getClassLoader() != null) { 74 ClassLoader cl = this.getClass().getClassLoader(); 75 config.setContextClassLoader(cl); 76 Thread.currentThread().setContextClassLoader(cl); 77 } else { 78 getLogger().warn("context classloader not set and class classloader is null!"); 79 } 80 } 81 this.cm = new DefaultContainerManager(config.getContext()); 83 ContainerUtil.initialize(cm); 84 85 this.container = (DefaultContainer) cm.getContainer(); 86 this.sm = container.getServiceManager(); 87 } 88 } 89 90 public void run() throws Exception { 91 Object component = getServiceManager().lookup(lookupComponentRole); 92 Method method = component.getClass().getMethod(invokeMethod, null); 93 method.invoke(component, null); 94 } 95 96 99 public void execute() { 100 try { 101 initialize(); 102 try { 103 run(); 104 } catch (Exception e) { 105 getLogger().error("error while running", e); 106 } 107 } catch (Exception e) { 108 getLogger().error("error while initializing", e); 109 } 110 dispose(); 111 } 112 113 116 public void service(ServiceManager sm) throws ServiceException { 117 if (this.sm == null) this.sm = sm; 118 } 119 120 123 public void dispose() { 124 org.apache.avalon.framework.container.ContainerUtil.dispose( cm ); 126 127 if (this.systemExitOnDispose) { 129 Thread.yield(); 130 try { 131 Thread.sleep(100); 132 } catch (InterruptedException e) { 133 } 135 System.exit(0); 136 } 137 } 138 139 protected ServiceManager getServiceManager() { 140 return this.sm; 141 } 142 143 149 public void setContainerClass(String containerClass) throws Exception { 150 config.setContextClassLoader(getClass().getClassLoader()); 151 config.setContainerClass(containerClass); 152 } 153 154 public void setContainerConfiguration(String containerConfiguration) { 155 config.setContainerConfiguration(containerConfiguration); 156 } 157 158 public void setContextDirectory(String contextDirectory) { 159 config.setContextDirectory(contextDirectory); 160 } 161 162 public void setInstrumentManagerConfiguration(String instrumentManagerConfiguration) { 163 config.setInstrumentManagerConfiguration(instrumentManagerConfiguration); 164 } 165 166 public void setLoggerManagerConfiguration(String loggerManagerConfiguration) { 167 config.setLoggerManagerConfiguration(loggerManagerConfiguration); 168 } 169 170 public void setRoleManagerConfiguration(String roleManagerConfiguration) { 171 config.setRoleManagerConfiguration(roleManagerConfiguration); 172 } 173 174 public void setWorkDirectory(String workDirectory) { 175 config.setWorkDirectory(workDirectory); 176 } 177 178 public void setInvokeMethod(String invokeMethod) { 179 this.invokeMethod = invokeMethod; 180 } 181 182 public void setLookupComponentRole(String lookupComponentRole) { 183 this.lookupComponentRole = lookupComponentRole; 184 } 185 186 191 public void setSystemExitOnDispose(boolean systemExitOnDispose) { 192 this.systemExitOnDispose = systemExitOnDispose; 193 } 194 195 } 196 | Popular Tags |