KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-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: VContextFactory.java,v 1.7 2004/09/10 11:40:10 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ws.wsgen.generator.axis;
27
28 import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import javax.wsdl.Definition;
36 import javax.wsdl.Port;
37 import javax.wsdl.Service;
38 import javax.xml.namespace.QName JavaDoc;
39
40 import org.apache.velocity.VelocityContext;
41
42 import org.objectweb.jonas_lib.deployment.api.HandlerDesc;
43
44 import org.objectweb.jonas_ws.deployment.api.MappingFile;
45 import org.objectweb.jonas_ws.deployment.api.PortComponentDesc;
46 import org.objectweb.jonas_ws.deployment.api.SSBPortComponentDesc;
47 import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
48 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
49
50 /**
51  * This class allows to create the Velocity Context used to build the generated
52  * sources with the Velocity templates.
53  *
54  * @author Xavier Delplanque (Bull)
55  */

56 public class VContextFactory {
57
58     /**
59      * Provider property name
60      */

61     public static final String JavaDoc PROVIDER = "provider";
62
63     /**
64      * JOnAS EJB Provider name
65      */

66     public static final String JavaDoc EJB_PROVIDER = "JOnASEJB";
67
68     /**
69      * Default RPC Provider name
70      */

71     public static final String JavaDoc RPC_PROVIDER = "RPC";
72
73     /**
74      * Mapping property name
75      */

76     public static final String JavaDoc MAPPINGS = "mappings";
77
78     /**
79      * Port Components list property name
80      */

81     public static final String JavaDoc PORT_COMPONENTS = "portComponents";
82
83     /**
84      * WSDL directory in Webapp
85      */

86     private static final String JavaDoc WEB_WSDL = "WEB-INF/wsdl/";
87
88     /**
89      * WSDL directory in EjbJar/Client
90      */

91     private static final String JavaDoc META_WSDL = "META-INF/wsdl/";
92
93     /**
94      * Empty Constructor for Utility class
95      */

96     private VContextFactory() {
97     }
98
99     /**
100      * Creates the Velocity Context used to build the generated files with the
101      * Velocity templates.
102      *
103      * @param sd The ServiceDesc Deployment Descriptor
104      *
105      * @return a VelocityContext customized with the SD
106      */

107     public static VelocityContext getContext(ServiceDesc sd) {
108
109         VelocityContext vc = new VelocityContext();
110
111         // Set provider value
112
String JavaDoc wsdl;
113         if (sd.getPortComponents().get(0) instanceof SSBPortComponentDesc) {
114             // EJB Provider
115
vc.put(PROVIDER, EJB_PROVIDER);
116             // get wsdl name
117
wsdl = sd.getWSDL().getName().substring(META_WSDL.length());
118         } else {
119             // JAXRPC Provider
120
vc.put(PROVIDER, RPC_PROVIDER);
121             // get wsdl name
122
wsdl = sd.getWSDL().getName().substring(WEB_WSDL.length());
123         }
124
125         // Add all ports
126
List JavaDoc ports = sd.getPortComponents();
127         List JavaDoc portComponents = new Vector JavaDoc();
128         for (Iterator JavaDoc it = ports.iterator(); it.hasNext();) {
129             PortComponentDesc pcd = (PortComponentDesc) it.next();
130             portComponents.add(new VcPortComponent(pcd, wsdl));
131         }
132         vc.put(PORT_COMPONENTS, portComponents);
133
134         // add types mappings
135
List JavaDoc mappings = new Vector JavaDoc();
136         MappingFile mf = sd.getMapping();
137         for (Iterator JavaDoc m = mf.getXmlTypeMappings(); m.hasNext();) {
138             QName JavaDoc xml = (QName JavaDoc) m.next();
139             String JavaDoc classname = mf.getClassname(xml);
140
141             // if we have an Array, Axis use a different representation
142
if (classname.endsWith("[]")) {
143                 mappings.add(new VcArrayMapping(xml, classname));
144             } else {
145                 mappings.add(new VcBeanMapping(xml, classname));
146             }
147         }
148         vc.put(MAPPINGS, mappings);
149
150         return vc;
151     }
152
153     /**
154      * Creates the Velocity Context used to build the generated files with the
155      * Velocity templates.
156      *
157      * @param sr The ServiceRefDesc Deployment Descriptor
158      *
159      * @return a VelocityContext customized with the SR
160      */

161     public static VelocityContext getContext(ServiceRefDesc sr) {
162
163         VelocityContext vc = new VelocityContext();
164
165         Hashtable JavaDoc pcds = new Hashtable JavaDoc();
166
167         // for each service-ref handler
168
List JavaDoc hrs = sr.getHandlerRefs();
169
170         Vector JavaDoc commonh = new Vector JavaDoc();
171         for (Iterator JavaDoc itHr = hrs.iterator(); itHr.hasNext();) {
172             HandlerDesc hr = (HandlerDesc) itHr.next();
173             List JavaDoc pcns = hr.getPortNames();
174
175             if (pcns.size() != 0) {
176                 // a port name list is defined : add the handler to each
177
// specified port
178
for (Iterator JavaDoc itPcn = pcns.iterator(); itPcn.hasNext();) {
179                     String JavaDoc pcn = (String JavaDoc) itPcn.next();
180                     if (!pcds.containsKey(pcn)) {
181                         pcds.put(pcn, new Vector JavaDoc());
182                     }
183                     ((Vector JavaDoc) pcds.get(pcn)).add(hr);
184                 }
185             } else {
186                 // no port name is specified : the handler is added to each
187
// port
188
commonh.add(hr);
189             }
190         }
191
192         // now, we can create the port component list
193
Vector JavaDoc portComponents = new Vector JavaDoc();
194
195         // if there is not port-component-ref
196
if (pcds.isEmpty()) {
197             Definition def = sr.getWSDLFile().getDefinition();
198             Service s = def.getService(sr.getServiceQName());
199             Map JavaDoc ports = s.getPorts();
200             // for each wsdl:port
201
for (Iterator JavaDoc i = ports.values().iterator(); i.hasNext();) {
202                 Port p = (Port) i.next();
203                 portComponents.add(new VcPortComponent(p.getName(), commonh));
204             }
205         } else {
206             for (Enumeration JavaDoc enPc = pcds.keys(); enPc.hasMoreElements();) {
207                 String JavaDoc pcn = (String JavaDoc) enPc.nextElement();
208                 // add common handlers to the list
209
Vector JavaDoc pchrs = (Vector JavaDoc) pcds.get(pcn);
210                 pchrs.addAll(commonh);
211                 portComponents.add(new VcPortComponent(pcn, pchrs));
212             }
213         }
214
215         vc.put(PORT_COMPONENTS, portComponents);
216
217         // add types mappings
218
List JavaDoc mappings = new Vector JavaDoc();
219         MappingFile mf = sr.getMappingFile();
220         if (mf != null) {
221             for (Iterator JavaDoc m = mf.getXmlTypeMappings(); m.hasNext();) {
222                 QName JavaDoc xml = (QName JavaDoc) m.next();
223                 String JavaDoc classname = mf.getClassname(xml);
224
225                 // if we have an Array, Axis use a different representaion
226
if (classname.endsWith("[]")) {
227                     mappings.add(new VcArrayMapping(xml, classname));
228                 } else {
229                     mappings.add(new VcBeanMapping(xml, classname));
230                 }
231             }
232         }
233         vc.put(MAPPINGS, mappings);
234
235         return vc;
236     }
237 }
Popular Tags