for (PersistenceUnitInfo info : appInfo.persistenceUnits) {
try {
EntityManagerFactory factory = persistenceBuilder.createEntityManagerFactory(info, classLoader);
containerSystem.getJNDIContext().bind(PERSISTENCE_UNIT_NAMING_CONTEXT + info.id, factory);
} catch (NameAlreadyBoundException e) {
throw new OpenEJBException("PersistenceUnit already deployed: " + info.persistenceUnitRootUrl);
} catch (Exception e) {
throw new OpenEJBException(e);
}
}
// Connectors
for (ConnectorInfo connector : appInfo.connectors) {
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
try {
// todo add undeployment code for these
if (connector.resourceAdapter != null) {
createResource(connector.resourceAdapter);
}
for (ResourceInfo outbound : connector.outbound) {
createResource(outbound);
}
for (MdbContainerInfo inbound : connector.inbound) {
createContainer(inbound);
}
for (ResourceInfo adminObject : connector.adminObject) {
createResource(adminObject);
}
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
List<DeploymentInfo> allDeployments = new ArrayList<DeploymentInfo>();
// EJB
EjbJarBuilder ejbJarBuilder = new EjbJarBuilder(props, classLoader);
for (EjbJarInfo ejbJar : appInfo.ejbJars) {
HashMap<String, DeploymentInfo> deployments = ejbJarBuilder.build(ejbJar);
JaccPermissionsBuilder jaccPermissionsBuilder = new JaccPermissionsBuilder();
PolicyContext policyContext = jaccPermissionsBuilder.build(ejbJar, deployments);
if (System.getProperty(DUCT_TAPE_PROPERTY) == null) {
jaccPermissionsBuilder.install(policyContext);
}
TransactionPolicyFactory transactionPolicyFactory = createTransactionPolicyFactory(ejbJar, classLoader);
for (DeploymentInfo deploymentInfo : deployments.values()) {
CoreDeploymentInfo coreDeploymentInfo = (CoreDeploymentInfo) deploymentInfo;
coreDeploymentInfo.setTransactionPolicyFactory(transactionPolicyFactory);
}
MethodTransactionBuilder methodTransactionBuilder = new MethodTransactionBuilder();
methodTransactionBuilder.build(deployments, ejbJar.methodTransactions);
MethodConcurrencyBuilder methodConcurrencyBuilder = new MethodConcurrencyBuilder();
methodConcurrencyBuilder.build(deployments, ejbJar.methodConcurrency);
for (DeploymentInfo deploymentInfo : deployments.values()) {
containerSystem.addDeployment(deploymentInfo);
}
jndiBuilder.build(ejbJar, deployments);
// setup timers - must be after transaction attibutes are set
for (DeploymentInfo deploymentInfo : deployments.values()) {
CoreDeploymentInfo coreDeploymentInfo = (CoreDeploymentInfo) deploymentInfo;
if (coreDeploymentInfo.getComponentType() != BeanType.STATEFUL) {
Method ejbTimeout = coreDeploymentInfo.getEjbTimeout();
if (ejbTimeout != null) {
// If user set the tx attribute to RequiresNew change it to Required so a new transaction is not started
if (coreDeploymentInfo.getTransactionType(ejbTimeout) == TransactionType.RequiresNew) {
coreDeploymentInfo.setMethodTransactionAttribute(ejbTimeout, "Required");
}
// Create the timer
EjbTimerServiceImpl timerService = new EjbTimerServiceImpl(coreDeploymentInfo);
coreDeploymentInfo.setEjbTimerService(timerService);
} else {
coreDeploymentInfo.setEjbTimerService(new NullEjbTimerServiceImpl());
}
}
}
// process application exceptions
for (ApplicationExceptionInfo exceptionInfo : ejbJar.applicationException) {
try {
Class exceptionClass = classLoader.loadClass(exceptionInfo.exceptionClass);
for (DeploymentInfo deploymentInfo : deployments.values()) {
CoreDeploymentInfo coreDeploymentInfo = (CoreDeploymentInfo) deploymentInfo;
coreDeploymentInfo.addApplicationException(exceptionClass, exceptionInfo.rollback);
}
} catch (ClassNotFoundException e) {
logger.error("createApplication.invalidClass", e, exceptionInfo.exceptionClass, e.getMessage());
}
}
allDeployments.addAll(deployments.values());
}
allDeployments = sort(allDeployments);
// now that everything is configured, deploy to the container
if (start) {
for (DeploymentInfo deployment : allDeployments) {
try {
Container container = deployment.getContainer();
container.deploy(deployment);
logger.info("createApplication.createdEjb", deployment.getDeploymentID(), deployment.getEjbName(), container.getContainerID());
} catch (Throwable t) {
throw new OpenEJBException("Error deploying '"+deployment.getEjbName()+"'. Exception: "+t.getClass()+": "+t.getMessage(), t);
}
}
}
// App Client
for (ClientInfo clientInfo : appInfo.clients) {
// determind the injections
InjectionBuilder injectionBuilder = new InjectionBuilder(classLoader);
List<Injection> injections = injectionBuilder.buildInjections(clientInfo.jndiEnc);
// build the enc
JndiEncBuilder jndiEncBuilder = new JndiEncBuilder(clientInfo.jndiEnc, injections, clientInfo.moduleId, classLoader);
jndiEncBuilder.setClient(true);
jndiEncBuilder.setUseCrossClassLoaderRef(false);
Context context = (Context) jndiEncBuilder.build().lookup("env");
containerSystem.getJNDIContext().bind("java:openejb/client/" + clientInfo.moduleId + "/comp/env", context);
if (clientInfo.codebase != null) {
containerSystem.getJNDIContext().bind("java:openejb/client/" + clientInfo.moduleId + "/comp/path", clientInfo.codebase);
}
if (clientInfo.mainClass != null) {
containerSystem.getJNDIContext().bind("java:openejb/client/" + clientInfo.moduleId + "/comp/mainClass", clientInfo.mainClass);
}
if (clientInfo.callbackHandler != null) {
containerSystem.getJNDIContext().bind("java:openejb/client/" + clientInfo.moduleId + "/comp/callbackHandler", clientInfo.callbackHandler);
}
containerSystem.getJNDIContext().bind("java:openejb/client/" + clientInfo.moduleId + "/comp/injections", injections);
}
SystemInstance systemInstance = SystemInstance.get();
// WebApp
WebAppBuilder webAppBuilder = systemInstance.getComponent(WebAppBuilder.class);
if (webAppBuilder != null) {
webAppBuilder.deployWebApps(appInfo, classLoader);
}
if (start) {
EjbResolver globalEjbResolver = systemInstance.getComponent(EjbResolver.class);
globalEjbResolver.addAll(appInfo.ejbJars);
}
logger.info("createApplication.success", appInfo.jarPath);
deployedApplications.put(appInfo.jarPath, appInfo);
fireAfterApplicationCreated(appInfo);
return allDeployments;
} catch (Throwable t) {
try {
destroyApplication(appInfo);
} catch (Exception e1) {
logger.debug("createApplication.undeployFailed", e1, appInfo.jarPath);
}
throw new OpenEJBException(messages.format("createApplication.failed", appInfo.jarPath), t);
}
}