use of org.apache.openejb.assembler.classic.ResourceInfo in project tomee by apache.
the class AutoConfig method getResourceId.
private String getResourceId(final String beanName, String resourceId, final String type, final Properties required, AppResources appResources) throws OpenEJBException {
resourceId = normalizeResourceId(resourceId);
if (resourceId == null) {
return null;
}
if (appResources == null) {
appResources = EMPTY_APP_RESOURCES;
}
// skip references such as URL which are automatically handled by the server
if (type != null && ignoredReferenceTypes.contains(type)) {
return null;
}
// check for existing resource with specified resourceId and type and properties
// check first in app namespace
String id = findResourceId(beanName + '/' + resourceId, type, required, appResources);
if (id != null) {
return id;
}
id = findResourceId(resourceId, type, required, appResources);
if (id != null) {
return id;
}
// expand search to any type -- may be asking for a reference to a sub-type
id = findResourceId(resourceId, null, required, appResources);
if (id != null) {
return id;
}
// app resources
if (appResources.appId != null && !appResources.appId.isEmpty() && resourceId.startsWith(appResources.appId + '/')) {
id = findResourceId(resourceId.substring(appResources.appId.length() + 1), type, required, appResources);
if (id != null) {
return id;
}
}
// throw an exception or log an error
final String shortName = toShortName(resourceId);
final String message = "No existing resource found while attempting to Auto-link unmapped resource-ref '" + resourceId + "' of type '" + type + "' for '" + beanName + "'. Looked for Resource(id=" + resourceId + ") and Resource(id=" + shortName + ")";
if (!autoCreateResources) {
throw new OpenEJBException(message);
}
logger.debug(message);
// if there is a provider with the specified name. use it
if (ServiceUtils.hasServiceProvider(resourceId)) {
final ResourceInfo resourceInfo = configFactory.configureService(resourceId, ResourceInfo.class);
return installResource(beanName, resourceInfo);
} else if (ServiceUtils.hasServiceProvider(shortName)) {
final ResourceInfo resourceInfo = configFactory.configureService(shortName, ResourceInfo.class);
return installResource(beanName, resourceInfo);
}
// if there are any resources of the desired type, use the first one
id = firstMatching(beanName, type, required, appResources);
if (id != null) {
return id;
}
// Auto create a resource using the first provider that can supply a resource of the desired type
return autoCreateResource(type, required, beanName);
}
use of org.apache.openejb.assembler.classic.ResourceInfo in project tomee by apache.
the class AutoConfig method processResourceRef.
private void processResourceRef(final ResourceRef ref, final EjbDeployment ejbDeployment, final AppResources appResources, final EjbModule ejbModule) throws OpenEJBException {
// skip destinations with lookup name
if (ref.getLookupName() != null) {
return;
}
// skip destinations with a global jndi name
final String mappedName = ref.getMappedName() == null ? "" : ref.getMappedName();
if (mappedName.startsWith("jndi:")) {
return;
}
final String refName = ref.getName();
final String refType = getType(ref, ejbModule.getClassLoader());
// skip references such as URLs which are automatically handled by the server
if (ignoredReferenceTypes.contains(refType)) {
final ResourceInfo resourceInfo = configFactory.getResourceInfo(refName.replace("java:", "").replace("comp/env/", ""));
if (resourceInfo != null) {
ref.setMappedName("jndi:" + (resourceInfo.id.startsWith("java:") ? resourceInfo.id : "openejb:Resource/" + resourceInfo.id));
}
return;
}
try {
final Class<?> clazz = ejbModule.getClassLoader().loadClass(refType);
if (clazz.isAnnotationPresent(ManagedBean.class)) {
return;
}
} catch (final Throwable t) {
// no-op
}
try {
ResourceLink link = ejbDeployment.getResourceLink(refName);
if (link == null) {
String id = mappedName.length() == 0 ? ref.getName() : mappedName;
if (id.startsWith("java:")) {
id = id.substring("java:".length());
}
if (id.startsWith("/")) {
id = id.substring(1);
}
try {
final AppModule appModule = ejbModule.getAppModule();
if (appModule != null) {
final String newId = findResourceId(appModule.getModuleId() + '/' + id.replace("java:", "").replaceAll("^comp/env/", ""), refType, new Properties(), appResources);
if (newId != null) {
// app scoped resources, try to find it without creating it first
id = getResourceId(ejbModule.getModuleId(), newId, refType, appResources);
} else {
id = getResourceId(ejbDeployment.getDeploymentId(), id, refType, appResources);
}
} else {
id = getResourceId(ejbDeployment.getDeploymentId(), id, refType, appResources);
}
} catch (final OpenEJBException e) {
// changing the message to be explicit
throw new OpenEJBException("Can't find resource for " + ref.getOrigin() + ". (" + e.getMessage() + ")", e.getCause());
}
logger.info("Auto-linking resource-ref '" + refName + "' in bean " + ejbDeployment.getDeploymentId() + " to Resource(id=" + id + ")");
link = new ResourceLink();
link.setResId(id);
link.setResRefName(refName);
ejbDeployment.addResourceLink(link);
} else {
final String id = getResourceId(ejbDeployment.getDeploymentId(), link.getResId(), refType, appResources);
link.setResId(id);
link.setResRefName(refName);
}
} catch (final OpenEJBException ex) {
if (!(ref instanceof ContextRef)) {
throw ex;
}
}
}
use of org.apache.openejb.assembler.classic.ResourceInfo in project tomee by apache.
the class NoServiceJarTest method test.
public void test() throws Exception {
final ConfigurationFactory factory = new ConfigurationFactory();
final Assembler assembler = new Assembler();
final Resource orange = new Resource("Orange");
orange.setClassName(Color.class.getName());
orange.getProperties().setProperty("red", "255");
orange.getProperties().setProperty("green", "200");
orange.getProperties().setProperty("blue", "0");
final ResourceInfo resourceInfo = factory.configureService(orange, ResourceInfo.class);
assembler.createResource(resourceInfo);
assembler.createSecurityService(factory.configureService(SecurityServiceInfo.class));
assembler.createTransactionManager(factory.configureService(TransactionServiceInfo.class));
final EjbJar ejbJar = new EjbJar();
ejbJar.addEnterpriseBean(new SingletonBean(MyBean.class));
final AppContext application = assembler.createApplication(factory.configureApplication(new EjbModule(ejbJar)));
final MyBean myBean = (MyBean) application.getBeanContexts().get(0).getBusinessLocalBeanHome().create();
final Color color = myBean.getColor();
assertNotNull(color);
assertEquals(255, color.getRed());
assertEquals(200, color.getGreen());
assertEquals(0, color.getBlue());
}
use of org.apache.openejb.assembler.classic.ResourceInfo in project tomee by apache.
the class DefaultDataSourceTest method test.
/**
* Default DataSource should be a JtaManaged DataSource
*
* @throws Exception
*/
public void test() throws Exception {
final ConfigurationFactory config = new ConfigurationFactory();
final Assembler assembler = new Assembler();
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
final EjbJar ejbJar = new EjbJar();
ejbJar.addEnterpriseBean(new SingletonBean(MyBean.class));
assembler.createApplication(config.configureApplication(new EjbModule(ejbJar)));
final OpenEjbConfiguration configuration = SystemInstance.get().getComponent(OpenEjbConfiguration.class);
final ResourceInfo resourceInfo = configuration.facilities.resources.get(0);
assertEquals("Default JDBC Database", resourceInfo.id);
assertEquals("true", resourceInfo.properties.getProperty("JtaManaged"));
}
use of org.apache.openejb.assembler.classic.ResourceInfo in project tomee by apache.
the class AutoConfigPersistenceUnitsTest method testInvalidRefs.
/**
* Existing data source "Orange", jta managed
* Existing data source "OrangeUnmanaged", not jta managed
* <p/>
* Persistence xml like so:
* <p/>
* <persistence-unit name="orange-unit">
* <jta-data-source>DoesNotExist</jta-data-source>
* <non-jta-data-source>AlsoDoesNotExist</non-jta-data-source>
* </persistence-unit>
* <p/>
* We should automatically hook them up to the configured
* datasources that do match
*
* @throws Exception
*/
public void testInvalidRefs() throws Exception {
final ResourceInfo jta = addDataSource("Orange", OrangeDriver.class, "jdbc:orange:some:stuff", true);
final ResourceInfo nonJta = addDataSource("OrangeUnmanaged", OrangeDriver.class, "jdbc:orange:some:stuff", false);
assertSame(jta, resources.get(0));
assertSame(nonJta, resources.get(1));
final PersistenceUnitInfo unitInfo = addPersistenceUnit("orange-unit", "DoesNotExist", "AlsoDoesNotExist");
assertNotNull(unitInfo);
assertEquals(jta.id, unitInfo.jtaDataSource);
assertEquals(nonJta.id, unitInfo.nonJtaDataSource);
}
Aggregations