You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by da...@apache.org on 2007/04/06 20:56:42 UTC

svn commit: r526250 - in /incubator/openejb/trunk/openejb3: container/openejb-core/src/main/java/org/apache/openejb/core/ container/openejb-core/src/main/java/org/apache/openejb/core/entity/ itests/openejb-itests-beans/src/main/java/org/apache/openejb/...

Author: dain
Date: Fri Apr  6 11:56:42 2007
New Revision: 526250

URL: http://svn.apache.org/viewvc?view=rev&rev=526250
Log:
Handle NoSuchEntityException

Added:
    incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/NoSuchObjectException.java
Modified:
    incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityContainer.java
    incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityInstanceManager.java
    incubator/openejb/trunk/openejb3/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/entity/bmp/BasicBmpBean.java

Added: incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/NoSuchObjectException.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/NoSuchObjectException.java?view=auto&rev=526250
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/NoSuchObjectException.java (added)
+++ incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/NoSuchObjectException.java Fri Apr  6 11:56:42 2007
@@ -0,0 +1,198 @@
+/**
+ *
+ * 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.core;
+
+import java.io.PrintStream;
+import java.io.PrintWriter;
+
+/**
+ * Subclass of java.rmi.NoSuchObjectException which adds init cause to the exception.
+ */
+public class NoSuchObjectException extends java.rmi.NoSuchObjectException {
+    private Throwable cause = this;
+
+    public NoSuchObjectException(String detailMessage) {
+        super(detailMessage);
+    }
+
+    /**
+     * Constructs a new instance of this class with its walkback, message and
+     * cause filled in.
+     *
+     * @param detailMessage String The detail message for the exception.
+     * @param throwable The cause of this Throwable
+     */
+    public NoSuchObjectException(String detailMessage, Throwable throwable) {
+        super(detailMessage);
+        cause = throwable;
+    }
+
+    public NoSuchObjectException(Throwable throwable) {
+        super(throwable == null ? null : throwable.toString());
+        cause = throwable;
+    }
+
+    /**
+     * Answers the extra information message which was provided when the
+     * throwable was created. If no message was provided at creation time, then
+     * answer null. Subclasses may override this method to answer localized text
+     * for the message.
+     *
+     * @return String The receiver's message.
+     */
+    public String getLocalizedMessage() {
+        return getMessage();
+    }
+
+    /**
+     * Outputs a printable representation of the receiver's walkback on the
+     * System.err stream.
+     */
+    public void printStackTrace() {
+        printStackTrace(System.err);
+    }
+
+    /**
+     * Count the number of duplicate stack frames, starting from
+     * the end of the stack.
+     *
+     * @param currentStack a stack to compare
+     * @param parentStack a stack to compare
+     * @return the number of duplicate stack frames.
+     */
+    private static int countDuplicates(StackTraceElement[] currentStack,
+            StackTraceElement[] parentStack) {
+        int duplicates = 0;
+        int parentIndex = parentStack.length;
+        for (int i = currentStack.length; --i >= 0 && --parentIndex >= 0;) {
+            StackTraceElement parentFrame = parentStack[parentIndex];
+            if (parentFrame.equals(currentStack[i])) {
+                duplicates++;
+            } else {
+                break;
+            }
+        }
+        return duplicates;
+    }
+
+    /**
+     * Outputs a printable representation of the receiver's walkback on the
+     * stream specified by the argument.
+     *
+     * @param err PrintStream The stream to write the walkback on.
+     */
+    public void printStackTrace(PrintStream err) {
+        err.println(toString());
+        // Don't use getStackTrace() as it calls clone()
+        // Get stackTrace, in case stackTrace is reassigned
+        StackTraceElement[] stack = getStackTrace();
+        for (int i = 0; i < stack.length; i++) {
+            err.println("\tat " + stack[i]);
+        }
+
+        StackTraceElement[] parentStack = stack;
+        Throwable throwable = getCause();
+        while (throwable != null) {
+            err.print("Caused by: ");
+            err.println(throwable);
+            StackTraceElement[] currentStack = throwable.getStackTrace();
+            int duplicates = countDuplicates(currentStack, parentStack);
+            for (int i = 0; i < currentStack.length - duplicates; i++) {
+                err.println("\tat " + currentStack[i]);
+            }
+            if (duplicates > 0) {
+                err.println("\t... " + duplicates + " more");
+            }
+            parentStack = currentStack;
+            throwable = throwable.getCause();
+        }
+    }
+
+    /**
+     * Outputs a printable representation of the receiver's walkback on the
+     * writer specified by the argument.
+     *
+     * @param err PrintWriter The writer to write the walkback on.
+     */
+    public void printStackTrace(PrintWriter err) {
+        err.println(toString());
+        // Don't use getStackTrace() as it calls clone()
+        // Get stackTrace, in case stackTrace is reassigned
+        StackTraceElement[] stack = getStackTrace();
+        for (int i = 0; i < stack.length; i++) {
+            err.println("\tat " + stack[i]);
+        }
+
+        StackTraceElement[] parentStack = stack;
+        Throwable throwable = getCause();
+        while (throwable != null) {
+            err.print("Caused by: ");
+            err.println(throwable);
+            StackTraceElement[] currentStack = throwable.getStackTrace();
+            int duplicates = countDuplicates(currentStack, parentStack);
+            for (int i = 0; i < currentStack.length - duplicates; i++) {
+                err.println("\tat " + currentStack[i]);
+            }
+            if (duplicates > 0) {
+                err.println("\t... " + duplicates + " more");
+            }
+            parentStack = currentStack;
+            throwable = throwable.getCause();
+        }
+    }
+
+    /**
+     * Answers a string containing a concise, human-readable description of the
+     * receiver.
+     *
+     * @return String a printable representation for the receiver.
+     */
+    public String toString() {
+        String msg = getLocalizedMessage();
+        String name = getClass().getName();
+        if (msg == null) {
+            return name;
+        }
+        return new StringBuffer(name.length() + 2 + msg.length()).append(name).append(": ").append(msg).toString();
+    }
+
+    /**
+     * Initialize the cause of the receiver. The cause cannot be reassigned.
+     *
+     * @param throwable The cause of this Throwable
+     * @return the receiver.
+     * @throws IllegalArgumentException when the cause is the receiver
+     * @throws IllegalStateException when the cause has already been initialized
+     */
+    public synchronized NoSuchObjectException initCause(Throwable throwable) {
+        cause = throwable;
+        return this;
+    }
+
+    /**
+     * Answers the cause of this Throwable, or null if there is no cause.
+     *
+     * @return Throwable The receiver's cause.
+     */
+    public Throwable getCause() {
+        if (cause == this) {
+            return null;
+        }
+        return cause;
+    }
+}

