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  package org.apache.geronimo.ews.ws4j2ee.toWs.wrapperWs;
17  
18  import org.apache.geronimo.ews.ws4j2ee.context.J2EEWebServiceContext;
19  import org.apache.geronimo.ews.ws4j2ee.context.SEIOperation;
20  import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  
25  /***
26   * @author Srinath Perera(hemapani@opensource.lk)
27   */
28  public abstract class SimpleRemoteInterfaceBasedWrapperClassWriter extends EJBBasedWrapperClassWriter {
29  
30      /***
31       * @param j2eewscontext
32       * @throws GenerationFault
33       */
34      public SimpleRemoteInterfaceBasedWrapperClassWriter(J2EEWebServiceContext j2eewscontext)
35              throws GenerationFault {
36          super(j2eewscontext);
37          seiName = context.getEjbRemoteInterface();
38      }
39  
40      protected abstract String getJNDIInitialContextFactory();
41  
42      protected abstract String getJNDIHostAndPort();
43  
44      protected String getimplementsPart() {
45          return " implements org.apache.geronimo.ews.ws4j2ee.wsutils.ContextAccessible";
46      }
47  
48      protected void writeMethods() throws GenerationFault {
49          out.write("\tpublic void setMessageContext(org.apache.axis.MessageContext msgcontext){;\n");
50          out.write("\t\tthis.msgcontext = msgcontext;\n");
51          out.write("\t}\n");
52          writeGetRemoteRef(classname);
53          String parmlistStr = null;
54          ArrayList operations = j2eewscontext.getMiscInfo().getSEIOperations();
55          for (int i = 0; i < operations.size(); i++) {
56              parmlistStr = "";
57              SEIOperation op = (SEIOperation) operations.get(i);
58              String returnType = op.getReturnType();
59              if (returnType == null)
60                  returnType = "void";
61              out.write("\tpublic " + returnType + " " + op.getMethodName() + "(");
62              Iterator pas = op.getParameterNames().iterator();
63              boolean first = true;
64              while (pas.hasNext()) {
65                  String name = (String) pas.next();
66                  String type = op.getParameterType(name);
67                  if (first) {
68                      first = false;
69                      out.write(type + " " + name);
70                      parmlistStr = parmlistStr + name;
71                  } else {
72                      out.write("," + type + " " + name);
73                      parmlistStr = parmlistStr + "," + name;
74                  }
75              }
76              out.write(") throws java.rmi.RemoteException");
77              ArrayList faults = op.getFaults();
78              for (int j = 0; j < faults.size(); j++) {
79                  out.write("," + (String) faults.get(j));
80              }
81              out.write("{\n");
82              out.write("\t\tif(ejb ==  null)\n");
83              out.write("\t\t\tejb = getRemoteRef();\n");
84              if (!"void".equals(returnType))
85                  out.write("\t\treturn ejb." + op.getMethodName() + "(" + parmlistStr + ");\n");
86              else
87                  out.write("\t\tejb." + op.getMethodName() + "(" + parmlistStr + ");\n");
88              out.write("\t}\n");
89          }
90          //out.write("}\n");	
91      }
92  
93      private void writeGetRemoteRef(String classname) {
94          out.write("\tpublic " + seiName + " getRemoteRef()throws org.apache.axis.AxisFault{\n");
95         
96  //TODO remove the security code for the time been
97  //	   out.write("\t\ttry {\n");
98  //	   out.write("\t\tif(msgcontext == null){\n");
99  //	   out.write("\t\t		msgcontext = org.apache.axis.MessageContext.getCurrentContext();\n");
100 //	   out.write("\t\t}\n");
101 //	   out.write("\t\tif(msgcontext == null){\n");
102 //       out.write("\t\t		throw new RuntimeException(\"Message Context can not be null\");\n");
103 //	   out.write("\t\t}\n");
104 //	   
105 //	   out.write("\t\torg.apache.geronimo.ews.ws4j2ee.context.security.SecurityContext4J2EE seccontext =\n"); 
106 //	   out.write("\t\t			   (org.apache.geronimo.ews.ws4j2ee.context.security.SecurityContext4J2EE)msgcontext\n");
107 //	   out.write("\t\t.getProperty(org.apache.ws.axis.security.WSS4J2EEConstants.SEC_CONTEXT_4J2EE);\n");
108 //	   out.write("\t\tif(seccontext != null){\n");
109 //	   out.write("\t\t\t    javax.security.auth.callback.CallbackHandler handler\n");
110 //	   out.write("\t\t\t        = seccontext.getPWDCallbackHandler4J2EE();\n");
111 //	   out.write("\t\t\t    if(handler != null){\n");
112 //	   out.write("\t\t\t        javax.security.auth.login.LoginContext lc\n"); 
113 //	   out.write("\t\t\t            = new javax.security.auth.login.LoginContext(\"TestClient\", handler);\n");
114 //	   out.write("\t\t\t        lc.login();\n");
115 //	   out.write("\t\t\t    }\n");
116 //	   out.write("\t\t}\n");
117 //	   
118 //	   out.write("\t\t}catch (javax.security.auth.login.LoginException e) {\n");
119 //	   out.write("\t\t     e.printStackTrace();\n");
120 //	   out.write("\t\t     throw org.apache.axis.AxisFault.makeFault(e);\n");
121 //	   out.write("\t\t}\n");
122    	
123         out.write("\t\ttry{\n");
124 //	   use the properties set
125 //	   out.write("\t\t\tjava.util.Properties env = new java.util.Properties();\n");
126 //	   out.write("\t\t\tenv.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,\""+getJNDIInitialContextFactory()+"\");\n");
127 //	   out.write("\t\t\tenv.put(javax.naming.Context.PROVIDER_URL, \""+getJNDIHostAndPort()+"\");\n");
128         out.write("\t\t\tjava.util.Properties env = new java.util.Properties();\n");
129         out.write("\t\t\tenv.load(getClass().getClassLoader().getResourceAsStream(\"jndi.properties\"));\n");
130 //use the propertyfile
131 //       out.write("\t\t\torg.apache.geronimo.ews.ws4j2ee.wsutils.PropertyLoader ploader = new org.apache.geronimo.ews.ws4j2ee.wsutils.PropertyLoader();\n");
132 //  	   out.write("\t\t\tjava.util.Properties env = " +
133 //  			"ploader.loadProperties(\"jndi.properties\");\n");
134 		
135         out.write("\t\t\tjavax.naming.Context initial = new javax.naming.InitialContext(env);\n");
136 
137         String ejbname = j2eewscontext.getWSCFContext().getWscfport().getServiceImplBean().getEjblink();
138         out.write("\t\t\tObject objref = initial.lookup(\"ejb/" + ejbname + "\");\n");
139         String ejbhome = j2eewscontext.getEJBDDContext().getEjbhomeInterface();
140         out.write("\t\t\t" + ejbhome + " home = \n\t\t\t\t(" + ejbhome
141                 + ")javax.rmi.PortableRemoteObject.narrow(objref," + ejbhome + ".class);\n");
142         out.write("\t\t\treturn home.create();\n");
143         out.write("\t\t}catch (Exception e) {\n");
144         out.write("\t\t    throw org.apache.axis.AxisFault.makeFault(e);\n");
145         out.write("\t\t}\n");
146         out.write("\t}\n");
147     }
148 }