Search in sources :

Example 41 with ContainerSystem

use of org.apache.openejb.spi.ContainerSystem in project tomee by apache.

the class javaURLContextFactory method getContext.

public static Context getContext() {
    final ThreadContext callContext = ThreadContext.getThreadContext();
    if (callContext == null) {
        final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
        final ClassLoader current = Thread.currentThread().getContextClassLoader();
        final Context globalContext = containerSystem.getJNDIContext();
        if (current == null) {
            return globalContext;
        }
        for (final AppContext appContext : containerSystem.getAppContexts()) {
            for (final WebContext web : appContext.getWebContexts()) {
                // more specific first
                if (current.equals(web.getClassLoader())) {
                    return new ContextHandler(web.getJndiEnc());
                }
            }
            if (current.equals(appContext.getClassLoader())) {
                return new ContextHandler(appContext.getAppJndiContext());
            }
        }
        return globalContext;
    }
    final BeanContext di = callContext.getBeanContext();
    if (di != null) {
        return di.getJndiEnc();
    } else {
        final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
        return containerSystem.getJNDIContext();
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) BeanContext(org.apache.openejb.BeanContext) WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) Context(javax.naming.Context) ThreadContext(org.apache.openejb.core.ThreadContext) ContextHandler(org.apache.openejb.core.ivm.ContextHandler) BeanContext(org.apache.openejb.BeanContext) WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) ThreadContext(org.apache.openejb.core.ThreadContext)

Example 42 with ContainerSystem

use of org.apache.openejb.spi.ContainerSystem in project tomee by apache.

the class SQLLoginModule method initialize.

