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.wsutils;
18  
19  import org.apache.axis.AxisFault;
20  import org.apache.axis.MessageContext;
21  import org.apache.axis.handlers.soap.SOAPService;
22  import org.apache.axis.providers.java.RPCProvider;
23  import org.apache.axis.utils.ClassUtils;
24  
25  import java.io.InputStream;
26  import java.lang.reflect.Method;
27  
28  /***
29   * register the MessageContext in the jax-rpc runtime of the JSR109
30   *
31   * @author Srinath Perera(hemapani@opensource.lk)
32   */
33  public class EWSProvider extends RPCProvider {
34      public static final String OPTION_EJB_NAME = "beanName";
35      public static final String OPTION_JNDI_LOOKUP_NAME = "beanJndiName";
36      public static final String OPTION_HOMEINTERFACE_NAME = "homeInterfaceName";
37      public static final String OPTION_REMOTEINTERFACE_NAME = "remoteInterfaceName";
38      public static final String OPTION_LOCALHOMEINTERFACE_NAME = "localHomeInterfaceName";
39      public static final String OPTION_LOCALINTERFACE_NAME = "localInterfaceName";
40      public static final String OPTION_USE_EJB = "j2eeStyle";
41  
42      private String ejblookupName;
43      private String localhome;
44      private String home;
45      private String remote;
46      private String local;
47  
48      private boolean ejbbased = false;
49  
50      protected Object makeNewServiceObject(MessageContext msgContext,
51                                            String clsName)
52              throws Exception {
53          SOAPService service = msgContext.getService();
54          String j2eeStyle = (String) service.getOption(OPTION_USE_EJB);
55          if("ejb".equals(j2eeStyle) ){
56              java.util.Properties env = new java.util.Properties();
57              InputStream jndiIn = ClassUtils.getResourceAsStream(getClass(), "jndi.properties");
58              if (jndiIn != null) {
59                  env.load(jndiIn);
60              } else {
61                  throw new AxisFault("Do not find the JNDI properties file in the class path");
62              }
63              javax.naming.Context initial = new javax.naming.InitialContext(env);
64              ejblookupName = (String) service.getOption(OPTION_JNDI_LOOKUP_NAME);
65              remote = (String) service.getOption(OPTION_REMOTEINTERFACE_NAME);
66              home = (String) service.getOption(OPTION_HOMEINTERFACE_NAME);
67              local = (String) service.getOption(OPTION_LOCALINTERFACE_NAME);
68              localhome = (String) service.getOption(OPTION_LOCALHOMEINTERFACE_NAME);
69              if (remote != null && home != null && ejblookupName != null) {
70                  Object objref = initial.lookup(ejblookupName);
71                  Class homeClass = ClassUtils.forName(home);
72                  Object homeObj = javax.rmi.PortableRemoteObject.narrow(objref, homeClass);
73                  Method method = homeClass.getMethod("create", new Class[]{});
74                  return method.invoke(homeObj, new Object[]{});
75              } else if (local != null && localhome != null && ejblookupName != null) {
76                  Object homeObj = initial.lookup("java:comp/" + ejblookupName);
77                  Class homeClass = ClassUtils.forName(localhome);
78                  Method method = homeClass.getMethod("create", new Class[]{});
79                  return method.invoke(homeObj, new Object[]{});
80              }
81              throw new AxisFault("Wrong configuration remote="+ remote + "home =" + home + "ejblookupName" + ejblookupName );
82          } else {
83              return super.makeNewServiceObject(msgContext, clsName);
84          }
85      }
86  
87      protected Object invokeMethod(MessageContext msgContext,
88                                    Method method,
89                                    Object obj,
90                                    Object[] argValues)
91              throws Exception {
92          Method invokeMethod = obj.getClass().getMethod(method.getName(), method.getParameterTypes());
93          return invokeMethod.invoke(obj, argValues);
94      }
95  }