001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2003-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.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: EjbJarModifier.java 9457 2006-08-24 12:58:41Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ws.wsgen.modifier;
025:
026: import java.io.File;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.jar.Attributes;
030:
031: import org.w3c.dom.Document;
032:
033: import org.objectweb.jonas_lib.genbase.GenBaseException;
034: import org.objectweb.jonas_lib.genbase.archive.Application;
035: import org.objectweb.jonas_lib.genbase.archive.Archive;
036: import org.objectweb.jonas_lib.genbase.archive.DummyApplication;
037: import org.objectweb.jonas_lib.genbase.archive.DummyWebApp;
038: import org.objectweb.jonas_lib.genbase.archive.Ejb;
039: import org.objectweb.jonas_lib.genbase.archive.EjbJar;
040: import org.objectweb.jonas_lib.genbase.archive.WebApp;
041: import org.objectweb.jonas_lib.genbase.modifier.ArchiveModifier;
042: import org.objectweb.jonas_lib.version.Version;
043:
044: import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
045: import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
046: import org.objectweb.jonas_ws.wsgen.ddmodifier.ContextDDModifier;
047: import org.objectweb.jonas_ws.wsgen.ddmodifier.WebJettyDDModifier;
048: import org.objectweb.jonas_ws.wsgen.ddmodifier.WsClientDDModifier;
049: import org.objectweb.jonas_ws.wsgen.ddmodifier.WsEndpointDDModifier;
050: import org.objectweb.jonas_ws.wsgen.generator.Generator;
051: import org.objectweb.jonas_ws.wsgen.generator.GeneratorFactory;
052: import org.objectweb.jonas_ws.wsgen.generator.SecurityGenerator;
053:
054: import org.objectweb.util.monolog.api.BasicLevel;
055:
056: /**
057: * Modify a given EjbJar.
058: *
059: * @author Guillaume Sauthier
060: */
061: public class EjbJarModifier extends ArchiveModifier {
062:
063: /** modified ejbjar */
064: private EjbJar ejbjar = null;
065:
066: /**
067: * Creates a new EjbJarModifier object.
068: *
069: * @param ejbjar EjbJar Archive
070: */
071: public EjbJarModifier(EjbJar ejbjar) {
072: super (ejbjar);
073: this .ejbjar = ejbjar;
074: }
075:
076: /**
077: * modify the current EjbJar. If EjbJar is contained in not an application
078: * and have webservices endpoints, A DummyApplication is created and
079: * modification process launched against the newly created application. If
080: * EjbJar is contained in an application + webservices endpoints, a
081: * DummyWebApp is created to hold "facade" servlet managing SOAP processing.
082: *
083: * @return an EjbJar or an Application Archive
084: *
085: * @throws GenBaseException When generation or storing fails
086: */
087: public Archive modify() throws GenBaseException {
088:
089: getLogger().log(BasicLevel.INFO,
090: "Processing EjbJar " + ejbjar.getName());
091:
092: // Update MANIFEST with Version Number
093: // used to say if WsGen has already been applied to this Module
094: Attributes main = this .ejbjar.getManifest().getMainAttributes();
095: main.put(new Attributes.Name(
096: WsGenModifierConstants.WSGEN_JONAS_VERSION_ATTR),
097: Version.getNumber());
098:
099: GeneratorFactory gf = GeneratorFactory.getInstance();
100: Document jejbjar = ejbjar.getJonasEjbJarDoc();
101:
102: // Webservices endpoint
103: List sds = ejbjar.getServiceDescs();
104:
105: if (sds.size() != 0) {
106: if (ejbjar.getApplication() == null) {
107: // we have webservices outside of an application
108:
109: /**
110: * Process : - create a default Application - add EjbJar within -
111: * return ApplicationModifier.modify()
112: */
113: String ejbName = ejbjar.getRootFile().getName();
114: String earName = ejbName.substring(0, ejbName.length()
115: - ".jar".length())
116: + ".ear";
117: Application application = new DummyApplication(earName);
118: ejbjar.setApplication(application);
119: application.addEjbJar(ejbjar);
120: ApplicationModifier am = new ApplicationModifier(
121: application);
122:
123: return am.modify();
124:
125: } else {
126: // we have webservices inside of an application
127:
128: /**
129: * Process : - create a default WebApp - add in Application -
130: * iterate over the services - add files in web instead of
131: * ejbjar
132: */
133:
134: String warName = null;
135: String war = ejbjar.getWarName();
136: if (war != null) {
137: warName = war;
138: } else {
139: String ejbName = ejbjar.getRootFile().getName();
140: String contextName = ejbName.substring(0, ejbName
141: .length()
142: - ".jar".length());
143: warName = contextName + ".war";
144: }
145:
146: Application application = ejbjar.getApplication();
147: WebApp web = new DummyWebApp(application, warName);
148:
149: for (Iterator i = sds.iterator(); i.hasNext();) {
150: ServiceDesc sd = (ServiceDesc) i.next();
151:
152: // create dd modifier
153: WsEndpointDDModifier ddm = new WsEndpointDDModifier(
154: web.getWebAppDoc());
155:
156: // launch generation
157: Generator g = gf
158: .newGenerator(sd, ddm, null, ejbjar);
159: g.generate();
160: g.compile();
161: // add files in web archive
162: g.addFiles(web);
163:
164: //Now generate the security for the web app
165: Document context = web.getContextDoc();
166: if (context == null) {
167: context = web.newContextDoc();
168: }
169: ContextDDModifier cddm = new ContextDDModifier(
170: context);
171: Document webJetty = web.getWebJettyDoc();
172: if (webJetty == null) {
173: webJetty = web.newWebJettyDoc();
174: }
175: WebJettyDDModifier wjddm = new WebJettyDDModifier(
176: webJetty);
177: Document jonaswebservices = ejbjar
178: .getJonasWebservicesDoc();
179: SecurityGenerator sm = new SecurityGenerator(
180: jonaswebservices);
181: sm.generate(ddm, cddm, wjddm);
182:
183: }
184: // save webapp
185: application
186: .addWebApp(
187: new WebApp(save(gf.getConfiguration(),
188: "webapps" + File.separator
189: + web.getName(), web)),
190: ejbjar.getContextRoot());
191: }
192: }
193:
194: // add client files
195: List ejbs = ejbjar.getEjbs();
196: for (Iterator i = ejbs.iterator(); i.hasNext();) {
197: Ejb ejb = (Ejb) i.next();
198: List refs = ejb.getServiceRefDescs();
199: for (Iterator j = refs.iterator(); j.hasNext();) {
200: ServiceRefDesc ref = (ServiceRefDesc) j.next();
201:
202: // create dd modifier
203: WsClientDDModifier ddm = new WsClientDDModifier(ref
204: .getServiceRefName(), jejbjar, ejb
205: .getJonasBeanElement());
206:
207: // launch generation
208: Generator g = gf.newGenerator(ref, ddm, ejbjar);
209: g.generate();
210: g.compile();
211: // add files in web archive
212: g.addFiles(ejbjar);
213: }
214: }
215:
216: return save(gf.getConfiguration(), "ejbjars" + File.separator
217: + ejbjar.getRootFile().getName());
218:
219: }
220: }
|