public void initialize(final Subject subject, final CallbackHandler callbackHandler, final Map sharedState, final Map options) {
    this.subject = subject;
    this.handler = callbackHandler;
    for (final Object key : options.keySet()) {
        final Option option = Option.findByName((String) key);
        if (option != null) {
            final String value = (String) options.get(key);
            optionsMap.put(option, value.trim());
        } else {
            log.warning("Ignoring option: {0}. Not supported.", key);
        }
    }
    userSelect = optionsMap.get(Option.USER_SELECT);
    groupSelect = optionsMap.get(Option.GROUP_SELECT);
    digest = optionsMap.get(Option.DIGEST);
    encoding = optionsMap.get(Option.ENCODING);
    if (!Strings.checkNullBlankString(digest)) {
        // Check if the digest algorithm is available
        try {
            MessageDigest.getInstance(digest);
        } catch (final NoSuchAlgorithmException e) {
            initError(e, "Digest algorithm %s is not available.", digest);
        }
        if (encoding != null && !"hex".equalsIgnoreCase(encoding) && !"base64".equalsIgnoreCase(encoding)) {
            initError(null, "Digest Encoding %s is not supported.", encoding);
        }
    }
    if (optionsMap.containsKey(Option.DATABASE_POOL_NAME)) {
        final String dataSourceName = optionsMap.get(Option.DATABASE_POOL_NAME);
        final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
        try {
            dataSource = (DataSource) containerSystem.getJNDIContext().lookup("openejb/Resource/" + dataSourceName);
        } catch (final NamingException e) {
            initError(e, "Data source %s not found.", dataSourceName);
        }
    } else if (optionsMap.containsKey(Option.CONNECTION_URL)) {
        connectionURL = optionsMap.get(Option.CONNECTION_URL);
        final String user = optionsMap.get(Option.USER);
        final String password = optionsMap.get(Option.PASSWORD);
        final String driverName = optionsMap.get(Option.DRIVER);
        properties = new Properties();
        if (user != null) {
            properties.put("user", user);
        }
        if (password != null) {
            properties.put("password", password);
        }
        if (driverName != null) {
            final ClassLoader cl = getClass().getClassLoader();
            try {
                driver = (Driver) cl.loadClass(driverName).newInstance();
            } catch (final ClassNotFoundException e) {
                initError(e, "Driver class %s is not available. Perhaps you need to add it as a dependency in your deployment plan?", driverName);
            } catch (final Exception e) {
                initError(e, "Unable to load, instantiate, register driver %s: %s", driverName, e.getMessage());
            }
        }
    } else {
        initError(null, "Neither %s nor %s was specified", Option.DATABASE_POOL_NAME.name, Option.CONNECTION_URL.name);
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) Driver(java.sql.Driver) NamingException(javax.naming.NamingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Properties(java.util.Properties) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) FailedLoginException(javax.security.auth.login.FailedLoginException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 43 with ContainerSystem

use of org.apache.openejb.spi.ContainerSystem in project tomee by apache.

the class SingletonInstanceManager method initializeDependencies.

private void initializeDependencies(final BeanContext beanContext) throws OpenEJBException {
    final SystemInstance systemInstance = SystemInstance.get();
    final ContainerSystem containerSystem = systemInstance.getComponent(ContainerSystem.class);
    for (final String dependencyId : beanContext.getDependsOn()) {
        final BeanContext dependencyContext = containerSystem.getBeanContext(dependencyId);
        if (dependencyContext == null) {
            throw new OpenEJBException("Deployment does not exist. Deployment(id='" + dependencyContext + "')");
        }
        final Object containerData = dependencyContext.getContainerData();
        // managed by a different container implementation
        if (containerData instanceof Data) {
            final Data data = (Data) containerData;
            data.initialize();
        }
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) BeanContext(org.apache.openejb.BeanContext) OpenEJBException(org.apache.openejb.OpenEJBException) SystemInstance(org.apache.openejb.loader.SystemInstance) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData)

Example 44 with ContainerSystem

use of org.apache.openejb.spi.ContainerSystem in project tomee by apache.

the class PropertiesPropogationTest method test.

public void test() throws Exception {
    EjbServer ejbServer = new EjbServer();
    Properties initProps = new Properties();
    initProps.setProperty("openejb.deployments.classpath.include", "");
    initProps.setProperty("openejb.deployments.classpath.filter.descriptors", "true");
    OpenEJB.init(initProps, new ServerFederation());
    ejbServer.init(new Properties());
    ServicePool pool = new ServicePool(ejbServer, 10);
    ServiceDaemon serviceDaemon = new ServiceDaemon(pool, 0, "localhost");
    serviceDaemon.start();
    int port = serviceDaemon.getPort();
    Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
    ConfigurationFactory config = new ConfigurationFactory();
    EjbModule ejbModule = new EjbModule(new EjbJar(), new OpenejbJar());
    EjbJar ejbJar = ejbModule.getEjbJar();
    OpenejbJar openejbJar = ejbModule.getOpenejbJar();
    StatelessBean statelessBean = ejbJar.addEnterpriseBean(new StatelessBean(WidgetBean.class));
    EjbDeployment deployment = openejbJar.addEjbDeployment(statelessBean);
    deployment.getProperties().put("color", "orange");
    deployment.getProperties().put("openejb.client.color", "red");
    EjbJarInfo ejbJarInfo = config.configureApplication(ejbModule);
    EnterpriseBeanInfo beanInfo = ejbJarInfo.enterpriseBeans.get(0);
    assertTrue(beanInfo.properties.containsKey("color"));
    assertTrue(beanInfo.properties.containsKey("openejb.client.color"));
    assertEquals("orange", beanInfo.properties.get("color"));
    assertEquals("red", beanInfo.properties.get("openejb.client.color"));
    assembler.createApplication(ejbJarInfo);
    ContainerSystem cs = SystemInstance.get().getComponent(ContainerSystem.class);
    BeanContext info = cs.getBeanContext("WidgetBean");
    assertNotNull(info);
    assertTrue(info.getProperties().containsKey("color"));
    assertTrue(info.getProperties().containsKey("openejb.client.color"));
    assertEquals("orange", info.getProperties().get("color"));
    assertEquals("red", info.getProperties().get("openejb.client.color"));
    Properties props = new Properties();
    props.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
    props.put("java.naming.provider.url", "ejbd://127.0.0.1:" + port);
    Context context = new InitialContext(props);
    Widget remote = (Widget) context.lookup("WidgetBeanRemote");
    InvocationHandler handler = ProxyManager.getInvocationHandler(remote);
    EJBObjectHandler objectHandler = EJBObjectHandler.class.cast(handler);
    Properties properties = objectHandler.getEjb().getProperties();
    // Should only contain "openejb.client.*" properties
    assertFalse(properties.containsKey("color"));
    // The openejb.client.color property should have been propogated
    assertTrue(properties.containsKey("openejb.client.color"));
    assertEquals("red", properties.getProperty("openejb.client.color"));
    serviceDaemon.stop();
    OpenEJB.destroy();
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) InitialContext(javax.naming.InitialContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) ServerFederation(org.apache.openejb.core.ServerFederation) EJBObjectHandler(org.apache.openejb.client.EJBObjectHandler) ServicePool(org.apache.openejb.server.ServicePool) EjbModule(org.apache.openejb.config.EjbModule) Properties(java.util.Properties) InvocationHandler(org.apache.openejb.client.proxy.InvocationHandler) InitialContext(javax.naming.InitialContext) EnterpriseBeanInfo(org.apache.openejb.assembler.classic.EnterpriseBeanInfo) BeanContext(org.apache.openejb.BeanContext) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) StatelessBean(org.apache.openejb.jee.StatelessBean) ServiceDaemon(org.apache.openejb.server.ServiceDaemon) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) Assembler(org.apache.openejb.assembler.classic.Assembler) EjbJarInfo(org.apache.openejb.assembler.classic.EjbJarInfo) EjbJar(org.apache.openejb.jee.EjbJar)

Example 45 with ContainerSystem

