Search in sources :

Example 46 with ConfigurationFactory

use of org.apache.openejb.config.ConfigurationFactory in project tomee by apache.

the class RedeployTest method test.

public void test() throws Exception {
    // create reference to openejb itests
    final File file = JarLocation.jarLocation(BasicStatelessBean.class);
    if (!file.exists()) {
        throw new Exception("File not found: " + file);
    }
    // These two objects pretty much encompas all the EJB Container
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    createAndDestroy(assembler, config, file);
    createAndDestroy(assembler, config, file);
    createAndDestroy(assembler, config, file);
}
Also used : ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) File(java.io.File) NamingException(javax.naming.NamingException)

Example 47 with ConfigurationFactory

use of org.apache.openejb.config.ConfigurationFactory in project tomee by apache.

the class ResourcesJsonTest method setUp.

@Before
public void setUp() throws OpenEJBException, NamingException, IOException {
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    final AppModule app = new AppModule(ResourcesJsonTest.class.getClassLoader(), ResourcesJsonTest.class.getSimpleName());
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new SingletonBean(ConfiguredThroughJSonBean.class));
    app.getEjbModules().add(new EjbModule(ejbJar));
    app.getEjbModules().iterator().next().getAltDDs().put("resources.json", getClass().getClassLoader().getResource("appresource.resources.json"));
    assembler.createApplication(config.configureApplication(app));
    final Properties properties = new Properties();
    properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
    properties.setProperty("openejb.embedded.initialcontext.close", "destroy");
    // some hack to be sure to call destroy()
    context = new InitialContext(properties);
    bean = (ConfiguredThroughJSonBean) context.lookup("ConfiguredThroughJSonBeanLocalBean");
}
Also used : AppModule(org.apache.openejb.config.AppModule) EjbModule(org.apache.openejb.config.EjbModule) Properties(java.util.Properties) LocalInitialContextFactory(org.apache.openejb.core.LocalInitialContextFactory) InitialContext(javax.naming.InitialContext) SingletonBean(org.apache.openejb.jee.SingletonBean) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) EjbJar(org.apache.openejb.jee.EjbJar) Before(org.junit.Before)

Example 48 with ConfigurationFactory

use of org.apache.openejb.config.ConfigurationFactory in project tomee by apache.

the class Container method start.

