tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dblev...@apache.org
Subject svn commit: r813292 - in /openejb/trunk/openejb3/server/openejb-client/src: main/java/org/apache/openejb/client/ test/java/org/apache/openejb/client/
Date Thu, 10 Sep 2009 08:08:39 GMT
Author: dblevins
Date: Thu Sep 10 08:08:38 2009
New Revision: 813292

URL: http://svn.apache.org/viewvc?rev=813292&view=rev
Log:
Fix for OPENEJB-1062: StickyConnectionStrategy incorrectly caches connections

Added:
    openejb/trunk/openejb3/server/openejb-client/src/test/java/org/apache/openejb/client/OverlyStickyConnectionTest.java
  (with props)
Modified:
    openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ClusterMetaData.java
    openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ClusterResponse.java
    openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
    openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/StickyConnectionStrategy.java
    openejb/trunk/openejb3/server/openejb-client/src/test/java/org/apache/openejb/client/MainTest.java

Modified: openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ClusterMetaData.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ClusterMetaData.java?rev=813292&r1=813291&r2=813292&view=diff
==============================================================================
--- openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ClusterMetaData.java
(original)
+++ openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ClusterMetaData.java
Thu Sep 10 08:08:38 2009
@@ -30,6 +30,7 @@
     private URI[] locations;
     private long version;
     private String connectionStrategy;
+    private URI lastLocation;
 
     public ClusterMetaData() {
     }
@@ -39,6 +40,14 @@
         this.version = version;
     }
 
+    public URI getLastLocation() {
+        return lastLocation;
+    }
+
+    public void setLastLocation(URI lastLocation) {
+        this.lastLocation = lastLocation;
+    }
+
     public URI[] getLocations() {
         return locations;
     }

Modified: openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ClusterResponse.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ClusterResponse.java?rev=813292&r1=813291&r2=813292&view=diff
==============================================================================
--- openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ClusterResponse.java
(original)
+++ openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ClusterResponse.java
Thu Sep 10 08:08:38 2009
@@ -32,6 +32,10 @@
     private ClusterMetaData updatedMetaData;
     private Throwable failure;
 
+    public ClusterResponse(Code responseCode) {
+        this.responseCode = responseCode;
+    }
+
     public ClusterResponse() {
     }
 

Modified: openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java?rev=813292&r1=813291&r2=813292&view=diff
==============================================================================
--- openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
(original)
+++ openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
Thu Sep 10 08:08:38 2009
@@ -56,6 +56,7 @@
     private ClientMetaData client;
     private Hashtable env;
     private String moduleId;
+    private ClientInstance clientIdentity;
 
     public JNDIContext() {
     }
@@ -69,6 +70,7 @@
         this.client = that.client;
         this.moduleId = that.moduleId;
         this.env = (Hashtable) that.env.clone();
