KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > wsgen > generator > axis > AxisWsEndpointGenerator


1 /**
2  * JOnAS : Java(TM) OpenSource Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: AxisWsEndpointGenerator.java,v 1.9 2005/01/21 13:35:50 pelletib Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ws.wsgen.generator.axis;
27
28 import java.io.File JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import org.apache.velocity.VelocityContext;
34
35 import org.objectweb.jonas_lib.genbase.GenBaseException;
36 import org.objectweb.jonas_lib.genbase.archive.Archive;
37 import org.objectweb.jonas_lib.genbase.archive.WebApp;
38 import org.objectweb.jonas_lib.genbase.generator.Config;
39
40 import org.objectweb.jonas_ws.deployment.api.JaxRpcPortComponentDesc;
41 import org.objectweb.jonas_ws.deployment.api.PortComponentDesc;
42 import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
43 import org.objectweb.jonas_ws.wsgen.WsGenException;
44 import org.objectweb.jonas_ws.wsgen.ddmodifier.WebServicesDDModifier;
45 import org.objectweb.jonas_ws.wsgen.ddmodifier.WsEndpointDDModifier;
46 import org.objectweb.jonas_ws.wsgen.generator.WsEndpointGenerator;
47
48 import org.objectweb.jonas.common.Log;
49
50 import org.objectweb.util.monolog.api.BasicLevel;
51 import org.objectweb.util.monolog.api.Logger;
52
53 /**
54  * Generate Axis specific config files for Endpoint
55  *
56  * @author Guillaume Sauthier
57  */

58 public class AxisWsEndpointGenerator extends WsEndpointGenerator {
59
60     /**
61      * unique JVelocity instance
62      */

63     private static JVelocity jvelocity = null;
64
65     /**
66      * logger
67      */

68     private static Logger logger = Log.getLogger(Log.JONAS_WSGEN_PREFIX);
69
70     /**
71      * Axis Servlet classname
72      */

73     private static final String JavaDoc AXIS_SERVLET_CLASS = "org.objectweb.jonas.ws.axis.JAxisServlet";
74
75     /**
76      * init-param name for declaring server configuration file
77      */

78     private static final String JavaDoc SERVER_CONFIG = "axis.serverConfigFile";
79
80     /**
81      * WSDD Extension suffix
82      */

83     private static final String JavaDoc WSDD_SUFFIX = ".wsdd";
84
85     /**
86      * WSDD Extension prefix
87      */

88     private static final String JavaDoc WSDD_PREFIX = "deploy-server-";
89
90     /**
91      * count generated files
92      */

93     private static int count = 0;
94
95     /**
96      * generated server-config file
97      */

98     private File JavaDoc generatedServerConfig;
99
100     /**
101      * Creates a new AxisWsEndpointGenerator
102      *
103      * @param config Generator Configuration
104      * @param serviceDesc WebService Endpoint description
105      * @param ddm Web DD Modifier
106      * @param wsddm webservices.xml DD modifier
107      *
108      * @throws GenBaseException When instanciation fails
109      * @throws WsGenException When instanciation fails.
110      */

111     public AxisWsEndpointGenerator(Config config, ServiceDesc serviceDesc, WsEndpointDDModifier ddm,
112             WebServicesDDModifier wsddm, Archive arch) throws GenBaseException, WsGenException {
113         super(config, serviceDesc, ddm, wsddm, arch);
114
115         // init velocity
116
if (jvelocity == null) {
117             jvelocity = new JVelocity("deploy_endpoint.vm");
118         }
119     }
120
121     /**
122      * Generate server side configuration file
123      *
124      * @throws WsGenException When generation fails
125      */

126     public void generate() throws WsGenException {
127         String JavaDoc sName = getService().getName();
128
129         // construct VelocityContext
130
VelocityContext vc = VContextFactory.getContext(getService());
131
132         // Generate file
133
String JavaDoc filename = WSDD_PREFIX + (count++) + WSDD_SUFFIX;
134         generatedServerConfig = new File JavaDoc(getSources(), filename);
135         jvelocity.generate(generatedServerConfig, vc);
136
137         // remove existing servlets
138
for (Iterator JavaDoc i = getService().getPortComponents().iterator(); i.hasNext();) {
139             Object JavaDoc obj = i.next();
140             if (obj instanceof JaxRpcPortComponentDesc) {
141                 JaxRpcPortComponentDesc jax = (JaxRpcPortComponentDesc) obj;
142
143                 if (logger.isLoggable(BasicLevel.DEBUG)) {
144                     logger.log(BasicLevel.DEBUG, "Removing servlet '" + jax.getSibLink() + "'");
145                 }
146
147                 // remove servlet mapping
148
getModifier().removeServletMapping(jax.getSibLink());
149                 // remove old servlet
150
getModifier().removeServlet(jax.getSibLink());
151                 // change sib-link
152
getWsModifier().changeServletLink(sName, jax.getSibLink(), sName);
153             }
154         }
155
156         // and use our own
157
getModifier().addServlet(sName, AXIS_SERVLET_CLASS);
158         getModifier().addServletParam(sName, SERVER_CONFIG, filename);
159
160         // setup servlet-mappings
161
boolean requireDefaultMapping = false;
162         List JavaDoc usedServletMappings = new Vector JavaDoc();
163         for (Iterator JavaDoc i = getService().getPortComponents().iterator(); i.hasNext();) {
164             PortComponentDesc pcd = (PortComponentDesc) i.next();
165             String JavaDoc mapping = pcd.getMapping();
166             if (mapping != null) {
167                 // port has specified an endpoint URI
168
// use it for servlet-mapping
169
if (!usedServletMappings.contains(mapping)) {
170                     usedServletMappings.add(mapping);
171
172                     if (logger.isLoggable(BasicLevel.DEBUG)) {
173                         logger.log(BasicLevel.DEBUG, "Adding servlet-mapping for '" + sName + "' -> '" + mapping + "'");
174                     }
175
176                     getModifier().addServletMapping(sName, mapping);
177                 }
178             } else {
179                 // no specified endpoint uri for the port
180
requireDefaultMapping = true;
181             }
182         }
183         if (requireDefaultMapping) {
184             // try to set a default mapping
185
String JavaDoc defaultEndpointURI = getService().getEndpointURI();
186             if (defaultEndpointURI == null) {
187                 // default behavior
188
getModifier().addServletMapping(sName, "/" + sName + "/*");
189             } else {
190                 // use retieved value
191
getModifier().addServletMapping(sName, defaultEndpointURI);
192             }
193         }
194
195     }
196
197     /**
198      * Add generated files in given Archive
199      *
200      * @param archive WebApp archive
201      *
202      * @throws WsGenException When cannot add files in archive
203      */

204     public void addFiles(Archive archive) throws WsGenException {
205         // archive must be a WebApp
206
if (!(archive instanceof WebApp)) {
207             String JavaDoc err = getI18n().getMessage("AxisWsEndpointGenerator.addFiles.illegal", archive.getRootFile());
208             throw new IllegalArgumentException JavaDoc(err);
209         }
210
211         WebApp web = (WebApp) archive;
212         web.addFileIn("WEB-INF/", generatedServerConfig);
213
214     }
215 }
Popular Tags