public void start() throws Exception {
    if (base == null || !base.exists()) {
        setup(configuration);
    }
    final Properties props = configuration.getProperties();
    if (props != null) {
        StrSubstitutor substitutor = null;
        for (final String s : props.stringPropertyNames()) {
            final String v = props.getProperty(s);
            if (v != null && v.contains("${")) {
                if (substitutor == null) {
                    final Map<String, String> placeHolders = new HashMap<>();
                    placeHolders.put("tomee.embedded.http", Integer.toString(configuration.getHttpPort()));
                    placeHolders.put("tomee.embedded.https", Integer.toString(configuration.getHttpsPort()));
                    placeHolders.put("tomee.embedded.stop", Integer.toString(configuration.getStopPort()));
                    substitutor = new StrSubstitutor(placeHolders);
                }
                props.put(s, substitutor.replace(v));
            }
        }
        // inherit from system props
        final Properties properties = new Properties(System.getProperties());
        properties.putAll(configuration.getProperties());
        Logger.configure(properties);
    } else {
        Logger.configure();
    }
    final File conf = new File(base, "conf");
    final File webapps = new File(base, "webapps");
    final String catalinaBase = base.getAbsolutePath();
    // set the env before calling anoything on tomcat or Catalina!!
    // TODO: save previous value and restore in stop
    System.setProperty("catalina.base", catalinaBase);
    System.setProperty("openejb.deployments.classpath", "false");
    System.setProperty("catalina.home", catalinaBase);
    System.setProperty("catalina.base", catalinaBase);
    System.setProperty("openejb.home", catalinaBase);
    System.setProperty("openejb.base", catalinaBase);
    System.setProperty("openejb.servicemanager.enabled", "false");
    copyFileTo(conf, "catalina.policy");
    copyTemplateTo(conf, "catalina.properties");
    copyFileTo(conf, "context.xml");
    copyFileTo(conf, "openejb.xml");
    copyFileTo(conf, "tomcat-users.xml");
    copyFileTo(conf, "web.xml");
    final boolean initialized;
    if (configuration.hasServerXml()) {
        final File file = new File(conf, "server.xml");
        if (!file.equals(configuration.getServerXmlFile())) {
            final FileOutputStream fos = new FileOutputStream(file);
            try {
                IO.copy(configuration.getServerXmlFile(), fos);
            } finally {
                IO.close(fos);
            }
        }
        // respect config (host/port) of the Configuration
        final QuickServerXmlParser ports = QuickServerXmlParser.parse(file);
        if (configuration.isKeepServerXmlAsThis()) {
            // force ports to be able to stop the server and get @ArquillianResource
            configuration.setHttpPort(Integer.parseInt(ports.http()));
            configuration.setStopPort(Integer.parseInt(ports.stop()));
        } else {
            final Map<String, String> replacements = new HashMap<String, String>();
            replacements.put(ports.http(), String.valueOf(configuration.getHttpPort()));
            replacements.put(ports.https(), String.valueOf(configuration.getHttpsPort()));
            replacements.put(ports.stop(), String.valueOf(configuration.getStopPort()));
            IO.copy(IO.slurp(new ReplaceStringsInputStream(IO.read(file), replacements)).getBytes(), file);
        }
        tomcat.server(createServer(file.getAbsolutePath()));
        initialized = true;
    } else {
        copyFileTo(conf, "server.xml");
        initialized = false;
    }
    if (props != null && !props.isEmpty()) {
        final File file = new File(conf, "system.properties");
        if (file.isFile()) {
            final Properties existing = IO.readProperties(file);
            for (final String key : existing.stringPropertyNames()) {
                if (!props.containsKey(key)) {
                    props.put(key, existing.getProperty(key));
                }
            }
        }
        final FileWriter systemProperties = new FileWriter(file);
        try {
            props.store(systemProperties, "");
        } finally {
            IO.close(systemProperties);
        }
    }
    // Need to use JULI so log messages from the tests are visible
    // using openejb logging conf in embedded mode
    /* if we use our config (Logger.configure()) don't override it
        copyFileTo(conf, "logging.properties");
        System.setProperty("java.util.logging.manager", "org.apache.juli.ClassLoaderLogManager");
        final File logging = new File(conf, "logging.properties");
        if (logging.exists()) {
            System.setProperty("java.util.logging.config.file", logging.getAbsolutePath());
        }
        */
    // Trigger loading of catalina.properties
    CatalinaProperties.getProperty("foo");
    tomcat.setBaseDir(base.getAbsolutePath());
    tomcat.setHostname(configuration.getHost());
    if (!initialized) {
        tomcat.getHost().setAppBase(webapps.getAbsolutePath());
        tomcat.getEngine().setDefaultHost(configuration.getHost());
        tomcat.setHostname(configuration.getHost());
    }
    if (configuration.getRealm() != null) {
        tomcat.getEngine().setRealm(configuration.getRealm());
    }
    if (tomcat.getRawConnector() == null && !configuration.isSkipHttp()) {
        final Connector connector = createConnector();
        connector.setPort(configuration.getHttpPort());
        if (connector.getAttribute("connectionTimeout") == null) {
            connector.setAttribute("connectionTimeout", "3000");
        }
        if (configuration.isHttp2()) {
            // would likely need SSLHostConfig programmatically
            connector.addUpgradeProtocol(new Http2Protocol());
        }
        tomcat.getService().addConnector(connector);
        tomcat.setConnector(connector);
    }
    // create https connector
    if (configuration.isSsl()) {
        final Connector httpsConnector = createConnector();
        httpsConnector.setPort(configuration.getHttpsPort());
        httpsConnector.setSecure(true);
        httpsConnector.setProperty("SSLEnabled", "true");
        httpsConnector.setProperty("sslProtocol", configuration.getSslProtocol());
        if (configuration.getKeystoreFile() != null) {
            httpsConnector.setAttribute("", configuration.getKeystoreFile());
        }
        if (configuration.getKeystorePass() != null) {
            httpsConnector.setAttribute("keystorePass", configuration.getKeystorePass());
        }
        httpsConnector.setAttribute("keystoreType", configuration.getKeystoreType());
        if (configuration.getClientAuth() != null) {
            httpsConnector.setAttribute("clientAuth", configuration.getClientAuth());
        }
        if (configuration.getKeyAlias() != null) {
            httpsConnector.setAttribute("keyAlias", configuration.getKeyAlias());
        }
        if (configuration.isHttp2()) {
            // would likely need SSLHostConfig programmatically
            httpsConnector.addUpgradeProtocol(new Http2Protocol());
        }
        tomcat.getService().addConnector(httpsConnector);
        if (configuration.isSkipHttp()) {
            tomcat.setConnector(httpsConnector);
        }
    }
    for (final Connector c : configuration.getConnectors()) {
        tomcat.getService().addConnector(c);
    }
    if (!configuration.isSkipHttp() && !configuration.isSsl() && !configuration.getConnectors().isEmpty()) {
        tomcat.setConnector(configuration.getConnectors().iterator().next());
    }
    // Bootstrap Tomcat
    // create it after Logger is configured
    Logger.getInstance(LogCategory.OPENEJB_STARTUP, Container.class).info("Starting TomEE from: " + base.getAbsolutePath());
    if (configuration.getUsers() != null) {
        for (final Map.Entry<String, String> user : configuration.getUsers().entrySet()) {
            tomcat.addUser(user.getKey(), user.getValue());
        }
    }
    if (configuration.getRoles() != null) {
        for (final Map.Entry<String, String> user : configuration.getRoles().entrySet()) {
            for (final String role : user.getValue().split(" *, *")) {
                tomcat.addRole(user.getKey(), role);
            }
        }
    }
    if (!initialized) {
        tomcat.init();
    }
    tomcat.start();
    // Bootstrap OpenEJB
    final Properties properties = new Properties();
    properties.setProperty("openejb.deployments.classpath", "false");
    properties.setProperty("openejb.loader", "tomcat-system");
    properties.setProperty("openejb.home", catalinaBase);
    properties.setProperty("openejb.base", catalinaBase);
    properties.setProperty("openejb.servicemanager.enabled", "false");
    if (configuration.getProperties() != null) {
        properties.putAll(configuration.getProperties());
    }
    if (properties.getProperty("openejb.system.apps") == null) {
        // will make startup faster and it is rarely useful for embedded case
        properties.setProperty("openejb.system.apps", "false");
    }
    if (configuration.isQuickSession()) {
        properties.put("openejb.session.manager", QuickSessionManager.class.getName());
    }
    try {
        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        final Properties tomcatServerInfo = IO.readProperties(classLoader.getResourceAsStream("org/apache/catalina/util/ServerInfo.properties"), new Properties());
        String serverNumber = tomcatServerInfo.getProperty("server.number");
        if (serverNumber == null) {
            // Tomcat5 only has server.info
            final String serverInfo = tomcatServerInfo.getProperty("server.info");
            if (serverInfo != null) {
                final int slash = serverInfo.indexOf('/');
                serverNumber = serverInfo.substring(slash + 1);
            }
        }
        if (serverNumber != null) {
            System.setProperty("tomcat.version", serverNumber);
        }
        final String serverBuilt = tomcatServerInfo.getProperty("server.built");
        if (serverBuilt != null) {
            System.setProperty("tomcat.built", serverBuilt);
        }
    } catch (final Throwable e) {
    // no-op
    }
    final TomcatLoader loader = new TomcatLoader();
    loader.initDefaults(properties);
    // need to add properties after having initialized defaults
    // to properties passed to SystemInstance otherwise we loose some of them
    final Properties initProps = new Properties();
    initProps.putAll(System.getProperties());
    initProps.putAll(properties);
    if (SystemInstance.isInitialized()) {
        SystemInstance.get().getProperties().putAll(initProps);
    } else {
        SystemInstance.init(initProps);
    }
    SystemInstance.get().setComponent(StandardServer.class, (StandardServer) tomcat.getServer());
    // needed again cause of init()
    SystemInstance.get().setComponent(Server.class, tomcat.getServer());
    loader.initialize(properties);
    assembler = SystemInstance.get().getComponent(Assembler.class);
    configurationFactory = new ConfigurationFactory();
    if (configuration.isWithEjbRemote()) {
        tomcat.getHost().addChild(new TomEERemoteWebapp());
    }
}
Also used : Connector(org.apache.catalina.connector.Connector) HashMap(java.util.HashMap) TomEERemoteWebapp(org.apache.tomee.catalina.remote.TomEERemoteWebapp) FileWriter(java.io.FileWriter) CatalinaProperties(org.apache.catalina.startup.CatalinaProperties) Properties(java.util.Properties) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) QuickServerXmlParser(org.apache.tomee.util.QuickServerXmlParser) ReplaceStringsInputStream(org.codehaus.swizzle.stream.ReplaceStringsInputStream) QuickSessionManager(org.apache.tomee.catalina.session.QuickSessionManager) FileOutputStream(java.io.FileOutputStream) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) TomcatLoader(org.apache.tomee.catalina.TomcatLoader) Assembler(org.apache.openejb.assembler.classic.Assembler) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) Http2Protocol(org.apache.coyote.http2.Http2Protocol)

