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: AxisWsEndpointGenerator.java 6137 2005-01-21 13:35:50Z pelletib $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ws.wsgen.generator.axis;
025:
026: import java.io.File;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Vector;
030:
031: import org.apache.velocity.VelocityContext;
032:
033: import org.objectweb.jonas_lib.genbase.GenBaseException;
034: import org.objectweb.jonas_lib.genbase.archive.Archive;
035: import org.objectweb.jonas_lib.genbase.archive.WebApp;
036: import org.objectweb.jonas_lib.genbase.generator.Config;
037:
038: import org.objectweb.jonas_ws.deployment.api.JaxRpcPortComponentDesc;
039: import org.objectweb.jonas_ws.deployment.api.PortComponentDesc;
040: import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
041: import org.objectweb.jonas_ws.wsgen.WsGenException;
042: import org.objectweb.jonas_ws.wsgen.ddmodifier.WebServicesDDModifier;
043: import org.objectweb.jonas_ws.wsgen.ddmodifier.WsEndpointDDModifier;
044: import org.objectweb.jonas_ws.wsgen.generator.WsEndpointGenerator;
045:
046: import org.objectweb.jonas.common.Log;
047:
048: import org.objectweb.util.monolog.api.BasicLevel;
049: import org.objectweb.util.monolog.api.Logger;
050:
051: /**
052: * Generate Axis specific config files for Endpoint
053: *
054: * @author Guillaume Sauthier
055: */
056: public class AxisWsEndpointGenerator extends WsEndpointGenerator {
057:
058: /**
059: * unique JVelocity instance
060: */
061: private static JVelocity jvelocity = null;
062:
063: /**
064: * logger
065: */
066: private static Logger logger = Log
067: .getLogger(Log.JONAS_WSGEN_PREFIX);
068:
069: /**
070: * Axis Servlet classname
071: */
072: private static final String AXIS_SERVLET_CLASS = "org.objectweb.jonas.ws.axis.JAxisServlet";
073:
074: /**
075: * init-param name for declaring server configuration file
076: */
077: private static final String SERVER_CONFIG = "axis.serverConfigFile";
078:
079: /**
080: * WSDD Extension suffix
081: */
082: private static final String WSDD_SUFFIX = ".wsdd";
083:
084: /**
085: * WSDD Extension prefix
086: */
087: private static final String WSDD_PREFIX = "deploy-server-";
088:
089: /**
090: * count generated files
091: */
092: private static int count = 0;
093:
094: /**
095: * generated server-config file
096: */
097: private File generatedServerConfig;
098:
099: /**
100: * Creates a new AxisWsEndpointGenerator
101: *
102: * @param config Generator Configuration
103: * @param serviceDesc WebService Endpoint description
104: * @param ddm Web DD Modifier
105: * @param wsddm webservices.xml DD modifier
106: *
107: * @throws GenBaseException When instanciation fails
108: * @throws WsGenException When instanciation fails.
109: */
110: public AxisWsEndpointGenerator(Config config,
111: ServiceDesc serviceDesc, WsEndpointDDModifier ddm,
112: WebServicesDDModifier wsddm, Archive arch)
113: throws GenBaseException, WsGenException {
114: super (config, serviceDesc, ddm, wsddm, arch);
115:
116: // init velocity
117: if (jvelocity == null) {
118: jvelocity = new JVelocity("deploy_endpoint.vm");
119: }
120: }
121:
122: /**
123: * Generate server side configuration file
124: *
125: * @throws WsGenException When generation fails
126: */
127: public void generate() throws WsGenException {
128: String sName = getService().getName();
129:
130: // construct VelocityContext
131: VelocityContext vc = VContextFactory.getContext(getService());
132:
133: // Generate file
134: String filename = WSDD_PREFIX + (count++) + WSDD_SUFFIX;
135: generatedServerConfig = new File(getSources(), filename);
136: jvelocity.generate(generatedServerConfig, vc);
137:
138: // remove existing servlets
139: for (Iterator i = getService().getPortComponents().iterator(); i
140: .hasNext();) {
141: Object obj = i.next();
142: if (obj instanceof JaxRpcPortComponentDesc) {
143: JaxRpcPortComponentDesc jax = (JaxRpcPortComponentDesc) obj;
144:
145: if (logger.isLoggable(BasicLevel.DEBUG)) {
146: logger.log(BasicLevel.DEBUG, "Removing servlet '"
147: + jax.getSibLink() + "'");
148: }
149:
150: // remove servlet mapping
151: getModifier().removeServletMapping(jax.getSibLink());
152: // remove old servlet
153: getModifier().removeServlet(jax.getSibLink());
154: // change sib-link
155: getWsModifier().changeServletLink(sName,
156: jax.getSibLink(), sName);
157: }
158: }
159:
160: // and use our own
161: getModifier().addServlet(sName, AXIS_SERVLET_CLASS);
162: getModifier().addServletParam(sName, SERVER_CONFIG, filename);
163:
164: // setup servlet-mappings
165: boolean requireDefaultMapping = false;
166: List usedServletMappings = new Vector();
167: for (Iterator i = getService().getPortComponents().iterator(); i
168: .hasNext();) {
169: PortComponentDesc pcd = (PortComponentDesc) i.next();
170: String mapping = pcd.getMapping();
171: if (mapping != null) {
172: // port has specified an endpoint URI
173: // use it for servlet-mapping
174: if (!usedServletMappings.contains(mapping)) {
175: usedServletMappings.add(mapping);
176:
177: if (logger.isLoggable(BasicLevel.DEBUG)) {
178: logger.log(BasicLevel.DEBUG,
179: "Adding servlet-mapping for '" + sName
180: + "' -> '" + mapping + "'");
181: }
182:
183: getModifier().addServletMapping(sName, mapping);
184: }
185: } else {
186: // no specified endpoint uri for the port
187: requireDefaultMapping = true;
188: }
189: }
190: if (requireDefaultMapping) {
191: // try to set a default mapping
192: String defaultEndpointURI = getService().getEndpointURI();
193: if (defaultEndpointURI == null) {
194: // default behavior
195: getModifier().addServletMapping(sName,
196: "/" + sName + "/*");
197: } else {
198: // use retieved value
199: getModifier().addServletMapping(sName,
200: defaultEndpointURI);
201: }
202: }
203:
204: }
205:
206: /**
207: * Add generated files in given Archive
208: *
209: * @param archive WebApp archive
210: *
211: * @throws WsGenException When cannot add files in archive
212: */
213: public void addFiles(Archive archive) throws WsGenException {
214: // archive must be a WebApp
215: if (!(archive instanceof WebApp)) {
216: String err = getI18n().getMessage(
217: "AxisWsEndpointGenerator.addFiles.illegal",
218: archive.getRootFile());
219: throw new IllegalArgumentException(err);
220: }
221:
222: WebApp web = (WebApp) archive;
223: web.addFileIn("WEB-INF/", generatedServerConfig);
224:
225: }
226: }
|