001: /**
002: * JOnAS : Java(TM) OpenSource Application Server
003: * Copyright (C) 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: EWSWsEndpointGenerator.java 7560 2005-10-21 13:50:04Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ws.wsgen.generator.ews;
025:
026: import java.io.File;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import org.w3c.dom.NodeList;
031:
032: import org.apache.axis.constants.Scope;
033: import org.apache.axis.wsdl.toJava.GeneratedFileInfo;
034: import org.apache.ws.ews.mapper.J2eeGeneratorFactory;
035: import org.apache.ws.ews.mapper.context.JAXRPCMapperContext;
036:
037: import org.objectweb.jonas_lib.I18n;
038: import org.objectweb.jonas_lib.genbase.GenBaseException;
039: import org.objectweb.jonas_lib.genbase.archive.Archive;
040: import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
041: import org.objectweb.jonas_lib.genbase.archive.WebApp;
042: import org.objectweb.jonas_lib.genbase.generator.Config;
043: import org.objectweb.jonas_lib.loader.AbsModuleClassLoader;
044:
045: import org.objectweb.jonas_ws.deployment.api.JaxRpcPortComponentDesc;
046: import org.objectweb.jonas_ws.deployment.api.PortComponentDesc;
047: import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
048: import org.objectweb.jonas_ws.wsgen.WsGenException;
049: import org.objectweb.jonas_ws.wsgen.ddmodifier.WebServicesDDModifier;
050: import org.objectweb.jonas_ws.wsgen.ddmodifier.WsEndpointDDModifier;
051: import org.objectweb.jonas_ws.wsgen.generator.WsEndpointGenerator;
052: import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.JOnASJ2EEWebServicesContext;
053: import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.emitter.FullEmitter;
054: import org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.factory.JOnASEndpointGeneratorFactory;
055:
056: import org.objectweb.jonas.common.Log;
057:
058: import org.objectweb.util.monolog.api.BasicLevel;
059: import org.objectweb.util.monolog.api.Logger;
060:
061: /**
062: * Generate Axis specific config files for Endpoint
063: * @author Guillaume Sauthier
064: */
065: public class EWSWsEndpointGenerator extends WsEndpointGenerator {
066:
067: /**
068: * logger
069: */
070: private static Logger logger = Log
071: .getLogger(Log.JONAS_WSGEN_EWS_PREFIX);
072:
073: /**
074: * Axis Servlet classname
075: */
076: private static final String AXIS_SERVLET_CLASS = "org.objectweb.jonas.ws.axis.JAxisServlet";
077:
078: /**
079: * init-param name for declaring server configuration file
080: */
081: private static final String SERVER_CONFIG = "axis.serverConfigFile";
082:
083: /**
084: * generated server-config file
085: */
086: private File generatedServerConfig;
087:
088: /**
089: * I18n
090: */
091: private static I18n i18n = I18n
092: .getInstance(EWSWsClientGenerator.class);
093:
094: /**
095: * Creates a new AxisWsEndpointGenerator
096: * @param config Generator Configuration
097: * @param serviceDesc WebService Endpoint description
098: * @param ddm Web DD Modifier
099: * @param wsddm webservices.xml DD modifier
100: * @param arch base archive
101: * @throws GenBaseException When instanciation fails
102: */
103: public EWSWsEndpointGenerator(Config config,
104: ServiceDesc serviceDesc, WsEndpointDDModifier ddm,
105: WebServicesDDModifier wsddm, Archive arch)
106: throws GenBaseException {
107: super (config, serviceDesc, ddm, wsddm, arch);
108: }
109:
110: /**
111: * Generate server side configuration file
112: * @throws WsGenException When generation fails
113: */
114: public void generate() throws WsGenException {
115:
116: // 1. Generation of wsdd server file
117: String filename = null;
118: try {
119: FullEmitter emitter = new FullEmitter();
120:
121: emitter
122: .setWscontext(prepareJ2EEWebServicesContext(getService()));
123: emitter.setMappingFileInputStream(getService()
124: .getMappingFileURL().openStream());
125: emitter.setOutputDir(this .getSources().getCanonicalPath());
126: J2eeGeneratorFactory factory = new JOnASEndpointGeneratorFactory();
127: factory.setEmitter(emitter);
128: emitter.setFactory(factory);
129: emitter.setTypeMappingVersion("1.3");
130: emitter.setScope(Scope.REQUEST);
131: emitter.setWrapArrays(true);
132:
133: emitter.setServerSide(true);
134: emitter
135: .run(getService().getLocalWSDLURL()
136: .toExternalForm());
137:
138: generatedServerConfig = new File(
139: findServerConfigFile(emitter.getGeneratedFileInfo()));
140: filename = generatedServerConfig.getName();
141: getLogger().log(
142: BasicLevel.INFO,
143: "Webservice endpoint WSDD file '" + filename
144: + "' sucessfully generated by EWS.");
145: } catch (Exception e) {
146: String err = i18n.getMessage(
147: "EWSWsEndpointGenerator.generate.WSDL2Java",
148: getService().getName(), e.getMessage());
149: logger.log(BasicLevel.ERROR, err, e);
150: throw new WsGenException(err, e);
151: }
152:
153: // 2. classpath creation
154: J2EEArchive j2eeArchive = (J2EEArchive) getArchive();
155: AbsModuleClassLoader cl = (AbsModuleClassLoader) j2eeArchive
156: .getModuleClassloader();
157: getConfig().setClasspath(
158: getConfig().getClasspath() + cl.getClasspath());
159:
160: // 3. Remove existing servlets
161: // * remove the servlet used by the port (store security informations)
162: // * remove the associated servlet-mapping (store old url-pattern value)
163: for (Iterator i = getService().getPortComponents().iterator(); i
164: .hasNext();) {
165: PortComponentDesc desc = (PortComponentDesc) i.next();
166:
167: String urlPatternOld = null;
168: NodeList securityRoleRefs = null;
169:
170: // port-component linked to a servlet :
171: if (desc.hasJaxRpcImpl()) {
172: JaxRpcPortComponentDesc jax = (JaxRpcPortComponentDesc) desc;
173:
174: if (logger.isLoggable(BasicLevel.DEBUG)) {
175: logger.log(BasicLevel.DEBUG, "Removing servlet '"
176: + jax.getSibLink() + "'");
177: }
178:
179: // remove servlet mapping
180: urlPatternOld = getModifier().removeServletMapping(
181: jax.getSibLink());
182:
183: // remove old servlet but pick up its security role refs
184: //getModifier().removeServlet(jax.getSibLink());
185: securityRoleRefs = getModifier()
186: .removeServletWithSecurity(jax.getSibLink());
187: }
188:
189: // in all case :
190: // create a servlet with all needed configuration
191: // and use our own
192: getModifier().addServlet(desc.getSibLink(),
193: AXIS_SERVLET_CLASS);
194: getModifier().addServletParam(desc.getSibLink(),
195: SERVER_CONFIG, filename);
196: getModifier().addServletParam(desc.getSibLink(),
197: "axis.development.system", "true");
198:
199: String mapping = null;
200: if (desc.getEndpointURI() != null) {
201: // simple style
202: mapping = desc.getEndpointURI();
203: } else {
204: // old style
205: mapping = "/" + desc.getName() + "/*";
206: }
207: getModifier().addServletMapping(desc.getSibLink(), mapping);
208:
209: if (desc.hasJaxRpcImpl()) {
210: // add the first element in the servletToSecurityRoleRefs map
211: // (currently consider that only one servlet with security
212: // constraints was removed !! - if there were more, keep the
213: // constraints of the last removed)
214: if (securityRoleRefs != null) {
215: getModifier().addServletSecurityRoleRefs(
216: desc.getSibLink(), securityRoleRefs);
217: }
218: // update url-pattern
219: getModifier().updateSecurityConstraint(urlPatternOld,
220: mapping);
221: }
222: }
223: }
224:
225: /**
226: * @param generatedFileInfo files generated by Axis
227: * @return Returns the first filename that matches "deploy-server-*.wsdd"
228: */
229: private static String findServerConfigFile(
230: GeneratedFileInfo generatedFileInfo) {
231: List generated = generatedFileInfo.getFileNames();
232: boolean found = false;
233: String filename = null;
234: for (Iterator i = generated.iterator(); i.hasNext() && !found;) {
235: String entry = (String) i.next();
236: // */deploy-server-*.wsdd
237: if (entry.matches(".*deploy-server-\\d+\\.wsdd$")) {
238: found = true;
239: filename = entry;
240: }
241: }
242: return filename;
243: }
244:
245: /**
246: * @param service the ServiceDesc to be used in J2EE WS Context
247: * @return Returns a configured J2EEWebServiceContext
248: */
249: private JAXRPCMapperContext prepareJ2EEWebServicesContext(
250: ServiceDesc service) {
251: JOnASJ2EEWebServicesContext ctx = new JOnASJ2EEWebServicesContext();
252: ctx.setServiceDesc(service);
253: return ctx;
254: }
255:
256: /**
257: * Add generated files in given Archive
258: * @param archive WebApp archive
259: * @throws WsGenException When cannot add files in archive
260: */
261: public void addFiles(Archive archive) throws WsGenException {
262: // archive must be a WebApp
263: if (!(archive instanceof WebApp)) {
264: String err = getI18n().getMessage(
265: "AxisWsEndpointGenerator.addFiles.illegal",
266: archive.getRootFile());
267: throw new IllegalArgumentException(err);
268: }
269:
270: WebApp web = (WebApp) archive;
271: web.addFileIn("WEB-INF/", generatedServerConfig);
272:
273: web.addDirectoryIn("WEB-INF/classes/", getClasses());
274: web.addDirectoryIn("WEB-INF/sources/", getSources());
275: }
276: }
|