001: /**
002: * EasyBeans
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id:$
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployer;
025:
026: import java.net.URL;
027: import java.net.URLClassLoader;
028: import java.util.ArrayList;
029: import java.util.List;
030:
031: import org.ow2.easybeans.api.EZBContainer;
032: import org.ow2.easybeans.api.EZBContainerException;
033: import org.ow2.easybeans.loader.EasyBeansClassLoader;
034: import org.ow2.easybeans.persistence.PersistenceUnitManager;
035: import org.ow2.easybeans.persistence.xml.PersistenceXmlFileAnalyzer;
036: import org.ow2.easybeans.persistence.xml.PersistenceXmlFileAnalyzerException;
037: import org.ow2.util.ee.deploy.api.archive.ArchiveException;
038: import org.ow2.util.ee.deploy.api.archive.IArchive;
039: import org.ow2.util.ee.deploy.api.deployable.CARDeployable;
040: import org.ow2.util.ee.deploy.api.deployable.EARDeployable;
041: import org.ow2.util.ee.deploy.api.deployable.EJB3Deployable;
042: import org.ow2.util.ee.deploy.api.deployable.EJBDeployable;
043: import org.ow2.util.ee.deploy.api.deployable.IDeployable;
044: import org.ow2.util.ee.deploy.api.deployable.LibDeployable;
045: import org.ow2.util.ee.deploy.api.deployable.WARDeployable;
046: import org.ow2.util.ee.deploy.api.deployer.DeployerException;
047: import org.ow2.util.log.Log;
048: import org.ow2.util.log.LogFactory;
049:
050: /**
051: * This class manage the deployment of EAR for web container. It extracts EJB3
052: * and send them to EasyBeans while the War are given to the web container.
053: * @author Florent Benoit
054: */
055: public abstract class AbsWebContainerDeployer extends AbsDeployer {
056:
057: /**
058: * Logger.
059: */
060: private static Log logger = LogFactory
061: .getLog(AbsWebContainerDeployer.class);
062:
063: /**
064: * Deploy the WAR files present in the given EAR.
065: * @param earDeployable the EAR containing the WARs
066: * @param earURL the EAR URL
067: * @param earClassLoader the EAR classloader
068: * @param parentClassLoader the parent classloader (EJB) to use
069: * @throws DeployerException if the wars are not deployed.
070: */
071: protected abstract void deployWARs(
072: final EARDeployable earDeployable, final URL earURL,
073: final ClassLoader earClassLoader,
074: final ClassLoader parentClassLoader)
075: throws DeployerException;
076:
077: /**
078: * Deploy an EAR (called by the deploy method).
079: * @param earDeployable a given EAR deployable
080: * @throws DeployerException if the deployment is not done.
081: */
082: protected void deployEAR(final EARDeployable earDeployable)
083: throws DeployerException {
084:
085: logger.info("Deploying {0}", earDeployable);
086:
087: // Get URL of this EAR
088: URL earURL = null;
089: try {
090: earURL = earDeployable.getArchive().getURL();
091: } catch (ArchiveException e) {
092: throw new DeployerException(
093: "Cannot get the URL for the deployable '"
094: + earDeployable + "'.", e);
095: }
096:
097: // Create a Root classloader for this EAR
098: // Empty classloader
099: ClassLoader earClassLoader = new URLClassLoader(new URL[0],
100: Thread.currentThread().getContextClassLoader());
101:
102: // Get EJBs of this EAR
103: List<EJB3Deployable> ejb3s = earDeployable.getEJB3Deployables();
104:
105: // Get the URLs of EJB, WEB and Clients
106: List<URL> urlsEJB = new ArrayList<URL>();
107: for (EJB3Deployable ejb : ejb3s) {
108: try {
109: urlsEJB.add(ejb.getArchive().getURL());
110: } catch (ArchiveException e) {
111: throw new DeployerException(
112: "Cannot get the URL for the archive '"
113: + ejb.getArchive() + "'", e);
114: }
115: }
116: List<URL> urlsWAR = new ArrayList<URL>();
117: for (WARDeployable war : earDeployable.getWARDeployables()) {
118: try {
119: urlsWAR.add(war.getArchive().getURL());
120: } catch (ArchiveException e) {
121: throw new DeployerException(
122: "Cannot get the URL for the archive '"
123: + war.getArchive() + "'", e);
124: }
125: }
126: List<URL> urlsClient = new ArrayList<URL>();
127: for (CARDeployable car : earDeployable.getCARDeployables()) {
128: try {
129: urlsClient.add(car.getArchive().getURL());
130: } catch (ArchiveException e) {
131: throw new DeployerException(
132: "Cannot get the URL for the archive '"
133: + car.getArchive() + "'", e);
134: }
135: }
136:
137: // Get libraries of this EAR
138: List<LibDeployable> libs = earDeployable.getLibDeployables();
139:
140: // Create array of URLs with EJBs + Libraries
141: List<URL> urls = new ArrayList<URL>();
142: for (EJBDeployable ejb : ejb3s) {
143: try {
144: urls.add(ejb.getArchive().getURL());
145: } catch (ArchiveException e) {
146: throw new DeployerException(
147: "Cannot get the URL for the Archive '"
148: + ejb.getArchive() + "'.", e);
149: }
150: }
151: for (LibDeployable lib : libs) {
152: try {
153: urls.add(lib.getArchive().getURL());
154: } catch (ArchiveException e) {
155: throw new DeployerException(
156: "Cannot get the URL for the Archive '"
157: + lib.getArchive() + "'.", e);
158:
159: }
160: }
161:
162: // Create classloader with these URLs
163: URL[] arrayURLs = urls.toArray(new URL[urls.size()]);
164: ClassLoader ejbClassLoader = new EasyBeansClassLoader(
165: arrayURLs, Thread.currentThread()
166: .getContextClassLoader());
167:
168: // Get Persistence unit manager
169: PersistenceUnitManager persistenceUnitManager = getPersistenceUnitManager(
170: earDeployable, ejbClassLoader);
171:
172: // Get Extra libraries
173: List<IArchive> libArchives = getLibArchives(earDeployable);
174:
175: // Create containers for each EJB deployable
176: List<EZBContainer> containers = new ArrayList<EZBContainer>();
177: for (EJBDeployable ejb : ejb3s) {
178: containers.add(getEmbedded().createContainer(
179: ejb.getArchive()));
180: }
181:
182: // Configure containers
183: for (EZBContainer container : containers) {
184: // Set the classloader that needs to be used
185: container.setClassLoader(ejbClassLoader);
186:
187: // Add persistence context found
188: container.setPersistenceUnitManager(persistenceUnitManager);
189:
190: // Add the metadata
191: container.setExtraArchives(libArchives);
192: }
193:
194: // Start containers
195: for (EZBContainer container : containers) {
196: try {
197: container.start();
198: } catch (EZBContainerException e) {
199: logger.error("Cannot start container {0}", container
200: .getName(), e);
201: }
202: }
203:
204: // Deploy Web App
205: deployWARs(earDeployable, earURL, earClassLoader,
206: ejbClassLoader);
207:
208: }
209:
210: /**
211: * Undeploy an EAR (called by the undeploy method).
212: * @param tmpEARDeployable a given EAR deployable
213: * @throws DeployerException if the undeployment is not done.
214: */
215: protected void undeployEAR(final EARDeployable tmpEARDeployable)
216: throws DeployerException {
217: logger.info("Undeploying {0}", tmpEARDeployable);
218:
219: // From which deployable get the containers deployed
220: EARDeployable earDeployable = tmpEARDeployable;
221:
222: // Check if this archive has been unpacked ?
223: EARDeployable unpackedDeployable = earDeployable
224: .getUnpackedDeployable();
225: if (unpackedDeployable != null) {
226: earDeployable = unpackedDeployable;
227: }
228:
229: // Need to undeploy Wars from the EAR
230: List<WARDeployable> wars = earDeployable.getWARDeployables();
231: if (wars != null) {
232: for (WARDeployable war : wars) {
233: // undeploy the given war
234: try {
235: undeployWAR(war);
236: } catch (DeployerException e) {
237: logger
238: .error(
239: "Cannot undeploy the WAR deployable ''{0}''",
240: war, e);
241: }
242: }
243: }
244:
245: // Undeploy EJB3s
246: undeployEJB3FromEAR(earDeployable);
247:
248: logger.info("''{0}'' EAR Deployable is now undeployed",
249: tmpEARDeployable);
250: }
251:
252: /**
253: * Undeploy the given deployable. It can be an EJB jar, EAR, WAR, etc.
254: * @param deployable a given deployable to undeploy
255: * @throws DeployerException if the undeploy operation fails.
256: */
257: public void undeploy(final IDeployable deployable)
258: throws DeployerException {
259: if (deployable instanceof EARDeployable) {
260: undeployEAR((EARDeployable) deployable);
261: } else {
262: throw new UnsupportedOperationException(
263: "Undeploy only .ear files");
264: }
265: }
266:
267: /**
268: * Undeploy an given WAR (called by the undeploy method).
269: * @param warDeployable a given WAR deployable
270: * @throws DeployerException if the undeployment is not done.
271: */
272: protected abstract void undeployWAR(
273: final WARDeployable warDeployable) throws DeployerException;
274: }
|