use of org.apache.openejb.spi.ContainerSystem in project tomee by apache.

the class HsqlService method init.

@Override
public void init(final Properties p) throws Exception {
    final Properties properties = new Properties();
    for (final Map.Entry<Object, Object> entry : p.entrySet()) {
        // Sometimes the properties object has non string values
        if (!(entry.getKey() instanceof String))
            continue;
        if (!(entry.getValue() instanceof String))
            continue;
        final String property = (String) entry.getKey();
        final String value = (String) entry.getValue();
        if (property.startsWith(sc_key_dbname + ".") || property.startsWith(sc_key_database + ".")) {
            throw new ServiceException("Databases cannot be declared in the hsql.properties.  " + "Instead declare a database connection in the openejb.conf file");
        }
        if ("port".equals(property)) {
            properties.setProperty(sc_key_port, value);
        } else if ("bind".equals(property)) {
            properties.setProperty(sc_key_address, value);
        } else {
            properties.setProperty(property, value);
        }
    }
    properties.setProperty(sc_key_no_system_exit, "true");
    final boolean disabled = Boolean.parseBoolean(properties.getProperty("disabled"));
    final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
    if (!disabled && containerSystem != null) {
        final NamingEnumeration<Binding> bindings;
        try {
            bindings = containerSystem.getJNDIContext().listBindings("openejb/Resource/");
            final Set<String> dbnames = new TreeSet<String>();
            for (final Binding binding : Collections.list(bindings)) {
                final Object value = binding.getObject();
                if (value instanceof DataSource) {
                    final DataSource jdbc = (DataSource) value;
                    Connection connection = null;
                    String path = null;
                    try {
                        connection = jdbc.getConnection();
                        final DatabaseMetaData meta = connection.getMetaData();
                        path = getPath(meta.getDriverName(), meta.getURL());
                    } catch (Throwable t) {
                        continue;
                    } finally {
                        if (connection != null) {
                            try {
                                connection.close();
                            } catch (SQLException sqlEx) {
                            // no-op
                            }
                        }
                    }
                    if (path != null) {
                        if (dbnames.size() > 9) {
                            throw new ServiceException("Hsql Server can only host 10 database instances");
                        }
                        String dbname = path.substring(path.lastIndexOf(':') + 1);
                        dbname = dbname.substring(dbname.lastIndexOf('/') + 1);
                        if (!dbnames.contains(dbname)) {
                            properties.put(sc_key_dbname + "." + dbnames.size(), dbname);
                            properties.put(sc_key_database + "." + dbnames.size(), path);
                            dbnames.add(dbname);
                        }
                    }
                }
            }
        } catch (NameNotFoundException e) {
        // Ignore
        }
        // create the server
        server = new Server();
        // add the silent property
        properties.setProperty(sc_key_silent, "true");
        // set the log and error writers
        server.setLogWriter(new HsqlPrintWriter(false));
        server.setErrWriter(new HsqlPrintWriter(true));
        server.setProperties(new HsqlProperties(properties));
        // get the port
        port = server.getPort();
        // get the Address
        final String ipString = server.getAddress();
        if (ipString != null && ipString.length() > 0) {
            this.ip = ipString;
        }
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) Binding(javax.naming.Binding) Server(org.hsqldb.Server) SQLException(java.sql.SQLException) NameNotFoundException(javax.naming.NameNotFoundException) Connection(java.sql.Connection) Properties(java.util.Properties) HsqlProperties(org.hsqldb.persist.HsqlProperties) HsqlDatabaseProperties(org.hsqldb.persist.HsqlDatabaseProperties) DatabaseMetaData(java.sql.DatabaseMetaData) DataSource(javax.sql.DataSource) ServiceException(org.apache.openejb.server.ServiceException) TreeSet(java.util.TreeSet) HsqlProperties(org.hsqldb.persist.HsqlProperties) Map(java.util.Map)

Aggregations

ContainerSystem (org.apache.openejb.spi.ContainerSystem)67 Assembler (org.apache.openejb.assembler.classic.Assembler)33 BeanContext (org.apache.openejb.BeanContext)22 HashMap (java.util.HashMap)21 Map (java.util.Map)19 File (java.io.File)17 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)13 EjbJar (org.apache.openejb.jee.EjbJar)13 StatelessBean (org.apache.openejb.jee.StatelessBean)13 Context (javax.naming.Context)12 AppInfo (org.apache.openejb.assembler.classic.AppInfo)12 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)11 AppContext (org.apache.openejb.AppContext)10 ArrayList (java.util.ArrayList)9 Properties (java.util.Properties)9 EjbModule (org.apache.openejb.config.EjbModule)9 NamingException (javax.naming.NamingException)8 WebContext (org.apache.openejb.core.WebContext)8 InitialContext (javax.naming.InitialContext)7 NameNotFoundException (javax.naming.NameNotFoundException)6