1 25 26 package org.objectweb.easybeans.server; 27 28 import java.io.File ; 29 import java.rmi.RemoteException ; 30 import java.util.ArrayList ; 31 import java.util.HashMap ; 32 import java.util.List ; 33 import java.util.ListIterator ; 34 import java.util.Map ; 35 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 38 import javax.naming.NamingException ; 39 40 import org.objectweb.easybeans.api.EZBArchive; 41 import org.objectweb.easybeans.api.EZBContainer; 42 import org.objectweb.easybeans.api.EZBContainerConfig; 43 import org.objectweb.easybeans.api.EZBServer; 44 import org.objectweb.easybeans.component.ComponentManager; 45 import org.objectweb.easybeans.component.Components; 46 import org.objectweb.easybeans.component.api.EZBComponent; 47 import org.objectweb.easybeans.component.api.EZBComponentException; 48 import org.objectweb.easybeans.component.itf.RegistryComponent; 49 import org.objectweb.easybeans.container.JContainer3; 50 import org.objectweb.easybeans.container.JContainerConfig; 51 import org.objectweb.easybeans.deployer.Deployer; 52 import org.objectweb.easybeans.jmx.CommonsModelerException; 53 import org.objectweb.easybeans.jmx.CommonsModelerHelper; 54 import org.objectweb.easybeans.jmx.JMXRemoteException; 55 import org.objectweb.easybeans.jmx.JMXRemoteHelper; 56 import org.objectweb.easybeans.jmx.MBeanServerException; 57 import org.objectweb.easybeans.jmx.MBeanServerHelper; 58 import org.objectweb.easybeans.jmx.MBeansException; 59 import org.objectweb.easybeans.jmx.MBeansHelper; 60 import org.objectweb.easybeans.log.JLog; 61 import org.objectweb.easybeans.log.JLogFactory; 62 import org.objectweb.easybeans.rpc.rmi.server.RMIServerRPC; 63 import org.objectweb.easybeans.rpc.rmi.server.RMIServerRPCImpl; 64 import org.objectweb.easybeans.security.jacc.PolicyProvider; 65 66 70 public class Embedded implements EZBServer { 71 72 75 private static final int SLEEP_VALUE = 10000; 76 77 80 private static JLog logger = JLogFactory.getLog(Embedded.class); 81 82 85 private static int counter = 0; 86 87 90 private Integer id = null; 91 92 95 private ServerConfig serverConfig = null; 96 97 100 private Map <String , EZBContainer> containers = null; 101 102 105 private List <EasyBeansConfigurationExtension> extensionFactories = null; 106 107 110 private Deployer deployer = null; 111 112 115 private Components components = null; 116 117 120 private ComponentManager componentManager = null; 121 122 125 private boolean started = false; 126 127 130 private ContainersMonitor monitor = null; 131 132 136 public Embedded() { 137 this.id = new Integer (counter++); 138 this.containers = new HashMap <String , EZBContainer>(); 139 this.extensionFactories = new ArrayList <EasyBeansConfigurationExtension>(); 140 this.serverConfig = new ServerConfig(); 141 EmbeddedManager.addEmbedded(this); 143 } 144 145 150 public void start() throws EmbeddedException { 151 long tStart = System.currentTimeMillis(); 152 153 if (serverConfig.initJACC()) { 155 PolicyProvider.init(); 156 } 157 158 159 configure(); 161 162 componentManager = new ComponentManager(components); 164 165 try { 167 componentManager.initComponents(); 168 } catch (EZBComponentException e) { 169 throw new EmbeddedException("Cannot init components", e); 170 } 171 172 try { 174 componentManager.startComponents(); 175 } catch (EZBComponentException e) { 176 throw new EmbeddedException("Cannot start components", e); 177 } 178 179 if (!serverConfig.getExtensionFactories().isEmpty()) { 181 List <String > classnames = serverConfig.getExtensionFactories(); 182 for (String fqn : classnames) { 183 EasyBeansConfigurationExtension factory = null; 184 try { 185 factory = Class.forName(fqn).asSubclass(EasyBeansConfigurationExtension.class).newInstance(); 186 } catch (Exception e) { 187 throw new EmbeddedException("Cannot instantiate CallbackFacory : " + fqn, e); 188 } 189 this.extensionFactories.add(factory); 190 } 191 } 192 193 if (serverConfig.isUsingNaming()) { 194 System.setProperty(Context.URL_PKG_PREFIXES, "org.objectweb.easybeans.naming.pkg"); 196 } 197 198 deployer = new Deployer(this); 200 201 MBeansHelper.getInstance().activate(serverConfig.isUsingMBeans()); 202 if (serverConfig.isUsingMBeans()) { 203 204 try { 206 CommonsModelerHelper.initRegistry(); 207 } catch (CommonsModelerException e) { 208 throw new EmbeddedException("Cannot init MBean server", e); 209 } 210 211 try { 213 MBeanServerHelper.startMBeanServer(); 214 } catch (MBeanServerException e) { 215 throw new EmbeddedException("Cannot start MBean server", e); 216 } 217 218 try { 219 JMXRemoteHelper.startConnector((RegistryComponent) this 220 .getComponent("org.objectweb.easybeans.component.carol.CarolComponent")); 221 } catch (JMXRemoteException e) { 222 throw new EmbeddedException("Cannot start JMX Remote connector", e); 223 } 224 225 try { 227 MBeansHelper.getInstance().registerMBean(deployer); 228 } catch (MBeansException e) { 229 throw new EmbeddedException("Cannot init MBeans", e); 230 } 231 232 try { 234 MBeansHelper.getInstance().registerMBean(this); 235 } catch (MBeansException e) { 236 throw new EmbeddedException("Cannot init MBeans", e); 237 } 238 } 239 240 RMIServerRPC invoker = null; 242 try { 243 invoker = new RMIServerRPCImpl(this); 244 } catch (RemoteException e) { 245 throw new EmbeddedException("Cannot build RPC invoker", e); 246 } 247 try { 248 new InitialContext ().rebind(RMIServerRPC.RPC_JNDI_NAME, invoker); 249 } catch (NamingException e) { 250 throw new EmbeddedException("Cannot bind the RPC invoker", e); 251 } 252 253 254 if (serverConfig.isDirectoryScanningEnabled()) { 255 monitor = new ContainersMonitor(this); 257 monitor.init(); 258 } 259 260 logger.info("Embedded.start.startup", Version.VERSION, Long.valueOf(System.currentTimeMillis() - tStart)); 261 logger.info("Embedded.start.created", Integer.valueOf(containers.size())); 262 263 logger.info("Embedded.start.waiting"); 264 if (serverConfig.isDirectoryScanningEnabled()) { 265 monitor.start(); 266 } 267 268 started = true; 270 271 if (serverConfig.shouldWait()) { 272 while (true) { 273 try { 274 Thread.sleep(SLEEP_VALUE); 275 } catch (InterruptedException e) { 276 e.printStackTrace(); 277 } 278 } 279 } 280 } 281 282 286 public synchronized void stop() throws EmbeddedException { 287 if (!started) { 289 throw new EmbeddedException("Cannot stop the server as it is not started."); 290 } 291 292 List <EZBContainer> containersList = new ArrayList <EZBContainer>(this.containers.values()); 296 ListIterator <EZBContainer> li = containersList.listIterator(); 297 while (li.hasNext()) { 298 EZBContainer container = li.next(); 299 container.stop(); 300 removeContainer(container); 301 } 302 monitor.stopOrder(); 303 304 if (serverConfig.isUsingMBeans()) { 306 307 try { 309 JMXRemoteHelper.stopConnector(); 310 } catch (JMXRemoteException e) { 311 logger.error("Cannot stop JMX Remote connector", e); 313 } 315 316 try { 318 MBeansHelper.getInstance().unregisterMBean(deployer); 319 } catch (MBeansException e) { 320 logger.error("Cannot unregister Deployer MBean", e); 322 } 323 324 try { 326 MBeansHelper.getInstance().unregisterMBean(this); 327 } catch (MBeansException e) { 328 logger.error("Cannot unregister Embedded MBean", e); 330 } 331 } 332 333 try { 335 new InitialContext ().unbind(RMIServerRPC.RPC_JNDI_NAME); 336 } catch (NamingException e) { 337 logger.error("Cannot unbind the RPC invoker", e); 339 } 340 341 componentManager.stopComponents(); 343 344 logger.info("Embedded.stop.stopped", Version.VERSION); 345 } 346 347 351 public void setServerConfig(final ServerConfig serverConfig) { 352 if (started) { 354 throw new IllegalStateException ("Cannot set the server configuration when server has been started."); 355 } 356 this.serverConfig = serverConfig; 357 } 358 359 364 public EZBContainer getContainer(final String id) { 365 return containers.get(id); 366 } 367 368 371 private void configure() { 372 File ejb3Directory = serverConfig.getEjb3Directory(); 373 if (ejb3Directory == null) { 375 ejb3Directory = new File ("ejb3s"); 376 logger.debug("No directory was configured, take the default value of '" + ejb3Directory.getAbsolutePath() + "'."); 377 if (!ejb3Directory.exists()) { 378 if (ejb3Directory.mkdir()) { 379 logger.warn("Directory '" + ejb3Directory.getAbsolutePath() + "' created."); 380 } else { 381 throw new IllegalStateException ("Cannot make directory '" + ejb3Directory.getAbsolutePath() + "'."); 382 } 383 } 384 serverConfig.setEjb3Path(ejb3Directory.getAbsolutePath()); 386 } 387 } 388 389 394 public EZBContainer createContainer(final EZBArchive archive) { 395 EZBContainerConfig jConfig = new JContainerConfig(archive); 396 jConfig.setEZBServer(this); 397 callJContainerConfigExtensions(jConfig); 398 EZBContainer container = new JContainer3(jConfig); 399 400 addContainer(container); 401 402 return container; 403 } 404 405 409 public void addContainer(final EZBContainer container) { 410 String id = container.getId(); 411 containers.put(id, container); 412 413 try { 414 MBeansHelper.getInstance().registerMBean(container); 415 } catch (MBeansException e) { 416 logger.error("Cannot register Container MBeans for " + container.getArchive().getName(), e); 418 } 419 } 420 421 425 public void removeContainer(final EZBContainer container) { 426 containers.remove(container.getId()); 427 428 try { 429 MBeansHelper.getInstance().unregisterMBean(container); 430 } catch (MBeansException e) { 431 logger.error("Cannot unregister Container MBeans for " + container.getArchive().getName(), e); 433 } 434 } 435 436 441 private void callJContainerConfigExtensions(final EZBContainerConfig jcc) { 442 443 for (EasyBeansConfigurationExtension extension : this.extensionFactories) { 444 try { 445 extension.configure(jcc); 446 } catch (Throwable t) { 447 logger.info("Failed to configure JContainerConfig with {0}", extension.getClass().getName()); 449 } 450 } 451 } 452 453 456 public ServerConfig getServerConfig() { 457 return serverConfig; 458 } 459 460 463 public Map <String , EZBContainer> getContainers() { 464 return containers; 465 } 466 467 471 public Integer getID() { 472 return id; 473 } 474 475 478 public Deployer getDeployer() { 479 return deployer; 480 } 481 482 486 public Components getComponents() { 487 return this.components; 488 } 489 490 494 public void setComponents(final Components components) { 495 this.components = components; 496 } 497 498 503 public EZBComponent getComponent(final String componentName) { 504 if (componentManager != null) { 506 return componentManager.getComponentRegistry().getComponent(componentName); 507 } 508 return null; 509 } 510 511 514 public ComponentManager getComponentManager() { 515 return componentManager; 516 } 517 } 518 | Popular Tags |