View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
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  
17  package org.apache.geronimo.ews.ws4j2ee.toWs.ejb;
18  
19  import org.apache.geronimo.ews.ws4j2ee.context.J2EEWebServiceContext;
20  import org.apache.geronimo.ews.ws4j2ee.context.SEIOperation;
21  import org.apache.geronimo.ews.ws4j2ee.context.j2eeDD.EJBContext;
22  import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
23  import org.apache.geronimo.ews.ws4j2ee.toWs.JavaClassWriter;
24  
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  
28  /***
29   * <p>This class can be used to write the appropriate SessionBean
30   * class for the given port type.
31   * A Stateless Session Bean, as defined by the Enterprise JavaBeans specification,
32   * can be used to implement a Web service to be deployed in the EJB container.
33   * A Stateless Session Bean does not have to worry about multi-threaded access.
34   * The EJB container is required to serialize request flow through any particular
35   * instance of a Service Implementation Bean. The requirements for creating a Service
36   * Implementation Bean as a Stateless Session EJB are repeated in part here.</p>
37   * <ol>
38   * <li>The Service Implementation Bean must have a default public constructor.</li>
39   * <li>The Service Implementation Bean may implement the Service Endpoint Interface,
40   * but it is not required to do so. The bean must implement all the method
41   * signatures of the SEI.<li>
42   * <li>The Service Implementation Bean methods are not required to throw
43   * javax.rmi.RemoteException. The business methods of the bean must be public
44   * and must not be final or static. It may implement other methods in addition
45   * to those defined by the SEI.</li>
46   * <li>A Service Implementation Bean must be a stateless object.
47   * A Service Implementation Bean must not save client specific state
48   * across method calls either within the bean instance?s data members or
49   * external to the instance.</li>
50   * <li>The class must be public, must not be final and must not be abstract.</li>
51   * <li>The class must not define the finalize() method.</li>
52   * <li>Currently, it must implement the ejbCreate() and ejbRemove() methods which
53   * take no arguments. This is a requirement of the EJB container, but generally
54   * can be stubbed out with an empty implementation.</li>
55   * <li>Currently, a Stateless Session Bean must implement the javax.ejb.SessionBean
56   * interface either directly or indirectly. This interface allows the container to notify the Service Implementation Bean of impending changes in its state. The full requirements of this interface are defined in the Enterprise JavaBeans specification section 7.5.1.</li>
57   * <li>The Enterprise JavaBeans specification section 7.8.2 defines the allowed
58   * container service access requirements.</li>
59   * </ol>
60   * <h5>Exposing an existing EJB</h5>
61   * <p>An existing Enterprise JavaBean may be used as a Service Implementation Bean if it meets the following requirements:</p>
62   * <ol>
63   * <li>The business methods of the EJB bean class that are exposed on the SEI must meet the Service</li>
64   * <li>Implementation Bean requirements defined in section 5.3.1.</li>
65   * <li>The SEI must meet the requirements described in the JAX-RPC specification for Service Endpoint Interfaces.</li>
66   * <li>The transaction attributes of the SEI methods must not include Mandatory.</li>
67   * <li>The developer must package the Web service as described in section 5.4 and must specify an ejb-link from the port in the Web services deployment descriptor to the existing EJB.</li>
68   * <ol>
69   *
70   * @author Rajith Priyanga
71   * @author Srinath Perera
72   * @date Nov 26, 2003
73   */
74  public class SessionBeanWriter extends JavaClassWriter {
75      private String name;
76      protected EJBContext ejbcontext;
77  
78      /***
79       * Constructs a SessionBeanWriter.
80       *
81       * @param portType The port type which contains the details.
82       * @throws GenerationFault
83       */
84      public SessionBeanWriter(J2EEWebServiceContext context, EJBContext ejbcontext) throws GenerationFault {
85          super(context, ejbcontext.getImplBean());
86          this.ejbcontext = ejbcontext;
87      }
88  
89      protected void writeAttributes() throws GenerationFault {
90      }
91  
92      protected void writeConstructors() throws GenerationFault {
93      }
94  
95      protected void writeMethods() throws GenerationFault {
96          String parmlistStr = "";
97          ArrayList operations = j2eewscontext.getMiscInfo().getSEIOperations();
98          for (int i = 0; i < operations.size(); i++) {
99              SEIOperation op = (SEIOperation) operations.get(i);
100             String returnType = op.getReturnType();
101             returnType = (returnType == null) ? "void" : returnType;
102             out.write("\tpublic " + returnType + " " + op.getMethodName() + "(");
103             Iterator pas = op.getParameterNames().iterator();
104             boolean first = true;
105             while (pas.hasNext()) {
106                 String name = (String) pas.next();
107                 String type = (String) op.getParameterType(name);
108                 if (first) {
109                     first = false;
110                     out.write(type + " " + name);
111                     parmlistStr = parmlistStr + name;
112                 } else {
113                     out.write("," + type + " " + name);
114                     parmlistStr = "," + name;
115                 }
116             }
117             out.write(")");
118 //			out.write(") throws java.rmi.RemoteException");
119 //			ejb giving problems deploying check this            
120 //			  ArrayList faults = op.getFaults();
121 //			  for (int j = 0; j < faults.size(); j++) {
122 //				  out.write("," + (String) faults.get(i));
123 //			  }
124             out.write("{\n");
125             if ("int".equals(returnType)) {
126                 out.write("\t\t\treturn 12;\n");
127             } else if ("float".equals(returnType)) {
128                 out.write("\t\t\treturn 0.0f;\n");
129             } else if ("double".equals(returnType)) {
130                 out.write("\t\t\treturn 0.0d;\n");
131             } else if ("short".equals(returnType)) {
132                 out.write("\t\t\treturn (short)0.0;\n");
133             } else if ("boolean".equals(returnType)) {
134                 out.write("\t\t\treturn false;\n");
135             } else if ("byte".equals(returnType)) {
136                 out.write("\t\t\treturn (byte)24;\n");
137             } else if ("long".equals(returnType)) {
138                 out.write("\t\t\treturn (long)0.0l;\n");
139             } else if ("char".equals(returnType)) {
140                 out.write("\t\t\treturn 'w';\n");
141             } else if ("void".equals(returnType)) {
142             } else {
143                 out.write("\t\t\treturn null;\n");
144             }
145             out.write("\t}\n");
146         }
147         out.write("\tpublic javax.naming.Context getInitialContext()throws javax.naming.NamingException{\n");
148         out.write("\t	java.util.Properties env = new java.util.Properties();\n");
149         out.write("\t	env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,\"org.jnp.interfaces.NamingContextFactory\");\n");
150         out.write("\t	env.put(javax.naming.Context.PROVIDER_URL, \"127.0.0.1:1099\");\n");
151         out.write("\t	return new javax.naming.InitialContext(env);\n");
152         out.write("\t}\n");
153         out.write("\tpublic void ejbCreate() {}\n");
154         out.write("\tpublic void ejbActivate() throws javax.ejb.EJBException, java.rmi.RemoteException {}\n");
155         out.write("\tpublic void ejbPassivate() throws javax.ejb.EJBException, java.rmi.RemoteException {}\n");
156         out.write("\tpublic void ejbRemove() throws javax.ejb.EJBException, java.rmi.RemoteException {}\n");
157         out.write("\tpublic void setSessionContext(javax.ejb.SessionContext arg0)throws javax.ejb.EJBException, java.rmi.RemoteException {}\n");
158     }
159 
160     /* (non-Javadoc)
161      * @see org.apache.geronimo.ews.ws4j2ee.toWs.JavaClassWriter#getimplementsPart()
162      */
163     protected String getimplementsPart() {
164         return " implements javax.ejb.SessionBean";
165     }
166 
167     /* (non-Javadoc)
168      * @see org.apache.geronimo.ews.ws4j2ee.toWs.AbstractWriter#isOverWrite()
169      */
170     protected boolean isOverWrite() {
171         return false;
172     }
173 
174 }