+        this.clientIdentity = that.clientIdentity;
     }
 
     private JNDIResponse request(JNDIRequest req) throws Exception {

Modified: openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/StickyConnectionStrategy.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/StickyConnectionStrategy.java?rev=813292&r1=813291&r2=813292&view=diff
==============================================================================
--- openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/StickyConnectionStrategy.java
(original)
+++ openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/StickyConnectionStrategy.java
Thu Sep 10 08:08:38 2009
@@ -22,27 +22,25 @@
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.Set;
-import java.util.HashSet;
 import java.util.Arrays;
 import java.util.LinkedHashSet;
 
 public class StickyConnectionStrategy implements ConnectionStrategy {
     private static final Logger LOGGER = Logger.getLogger("OpenEJB.client");
 
-    private URI lastLocation;
-
     public Connection connect(ClusterMetaData cluster, ServerMetaData server) throws IOException
{
         Set<URI> failed = Client.getFailed();
 
         URI[] locations = cluster.getLocations();
 
         if (locations.length == 0){
-            return connect(server.getLocation());
+            return connect(cluster, server.getLocation());
         }
 
+        URI lastLocation = cluster.getLastLocation();
         if (null != lastLocation && !failed.contains(lastLocation)) {
             try {
-                return connect(lastLocation);
+                return connect(cluster, lastLocation);
             } catch (IOException e) {
                 if (locations.length > 1){
                     LOGGER.log(Level.WARNING, "Failing over.  Cannot connect to last server:
" + lastLocation.toString() + " Exception: " + e.getClass().getName() +" " + e.getMessage());
@@ -57,7 +55,7 @@
 
         for (URI uri : remaining) {
             try {
-                return connect(uri);
+                return connect(cluster, uri);
             } catch (IOException e) {
                 failed.add(uri);
                 LOGGER.log(Level.WARNING, "Failover: Cannot connect to server(s): " + uri.toString()
+ " Exception: " + e.getMessage()+".  Trying next.");
@@ -70,7 +68,7 @@
         remaining.removeAll(failed);
 
         if (remaining.size() == 0 && !failed.contains(server.getLocation())){
-            return connect(server.getLocation());
+            return connect(cluster, server.getLocation());
         }
 
         // If no servers responded, throw an error
@@ -82,13 +80,13 @@
         throw new RemoteException("Cannot connect to any servers: " + buffer.toString());
     }
 
-    protected Connection connect(URI uri) throws IOException {
+    protected Connection connect(ClusterMetaData cluster, URI uri) throws IOException {
         Connection connection = ConnectionManager.getConnection(uri);
 
         // Grabbing the URI from the associated connection allows the ConnectionFactory to
         // employ discovery to find and connect to a server.  We then attempt to connect
         // to the discovered server rather than repeat the discovery process again.
-        lastLocation = connection.getURI();
+        cluster.setLastLocation(connection.getURI());
         return connection;
     }
 

Modified: openejb/trunk/openejb3/server/openejb-client/src/test/java/org/apache/openejb/client/MainTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/test/java/org/apache/openejb/client/MainTest.java?rev=813292&r1=813291&r2=813292&view=diff
==============================================================================
--- openejb/trunk/openejb3/server/openejb-client/src/test/java/org/apache/openejb/client/MainTest.java
(original)
+++ openejb/trunk/openejb3/server/openejb-client/src/test/java/org/apache/openejb/client/MainTest.java
Thu Sep 10 08:08:38 2009
@@ -38,10 +38,11 @@
 import java.security.AccessController;
 
 public class MainTest extends TestCase {
-    static {
+
+    private static void initialize() {
         try {
             NamingManager.setInitialContextFactoryBuilder(new MockContextFactoryBuilder());
-        } catch (NamingException e) {
+        } catch (Exception e) {
         }
     }
 
@@ -49,6 +50,7 @@
 
     protected void setUp() throws Exception {
         super.setUp();
+        initialize();
         LoginTestUtil.initialize();
 
         jndi.clear();

Added: openejb/trunk/openejb3/server/openejb-client/src/test/java/org/apache/openejb/client/OverlyStickyConnectionTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/test/java/org/apache/openejb/client/OverlyStickyConnectionTest.java?rev=813292&view=auto
==============================================================================
--- openejb/trunk/openejb3/server/openejb-client/src/test/java/org/apache/openejb/client/OverlyStickyConnectionTest.java
(added)
+++ openejb/trunk/openejb3/server/openejb-client/src/test/java/org/apache/openejb/client/OverlyStickyConnectionTest.java
Thu Sep 10 08:08:38 2009
@@ -0,0 +1,104 @@
+/**
+ * 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 junit.framework.TestCase;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectOutputStream;
+import java.net.URI;
+import java.util.Properties;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class OverlyStickyConnectionTest extends TestCase {
+
+    public void test() throws Exception {
+        ConnectionManager.registerFactory("mock", new MockConnectionFactory());
+
+        getContext("mock://red:4201").lookup("foo");
+
+        assertEquals(new URI("mock://red:4201"), connection.get().getURI());
+
+        getContext("mock://blue:4201").lookup("foo");
+
+        assertEquals(new URI("mock://blue:4201"), connection.get().getURI());
+
+        getContext("mock://red:4201").lookup("foo");
+
+        assertEquals(new URI("mock://red:4201"), connection.get().getURI());
+    }
+
+    private InitialContext getContext(String uri) throws NamingException {
+        Properties p = new Properties();
+        p.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
+        p.put("java.naming.provider.url", uri);
+
+        InitialContext context = new InitialContext(p);
+        return context;
+    }
+
+    private final ThreadLocal<Connection> connection = new ThreadLocal<Connection>();
+
+    public class MockConnectionFactory implements ConnectionFactory {
+
+        public Connection getConnection(final URI uri) throws IOException {
+            return new Connection() {
+                private ByteArrayInputStream in;
+                private ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+                {
+                    connection.set(this);
+                    new ProtocolMetaData("3.1").writeExternal(out);
+                    ObjectOutputStream oos = new ObjectOutputStream(out);
+                    new ClusterResponse(ClusterResponse.Code.CURRENT).writeExternal(oos);
+                    new JNDIResponse(ResponseCodes.JNDI_CONTEXT, null).writeExternal(oos);
+                    oos.close();
+
+                    in = new ByteArrayInputStream(out.toByteArray());
+                    out.reset();
+                }
+
+                public URI getURI() {
+                    return uri;
+                }
+
+                public void discard() {
+                }
+
+                public void close() throws IOException {
+                }
+
+                public InputStream getInputStream() throws IOException {
+                    return in;
+                }
+
+                public OutputStream getOuputStream() throws IOException {
+                    return out;
+                }
+            };
+        }
+    }
+
+}

Propchange: openejb/trunk/openejb3/server/openejb-client/src/test/java/org/apache/openejb/client/OverlyStickyConnectionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message