1 22 package org.jboss.kernel; 23 24 import org.jboss.kernel.spi.bootstrap.KernelInitializer; 25 import org.jboss.kernel.spi.config.KernelConfig; 26 import org.jboss.logging.Logger; 27 28 41 public class KernelFactory 42 { 43 44 private static final KernelFactory singleton = new KernelFactory(); 45 46 47 protected final Logger log = Logger.getLogger(getClass()); 48 49 52 private KernelFactory() 53 { 54 } 55 56 65 public static Kernel newInstance(KernelConfig cfg) 66 { 67 return singleton.assembleNewKernel(cfg); 68 } 69 70 80 protected Kernel assembleNewKernel(KernelConfig cfg) 81 throws RuntimeException 82 { 83 long begin = 0; 84 long end = 0; 85 86 log.debug("Starting JBoss Kernel construction..."); 87 begin = System.currentTimeMillis(); 88 89 boolean trace = log.isTraceEnabled(); 91 92 if (trace) 93 log.trace("Using KernelConfig: " + cfg); 94 95 Kernel kernel = createKernel(); 96 if (trace) 97 log.trace("Using Kernel: " + kernel); 98 99 KernelInitializer initializer = createKernelInitializer(cfg); 100 if (trace) 101 log.trace("Using KernelInitializer: " + initializer); 102 103 configureKernel(kernel, cfg); 104 if (trace) 105 log.trace("Configured kernel from KernelConfig"); 106 107 initializeKernel(kernel, initializer); 108 if (trace) 109 log.trace("Kernel instance initialzed"); 110 111 end = System.currentTimeMillis(); 112 log.debug("Completed JBoss Kernel construction. Duration: " + (end - begin) + " milliseconds"); 113 114 return kernel; 115 } 116 117 127 protected Kernel createKernel() 128 { 129 return new Kernel(); 130 } 131 132 146 protected KernelInitializer createKernelInitializer(KernelConfig config) 147 { 148 try 149 { 150 return config.createKernelInitializer(); 151 } 152 catch (Throwable t) 153 { 154 String msg = "Unable to create a KernelInitializer based on the " + 155 "specified KernelConfig"; 156 throw new RuntimeException (msg, t); 157 } 158 } 159 160 170 protected void configureKernel(Kernel kernel, KernelConfig cfg) 171 { 172 kernel.setConfig(cfg); 173 try 174 { 175 cfg.setKernel(kernel); 176 } 177 catch (Throwable t) 178 { 179 String msg = "Unable to update the KernelConfig with the specified Kernel"; 180 throw new RuntimeException (msg, t); 181 } 182 } 183 184 194 protected void initializeKernel(Kernel kernel, KernelInitializer initializer) 195 { 196 try 197 { 198 initializer.initKernel(kernel); 199 } 200 catch (Throwable t) 201 { 202 String msg = "Unable to properly initialize the Kernel"; 203 throw new RuntimeException (msg, t); 204 } 205 } 206 } 207 | Popular Tags |