snichol     2002/08/29 13:06:13

  Modified:    java/src/org/apache/soap/providers StatelessEJBProvider.java
  Log:
  Clean up:
  
     - Remove output to System.out and System.err
     - Provide more precise and complete exception information
     - Improve formatting (mainly indentation) of the source
  
  Revision  Changes    Path
  1.8       +153 -186  
xml-soap/java/src/org/apache/soap/providers/StatelessEJBProvider.java
  
  Index: StatelessEJBProvider.java
  ===================================================================
  RCS file: 
/home/cvs/xml-soap/java/src/org/apache/soap/providers/StatelessEJBProvider.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- StatelessEJBProvider.java 28 Aug 2002 18:43:32 -0000      1.7
  +++ StatelessEJBProvider.java 29 Aug 2002 20:06:13 -0000      1.8
  @@ -59,8 +59,6 @@
   
   import java.io.* ;
   import java.util.* ;
  -import javax.servlet.* ;
  -import javax.servlet.http.* ;
   import org.apache.soap.* ;
   import org.apache.soap.rpc.* ;
   import org.apache.soap.server.* ;
  @@ -72,231 +70,200 @@
   import javax.naming.*;
   import javax.naming.Context.*;
   
  -public class StatelessEJBProvider implements org.apache.soap.util.Provider {
  -
  -        private DeploymentDescriptor dd ;
  -        private Envelope             envelope ;
  -        private Call                 call ;
  -        private String               methodName ;
  -        private String               targetObjectURI ;
  -        private HttpServlet          servlet ;
  -        private HttpSession          session ;
  -
  -        private javax.naming.Context contxt = null;
  -        private EJBObject remoteObjRef = null;
  -        public static java.lang.String CNTXT_PROVIDER_URL = "iiop://localhost:900";
  -        public static java.lang.String CNTXT_FACTORY_NAME = 
"com.ibm.ejs.ns.jndi.CNInitialContextFactory";
  -        private Vector methodParameters = null;
  -        private String respEncStyle = null;
   /**
  - * StatelessEJBProvider constructor comment.
  + *    @author unknown
  + *    @author Scott Nichol ([EMAIL PROTECTED])
    */
  -public StatelessEJBProvider() {
  -        super();
  -}
  -
  -private void initialize() throws SOAPException {
  -
  -        if(contxt == null) {
  -
  -                java.util.Properties properties = new java.util.Properties();
  -                properties.put(javax.naming.Context.PROVIDER_URL, 
CNTXT_PROVIDER_URL);
  -                properties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, 
CNTXT_FACTORY_NAME);
  -                try {
  -                        contxt = new javax.naming.InitialContext(properties);
  -                } catch (NamingException ne) {
  -                        // ErrorListener?
  -                        System.out.println("Naming Exception caught during 
InitialContext creation @ " + CNTXT_PROVIDER_URL);
  -                        throw new SOAPException(Constants.FAULT_CODE_SERVER, 
"Unable to initialize context" );
  -                }
  -        }
  +public class StatelessEJBProvider implements org.apache.soap.util.Provider {
  +    private DeploymentDescriptor dd ;
  +    private Envelope             envelope ;
  +    private Call                 call ;
  +    private String               methodName ;
  +    private String               targetObjectURI ;
  +
  +    private javax.naming.Context contxt = null;
  +    private EJBObject remoteObjRef = null;
  +    public static java.lang.String CNTXT_PROVIDER_URL = "iiop://localhost:900";
  +    public static java.lang.String CNTXT_FACTORY_NAME = 
"com.ibm.ejs.ns.jndi.CNInitialContextFactory";
  +
  +    /**
  +     *  Default constructor.
  +     */
  +    public StatelessEJBProvider() {
  +            super();
  +    }
   
  -}
  +    /**
  +     *  Initializes the naming context using default properties.
  +     */
  +    private void initialize() throws SOAPException {
  +        initialize(CNTXT_PROVIDER_URL, CNTXT_FACTORY_NAME);
  +    }
   
  -  private void initialize(String url, String factory)
  -    throws SOAPException
  -  {
  -    if(contxt == null)
  -    {
  -      java.util.Properties properties = new java.util.Properties();
  -      
  -      if ((url != null) && (!url.trim().equals("")))
  -      {
  -     properties.put(javax.naming.Context.PROVIDER_URL, url);
  -      }
  -      if ((factory != null) && (!factory.trim().equals("")))
  -      {
  -     properties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, factory);
  -      }
  +    /**
  +     *  Initializes the naming context using specified properties, either
  +     *  defaults or as specified in the deployment descriptor.
  +     */
  +    private void initialize(String url, String factory) throws SOAPException {
  +        if (contxt == null) {
  +            java.util.Properties properties = new java.util.Properties();
  +            if ((url != null) && (!url.trim().equals(""))) {
  +                properties.put(javax.naming.Context.PROVIDER_URL, url);
  +            }
  +            if ((factory != null) && (!factory.trim().equals(""))) {
  +                properties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, 
factory);
  +            }
         
  -      try
  -      {
  -     contxt = new javax.naming.InitialContext(properties);
  -      }
  -      catch (NamingException ne)
  -      {
  -     // ErrorListener?
  -     System.out.println("Naming Exception caught during InitialContext creation @ " 
+ url);
  -     throw new SOAPException(Constants.FAULT_CODE_SERVER, "Unable to initialize 
context");
  -      }
  +            try {
  +                contxt = new javax.naming.InitialContext(properties);
  +            } catch (NamingException ne) {
  +                // TODO: ErrorListener?
  +                //System.out.println("Naming Exception caught during InitialContext 
creation @ " + url);
  +                throw new SOAPException(Constants.FAULT_CODE_SERVER,
  +                                        "Unable to initialize naming context; URL " 
+
  +                                        url + " factory " + factory + ": " + ne,
  +                                        ne);
  +            }
  +        }
       }
  -  }
   
   
  -/**
  - * invoke method comment.
  - */
  -public void invoke(SOAPContext reqContext, SOAPContext resContext)
  -                    throws SOAPException {
  -                  System.err.println( 
"=============================================" );
  -          System.err.println("In TemplateProvider.invoke()" );
  -
  +    /**
  +     *  Invokes the method.
  +     */
  +    public void invoke(SOAPContext reqContext, SOAPContext resContext)
  +                       throws SOAPException {
           Parameter ret = null;
           Object[] args = null;
           Class[] argTypes = null;
   
  -        respEncStyle = call.getEncodingStyleURI();
  +        String methodName = call.getMethodName();
  +        Vector methodParameters = call.getParams();
  +        String respEncStyle = call.getEncodingStyleURI();
   
           if (methodParameters != null) {
  -                int parametersCount = methodParameters.size ();
  -                args = new Object[parametersCount];
  -                argTypes = new Class[parametersCount];
  -
  -                for (int i = 0; i < parametersCount; i++) {
  -                        Parameter param = (Parameter) methodParameters.elementAt 
(i);
  -                        args[i] = param.getValue ();
  -                        argTypes[i] = param.getType ();
  -
  -                        if (respEncStyle == null) {
  -                                respEncStyle = param.getEncodingStyleURI ();
  -                        }
  +            int parametersCount = methodParameters.size ();
  +            args = new Object[parametersCount];
  +            argTypes = new Class[parametersCount];
  +
  +            for (int i = 0; i < parametersCount; i++) {
  +                Parameter param = (Parameter) methodParameters.elementAt (i);
  +                args[i] = param.getValue ();
  +                argTypes[i] = param.getType ();
  +
  +                if (respEncStyle == null) {
  +                    respEncStyle = param.getEncodingStyleURI ();
                   }
  +            }
           }
   
  -        if (respEncStyle == null) {
  -          respEncStyle = Constants.NS_URI_SOAP_ENC;
  -        }
  +        if (respEncStyle == null)
  +            respEncStyle = Constants.NS_URI_SOAP_ENC;
   
           try {
  +            Method m = MethodUtils.getMethod(remoteObjRef, methodName, argTypes);
  +            Bean result = new Bean(m.getReturnType(), m.invoke(remoteObjRef, args));
   
  -                Method m = MethodUtils.getMethod (remoteObjRef, methodName, 
argTypes);
  -                Bean result = new Bean (m.getReturnType (), m.invoke (remoteObjRef, 
args));
  -
  -
  -                if (result.type != void.class) {
  -                  ret = new Parameter (RPCConstants.ELEM_RETURN, result.type,
  -                                                   result.value, null);
  -                }
  -
  -
  +            if (result.type != void.class) {
  +                ret = new Parameter(RPCConstants.ELEM_RETURN, result.type,
  +                                    result.value, null);
  +            }
           } catch (InvocationTargetException e) {
  -                System.err.println("Exception Caught upon method invocation 
attempt: " + e.getMessage());
  -                Throwable t = e.getTargetException ();
  -          throw new SOAPException (Constants.FAULT_CODE_SERVER, t.getMessage(), t);
  -        }
  -         catch (Throwable t) {
  -                System.err.println("Exception Caught upon method invocation 
attempt: " + t.toString());
  -                throw new SOAPException (Constants.FAULT_CODE_SERVER, 
t.getMessage(), t);
  +            // TODO: ErrorListener?
  +            //System.err.println("Exception Caught upon method invocation attempt: 
" + e.getMessage());
  +            Throwable t = e.getTargetException();
  +            throw new SOAPException(Constants.FAULT_CODE_SERVER,
  +                                    "Error in invoked method " + methodName +
  +                                    ": " + t.getMessage(),
  +                                    t);
  +        } catch (Throwable t) {
  +            // TODO: ErrorListener?
  +            //System.err.println("Exception Caught upon method invocation attempt: 
" + t.toString());
  +            throw new SOAPException(Constants.FAULT_CODE_SERVER,
  +                                    "Error invoking method " + methodName +
  +                                    ": " + t.getMessage(),
  +                                    t);
           }
   
           try {
  -          Response resp = new Response( targetObjectURI,            // URI
  -                               call.getMethodName(),       // Method
  -                               (Parameter) ret,            // ReturnValue
  -                               null,                       // Params
  -                               null,                       // Header
  -                               respEncStyle,               // encoding
  -                               resContext );        // response soapcontext - not 
supported yet
  -                  Envelope env = resp.buildEnvelope();
  -                  StringWriter  sw = new StringWriter();
  -                  env.marshall( sw, call.getSOAPMappingRegistry(), resContext );
  -                  resContext.setRootPart( sw.toString(), 
Constants.HEADERVAL_CONTENT_TYPE_UTF8);
  -                }
  -                catch( Exception e ) {
  -                  if ( e instanceof SOAPException ) throw (SOAPException ) e ;
  -                  throw new SOAPException( Constants.FAULT_CODE_SERVER, 
e.toString() );
  -                }
  -}
  +            Response resp = new Response(targetObjectURI,       // URI
  +                                         call.getMethodName(),  // Method
  +                                         (Parameter) ret,       // ReturnValue
  +                                         null,                  // Params
  +                                         null,                  // Header
  +                                         respEncStyle,          // encoding
  +                                         resContext);           // response 
soapcontext
  +            Envelope env = resp.buildEnvelope();
  +            StringWriter sw = new StringWriter();
  +            env.marshall(sw, call.getSOAPMappingRegistry(), resContext);
  +            resContext.setRootPart(sw.toString(), 
Constants.HEADERVAL_CONTENT_TYPE_UTF8);
  +        } catch( Exception e ) {
  +            if (e instanceof SOAPException)
  +                throw (SOAPException) e;
  +            throw new SOAPException(Constants.FAULT_CODE_SERVER,
  +                                    "Error creating response: " + e,
  +                                    e);
  +        }
  +    }
   
  -/**
  - * locate method comment.
  - */
  -public void locate(DeploymentDescriptor dd, 
  -                   Envelope env, 
  -                   Call call, 
  -                   String methodName, 
  -                   String targetObjectURI, 
  -                   SOAPContext reqContext)
  -              throws org.apache.soap.SOAPException {
  -
  -          HttpServlet servlet = (HttpServlet) reqContext.getProperty( 
Constants.BAG_HTTPSERVLET );
  -          HttpSession session = (HttpSession) reqContext.getProperty( 
Constants.BAG_HTTPSESSION );
  -
  -          System.err.println( "=============================================" );
  -          System.err.println( "In TemplateProvider.locate()" );
  -          System.err.println( "URI: " + targetObjectURI );
  -          System.err.println( "DD.ServiceClass: " + dd.getServiceClass() );
  -          System.err.println( "DD.ProviderClass: " + dd.getProviderClass() );
  -          System.err.println( "Call.MethodName: " + call.getMethodName() );
  -
  -          this.dd              = dd ;
  -          this.envelope        = env ;
  -          this.call            = call ;
  -          this.methodName      = methodName ;
  -          this.targetObjectURI = targetObjectURI ;
  -          this.servlet         = servlet ;
  -          this.session         = session ;
  +    /**
  +     *  Locates the method.
  +     */
  +    public void locate(DeploymentDescriptor dd, Envelope env, Call call, 
  +                       String methodName, String targetObjectURI,
  +                       SOAPContext reqContext) throws SOAPException {
  +        this.dd              = dd ;
  +        this.envelope        = env ;
  +        this.call            = call ;
  +        this.methodName      = methodName ;
  +        this.targetObjectURI = targetObjectURI ;
   
  -          // call on a valid method name?
  -          if (!RPCRouter.validCall(dd, call)) {
  +        // call on a valid method name?
  +        if (!RPCRouter.validCall(dd, call)) {
               throw new SOAPException(Constants.FAULT_CODE_SERVER,
                                       "Method '" + call.getMethodName () +
                                       "' is not supported.");
  -          }
  -
  -          Hashtable props = dd.getProps();
  +        }
   
  +        Hashtable props = dd.getProps();
   
  +        // Initialize the naming context
           String ContxtProviderURL = (String) props.get("ContextProviderURL");
           String ContxtFactoryName = (String) props.get("FullContextFactoryName");
  -
           if ((ContxtProviderURL != null) || (ContxtFactoryName != null))
  -                initialize(ContxtProviderURL, ContxtFactoryName);
  +            initialize(ContxtProviderURL, ContxtFactoryName);
           else
  -                initialize();
  +            initialize();
   
  +        // Locate and narrow the EJB
           String homeInterfaceName = (String) props.get("FullHomeInterfaceName");
           if (homeInterfaceName == null)
  -                throw new SOAPException(Constants.FAULT_CODE_SERVER, "Error in 
Deployment Descriptor Property Settings");
  +            throw new SOAPException(Constants.FAULT_CODE_SERVER,
  +                                    "FullHomeInterfaceName must be supplied " +
  +                                    "in the deployment descriptor");
   
  -        // From the Deployment Descriptor get the JNDI lookup name that is inside 
the "java" element...
           String jndiName = (String) props.get("JNDIName");
  -        if ( jndiName == null ) jndiName = dd.getProviderClass();
  +        if (jndiName == null)
  +            jndiName = dd.getProviderClass();
   
           if ((jndiName != null) && (contxt != null)) {
  -
  -                try {
  -
  -                        // Use service name to locate EJB home object via the contxt
  -                        // EJBHome home = (EJBHome) contxt.lookup(jndiName);
  -                        EJBHome home = (EJBHome) 
PortableRemoteObject.narrow(contxt.lookup(jndiName), Class.forName(homeInterfaceName));
  -                        // call the 'create' method on the EJB home object, and 
store the
  -                        //   ref to the EJB object.
  -                        Method createMethod = home.getClass().getMethod("create", 
new Class[0]);
  -                        remoteObjRef = (EJBObject) createMethod.invoke((Object) 
home, new Object[0]);
  -
  -                } catch (Exception e) {
  -
  -                        System.out.println("Exception caught: " + e.toString());
  -                        throw new SOAPException(Constants.FAULT_CODE_SERVER,"Error 
in connecting to EJB", e);
  -                }
  +            try {
  +                // Use service name to locate EJB home object via the contxt
  +                // EJBHome home = (EJBHome) contxt.lookup(jndiName);
  +                EJBHome home = (EJBHome) 
PortableRemoteObject.narrow(contxt.lookup(jndiName), Class.forName(homeInterfaceName));
  +                // call the 'create' method on the EJB home object, and store the
  +                // ref to the EJB object.
  +                Method createMethod = home.getClass().getMethod("create", new 
Class[0]);
  +                remoteObjRef = (EJBObject) createMethod.invoke((Object) home, new 
Object[0]);
  +            } catch (Exception e) {
  +                // TODO: ErrorListener?
  +                //System.out.println("Exception caught: " + e.toString());
  +                throw new SOAPException(Constants.FAULT_CODE_SERVER,
  +                                        "Error in locating or narrowing EJB; " +
  +                                        "name " + jndiName + " interface " +
  +                                        homeInterfaceName + ": " + e,
  +                                        e);
  +            }
           }
  -
  -        // Once previous steps have been successful, then we take the Call Object
  -        //  and extract the method name from it, and any parameters, and store them.
  -        methodName = call.getMethodName();
  -        methodParameters = call.getParams();
  -
  -
  -}
  +    }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to