Example 49 with ConfigurationFactory

use of org.apache.openejb.config.ConfigurationFactory in project tomee by apache.

the class DeployerEjb method deploy.

@Override
public AppInfo deploy(final String inLocation, Properties properties) throws OpenEJBException {
    String rawLocation = inLocation;
    if (rawLocation == null && properties == null) {
        throw new NullPointerException("location and properties are null");
    }
    if (rawLocation == null) {
        rawLocation = properties.getProperty(FILENAME);
    }
    if (properties == null) {
        properties = new Properties();
    }
    AppModule appModule = null;
    final File file;
    if ("true".equalsIgnoreCase(properties.getProperty(OPENEJB_USE_BINARIES, "false"))) {
        file = copyBinaries(properties);
    } else {
        file = new File(realLocation(rawLocation).iterator().next());
    }
    final boolean autoDeploy = Boolean.parseBoolean(properties.getProperty(OPENEJB_APP_AUTODEPLOY, "false"));
    final String host = properties.getProperty(OPENEJB_DEPLOYER_HOST, null);
    if (WebAppDeployer.Helper.isWebApp(file) && !oldWarDeployer) {
        AUTO_DEPLOY.set(autoDeploy);
        try {
            final AppInfo appInfo = SystemInstance.get().getComponent(WebAppDeployer.class).deploy(host, contextRoot(properties, file.getAbsolutePath()), file);
            if (appInfo != null) {
                saveIfNeeded(properties, file, appInfo);
                return appInfo;
            }
            throw new OpenEJBException("can't deploy " + file.getAbsolutePath());
        } finally {
            AUTO_DEPLOY.remove();
        }
    }
    AppInfo appInfo = null;
    try {
        appModule = deploymentLoader.load(file, null);
        // Add any alternate deployment descriptors to the modules
        final Map<String, DeploymentModule> modules = new TreeMap<>();
        for (final DeploymentModule module : appModule.getEjbModules()) {
            modules.put(module.getModuleId(), module);
        }
        for (final DeploymentModule module : appModule.getClientModules()) {
            modules.put(module.getModuleId(), module);
        }
        for (final WebModule module : appModule.getWebModules()) {
            final String contextRoot = contextRoot(properties, module.getJarLocation());
            if (contextRoot != null) {
                module.setContextRoot(contextRoot);
                module.setHost(host);
            }
            modules.put(module.getModuleId(), module);
        }
        for (final DeploymentModule module : appModule.getConnectorModules()) {
            modules.put(module.getModuleId(), module);
        }
        for (final Map.Entry<Object, Object> entry : properties.entrySet()) {
            String name = (String) entry.getKey();
            if (name.startsWith(ALT_DD + "/")) {
                name = name.substring(ALT_DD.length() + 1);
                final DeploymentModule module;
                final int slash = name.indexOf('/');
                if (slash > 0) {
                    final String moduleId = name.substring(0, slash);
                    name = name.substring(slash + 1);
                    module = modules.get(moduleId);
                } else {
                    module = appModule;
                }
                if (module != null) {
                    final String value = (String) entry.getValue();
                    final File dd = new File(value);
                    if (dd.canRead()) {
                        module.getAltDDs().put(name, dd.toURI().toURL());
                    } else {
                        module.getAltDDs().put(name, value);
                    }
                }
            }
        }
        final OpenEjbConfiguration configuration = new OpenEjbConfiguration();
        configuration.containerSystem = new ContainerSystemInfo();
        configuration.facilities = new FacilitiesInfo();
        final ConfigurationFactory configurationFactory = new ConfigurationFactory(false, configuration);
        appInfo = configurationFactory.configureApplication(appModule);
        appInfo.autoDeploy = autoDeploy;
        if (properties != null && properties.containsKey(OPENEJB_DEPLOYER_FORCED_APP_ID_PROP)) {
            appInfo.appId = properties.getProperty(OPENEJB_DEPLOYER_FORCED_APP_ID_PROP);
        }
        if (!appInfo.webApps.isEmpty()) {
            appInfo.properties.setProperty("tomcat.unpackWar", "false");
        }
        // create any resources and containers defined in the application itself
        if (!appInfo.webApps.isEmpty()) {
            appInfo.properties.setProperty("tomcat.unpackWar", "false");
        }
        final ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
        final ClassLoader appClassLoader = assembler.createAppClassLoader(appInfo);
        try {
            Thread.currentThread().setContextClassLoader(appClassLoader);
            for (final ResourceInfo resource : configuration.facilities.resources) {
                assembler.createResource(resource);
            }
            for (final ContainerInfo container : configuration.containerSystem.containers) {
                assembler.createContainer(container);
            }
        } finally {
            Thread.currentThread().setContextClassLoader(oldCl);
        }
        assembler.createApplication(appInfo, appClassLoader);
        saveIfNeeded(properties, file, appInfo);
        return appInfo;
    } catch (final Throwable e) {
        // destroy the class loader for the failed application
        if (appModule != null) {
            ClassLoaderUtil.destroyClassLoader(appModule.getJarLocation());
        }
        if (null != appInfo) {
            ClassLoaderUtil.destroyClassLoader(appInfo.appId, appInfo.path);
        }
        LOGGER.error("Can't deploy " + inLocation, e);
        if (e instanceof ValidationException) {
            throw (ValidationException) e;
        }
        final Throwable ex;
        final DeploymentExceptionManager dem = SystemInstance.get().getComponent(DeploymentExceptionManager.class);
        if (dem != null) {
            if (dem.hasDeploymentFailed()) {
                ex = dem.getLastException();
            } else {
                ex = e;
            }
            if (appInfo != null) {
                dem.clearLastException(appInfo);
            }
        } else {
            ex = e;
        }
        if (ex instanceof OpenEJBException) {
            if (ex.getCause() instanceof ValidationException) {
                throw (ValidationException) ex.getCause();
            }
            throw (OpenEJBException) ex;
        }
        throw new OpenEJBException(ex);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) AppModule(org.apache.openejb.config.AppModule) ValidationException(javax.validation.ValidationException) Properties(java.util.Properties) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) WebModule(org.apache.openejb.config.WebModule) TreeMap(java.util.TreeMap) DeploymentModule(org.apache.openejb.config.DeploymentModule) File(java.io.File) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 50 with ConfigurationFactory

