You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2007/07/24 03:37:19 UTC

svn commit: r558909 - in /openejb/trunk/openejb3/server: openejb-client/src/main/java/org/apache/openejb/client/ openejb-ejbd/src/main/java/org/apache/openejb/server/ejbd/

Author: dblevins
Date: Mon Jul 23 18:37:18 2007
New Revision: 558909

URL: http://svn.apache.org/viewvc?view=rev&rev=558909
Log:
Ported protocol version code from 2.x

Added:
    openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ProtocolMetaData.java
Modified:
    openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/Client.java
    openejb/trunk/openejb3/server/openejb-ejbd/src/main/java/org/apache/openejb/server/ejbd/EjbDaemon.java

Modified: openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/Client.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/Client.java?view=diff&rev=558909&r1=558908&r2=558909
==============================================================================
--- openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/Client.java (original)
+++ openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/Client.java Mon Jul 23 18:37:18 2007
@@ -21,6 +21,7 @@
 import java.io.ObjectOutput;
 import java.io.ObjectOutputStream;
 import java.io.OutputStream;
+import java.io.InputStream;
 import java.net.URI;
 import java.rmi.RemoteException;
 import java.util.logging.Level;
@@ -29,6 +30,8 @@
 public class Client {
     private static final Logger logger = Logger.getLogger("OpenEJB.client");
 
+    private static final ProtocolMetaData PROTOCOL_VERSION = new ProtocolMetaData("3.0");
+
     private static Client client = new Client();
 
     // This lame hook point if only of testing
@@ -94,6 +97,17 @@
             }
 
             /*----------------------------------*/
+            /* Write the protocol magic         */
+            /*----------------------------------*/
+            try{
+
+                PROTOCOL_VERSION.writeExternal(out);
+
+            } catch (Throwable e){
+                throw new RemoteException("Cannot write the protocol metadata to the server: " , e );
+            }
+
+            /*----------------------------------*/
             /* Write request type */
             /*----------------------------------*/
             try {
@@ -141,16 +155,33 @@
             }
 
             /*----------------------------------*/
-            /* Get input streams */
+            /* Get input streams               */
             /*----------------------------------*/
+            InputStream in = null;
             try {
 
-                objectIn = new EjbObjectInputStream(conn.getInputStream());
+                in = conn.getInputStream();
+
             } catch (IOException e) {
-                throw new RemoteException("Cannot open object input stream to server: ", e);
+                throw new RemoteException("Cannot open input stream to server: " , e );
+            }
 
-            } catch (Throwable e) {
-                throw new RemoteException("Cannot open object input stream to server: ", e);
+            ProtocolMetaData protocolMetaData = null;
+            try {
+
+                protocolMetaData = new ProtocolMetaData();
+                protocolMetaData.readExternal(in);
+
+            } catch (IOException e) {
+                throw new RemoteException("Cannot deternmine server protocol version: Received "+protocolMetaData.getSpec() , e );
+            }
+
+            try{
+
+                objectIn = new EjbObjectInputStream(in);
+
+            } catch (Throwable e){
+                throw new RemoteException("Cannot open object input stream to server ("+protocolMetaData.getSpec() +") : "+e.getMessage() , e );
             }
 
             /*----------------------------------*/
@@ -162,13 +193,13 @@
             } catch (ClassNotFoundException e) {
                 throw new RemoteException("Cannot read the response from the server.  The class for an object being returned is not located in this system:", e);
 
-            } catch (IOException e) {
-                throw new RemoteException("Cannot read the response from the server.", e);
+            } catch (IOException e){
+                throw new RemoteException("Cannot read the response from the server ("+protocolMetaData.getSpec() +") : "+e.getMessage() , e );
 
-            } catch (Throwable e) {
-                throw new RemoteException("Error reading response from server: ", e);
+            } catch (Throwable e){
+                throw new RemoteException("Error reading response from server ("+protocolMetaData.getSpec() +") : "+e.getMessage() , e );
             }
