1 17 18 package org.apache.geronimo.system.main; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Enumeration ; 25 import java.util.Set ; 26 import java.util.Collection ; 27 import java.net.URL ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.geronimo.common.GeronimoEnvironment; 32 import org.apache.geronimo.kernel.GBeanNotFoundException; 33 import org.apache.geronimo.kernel.InternalKernelException; 34 import org.apache.geronimo.kernel.Kernel; 35 import org.apache.geronimo.kernel.KernelFactory; 36 import org.apache.geronimo.kernel.config.ConfigurationManager; 37 import org.apache.geronimo.kernel.config.ConfigurationUtil; 38 import org.apache.geronimo.kernel.config.NoSuchConfigException; 39 import org.apache.geronimo.kernel.config.LifecycleException; 40 import org.apache.geronimo.kernel.config.ConfigurationData; 41 import org.apache.geronimo.kernel.log.GeronimoLogging; 42 import org.apache.geronimo.kernel.repository.Artifact; 43 import org.apache.geronimo.kernel.repository.MissingDependencyException; 44 import org.apache.geronimo.gbean.AbstractName; 45 import org.apache.geronimo.gbean.AbstractNameQuery; 46 47 48 51 public class CommandLine { 52 protected static final Log log; 53 54 static { 55 GeronimoEnvironment.init(); 57 58 GeronimoLogging.initialize(GeronimoLogging.ERROR); 60 log = LogFactory.getLog(CommandLine.class.getName()); 61 } 62 63 67 public static void main(String [] args) { 68 log.info("Server startup begun"); 69 try { 70 CommandLineManifest manifest = CommandLineManifest.getManifestEntries(); 72 List configurations = manifest.getConfigurations(); 73 AbstractNameQuery mainGBean = manifest.getMainGBeanQuery(); 74 String mainMethod = manifest.getMainMethod(); 75 76 new CommandLine().invokeMainGBean(configurations, mainGBean, mainMethod, args); 77 78 log.info("Server shutdown completed"); 79 } catch (Exception e) { 80 ExceptionUtil.trimStackTrace(e); 81 e.printStackTrace(); 82 System.exit(2); 83 throw new AssertionError (); 84 } 85 } 86 87 private Kernel kernel; 88 private AbstractName configurationName; 89 90 public void invokeMainGBean(List configurations, AbstractNameQuery mainGBeanQuery, String mainMethod, String [] args) throws Exception { 91 startKernel(); 92 Runtime.getRuntime().addShutdownHook(new Thread ("Geronimo shutdown thread") { 93 public void run() { 94 log.info("Server shutdown begun"); 95 try { 96 stopKernel(); 97 } catch (GBeanNotFoundException e) { 98 99 } 100 } 101 }); 102 loadConfigurations(configurations); 103 104 log.info("Server startup completed"); 105 Set matches = kernel.listGBeans(mainGBeanQuery); 106 if (matches.isEmpty()) { 107 throw new Exception ("No match for AbstractNameQuery: " + mainGBeanQuery); 108 } 109 if (matches.size() > 1) { 110 throw new Exception ("Ambiguous AbstractNameQuery: " + mainGBeanQuery + " matches: " + matches); 111 } 112 AbstractName mainGBean = (AbstractName) matches.iterator().next(); 113 114 kernel.invoke( 116 mainGBean, 117 mainMethod, 118 new Object []{args}, 119 new String []{String [].class.getName()}); 120 121 } 122 123 protected void startKernel() throws Exception { 124 ClassLoader classLoader = CommandLine.class.getClassLoader(); 125 InputStream in = classLoader.getResourceAsStream("META-INF/config.ser"); 126 try { 127 kernel = KernelFactory.newInstance().createKernel("geronimo"); 129 kernel.boot(); 130 131 configurationName = ConfigurationUtil.loadBootstrapConfiguration(kernel, in, classLoader); 133 } finally { 134 if (in != null) { 135 try { 136 in.close(); 137 } catch (IOException ignored) { 138 } 140 } 141 } 142 } 143 144 protected void startKernel(Artifact moduleId) throws Exception { 145 kernel = KernelFactory.newInstance().createKernel("geronimo"); 147 kernel.boot(); 148 ClassLoader classLoader = CommandLine.class.getClassLoader(); 149 for (Enumeration modules = classLoader.getResources("META-INF/config.ser"); modules.hasMoreElements(); ) { 150 URL moduleDataURL = (URL ) modules.nextElement(); 151 InputStream in = moduleDataURL.openStream(); 152 try { 153 ConfigurationData moduleData = ConfigurationUtil.readConfigurationData(in); 154 if (moduleId.matches(moduleData.getId())) { 155 configurationName = ConfigurationUtil.loadBootstrapConfiguration(kernel, moduleData, classLoader); 157 return; 158 } 159 } finally { 160 in.close(); 161 } 162 } 163 throw new NoSuchConfigException(moduleId); 164 } 165 166 protected void loadConfigurations(List configurations) throws NoSuchConfigException, LifecycleException, MissingDependencyException { 167 ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel); 169 Collection resolvedConfigurations = configurationManager.getArtifactResolver().resolveInClassLoader(configurations); 170 try { 171 for (Iterator i = resolvedConfigurations.iterator(); i.hasNext();) { 172 Artifact configID = (Artifact) i.next(); 173 configurationManager.loadConfiguration(configID); 174 configurationManager.startConfiguration(configID); 175 } 176 } finally { 177 ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager); 178 } 179 } 180 181 protected Kernel getKernel() { 182 return kernel; 183 } 184 185 protected void stopKernel() throws GBeanNotFoundException, InternalKernelException { 186 kernel.stopGBean(configurationName); 188 189 kernel.shutdown(); 191 } 192 } 193 | Popular Tags |