use of org.apache.openejb.config.ConfigurationFactory in project tomee by apache.

the class InvokeMethod method setUp.

private void setUp() throws Exception {
    config = new ConfigurationFactory();
    assembler = new Assembler();
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
}
Also used : TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) Assembler(org.apache.openejb.assembler.classic.Assembler) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo)

Aggregations

ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)100 Assembler (org.apache.openejb.assembler.classic.Assembler)84 EjbJar (org.apache.openejb.jee.EjbJar)76 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)57 TransactionServiceInfo (org.apache.openejb.assembler.classic.TransactionServiceInfo)57 StatelessBean (org.apache.openejb.jee.StatelessBean)44 InitialContext (javax.naming.InitialContext)41 Properties (java.util.Properties)37 EjbModule (org.apache.openejb.config.EjbModule)30 ProxyFactoryInfo (org.apache.openejb.assembler.classic.ProxyFactoryInfo)29 LocalInitialContextFactory (org.apache.openejb.core.LocalInitialContextFactory)29 InitContextFactory (org.apache.openejb.core.ivm.naming.InitContextFactory)21 StatelessSessionContainerInfo (org.apache.openejb.assembler.classic.StatelessSessionContainerInfo)18 SingletonBean (org.apache.openejb.jee.SingletonBean)18 Context (javax.naming.Context)17 StatefulBean (org.apache.openejb.jee.StatefulBean)16 AppModule (org.apache.openejb.config.AppModule)15 ServerFederation (org.apache.openejb.core.ServerFederation)15 EjbJarInfo (org.apache.openejb.assembler.classic.EjbJarInfo)14 ContainerSystem (org.apache.openejb.spi.ContainerSystem)13