001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-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: VContextFactory.java 5409 2004-09-10 11:40:10Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ws.wsgen.generator.axis;
025:
026: import java.util.Enumeration;
027: import java.util.Hashtable;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.Vector;
032:
033: import javax.wsdl.Definition;
034: import javax.wsdl.Port;
035: import javax.wsdl.Service;
036: import javax.xml.namespace.QName;
037:
038: import org.apache.velocity.VelocityContext;
039:
040: import org.objectweb.jonas_lib.deployment.api.HandlerDesc;
041:
042: import org.objectweb.jonas_ws.deployment.api.MappingFile;
043: import org.objectweb.jonas_ws.deployment.api.PortComponentDesc;
044: import org.objectweb.jonas_ws.deployment.api.SSBPortComponentDesc;
045: import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
046: import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
047:
048: /**
049: * This class allows to create the Velocity Context used to build the generated
050: * sources with the Velocity templates.
051: *
052: * @author Xavier Delplanque (Bull)
053: */
054: public class VContextFactory {
055:
056: /**
057: * Provider property name
058: */
059: public static final String PROVIDER = "provider";
060:
061: /**
062: * JOnAS EJB Provider name
063: */
064: public static final String EJB_PROVIDER = "JOnASEJB";
065:
066: /**
067: * Default RPC Provider name
068: */
069: public static final String RPC_PROVIDER = "RPC";
070:
071: /**
072: * Mapping property name
073: */
074: public static final String MAPPINGS = "mappings";
075:
076: /**
077: * Port Components list property name
078: */
079: public static final String PORT_COMPONENTS = "portComponents";
080:
081: /**
082: * WSDL directory in Webapp
083: */
084: private static final String WEB_WSDL = "WEB-INF/wsdl/";
085:
086: /**
087: * WSDL directory in EjbJar/Client
088: */
089: private static final String META_WSDL = "META-INF/wsdl/";
090:
091: /**
092: * Empty Constructor for Utility class
093: */
094: private VContextFactory() {
095: }
096:
097: /**
098: * Creates the Velocity Context used to build the generated files with the
099: * Velocity templates.
100: *
101: * @param sd The ServiceDesc Deployment Descriptor
102: *
103: * @return a VelocityContext customized with the SD
104: */
105: public static VelocityContext getContext(ServiceDesc sd) {
106:
107: VelocityContext vc = new VelocityContext();
108:
109: // Set provider value
110: String wsdl;
111: if (sd.getPortComponents().get(0) instanceof SSBPortComponentDesc) {
112: // EJB Provider
113: vc.put(PROVIDER, EJB_PROVIDER);
114: // get wsdl name
115: wsdl = sd.getWSDL().getName().substring(META_WSDL.length());
116: } else {
117: // JAXRPC Provider
118: vc.put(PROVIDER, RPC_PROVIDER);
119: // get wsdl name
120: wsdl = sd.getWSDL().getName().substring(WEB_WSDL.length());
121: }
122:
123: // Add all ports
124: List ports = sd.getPortComponents();
125: List portComponents = new Vector();
126: for (Iterator it = ports.iterator(); it.hasNext();) {
127: PortComponentDesc pcd = (PortComponentDesc) it.next();
128: portComponents.add(new VcPortComponent(pcd, wsdl));
129: }
130: vc.put(PORT_COMPONENTS, portComponents);
131:
132: // add types mappings
133: List mappings = new Vector();
134: MappingFile mf = sd.getMapping();
135: for (Iterator m = mf.getXmlTypeMappings(); m.hasNext();) {
136: QName xml = (QName) m.next();
137: String classname = mf.getClassname(xml);
138:
139: // if we have an Array, Axis use a different representation
140: if (classname.endsWith("[]")) {
141: mappings.add(new VcArrayMapping(xml, classname));
142: } else {
143: mappings.add(new VcBeanMapping(xml, classname));
144: }
145: }
146: vc.put(MAPPINGS, mappings);
147:
148: return vc;
149: }
150:
151: /**
152: * Creates the Velocity Context used to build the generated files with the
153: * Velocity templates.
154: *
155: * @param sr The ServiceRefDesc Deployment Descriptor
156: *
157: * @return a VelocityContext customized with the SR
158: */
159: public static VelocityContext getContext(ServiceRefDesc sr) {
160:
161: VelocityContext vc = new VelocityContext();
162:
163: Hashtable pcds = new Hashtable();
164:
165: // for each service-ref handler
166: List hrs = sr.getHandlerRefs();
167:
168: Vector commonh = new Vector();
169: for (Iterator itHr = hrs.iterator(); itHr.hasNext();) {
170: HandlerDesc hr = (HandlerDesc) itHr.next();
171: List pcns = hr.getPortNames();
172:
173: if (pcns.size() != 0) {
174: // a port name list is defined : add the handler to each
175: // specified port
176: for (Iterator itPcn = pcns.iterator(); itPcn.hasNext();) {
177: String pcn = (String) itPcn.next();
178: if (!pcds.containsKey(pcn)) {
179: pcds.put(pcn, new Vector());
180: }
181: ((Vector) pcds.get(pcn)).add(hr);
182: }
183: } else {
184: // no port name is specified : the handler is added to each
185: // port
186: commonh.add(hr);
187: }
188: }
189:
190: // now, we can create the port component list
191: Vector portComponents = new Vector();
192:
193: // if there is not port-component-ref
194: if (pcds.isEmpty()) {
195: Definition def = sr.getWSDLFile().getDefinition();
196: Service s = def.getService(sr.getServiceQName());
197: Map ports = s.getPorts();
198: // for each wsdl:port
199: for (Iterator i = ports.values().iterator(); i.hasNext();) {
200: Port p = (Port) i.next();
201: portComponents.add(new VcPortComponent(p.getName(),
202: commonh));
203: }
204: } else {
205: for (Enumeration enPc = pcds.keys(); enPc.hasMoreElements();) {
206: String pcn = (String) enPc.nextElement();
207: // add common handlers to the list
208: Vector pchrs = (Vector) pcds.get(pcn);
209: pchrs.addAll(commonh);
210: portComponents.add(new VcPortComponent(pcn, pchrs));
211: }
212: }
213:
214: vc.put(PORT_COMPONENTS, portComponents);
215:
216: // add types mappings
217: List mappings = new Vector();
218: MappingFile mf = sr.getMappingFile();
219: if (mf != null) {
220: for (Iterator m = mf.getXmlTypeMappings(); m.hasNext();) {
221: QName xml = (QName) m.next();
222: String classname = mf.getClassname(xml);
223:
224: // if we have an Array, Axis use a different representaion
225: if (classname.endsWith("[]")) {
226: mappings.add(new VcArrayMapping(xml, classname));
227: } else {
228: mappings.add(new VcBeanMapping(xml, classname));
229: }
230: }
231: }
232: vc.put(MAPPINGS, mappings);
233:
234: return vc;
235: }
236: }
|