// Get URL of this EAR
URL earURL = null;
try {
earURL = earDeployable.getArchive().getURL();
} catch (ArchiveException e) {
throw new DeployerException("Cannot get the URL for the deployable '" + earDeployable + "'.", e);
}
// Create a Root classloader for this EAR
// Empty classloader
URLClassLoader earClassLoader = (URLClassLoader) newInstance(this.jClassLoaderConstructor, earURL.toExternalForm(), new URL[0],
Thread.currentThread().getContextClassLoader());
// Get the URLs of EJB, WEB and Clients
List<URL> urlsEJB = new ArrayList<URL>();
for (EJBDeployable ejb : earDeployable.getEJBDeployables()) {
try {
urlsEJB.add(ejb.getArchive().getURL());
} catch (ArchiveException e) {
throw new DeployerException("Cannot get the URL for the archive '" + ejb.getArchive() + "'", e);
}
}
List<URL> urlsWAR = new ArrayList<URL>();
for (WARDeployable war : earDeployable.getWARDeployables()) {
try {
urlsWAR.add(war.getArchive().getURL());
} catch (ArchiveException e) {
throw new DeployerException("Cannot get the URL for the archive '" + war.getArchive() + "'", e);
}
}
List<URL> urlsClient = new ArrayList<URL>();
for (CARDeployable car : earDeployable.getCARDeployables()) {
try {
urlsClient.add(car.getArchive().getURL());
} catch (ArchiveException e) {
throw new DeployerException("Cannot get the URL for the archive '" + car.getArchive() + "'", e);
}
}
// Set alt-dd
invoke(this.ejbSetAltDD, null, earClassLoader, urlsEJB.toArray(new URL[urlsEJB.size()]), new URL[urlsEJB.size()]);
invoke(this.webSetAltDD, null, earClassLoader, urlsWAR.toArray(new URL[urlsWAR.size()]), new URL[urlsWAR.size()]);
invoke(this.clientSetAltDD, null, earClassLoader, urlsClient.toArray(new URL[urlsClient.size()]), new URL[urlsClient.size()]);
// Deploy the RAR files of the EAR (if any)
deployRARs(earDeployable, earURL, earClassLoader);
// deploy EJB3s
// Get EJBs of this EAR
List<EJB3Deployable> ejb3s = earDeployable.getEJB3Deployables();
List<EJBDeployable<?>> ejbs = earDeployable.getEJBDeployables();
// Get libraries of this EAR
List<LibDeployable> libs = earDeployable.getLibDeployables();
// Create array of URLs with EJBs + Libraries
List<URL> urls = new ArrayList<URL>();
for (EJBDeployable<?> ejb : ejbs) {
try {
urls.add(ejb.getArchive().getURL());
} catch (ArchiveException e) {
throw new DeployerException("Cannot get the URL for the Archive '" + ejb.getArchive() + "'.", e);
}
}
for (LibDeployable lib : libs) {
try {
urls.add(lib.getArchive().getURL());
} catch (ArchiveException e) {
throw new DeployerException("Cannot get the URL for the Archive '" + lib.getArchive() + "'.", e);
}
}
// Create classloader with these URLs
URL[] arrayURLs = urls.toArray(new URL[urls.size()]);
// Child of the EAR classloader with RARs
ClassLoader ejbClassLoader = new EasyBeansClassLoader(arrayURLs, earClassLoader);
// Get Persistence unit manager
PersistenceUnitManager persistenceUnitManager = getPersistenceUnitManager(earDeployable, ejbClassLoader);
// Get Extra libraries
List<IArchive> libArchives = getLibArchives(earDeployable);
// Reset context ID of EJBs
addEjbContextIdToList(earDeployable, new ArrayList<String>(), true);
// Create containers for each EJB3 deployable
List<EZBContainer> containers = new ArrayList<EZBContainer>();
for (EJBDeployable ejb : ejb3s) {
containers.add(getEmbedded().createContainer(ejb));
}
// Create Resolver for EAR
EZBApplicationJNDIResolver applicationJNDIResolver = new ApplicationJNDIResolver();
// Create EasyBeans injection Holder
InjectionHolder ejbInjectionHolder = new InjectionHolder();
ejbInjectionHolder.setPersistenceUnitManager(persistenceUnitManager);
ejbInjectionHolder.setJNDIResolver(applicationJNDIResolver);
// Configure containers
for (EZBContainer container : containers) {
// Set the classloader that needs to be used
container.setClassLoader(ejbClassLoader);
// Set application name
container.setApplicationName(earDeployable.getModuleName());
// Add persistence context found
container.setPersistenceUnitManager(persistenceUnitManager);
// Add the metadata
container.setExtraArchives(libArchives);
// set parent JNDI Resolver
EZBContainerJNDIResolver containerJNDIResolver = container.getConfiguration().getContainerJNDIResolver();
containerJNDIResolver.setApplicationJNDIResolver(applicationJNDIResolver);
// Add child on application JNDI Resolver
applicationJNDIResolver.addContainerJNDIResolver(containerJNDIResolver);
// Resolve container
try {
container.resolve();
} catch (EZBContainerException e) {
throw new DeployerException("Cannot resolve the container '" + container.getArchive() + "'.", e);
}
}
// Start containers
for (EZBContainer container : containers) {
try {
container.start();
} catch (Exception e) {
logger.error("Cannot start container {0}", container.getName(), e);
// stop it
try {
container.stop();
getEmbedded().removeContainer(container);
} catch (Exception se) {
logger.error("Cannot stop failing container {0}", container.getName(), se);
}
// rethrow it
throw new DeployerException("Container '" + container.getName() + "' has failed", e);
}
}
// Deploy EJB 2.1
deployEJB21s(earDeployable, earURL, earClassLoader, ejbClassLoader);