View Javadoc

1   /*
2    *  Copyright (c) 2008 Rodrigo Ruiz
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.apache.axis.message.addressing.tools.wsdl;
17  
18  import java.io.IOException;
19  import java.io.PrintWriter;
20  import java.util.Map;
21  
22  import javax.wsdl.Binding;
23  import javax.wsdl.Port;
24  import javax.wsdl.Service;
25  
26  import org.apache.axis.utils.JavaUtils;
27  import org.apache.axis.utils.Messages;
28  import org.apache.axis.wsdl.symbolTable.BindingEntry;
29  import org.apache.axis.wsdl.symbolTable.PortTypeEntry;
30  import org.apache.axis.wsdl.symbolTable.ServiceEntry;
31  import org.apache.axis.wsdl.symbolTable.SymbolTable;
32  import org.apache.axis.wsdl.toJava.Emitter;
33  import org.apache.axis.wsdl.toJava.JavaBindingWriter;
34  import org.apache.axis.wsdl.toJava.Utils;
35  
36  /**
37   *
38   * @author Jarek Gawor
39   * @author Rodrigo Ruiz
40   * @version $Revision: 14 $
41   */
42  public class JavaAddressingServiceImplWriter extends JavaClassWithImportsWriter {
43  
44    /**
45     * Creates an instance.
46     *
47     * @param emitter      Emitter
48     * @param sEntry       Service
49     * @param symbolTable  Symbol table
50     */
51    protected JavaAddressingServiceImplWriter(Emitter emitter, ServiceEntry sEntry,
52      SymbolTable symbolTable) {
53      super(emitter, sEntry, symbolTable, sEntry.getName() + "AddressingLocator",
54        "service");
55    }
56  
57    /**
58     * {@inheritDoc}
59     */
60    @Override protected void initImports() {
61      super.initImports();
62      addImport("org.apache.axis.message.addressing.AddressingHeaders");
63      addImport("org.apache.axis.message.addressing.AttributedURI");
64      addImport("org.apache.axis.message.addressing.Constants");
65      addImport("org.apache.axis.EngineConfiguration");
66      addImport("javax.xml.namespace.QName");
67      addImport("java.net.URL");
68      addImport("java.net.MalformedURLException");
69    }
70  
71    /**
72     * {@inheritDoc}
73     */
74    protected String getExtendsText() {
75      return "extends " + resolve(this.sEntry.getName() + "Locator");
76    }
77  
78    /**
79     * {@inheritDoc}
80     */
81    protected String getImplementsText() {
82      return "implements " + resolve(this.sEntry.getName() + "Addressing");
83    }
84  
85    /**
86     * Write the body of the service file.
87     *
88     * @param pw Writer to print to
89     * @throws IOException If an error occurs
90     */
91    protected void writeFileBody(PrintWriter pw) throws IOException {
92      Service service = sEntry.getService();
93  
94      writeConstructors(pw);
95  
96      // get ports
97      Map<?, ?> portMap = service.getPorts();
98  
99      // write a get method for each of the ports with a SOAP binding
100     for (Object item : portMap.values()) {
101       Port p = (Port)item;
102       Binding binding = p.getBinding();
103 
104       if (binding == null) {
105         throw new IOException(Messages.getMessage("emitFailNoBinding01", new String[] { p
106             .getName() }));
107       }
108 
109       BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName());
110 
111       if (bEntry == null) {
112         throw new IOException(Messages.getMessage("emitFailNoBindingEntry01",
113             new String[] { binding.getQName().toString() }));
114       }
115 
116       PortTypeEntry ptEntry = symbolTable.getPortTypeEntry(binding.getPortType()
117           .getQName());
118 
119       if (ptEntry == null) {
120         throw new IOException(Messages.getMessage("emitFailNoPortType01",
121             new String[] { binding.getPortType().getQName().toString() }));
122       }
123 
124       // If this isn't an SOAP binding, skip it
125       if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) {
126         continue;
127       }
128 
129       String portName = p.getName();
130 
131       if (!JavaUtils.isJavaId(portName)) {
132         portName = Utils.xmlNameToJavaClass(portName);
133       }
134 
135       String bindingType = (String) bEntry
136       .getDynamicVar(JavaBindingWriter.INTERFACE_NAME);
137 
138       pw.println("  /**");
139       pw.println("   * {@inheritDoc}");
140       pw.println("   */");
141       pw.println("  public " + resolve(bindingType) + " get" + portName
142           + "(EndpointReferenceType reference)");
143       pw.println("    throws ServiceException {");
144       pw.println("    AttributedURI address = reference.getAddress();");
145 
146       pw.println("    if (address == null) {");
147       pw.println("      throw new ServiceException("
148         + "\"No address in EndpointReference\");");
149       pw.println("    }");
150       pw.println("    URL endpoint;");
151       pw.println("    try {");
152       pw.println("      endpoint = new URL(address.toString());");
153       pw.println("    } catch (MalformedURLException e) {");
154       pw.println("      throw new ServiceException(e);");
155       pw.println("    }");
156       pw.println("    " + resolve(bindingType) + " stub = " + "get" + portName
157                  + "(endpoint);");
158       pw.println("    if (stub != null) {");
159       pw.println("      AddressingHeaders headers = new AddressingHeaders();");
160       pw.println("      headers.setTo(address);");
161       pw.println("      headers.setReferenceProperties(reference.getProperties());");
162       pw.println("      ((javax.xml.rpc.Stub)stub)._setProperty(");
163       pw.println("        Constants.ENV_ADDRESSING_SHARED_HEADERS, headers);");
164       pw.println("    }");
165       pw.println("    return stub;");
166       pw.println("  }");
167       pw.println();
168     }
169 
170     pw.println();
171   }
172 
173   /**
174    * Writes the constructors of the generated class.
175    *
176    * @param pw Writer to print to
177    * @throws IOException If an error occurs
178    */
179   protected void writeConstructors(PrintWriter pw) throws IOException {
180 
181     String simpleClassName = resolve(this.sEntry.getName()) + "AddressingLocator";
182 
183     // Generate a default constructor
184     pw.println("  /**");
185     pw.println("   * Creates an instance.");
186     pw.println("   */");
187     pw.println("  public " + simpleClassName + "() {");
188     pw.println("  }");
189     pw.println();
190 
191     // Generate a constructor with a custom configuration
192     pw.println("  /**");
193     pw.println("   * Creates an instance.");
194     pw.println("   *");
195     pw.println("   * @param config Engine configuration to use");
196     pw.println("   */");
197     pw.println("  public " + simpleClassName + "(EngineConfiguration config) {");
198     pw.println("    super(config);");
199     pw.println("  }");
200     pw.println();
201 
202     // Generate a constructor with WSDL and service name parameters
203     pw.println("  /**");
204     pw.println("   * Creates an instance.");
205     pw.println("   *");
206     pw.println("   * @param wsdlLoc WSDL path");
207     pw.println("   * @param sName   Service name");
208     pw.println("   * @throws ServiceException If an error occurs");
209     pw.println("   */");
210     pw.println("  public " + simpleClassName + "(String wsdlLoc, QName sName)");
211     pw.println("    throws ServiceException {");
212     pw.println("    super(wsdlLoc, sName);");
213     pw.println("  }");
214     pw.println();
215   }
216 
217   /**
218    * {@inheritDoc}
219    */
220   @Override
221   protected void writeClassComment(PrintWriter pw) throws IOException {
222     pw.println("/**");
223     pw.println(" * Service interface with addressing support implementation.");
224     pw.println(" */");
225   }
226 
227 }