-
+ 
         } catch (RemoteException e) {
             throw e;
         } catch (Throwable error) {

Added: openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ProtocolMetaData.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ProtocolMetaData.java?view=auto&rev=558909
==============================================================================
--- openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ProtocolMetaData.java (added)
+++ openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ProtocolMetaData.java Mon Jul 23 18:37:18 2007
@@ -0,0 +1,94 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.openejb.client;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * OpenEJB Enterprise Javabean Protocol (OEJP)
+ *
+ * OEJP uses a "<major>.<minor>" numbering scheme to indicate versions of the protocol.
+ *
+ *     Protocol-Version   = "OEJP" "/" 1*DIGIT "." 1*DIGIT
+ *
+ * Some compatability is guaranteed with the major part of the version number.
+ *
+ * @version $Revision: 451417 $ $Date: 2006-09-29 15:13:22 -0500 (Fri, 29 Sep 2006) $
+ */
+public class ProtocolMetaData {
+
+    private static final String OEJB = "OEJP";
+    private String id;
+    private int major;
+    private int minor;
+
+    public ProtocolMetaData() {
+    }
+
+    public ProtocolMetaData(String version) {
+        init(OEJB+"/"+version);
+    }
+
+    private void init(String spec) {
+        assert spec.matches("^OEJP/[0-9]\\.[0-9]$"): "Protocol version spec must follow format [ \"OEJB\" \"/\" 1*DIGIT \".\" 1*DIGIT ]";
+
+        char[] chars = new char[8];
+        spec.getChars(0, chars.length, chars, 0);
+
+        this.id = new String(chars, 0, 4);
+        this.major = Integer.parseInt(new String(chars, 5,1));
+        this.minor = Integer.parseInt(new String(chars, 7,1));
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public int getMajor() {
+        return major;
+    }
+
+    public int getMinor() {
+        return minor;
+    }
+
+    public String getVersion() {
+        return major+"."+minor;
+    }
+
+    public String getSpec() {
+        return id+"/"+major+"."+minor;
+    }
+
+    public void writeExternal(OutputStream out) throws IOException {
+        out.write(getSpec().getBytes("UTF-8"));
+    }
+
+    public void readExternal(InputStream in) throws IOException {
+        byte[] spec = new byte[8];
+        for (int i = 0; i < spec.length; i++) {
+            spec[i] = (byte) in.read();
+            if (spec[i] == -1){
+                throw new IOException("Unable to read protocol version.  Reached the end of the stream.");
+            }
+        }
+        init(new String(spec,"UTF-8"));
+    }
+}

Modified: openejb/trunk/openejb3/server/openejb-ejbd/src/main/java/org/apache/openejb/server/ejbd/EjbDaemon.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-ejbd/src/main/java/org/apache/openejb/server/ejbd/EjbDaemon.java?view=diff&rev=558909&r1=558908&r2=558909
==============================================================================
--- openejb/trunk/openejb3/server/openejb-ejbd/src/main/java/org/apache/openejb/server/ejbd/EjbDaemon.java (original)
+++ openejb/trunk/openejb3/server/openejb-ejbd/src/main/java/org/apache/openejb/server/ejbd/EjbDaemon.java Mon Jul 23 18:37:18 2007
@@ -32,19 +32,22 @@
 import org.apache.openejb.client.EJBRequest;
 import org.apache.openejb.client.RequestMethodConstants;
 import org.apache.openejb.client.EjbObjectInputStream;
+import org.apache.openejb.client.ProtocolMetaData;
 import org.apache.openejb.util.Logger;
 import org.apache.openejb.util.Messages;
 
 public class EjbDaemon implements org.apache.openejb.spi.ApplicationServer {
 
-    Messages _messages = new Messages("org.apache.openejb.server.util.resources");
-    Logger logger = Logger.getInstance("OpenEJB.server.remote", "org.apache.openejb.server.util.resources");
+    private static final ProtocolMetaData PROTOCOL_VERSION = new ProtocolMetaData("2.0");
 
-    ClientObjectFactory clientObjectFactory;
+    private static final Messages _messages = new Messages("org.apache.openejb.server.util.resources");
+    static final Logger logger = Logger.getInstance("OpenEJB.server.remote", "org.apache.openejb.server.util.resources");
+
+    private ClientObjectFactory clientObjectFactory;
 //    DeploymentIndex deploymentIndex;
-    EjbRequestHandler ejbHandler;
-    JndiRequestHandler jndiHandler;
-    AuthRequestHandler authHandler;
+    private EjbRequestHandler ejbHandler;
+    private JndiRequestHandler jndiHandler;
+    private AuthRequestHandler authHandler;
 
     boolean stop = false;
 
@@ -88,11 +91,18 @@
     }
 
     public void service(InputStream in, OutputStream out) throws IOException {
+        ProtocolMetaData protocolMetaData = new ProtocolMetaData();
+        String requestTypeName = null;
+
         ObjectInputStream ois = null;
         ObjectOutputStream oos = null;
 
         try {
 
+            protocolMetaData.readExternal(in);
+
+            PROTOCOL_VERSION.writeExternal(out);
+
             byte requestType = (byte) in.read();
 
             if (requestType == -1) {
@@ -102,38 +112,42 @@
             ois = new EjbObjectInputStream(in);
             oos = new ObjectOutputStream(out);
 
+            // Exceptions should not be thrown from these methods
+            // They should handle their own exceptions and clean
+            // things up with the client accordingly.
             switch (requestType) {
                 case RequestMethodConstants.EJB_REQUEST:
+                    requestTypeName = "EJB_REQUEST";
                     processEjbRequest(ois, oos);
                     break;
                 case RequestMethodConstants.JNDI_REQUEST:
+                    requestTypeName = "JNDI_REQUEST";
                     processJndiRequest(ois, oos);
                     break;
                 case RequestMethodConstants.AUTH_REQUEST:
+                    requestTypeName = "AUTH_REQUEST";
                     processAuthRequest(ois, oos);
                     break;
                 default:
-                    logger.error("Unknown request type " + requestType);
-            }
-            try {
-                if (oos != null) {
-                    oos.flush();
-                }
-            } catch (Throwable t) {
-                logger.error("Encountered problem while communicating with client: " + t.getMessage());
-            }
+                    requestTypeName = requestType+" (UNKNOWN)";
+                    logger.error("\"" + requestTypeName + " " + protocolMetaData.getSpec() + "\" FAIL \"Unknown request type " + requestType);
 
+            }
         } catch (SecurityException e) {
-            logger.error("Security error: " + e.getMessage());
+            logger.error("\""+requestTypeName +" "+ protocolMetaData.getSpec() + "\" FAIL \"Security error - "+e.getMessage()+"\"",e);
         } catch (Throwable e) {
-            logger.error("Unexpected error", e);
+            logger.error("\""+requestTypeName +" "+ protocolMetaData.getSpec() + "\" FAIL \"Unexpected error - "+e.getMessage()+"\"",e);
         } finally {
             try {
                 if (oos != null) {
                     oos.flush();
+                    oos.close();
+                } else if (out != null) {
+                    out.flush();
+                    out.close();
                 }
             } catch (Throwable t) {
-                logger.error("Encountered problem while flushing connection with client: " + t.getMessage());
+                logger.error("\""+requestTypeName +" "+ protocolMetaData.getSpec() + "\" FAIL \""+t.getMessage()+"\"");
             }
         }
     }