org.ow2.easybeans.server.war.EmbeddedBootstrap Maven / Gradle / Ivy
/**
* EasyBeans
* Copyright (C) 2008 Bull S.A.S.
* Contact: [email protected]
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* --------------------------------------------------------------------------
* $Id: EmbeddedBootstrap.java 5778 2011-03-07 13:52:56Z benoitf $
* --------------------------------------------------------------------------
*/
package org.ow2.easybeans.server.war;
import java.net.URL;
import java.util.List;
import javax.naming.Context;
import javax.servlet.ServletContextEvent;
import org.ow2.easybeans.api.EZBServerConfig;
import org.ow2.easybeans.api.components.EZBComponentRegistry;
import org.ow2.easybeans.component.itf.EZBSmartComponent;
import org.ow2.easybeans.component.smartclient.spi.SmartContextFactory;
import org.ow2.easybeans.deployer.AbsDeployer;
import org.ow2.easybeans.deployer.EasyBeansDeployer;
import org.ow2.easybeans.deployer.JettyDeployer;
import org.ow2.easybeans.deployer.web.jetty.Jetty7Deployer;
import org.ow2.easybeans.deployer.web.tomcat.Tomcat6Deployer;
import org.ow2.easybeans.server.EasyBeans;
import org.ow2.easybeans.server.Embedded;
import org.ow2.easybeans.server.EmbeddedException;
import org.ow2.util.ee.deploy.api.deployer.DeployerException;
import org.ow2.util.ee.deploy.api.deployer.IDeployerManager;
import org.ow2.util.ee.deploy.impl.deployer.DeployerManager;
import org.ow2.util.log.Log;
import org.ow2.util.log.LogFactory;
/**
* This bootsrap is instanciated by the context listener. The context listener
* is registering some libraries in higher classloader and then it can't
* instantiate directly EasyBeans.
* @author Florent BENOIT
*/
public class EmbeddedBootstrap {
/**
* Embedded instance.
*/
private Embedded embedded = null;
/**
* Logger.
*/
private static Log logger = LogFactory.getLog(EasyBeansContextListener.class);
/**
* Container detected ?.
*/
private ContainerTypeEnum containerType;
/**
* Servlet context event.
*/
private ServletContextEvent servletContextEvent;
/**
* Enable the use of the smart factory ?
*/
private boolean enableSmartFactory = false;
/**
* @return the embedded instance
*/
public Embedded getEmbedded() {
return this.embedded;
}
/**
* @return the container type.
*/
public ContainerTypeEnum getContainerType() {
return this.containerType;
}
/**
* Defines the container type.
* @param containerType one of Tomcat5, Tomcat6, Jetty enum type
*/
public void setContainerType(final ContainerTypeEnum containerType) {
this.containerType = containerType;
}
/**
* @return the servlet context event used at startup of the context listener.
*/
public ServletContextEvent getServletContextEvent() {
return this.servletContextEvent;
}
/**
* Defines the context event used at context listener startup.
* @param servletContextEvent the given context event
*/
public void setServletContextEvent(final ServletContextEvent servletContextEvent) {
this.servletContextEvent = servletContextEvent;
}
/**
* Start EasyBeans embedded instance.
*/
public void start() {
logger.info("Configuring EasyBeans for Web Container type ''{0}''", this.containerType);
// Create embedded instance
this.embedded = new Embedded();
IDeployerManager deployerManager = new DeployerManager();
this.embedded.setDeployerManager(deployerManager);
AbsDeployer deployer = null;
// Register the deployers
switch (this.containerType) {
// Use Tomcat deployer if it is Tomcat
case TOMCAT6:
try {
deployer = new Tomcat6Deployer();
} catch (DeployerException e) {
logger.warn("Unable to set Tomcat6 deployer.", e);
}
break;
case JETTY6:
try {
JettyDeployer.setContextEvent(this.servletContextEvent);
deployer = new JettyDeployer();
} catch (DeployerException e) {
logger.warn("Unable to set the servlet context event on the jetty deployer."
+ "The default deployer will be used.", e);
}
break;
case JETTY7:
try {
deployer = new Jetty7Deployer(this.servletContextEvent);
} catch (DeployerException e) {
logger.warn("Unable to set the servlet context event on the jetty deployer."
+ "The default deployer will be used.", e);
}
break;
case UNKNOWN:
default:
logger.info("Using default deployer as the web container has not be detected");
deployer = new EasyBeansDeployer();
break;
}
// reset the deployer
deployer.setEmbedded(this.embedded);
deployerManager.register(deployer);
// user configuration ?
URL xmlConfigurationURL = Thread.currentThread().getContextClassLoader().getResource(EasyBeans.USER_XML_FILE);
if (xmlConfigurationURL == null) {
xmlConfigurationURL = Thread.currentThread().getContextClassLoader().getResource(
EasyBeansContextListener.DEFAULT_XML_FILE);
logger.info(
"No user-defined configuration file named ''{0}'' found in classpath. Using default settings from ''{1}''",
EasyBeans.USER_XML_FILE, xmlConfigurationURL);
}
// Add the configuration URL to the existing list
this.embedded.getServerConfig().getConfigurationURLs().add(xmlConfigurationURL);
// For jetty 7, enable the EZB naming
if (getContainerType() == ContainerTypeEnum.JETTY7) {
URL jetty7DefaultXML = Thread.currentThread().getContextClassLoader().getResource(
"/org/ow2/easybeans/server/war/easybeans-default-jetty7.xml");
this.embedded.getServerConfig().getConfigurationURLs().add(jetty7DefaultXML);
}
try {
this.embedded.start();
} catch (EmbeddedException e) {
throw new IllegalStateException("Cannot start embedded EasyBeans server", e);
}
// Use of smart factory ?
if (this.enableSmartFactory) {
// Get the components registry
EZBComponentRegistry registry = this.embedded.getComponentManager().getComponentRegistry();
// Get the timer components
List smartComponents = registry.getComponents(EZBSmartComponent.class);
// Exists ?
if (smartComponents.isEmpty()) {
logger.warn("Unable to enable the smart factory for web applications as"
+ " no SmartEndPoint component has been defined in the Easybeans configuration.");
return;
}
// Gets the port number of the smart endpoint
EZBSmartComponent smartComponent = smartComponents.get(0);
int portNumber = smartComponent.getPortNumber();
// Defines the PROVIDER_URL for the smart factory
String smartProviderURL = "smart://localhost:" + portNumber;
SmartContextFactory.setDefaultProviderUrl(smartProviderURL);
SmartContextFactory.setUseClassLoader(true);
// Sets the factory to the SmartFactory
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, SmartContextFactory.class.getName());
logger.info("Setting SmartFactory for Web Applications to ''{0}''", smartProviderURL);
} else {
logger.info("SmartFactory has been disabled. EJB3 client access won't be available from webapps deployed by '"
+ this.containerType + "'.");
}
}
/**
* Stop EasyBeans instance.
*/
public void stop() {
if (this.embedded != null) {
logger.info("Stopping EasyBeans embedded server...");
try {
this.embedded.stop();
} catch (EmbeddedException e) {
throw new IllegalStateException("Cannot stop the embedded server", e);
} catch (Throwable e) {
logger.error("Unexpected error when stopping the embedded server", e);
throw new IllegalStateException("Unexpected error when stopping the embedded server", e);
} finally {
this.embedded = null;
}
}
}
/**
* @return returns the Easybeans ServerConfig object.
*/
protected EZBServerConfig getServerConfig() {
return this.embedded.getServerConfig();
}
/**
* Allows to enable or disable the registration of the smart factory.
* @param enableSmartFactory true to enable or false to disable
*/
public void setEnableSmartFactory(final boolean enableSmartFactory) {
this.enableSmartFactory = enableSmartFactory;
}
}