use of org.apache.openejb.OpenEJBException in project tomee by apache.
the class ApplicationProperties method readPropertiesFiles.
private void readPropertiesFiles(final AppModule appModule) throws OpenEJBException {
final Collection<DeploymentModule> deploymentModule = appModule.getDeploymentModule();
// We intentionally add the AppModule itself LAST so its properties trump all
deploymentModule.add(appModule);
for (final DeploymentModule module : deploymentModule) {
final Object o = module.getAltDDs().get("application.properties");
if (o instanceof URL) {
final URL url = (URL) o;
try {
final Properties properties = IO.readProperties(url);
appModule.getProperties().putAll(properties);
} catch (final IOException e) {
throw new OpenEJBException("Cannot read application.properties: " + url, e);
}
} else if (o instanceof Properties) {
appModule.getProperties().putAll((Properties) o);
} else if (o != null) {
throw new OpenEJBException("Unknown application.properties type: " + o.getClass().getName());
}
}
}
use of org.apache.openejb.OpenEJBException 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.OpenEJBException in project tomee by apache.
the class AutoConfig method checkUnitDataSourceRefs.
private void checkUnitDataSourceRefs(final PersistenceUnit unit) throws OpenEJBException {
final Properties required = new Properties();
// check that non-jta-data-source does NOT point to a JtaManaged=true datasource
required.put("JtaManaged", "true");
final String invalidNonJta = findResourceId(unit.getNonJtaDataSource(), "DataSource", required, null);
if (invalidNonJta != null) {
throw new OpenEJBException("PeristenceUnit " + unit.getName() + " <non-jta-data-source> points to a jta managed Resource. Update Resource \"" + invalidNonJta + "\" to \"JtaManaged=false\", use a different Resource, or delete the <non-jta-data-source> element and a default will be supplied if possible.");
}
// check that jta-data-source does NOT point to a JtaManaged=false datasource
required.put("JtaManaged", "false");
final String invalidJta = findResourceId(unit.getJtaDataSource(), "DataSource", required, null);
if (invalidJta != null) {
throw new OpenEJBException("PeristenceUnit " + unit.getName() + " <jta-data-source> points to a non jta managed Resource. Update Resource \"" + invalidJta + "\" to \"JtaManaged=true\", use a different Resource, or delete the <jta-data-source> element and a default will be supplied if possible.");
}
}
use of org.apache.openejb.OpenEJBException 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.OpenEJBException in project tomee by apache.
the class AutoConfig method deploy.
private void deploy(final EjbModule ejbModule, final AppResources appResources) throws OpenEJBException {
final OpenejbJar openejbJar;
if (ejbModule.getOpenejbJar() != null) {
openejbJar = ejbModule.getOpenejbJar();
} else {
openejbJar = new OpenejbJar();
ejbModule.setOpenejbJar(openejbJar);
}
final Map<String, EjbDeployment> deployments = openejbJar.getDeploymentsByEjbName();
for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
final EjbDeployment ejbDeployment = deployments.get(bean.getEjbName());
if (ejbDeployment == null) {
throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
}
final String beanType = getType(bean);
final Class<? extends ContainerInfo> containerInfoType = ConfigurationFactory.getContainerInfoType(beanType);
logger.debug("Bean type of bean {0} is {1}", bean.getEjbName(), beanType);
if (ejbDeployment.getContainerId() == null && !skipMdb(bean)) {
logger.debug("Container for bean {0} is not set, looking for a suitable container", bean.getEjbName());
String containerId = getUsableContainer(containerInfoType, bean, appResources);
if (containerId == null) {
logger.debug("Suitable container for bean {0} not found, creating one", bean.getEjbName());
containerId = createContainer(containerInfoType, ejbDeployment, bean);
}
logger.debug("Setting container ID {0} for bean {1}", containerId, bean.getEjbName());
ejbDeployment.setContainerId(containerId);
}
logger.debug("Container ID for bean {0} is {1}", bean.getEjbName(), ejbDeployment.getContainerId());
// create the container if it doesn't exist
final List<String> containerIds = configFactory.getContainerIds();
final Collection<ContainerInfo> containerInfos = appResources.getContainerInfos();
for (final ContainerInfo containerInfo : containerInfos) {
containerIds.add(containerInfo.id);
}
if (!containerIds.contains(ejbDeployment.getContainerId()) && !skipMdb(bean)) {
logger.debug("Desired container {0} not found. Containers available: {1}. Creating a new container.", ejbDeployment.getContainerId(), Join.join(", ", containerIds));
createContainer(containerInfoType, ejbDeployment, bean);
}
// Resource reference
for (final ResourceRef ref : bean.getResourceRef()) {
processResourceRef(ref, ejbDeployment, appResources, ejbModule);
}
// Resource env reference
for (final JndiReference ref : bean.getResourceEnvRef()) {
processResourceEnvRef(ref, ejbDeployment, appResources, ejbModule.getClassLoader());
}
// Message destination reference
for (final MessageDestinationRef ref : bean.getMessageDestinationRef()) {
processResourceEnvRef(ref, ejbDeployment, appResources, ejbModule.getClassLoader());
}
// mdb message destination id
if (autoCreateResources && bean instanceof MessageDrivenBean) {
final MessageDrivenBean mdb = (MessageDrivenBean) bean;
final ResourceLink resourceLink = ejbDeployment.getResourceLink("openejb/destination");
if (resourceLink != null) {
try {
final String destinationId = getResourceEnvId(bean.getEjbName(), resourceLink.getResId(), mdb.getMessageDestinationType(), appResources);
resourceLink.setResId(destinationId);
} catch (final OpenEJBException e) {
// The MDB doesn't need the auto configured "openejb/destination" env entry
ejbDeployment.removeResourceLink("openejb/destination");
}
}
}
}
}
Aggregations