Modified: incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityContainer.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityContainer.java?view=diff&rev=526250&r1=526249&r2=526250
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityContainer.java (original)
+++ incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityContainer.java Fri Apr  6 11:56:42 2007
@@ -30,6 +30,8 @@
 import javax.ejb.EJBObject;
 import javax.ejb.EntityBean;
 import javax.ejb.Timer;
+import javax.ejb.NoSuchEntityException;
+import javax.ejb.EJBException;
 import javax.transaction.Transaction;
 import javax.transaction.TransactionManager;
 
@@ -43,6 +45,7 @@
 import org.apache.openejb.core.Operation;
 import org.apache.openejb.core.ThreadContext;
 import org.apache.openejb.core.CoreDeploymentInfo;
+import org.apache.openejb.core.NoSuchObjectException;
 import org.apache.openejb.core.timer.EjbTimerService;
 import org.apache.openejb.core.timer.EjbTimerServiceImpl;
 import org.apache.openejb.core.transaction.TransactionContainer;
@@ -253,10 +256,15 @@
                 callContext.setCurrentAllowedStates(EntityContext.getStates());
                 try {
                     bean.ejbLoad();
+                } catch (NoSuchEntityException e) {
+                    instanceManager.discardInstance(callContext, bean);
+                    throw new org.apache.openejb.InvalidateReferenceException(new NoSuchObjectException("Entity not found: " + callContext.getPrimaryKey())/*.initCause(e)*/);
                 } catch (Exception e) {
-
                     instanceManager.discardInstance(callContext, bean);
                     throw e;
+//                } catch (Throwable e) {
+//                    System.out.println(e.getClass().getName() + "[" + System.identityHashCode(e.getClass()) + "]@" + System.identityHashCode(e));
+//                    System.out.println(NoSuchEntityException.class.getName() + "[" + System.identityHashCode(NoSuchEntityException.class) + "]");
                 } finally {
                     callContext.setCurrentOperation(orginalOperation);
                     callContext.setCurrentAllowedStates(originalAllowedStates);

Modified: incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityInstanceManager.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityInstanceManager.java?view=diff&rev=526250&r1=526249&r2=526250
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityInstanceManager.java (original)
+++ incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/entity/EntityInstanceManager.java Fri Apr  6 11:56:42 2007
@@ -23,6 +23,7 @@
 import org.apache.openejb.core.CoreDeploymentInfo;
 import org.apache.openejb.core.Operation;
 import org.apache.openejb.core.ThreadContext;
+import org.apache.openejb.core.NoSuchObjectException;
 import org.apache.openejb.core.transaction.TransactionRolledbackException;
 import org.apache.openejb.util.LinkedListStack;
 import org.apache.openejb.util.Logger;
@@ -37,7 +38,6 @@
 import javax.transaction.SystemException;
 import java.util.HashMap;
 import java.util.Hashtable;
-import java.rmi.NoSuchObjectException;
 
 public class EntityInstanceManager {
 

Modified: incubator/openejb/trunk/openejb3/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/entity/bmp/BasicBmpBean.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/entity/bmp/BasicBmpBean.java?view=diff&rev=526250&r1=526249&r2=526250
==============================================================================
--- incubator/openejb/trunk/openejb3/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/entity/bmp/BasicBmpBean.java (original)
+++ incubator/openejb/trunk/openejb3/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/entity/bmp/BasicBmpBean.java Fri Apr  6 11:56:42 2007
@@ -28,6 +28,7 @@
 import javax.ejb.EntityContext;
 import javax.ejb.FinderException;
 import javax.ejb.RemoveException;
+import javax.ejb.NoSuchEntityException;
 import javax.naming.InitialContext;
 import javax.sql.DataSource;
 
@@ -318,12 +319,15 @@
             try {
                 PreparedStatement stmt = con.prepareStatement("select * from entity where id = ?");
                 try {
-                    Integer primaryKey = (Integer)ejbContext.getPrimaryKey();
-                    stmt.setInt(1, primaryKey.intValue());
+                    stmt.setInt(1, primaryKey);
                     ResultSet rs = stmt.executeQuery();
-                    while ( rs.next() ) {
-                        lastName = rs.getString("last_name");
-                        firstName = rs.getString("first_name");
+                    if (!rs.next()) {
+                        throw new NoSuchEntityException("" + primaryKey);
+                    }
+                    lastName = rs.getString("last_name");
+                    firstName = rs.getString("first_name");
+                    if (rs.next()) {
+                        throw new EJBException("Found more than one entity with id " + primaryKey);
                     }
                 } finally {
                     stmt.close();
@@ -400,8 +404,7 @@
             try {
                 PreparedStatement stmt = con.prepareStatement("delete from entity where id = ?");
                 try {
-                    Integer primaryKey = (Integer)ejbContext.getPrimaryKey();
-                    stmt.setInt(1, primaryKey.intValue());
+                    stmt.setInt(1, primaryKey);
                     stmt.executeUpdate();
                 } finally {
                     stmt.close();
@@ -423,6 +426,7 @@
      * the ready state.
      */
     public void ejbActivate() throws EJBException,RemoteException {
+        primaryKey = (Integer)ejbContext.getPrimaryKey();
         testAllowedOperations("ejbActivate");
     }
 
@@ -434,6 +438,7 @@
      */
     public void ejbPassivate() throws EJBException,RemoteException {
         testAllowedOperations("ejbPassivate");
+        primaryKey = -1;
     }