use of org.apache.openejb.assembler.classic.OpenEjbConfiguration in project tomee by apache.
the class ServiceManager method initServer.
protected ServerService initServer(final String serviceName, final Properties serviceProperties) throws IOException {
final DiscoveryRegistry registry = SystemInstance.get().getComponent(DiscoveryRegistry.class);
final OpenEjbConfiguration conf = SystemInstance.get().getComponent(OpenEjbConfiguration.class);
logger.debug("Processing ServerService(id=" + serviceName + ")");
overrideProperties(serviceName, serviceProperties);
serviceProperties.setProperty("name", serviceName);
if (conf != null && conf.facilities != null) {
final ServiceInfo info = new ServiceInfo();
info.className = ((Class) serviceProperties.get(ServerService.class)).getName();
info.service = "ServerService";
info.id = serviceName;
info.properties = serviceProperties;
conf.facilities.services.add(info);
}
final boolean enabled = isEnabled(serviceProperties);
logger.debug("Found ServerService(id=" + serviceName + ", disabled=" + (!enabled) + ")");
if (enabled) {
final Class serviceClass = (Class) serviceProperties.get(ServerService.class);
logger.info("Creating ServerService(id=" + serviceName + ")");
// log all properties on debug
if (logger.isDebugEnabled()) {
for (final Map.Entry<Object, Object> entry : serviceProperties.entrySet()) {
logger.debug(entry.getKey() + " = " + entry.getValue());
}
}
try {
// Create Service
ServerService service;
ObjectRecipe recipe = new ObjectRecipe(serviceClass);
try {
// Do not import. This class is not available in xbean-reflect-3.3
final ReflectionUtil.StaticFactory factory = ReflectionUtil.findStaticFactory(serviceClass, "createServerService", null, null, serviceProperties.stringPropertyNames(), Collections.singleton(Option.NAMED_PARAMETERS));
if (factory != null) {
// can throw an exception so call it before next line
recipe.setConstructorArgNames(factory.getParameterNames());
recipe.setFactoryMethod("createServerService");
} else if (ReflectionUtil.findStaticFactory(serviceClass, "createServerService", null, null) != null) {
// old behavior, remove when sure previous check is ok
recipe.setFactoryMethod("createServerService");
}
} catch (final Throwable e) {
//Ignore
}
recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
service = (ServerService) recipe.create(serviceClass.getClassLoader());
if (!(service instanceof SelfManaging)) {
service = manage(serviceName, serviceProperties, service);
}
service.init(serviceProperties);
if (service instanceof DiscoveryAgent) {
final DiscoveryAgent agent = (DiscoveryAgent) service;
registry.addDiscoveryAgent(agent);
}
if (LocalMBeanServer.isJMXActive()) {
final MBeanServer server = LocalMBeanServer.get();
register(serviceName, service, server);
}
return service;
} catch (Throwable t) {
t.printStackTrace();
logger.error("service.instantiation.err", t, serviceClass.getName(), t.getClass().getName(), t.getMessage());
}
}
return null;
}
use of org.apache.openejb.assembler.classic.OpenEjbConfiguration in project tomee by apache.
the class OpenEJBContextConfig method adjustDataSourceNameIfNecessary.
private void adjustDataSourceNameIfNecessary() {
if (context == null || "false".equalsIgnoreCase(ADJUST_DATASOURCE_JNDI_NAMES)) {
return;
}
final NamingResourcesImpl resources = context.getNamingResources();
if (resources == null) {
return;
}
final ContextResource[] foundResources = resources.findResources();
String[] ids = null;
if (foundResources != null) {
for (final ContextResource resource : foundResources) {
if ("javax.sql.DataSource".equals(resource.getType()) && !ResourceFactory.class.getName().equals(resource.getProperty(Constants.FACTORY))) {
String jndiName = (String) resource.getProperty("mappedName");
if (jndiName == null) {
jndiName = resource.getName();
}
if (jndiName == null) {
continue;
}
if (ids == null) {
final Properties props = new Properties();
final OpenEjbConfiguration runningConfig = SystemInstance.get().getComponent(OpenEjbConfiguration.class);
final List<String> resourceIds = new ArrayList<String>();
if (runningConfig != null) {
for (final ResourceInfo resourceInfo : runningConfig.facilities.resources) {
if (ConfigurationFactory.isResourceType(resourceInfo.service, resourceInfo.types, "javax.sql.DataSource") && ServiceUtils.implies(props, resourceInfo.properties)) {
resourceIds.add(resourceInfo.id);
}
}
}
ids = resourceIds.toArray(new String[resourceIds.size()]);
}
String mostMatchingId = null;
for (final String id : ids) {
if (id.equals(jndiName)) {
mostMatchingId = jndiName;
break;
} else if (jndiName.endsWith("/" + id) && mostMatchingId == null) {
mostMatchingId = id;
} else if (id.endsWith("/" + jndiName) && mostMatchingId == null) {
mostMatchingId = "openejb/Resource/" + id;
}
}
if (mostMatchingId != null) {
resource.setProperty("mappedName", "java:" + mostMatchingId);
resource.setProperty(NamingUtil.RESOURCE_ID, "java:" + mostMatchingId);
resource.setProperty(Constants.FACTORY, ResourceFactory.class.getName());
}
}
}
}
}
Aggregations