use of org.apache.openejb.OpenEJBException in project tomee by apache.
the class CdiPlugin method stop.
public void stop() throws OpenEJBException {
final ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
try {
// Setting context class loader for cleaning
Thread.currentThread().setContextClassLoader(classLoader);
// Fire shut down
webBeansContext.getBeanManagerImpl().fireEvent(new BeforeShutdownImpl());
// Destroys context
webBeansContext.getContextsService().destroy(null);
// Free all plugin resources
webBeansContext.getPluginLoader().shutDown();
// Clear extensions
webBeansContext.getExtensionLoader().clear();
// Delete Resolutions Cache
webBeansContext.getBeanManagerImpl().getInjectionResolver().clearCaches();
// Delete AnnotateTypeCache
webBeansContext.getAnnotatedElementFactory().clear();
// Clear the resource injection service
final CdiResourceInjectionService injectionServices = (CdiResourceInjectionService) webBeansContext.getService(ResourceInjectionService.class);
injectionServices.clear();
// Clear singleton list
WebBeansFinder.clearInstances(WebBeansUtil.getCurrentClassLoader());
} catch (final Exception e) {
throw new OpenEJBException(e);
} finally {
Thread.currentThread().setContextClassLoader(oldCl);
}
}
use of org.apache.openejb.OpenEJBException in project tomee by apache.
the class AutoConfig method resolveDestinationLinks.
/**
* Set resource id in all message-destination-refs and MDBs that are using message destination links.
*/
private void resolveDestinationLinks(final AppModule appModule) throws OpenEJBException {
// build up a link resolver
final LinkResolver<MessageDestination> destinationResolver = new LinkResolver<>();
for (final EjbModule ejbModule : appModule.getEjbModules()) {
final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
if (assembly != null) {
for (final MessageDestination destination : assembly.getMessageDestination()) {
destinationResolver.add(ejbModule.getModuleUri(), destination.getMessageDestinationName(), destination);
}
}
}
for (final ClientModule clientModule : appModule.getClientModules()) {
for (final MessageDestination destination : clientModule.getApplicationClient().getMessageDestination()) {
destinationResolver.add(appModule.getModuleUri(), destination.getMessageDestinationName(), destination);
}
}
for (final WebModule webModule : appModule.getWebModules()) {
for (final MessageDestination destination : webModule.getWebApp().getMessageDestination()) {
destinationResolver.add(appModule.getModuleUri(), destination.getMessageDestinationName(), destination);
}
}
// remember the type of each destination so we can use it to fillin MDBs that don't declare destination type
final Map<MessageDestination, String> destinationTypes = new HashMap<>();
// if MessageDestination does not have a mapped name assigned, give it the destination from the MDB
for (final EjbModule ejbModule : appModule.getEjbModules()) {
final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
if (assembly == null) {
continue;
}
final URI moduleUri = ejbModule.getModuleUri();
final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
// MDB destination is deploymentId if none set
if (bean instanceof MessageDrivenBean) {
final MessageDrivenBean mdb = (MessageDrivenBean) bean;
final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
if (ejbDeployment == null) {
throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
}
// skip destination refs without a destination link
final String link = mdb.getMessageDestinationLink();
if (link == null || link.length() == 0) {
continue;
}
// resolve the destination... if we don't find one it is a configuration bug
final MessageDestination destination = destinationResolver.resolveLink(link, moduleUri);
if (destination == null) {
throw new OpenEJBException("Message destination " + link + " for message driven bean " + mdb.getEjbName() + " not found");
}
// get the destinationId is the mapped name
String destinationId = destination.getMappedName();
if (destinationId == null) {
// if we don't have a mapped name use the destination of the mdb
final Properties properties = mdb.getActivationConfig().toProperties();
destinationId = properties.getProperty("destination");
destination.setMappedName(destinationId);
}
if (mdb.getMessageDestinationType() != null && !destinationTypes.containsKey(destination)) {
destinationTypes.put(destination, mdb.getMessageDestinationType());
}
// destination identifier
ResourceLink resourceLink = ejbDeployment.getResourceLink("openejb/destination");
if (resourceLink == null) {
resourceLink = new ResourceLink();
resourceLink.setResRefName("openejb/destination");
ejbDeployment.addResourceLink(resourceLink);
}
resourceLink.setResId(destinationId);
}
}
}
// resolve all message destination refs with links and assign a ref id to the reference
for (final EjbModule ejbModule : appModule.getEjbModules()) {
final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
if (assembly == null) {
continue;
}
final URI moduleUri = ejbModule.getModuleUri();
final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
if (ejbDeployment == null) {
throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
}
for (final MessageDestinationRef ref : bean.getMessageDestinationRef()) {
// skip destination refs with a resource link already assigned
if (ref.getMappedName() == null && ejbDeployment.getResourceLink(ref.getName()) == null) {
final String destinationId = resolveDestinationId(ref, appModule, moduleUri, destinationResolver, destinationTypes);
if (destinationId != null) {
// build the link and add it
final ResourceLink resourceLink = new ResourceLink();
resourceLink.setResId(destinationId);
resourceLink.setResRefName(ref.getName());
ejbDeployment.addResourceLink(resourceLink);
}
}
}
}
}
for (final ClientModule clientModule : appModule.getClientModules()) {
final URI moduleUri = clientModule.getModuleUri();
for (final MessageDestinationRef ref : clientModule.getApplicationClient().getMessageDestinationRef()) {
final String destinationId = resolveDestinationId(ref, appModule, moduleUri, destinationResolver, destinationTypes);
if (destinationId != null) {
// for client modules we put the destinationId in the mapped name
ref.setMappedName(destinationId);
}
}
}
for (final WebModule webModule : appModule.getWebModules()) {
final URI moduleUri = URLs.uri(webModule.getModuleId());
for (final MessageDestinationRef ref : webModule.getWebApp().getMessageDestinationRef()) {
final String destinationId = resolveDestinationId(ref, appModule, moduleUri, destinationResolver, destinationTypes);
if (destinationId != null) {
// for web modules we put the destinationId in the mapped name
ref.setMappedName(destinationId);
}
}
}
// the info from the destination (which got filled in from the references)
for (final EjbModule ejbModule : appModule.getEjbModules()) {
final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
if (assembly == null) {
continue;
}
final URI moduleUri = URLs.uri(ejbModule.getModuleId());
final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
// MDB destination is deploymentId if none set
if (bean instanceof MessageDrivenBean) {
final MessageDrivenBean mdb = (MessageDrivenBean) bean;
if (!isJms(mdb)) {
continue;
}
final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
if (ejbDeployment == null) {
throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
}
// if destination type is already set in, continue
String destinationType = mdb.getMessageDestinationType();
if (destinationType != null) {
continue;
}
final String link = mdb.getMessageDestinationLink();
if (link != null && link.length() != 0) {
// resolve the destination... if we don't find one it is a configuration bug
final MessageDestination destination = destinationResolver.resolveLink(link, moduleUri);
if (destination == null) {
throw new OpenEJBException("Message destination " + link + " for message driven bean " + mdb.getEjbName() + " not found");
}
destinationType = destinationTypes.get(destination);
}
if (destinationType == null) {
// couldn't determine type... we'll have to guess
// if destination name contains the string "queue" or "topic" we use that
final Properties properties = mdb.getActivationConfig().toProperties();
final String destination = properties.getProperty("destination").toLowerCase();
if (destination.contains("queue")) {
destinationType = Queue.class.getName();
} else if (destination.contains("topic")) {
destinationType = Topic.class.getName();
} else {
// Queue is the default
destinationType = Queue.class.getName();
}
logger.info("Auto-configuring a message driven bean " + ejbDeployment.getDeploymentId() + " destination " + properties.getProperty("destination") + " to be destinationType " + destinationType);
}
if (destinationType != null) {
mdb.getActivationConfig().addProperty("destinationType", destinationType);
mdb.setMessageDestinationType(destinationType);
// topics need a clientId and subscriptionName
if ("javax.jms.Topic".equals(destinationType)) {
final Properties properties = mdb.getActivationConfig().toProperties();
if (!properties.containsKey("clientId")) {
mdb.getActivationConfig().addProperty("clientId", ejbDeployment.getDeploymentId());
}
if (!properties.containsKey("subscriptionName")) {
mdb.getActivationConfig().addProperty("subscriptionName", ejbDeployment.getDeploymentId() + "_subscription");
}
}
}
}
}
}
}
use of org.apache.openejb.OpenEJBException in project tomee by apache.
the class AutoConfig method resolveDestinationId.
private String resolveDestinationId(final MessageDestinationRef ref, AppModule appModule, final URI moduleUri, final LinkResolver<MessageDestination> destinationResolver, final Map<MessageDestination, String> destinationTypes) throws OpenEJBException {
// skip destination refs without a destination link
final String link = ref.getMessageDestinationLink();
if (link == null || link.length() == 0) {
return null;
}
// resolve the destination... if we don't find one it is a configuration bug
MessageDestination destination = destinationResolver.resolveLink(link, moduleUri);
if (destination == null && link.contains("#")) {
// try the app module URI + "/" + link instead
final List<EjbModule> ejbModules = appModule.getEjbModules();
for (final EjbModule ejbModule : ejbModules) {
final String shortModuleName = link.substring(0, link.indexOf("#"));
if (ejbModule.getModuleUri().toString().endsWith(shortModuleName)) {
final String appModuleLink = ejbModule.getModuleUri() + "#" + link.substring(link.indexOf("#") + 1);
destination = destinationResolver.resolveLink(appModuleLink, moduleUri);
if (destination != null) {
break;
}
}
}
}
if (destination == null) {
throw new OpenEJBException("Message destination " + link + " for message-destination-ref " + ref.getMessageDestinationRefName() + " not found");
}
// remember the type of each destination so we can use it to fillin MDBs that don't declare destination type
if (ref.getMessageDestinationType() != null && !destinationTypes.containsKey(destination)) {
destinationTypes.put(destination, ref.getMessageDestinationType());
}
// get the destinationId
final String destinationId = destination.getMappedName();
if (destinationId == null) {
destination.getMessageDestinationName();
}
return destinationId;
}
use of org.apache.openejb.OpenEJBException in project tomee by apache.
the class AppInfoBuilder method build.
public AppInfo build(final AppModule appModule) throws OpenEJBException {
// send an event so that it becomes pretty easy at this step to dynamically change the module description
// before going into the info tree. Pretty easy to hack on portability issues.
SystemInstance.get().fireEvent(new BeforeAppInfoBuilderEvent(appModule));
final AppInfo appInfo = new AppInfo();
appInfo.appId = appModule.getModuleId();
appInfo.path = appModule.getJarLocation();
appInfo.standaloneModule = appModule.isStandaloneModule() || appModule.isWebapp();
appInfo.delegateFirst = appModule.isDelegateFirst();
appInfo.watchedResources.addAll(appModule.getWatchedResources());
appInfo.mbeans.addAll(appModule.getAdditionalLibMbeans());
appInfo.jaxRsProviders.addAll(appModule.getJaxRsProviders());
appInfo.properties.putAll(appModule.getProperties());
if (appInfo.appId == null) {
throw new IllegalArgumentException("AppInfo.appId cannot be null");
}
if (appInfo.path == null) {
appInfo.path = appInfo.appId;
}
this.buildPojoConfiguration(appModule, appInfo);
this.buildAppResources(appModule, appInfo);
this.buildAppContainers(appModule, appInfo);
this.buildAppServices(appModule, appInfo);
//
// J2EE Connectors
//
this.buildConnectorModules(appModule, appInfo);
//
// Persistence Units
//
this.buildPersistenceModules(appModule, appInfo);
final List<String> containerIds = this.configFactory.getContainerIds();
for (final ConnectorInfo connectorInfo : appInfo.connectors) {
for (final MdbContainerInfo containerInfo : connectorInfo.inbound) {
containerIds.add(containerInfo.id);
}
}
for (final ContainerInfo containerInfo : appInfo.containers) {
containerIds.add(containerInfo.id);
}
//
// EJB Jars
//
final Map<EjbModule, EjbJarInfo> ejbJarInfos = new HashMap<>();
for (final EjbModule ejbModule : appModule.getEjbModules()) {
try {
final EjbJarInfo ejbJarInfo = this.ejbJarInfoBuilder.buildInfo(ejbModule);
ejbJarInfo.mbeans = ejbModule.getMbeans();
final Map<String, EjbDeployment> deploymentsByEjbName = ejbModule.getOpenejbJar().getDeploymentsByEjbName();
for (final EnterpriseBeanInfo bean : ejbJarInfo.enterpriseBeans) {
final EjbDeployment d = deploymentsByEjbName.get(bean.ejbName);
if (d.getContainerId() != null && !containerIds.contains(d.getContainerId())) {
for (final ContainerInfo containerInfo : appInfo.containers) {
if (containerInfo.id.endsWith("/" + d.getContainerId())) {
d.setContainerId(containerInfo.id);
break;
}
}
}
/*
* JRG - there's probably a better way of handling this, but this code handles the case when:
*
* A connector with two or more inbound adapter is registered, causing two containers named with the format:
* <moduleId>-<message listener interface>
*
* This code adjusts the container id for the associated MDBs by sticking the message listener interface on the end.
*
*/
if (bean instanceof MessageDrivenBeanInfo && !containerIds.contains(d.getContainerId()) && !skipMdb(bean)) {
final MessageDrivenBeanInfo mdb = (MessageDrivenBeanInfo) bean;
final String newContainerId = d.getContainerId() + "-" + mdb.mdbInterface;
if (containerIds.contains(newContainerId)) {
d.setContainerId(newContainerId);
}
}
if (!containerIds.contains(d.getContainerId()) && !skipMdb(bean)) {
final String msg = new Messages("org.apache.openejb.util.resources").format("config.noContainerFound", d.getContainerId(), d.getEjbName());
logger.fatal(msg);
throw new OpenEJBException(msg);
}
bean.containerId = d.getContainerId();
}
for (final PojoDeployment pojoDeployment : ejbModule.getOpenejbJar().getPojoDeployment()) {
final IdPropertiesInfo info = new IdPropertiesInfo();
info.id = pojoDeployment.getClassName();
info.properties.putAll(pojoDeployment.getProperties());
ejbJarInfo.pojoConfigurations.add(info);
}
ejbJarInfo.validationInfo = ValidatorBuilder.getInfo(ejbModule.getValidationConfig());
ejbJarInfo.portInfos.addAll(this.configureWebservices(ejbModule.getWebservices()));
ejbJarInfo.uniqueId = ejbModule.getUniqueId();
ejbJarInfo.webapp = ejbModule.isWebapp();
this.configureWebserviceSecurity(ejbJarInfo, ejbModule);
ejbJarInfos.put(ejbModule, ejbJarInfo);
appInfo.ejbJars.add(ejbJarInfo);
} catch (final OpenEJBException e) {
ConfigUtils.logger.warning("conf.0004", ejbModule.getJarLocation(), e.getMessage());
throw e;
}
}
// Create the JNDI info builder
final JndiEncInfoBuilder jndiEncInfoBuilder = new JndiEncInfoBuilder(appInfo);
if (appModule.getApplication() != null) {
// TODO figure out how to prevent adding stuff to the module and comp contexts from the application
// or maybe validate the xml so this won't happen.
jndiEncInfoBuilder.build(appModule.getApplication(), appInfo.appId, null, appModule.getModuleUri(), new JndiEncInfo(), new JndiEncInfo());
}
final List<EnterpriseBeanInfo> beans = new ArrayList<>();
// Build the JNDI tree for each ejb
for (final EjbModule ejbModule : appModule.getEjbModules()) {
final EjbJarInfo ejbJar = ejbJarInfos.get(ejbModule);
final Map<String, EnterpriseBean> beanData = ejbModule.getEjbJar().getEnterpriseBeansByEjbName();
for (final EnterpriseBeanInfo beanInfo : ejbJar.enterpriseBeans) {
beans.add(beanInfo);
// Get the ejb-jar.xml object
final EnterpriseBean enterpriseBean = beanData.get(beanInfo.ejbName);
// Build the JNDI info tree for the EJB
jndiEncInfoBuilder.build(enterpriseBean, beanInfo.ejbName, ejbJar.moduleName, ejbModule.getModuleUri(), ejbJar.moduleJndiEnc, beanInfo.jndiEnc);
jndiEncInfoBuilder.buildDependsOnRefs(enterpriseBean, beanInfo, ejbJar.moduleName);
}
}
// Check for circular references in Singleton @DependsOn
try {
References.sort(beans, new References.Visitor<EnterpriseBeanInfo>() {
@Override
public String getName(final EnterpriseBeanInfo bean) {
return bean.ejbDeploymentId;
}
@Override
public Set<String> getReferences(final EnterpriseBeanInfo bean) {
return new LinkedHashSet<>(bean.dependsOn);
}
});
} catch (final CircularReferencesException e) {
// List<List> circuits = e.getCircuits();
// TODO Seems we lost circular reference detection, or we do it elsewhere and don't need it here
}
//
// Application Clients
//
this.buildClientModules(appModule, appInfo, jndiEncInfoBuilder);
//
// Webapps
//
this.buildWebModules(appModule, jndiEncInfoBuilder, appInfo);
//
// Final AppInfo creation
//
final List<URL> additionalLibraries = appModule.getAdditionalLibraries();
for (final URL url : additionalLibraries) {
final File file = toFile(url);
try {
appInfo.libs.add(file.getCanonicalPath());
} catch (final IOException e) {
throw new OpenEJBException("Invalid application lib path " + file.getAbsolutePath());
}
}
if (appModule.getCmpMappings() != null) {
try {
appInfo.cmpMappingsXml = JpaJaxbUtil.marshal(EntityMappings.class, appModule.getCmpMappings());
} catch (final JAXBException e) {
throw new OpenEJBException("Unable to marshal cmp entity mappings", e);
}
}
final ReportValidationResults reportValidationResults = new ReportValidationResults();
reportValidationResults.deploy(appModule);
logger.info("config.appLoaded", appInfo.path);
appInfo.webAppAlone = appModule.isWebapp();
return appInfo;
}
use of org.apache.openejb.OpenEJBException in project tomee by apache.
the class ConfigurationFactory method toConfigDeclaration.
public static Object toConfigDeclaration(final String id, final URI uri) throws OpenEJBException {
final String serviceType;
try {
serviceType = uri.getHost();
final Object object;
try {
object = JaxbOpenejb.create(serviceType);
} catch (final Exception e) {
throw new OpenEJBException("Invalid URI '" + uri + "'. " + e.getMessage());
}
final Map<String, String> map;
try {
map = URISupport.parseParamters(uri);
} catch (final URISyntaxException e) {
throw new OpenEJBException("Unable to parse URI parameters '" + uri + "'. URISyntaxException: " + e.getMessage());
}
if (object instanceof AbstractService) {
final AbstractService service = (AbstractService) object;
service.setId(id);
service.setType(map.remove("type"));
service.setProvider(map.remove("provider"));
service.setClassName(map.remove("class-name"));
service.setConstructor(map.remove("constructor"));
service.setFactoryName(map.remove("factory-name"));
service.setPropertiesProvider(map.remove("properties-provider"));
service.setTemplate(map.remove("template"));
final String cp = map.remove("classpath");
if (null != cp) {
service.setClasspath(cp);
}
service.setClasspathAPI(map.remove("classpath-api"));
if (object instanceof Resource) {
final Resource resource = Resource.class.cast(object);
final String aliases = map.remove("aliases");
if (aliases != null) {
resource.getAliases().addAll(Arrays.asList(aliases.split(",")));
}
final String depOn = map.remove("depends-on");
if (depOn != null) {
resource.getDependsOn().addAll(Arrays.asList(depOn.split(",")));
}
resource.setPostConstruct(map.remove("post-construct"));
resource.setPreDestroy(map.remove("pre-destroy"));
}
service.getProperties().putAll(map);
} else if (object instanceof Deployments) {
final Deployments deployments = (Deployments) object;
deployments.setDir(map.remove("dir"));
deployments.setFile(map.remove("jar"));
final String cp = map.remove("classpath");
if (cp != null) {
final String[] paths = cp.split(File.pathSeparator);
final List<URL> urls = new ArrayList<>();
for (final String path : paths) {
final Set<String> values = ProvisioningUtil.realLocation(PropertyPlaceHolderHelper.value(path));
for (final String v : values) {
urls.add(new File(v).toURI().normalize().toURL());
}
}
deployments.setClasspath(new URLClassLoaderFirst(urls.toArray(new URL[urls.size()]), ParentClassLoaderFinder.Helper.get()));
}
} else if (SystemProperty.class.isInstance(object)) {
final SystemProperty sp = SystemProperty.class.cast(object);
sp.setName(map.remove("name"));
sp.setValue(map.remove("value"));
}
return object;
} catch (final Exception e) {
throw new OpenEJBException("Error declaring service '" + id + "'. Unable to create Service definition from URI '" + uri.toString() + "'", e);
}
}
Aggregations