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();
}
}
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);
}
}
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();
}
}
}
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();
}
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;
}
}
}
Aggregations