geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r369313 - in /geronimo/trunk/modules/session/src: java/org/apache/geronimo/session/remote/ test/org/apache/geronimo/session/remote/
Date Mon, 16 Jan 2006 02:28:50 GMT
Author: jstrachan
Date: Sun Jan 15 18:28:48 2006
New Revision: 369313

URL: http://svn.apache.org/viewcvs?rev=369313&view=rev
Log:
added a simple version of a remote-capable Locator which supports local or remote session
objects which can be moved and new servers added or existing ones removed

Added:
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/DefaultRemoteSessionStrategy.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemotableSessionLocation.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteClient.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteLocator.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteServer.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/SessionListener.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/package.html
    geronimo/trunk/modules/session/src/test/org/apache/geronimo/session/remote/
    geronimo/trunk/modules/session/src/test/org/apache/geronimo/session/remote/RemoteSessionTest.java

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/DefaultRemoteSessionStrategy.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/DefaultRemoteSessionStrategy.java?rev=369313&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/DefaultRemoteSessionStrategy.java
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/DefaultRemoteSessionStrategy.java
Sun Jan 15 18:28:48 2006
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.geronimo.session.remote;
+
+import org.apache.geronimo.session.SessionLocation;
+
+/**
+ * 
+ * @version $Revision: $
+ */
+public class DefaultRemoteSessionStrategy {
+
+    public static final int REDIRECT = 1;
+    public static final int PROXY = 2;
+    public static final int MOVE = 3;
+
+    private boolean redirectSupported;
+    private int redirectWatermark = 5;
+    private int moveWatermark = 10;
+
+    public int decide(SessionLocation location) {
+        int count = getRecentRequestCount(location.getSessionId());
+        if (redirectSupported) {
+            if (count < redirectWatermark) {
+                return REDIRECT;
+            }
+        }
+        if (count < moveWatermark) {
+            return PROXY;
+        }
+        return MOVE;
+    }
+
+    private int getRecentRequestCount(String sessionId) {
+        return 0;
+    }
+}

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemotableSessionLocation.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemotableSessionLocation.java?rev=369313&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemotableSessionLocation.java
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemotableSessionLocation.java
Sun Jan 15 18:28:48 2006
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.geronimo.session.remote;
+
+import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
+import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
+
+import org.apache.geronimo.session.NoSuchSessionException;
+import org.apache.geronimo.session.Server;
+import org.apache.geronimo.session.Session;
+import org.apache.geronimo.session.SessionLocation;
+import org.apache.geronimo.session.SessionNotLocalException;
+import org.apache.geronimo.session.SessionNotMovableException;
+import org.apache.geronimo.session.WriteLockTimedOutException;
+import org.apache.geronimo.session.local.LocalLocator;
+import org.apache.geronimo.session.local.LocalSessionLocation;
+import org.apache.geronimo.session.local.SessionHandle;
+import org.apache.geronimo.session.local.SessionImpl;
+
+import java.util.Map;
+
+/**
+ * An implementation of {@link SessionLocation} which can be either local or
+ * remote.
+ * 
+ * @version $Revision: $
+ */
+public class RemotableSessionLocation extends LocalSessionLocation {
+    private final String sessionId;
+    private Server server;
+    private String lastServerToLockMe;
+    private long lockTimeout = 10000L;
+    private boolean movable = true;
+
+    public RemotableSessionLocation(LocalLocator locator, RemoteServer server, String sessionId)
{
+        super(locator, null);
+        this.server = server;
+        this.sessionId = sessionId;
+    }
+
+    public RemotableSessionLocation(LocalLocator locator, SessionImpl session) {
+        super(locator, session);
+        this.server = locator.getLocalServer();
+        this.sessionId = session.getSessionId();
+    }
+
+    public String getSessionId() {
+        return sessionId;
+    }
+
+    public boolean isLocal() {
+        Lock readLock = lock.readLock();
+        readLock.lock();
+        try {
+            return session != null;
+        }
+        finally {
+            readLock.unlock();
+        }
+    }
+
+    public Session moveLocally() throws NoSuchSessionException, WriteLockTimedOutException,
SessionNotLocalException,
+            SessionNotMovableException {
+        if (!isMovable()) {
+            throw new SessionNotMovableException(sessionId);
+        }
+        Lock writeLock = lock.writeLock();
+        writeLock.lock();
+        Server remoteServer = getServer();
+
+        try {
+            if (session != null) {
+                return new SessionHandle(this);
+            }
+            Server localServer = getLocator().getLocalServer();
+            String localServerName = localServer.getName();
+
+            RemoteClient remoteControl = ((RemoteServer) remoteServer).getRemoteClient();
+
+            Map state = remoteControl.moveState(localServerName, sessionId);
+            try {
+                session = new SessionImpl(sessionId, state);
+                server = localServer;
+                return new SessionHandle(this);
+            }
+            finally {
+                remoteControl.unlock(localServerName, sessionId);
+            }
+        }
+        catch (WriteLockTimedOutException e) {
+            session = null;
+            server = remoteServer;
+            throw e;
+        }
+        finally {
+            writeLock.unlock();
+        }
+    }
+
+    public Map lockForWrite(String serverName) throws SessionNotLocalException, WriteLockTimedOutException
{
+        Lock writeLock = lock.writeLock();
+        try {
+            if (writeLock.tryLock(lockTimeout, TimeUnit.MILLISECONDS)) {
+                if (session == null) {
+                    writeLock.unlock();
+                    throw new SessionNotLocalException(sessionId);
+                }
+                lastServerToLockMe = serverName;
+                return session.getStateMap();
+            }
+            else {
+                throw new WriteLockTimedOutException(sessionId);
+            }
+        }
+        catch (InterruptedException e) {
+            throw new WriteLockTimedOutException(sessionId);
+        }
+    }
+
+    public void unlock(String serverName) throws WriteLockTimedOutException {
+        //
+        // FIXME
+        //
+        // TODO: the locks don't actually work as they are typically called
+        // from different threads; we need a better way to grab the write lock
+        // for a timeout until the unlock method comes in
+        // 
+        Lock writeLock = lock.writeLock();
+        if (lastServerToLockMe != null && serverName.equals(lastServerToLockMe))
{
+            lastServerToLockMe = null;
+            writeLock.unlock();
+        }
+        else {
+            throw new WriteLockTimedOutException(sessionId);
+        }
+    }
+
+    public boolean isMovable() {
+        return movable;
+    }
+
+    public void setMovable(boolean movable) {
+        this.movable = movable;
+    }
+
+    public Server getServer() {
+        Lock readLock = lock.readLock();
+        readLock.lock();
+        try {
+            return server;
+        }
+        finally {
+            readLock.unlock();
+        }
+    }
+
+}

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteClient.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteClient.java?rev=369313&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteClient.java
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteClient.java
Sun Jan 15 18:28:48 2006
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.geronimo.session.remote;
+
+import org.apache.geronimo.session.NoSuchSessionException;
+import org.apache.geronimo.session.SessionNotLocalException;
+import org.apache.geronimo.session.WriteLockTimedOutException;
+
+import java.util.Map;
+
+/**
+ * The remote interface to a server; this interface will usually be wrapped in a dynamic
proxy
+ * to do some kind of remoting such as via Lingo, ActiveIO or OpenEJB
+ * 
+ * @version $Revision: $
+ */
+public interface RemoteClient extends SessionListener {
+
+    Map moveState(String serverName, String sessionId) throws NoSuchSessionException, SessionNotLocalException,
+            WriteLockTimedOutException;
+
+    void unlock(String serverName, String sessionId) throws NoSuchSessionException, WriteLockTimedOutException;
+
+}

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteLocator.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteLocator.java?rev=369313&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteLocator.java
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteLocator.java
Sun Jan 15 18:28:48 2006
@@ -0,0 +1,165 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.geronimo.session.remote;
+
+import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
+import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArraySet;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.geronimo.session.Locator;
+import org.apache.geronimo.session.NoSuchSessionException;
+import org.apache.geronimo.session.Server;
+import org.apache.geronimo.session.SessionNotLocalException;
+import org.apache.geronimo.session.WriteLockTimedOutException;
+import org.apache.geronimo.session.local.LocalLocator;
+import org.apache.geronimo.session.local.LocalSessionLocation;
+import org.apache.geronimo.session.local.SessionImpl;
+
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * A {@link Locator} which supports remote servers and remote sessions.
+ * 
+ * @version $Revision: $
+ */
+public class RemoteLocator extends LocalLocator implements RemoteClient, SessionListener
{
+    private static final Log log = LogFactory.getLog(RemoteLocator.class);
+
+    private SessionListener sessionListener;
+    private Set servers = new CopyOnWriteArraySet();
+    private Map serverMap = new ConcurrentHashMap();
+
+    public RemoteLocator(String localServerName) {
+        super(localServerName);
+    }
+
+    public synchronized void addSession(LocalSessionLocation location) {
+        super.addSession(location);
+        if (sessionListener != null && location.isLocal()) {
+            String localServerName = getLocalServerName();
+            String sessionId = location.getSessionId();
+            sessionListener.onSessionCreate(localServerName, sessionId);
+        }
+    }
+
+    public void removeMovedSession(SessionImpl session) {
+        super.removeMovedSession(session);
+        if (sessionListener != null) {
+            sessionListener.onSessionMove(getLocalServerName(), session.getSessionId());
+        }
+    }
+
+    // Properties
+    // -------------------------------------------------------------------------
+    public SessionListener getSessionListener() {
+        return sessionListener;
+    }
+
+    public void setSessionListener(SessionListener sessionListener) {
+        this.sessionListener = sessionListener;
+    }
+
+    // RemoteControl interface
+    // -------------------------------------------------------------------------
+    public Map moveState(String serverName, String sessionId) throws NoSuchSessionException,
SessionNotLocalException,
+            WriteLockTimedOutException {
+        RemotableSessionLocation location = (RemotableSessionLocation) getSessionLocation(sessionId);
+        if (location == null) {
+            throw new NoSuchSessionException(sessionId);
+        }
+        return location.lockForWrite(serverName);
+    }
+
+    public void unlock(String serverName, String sessionId) throws NoSuchSessionException,
WriteLockTimedOutException {
+        RemotableSessionLocation location = (RemotableSessionLocation) getSessionLocation(sessionId);
+        location.unlock(serverName);
+    }
+
+    // SessionListener interface
+    // -------------------------------------------------------------------------
+    public void onServerCreate(Server server, RemoteClient remoteLocator) {
+        server = new RemoteServer(server, remoteLocator);
+        servers.add(server);
+        serverMap.put(server.getName(), server);
+    }
+
+    public void onServerRemove(String serverName) {
+        Server server = (Server) serverMap.remove(serverName);
+        if (server != null) {
+            servers.remove(server);
+        }
+    }
+
+    public void onSessionCreate(String serverName, String sessionId) {
+        RemoteServer server = getRemoteServer(serverName);
+        if (server == null) {
+            log.warn("Unknown server: " + serverName + " for session: " + sessionId);
+        }
+        else {
+            RemotableSessionLocation location = new RemotableSessionLocation(this, server,
sessionId);
+            addSession(location);
+        }
+    }
+
+    public void onSessionMove(String serverName, String sessionId) {
+    }
+
+    public void onSessionDestroy(String serverName, String sessionId) {
+    }
+
+    // Helper methods
+    // -------------------------------------------------------------------------
+    public RemoteClient getRemoteClient() {
+        return this;
+    }
+
+    public RemoteServer[] getRemoteServers() {
+        synchronized (servers) {
+            RemoteServer[] answer = new RemoteServer[servers.size()];
+            servers.toArray(answer);
+            return answer;
+        }
+    }
+
+    public RemoteServer getRemoteServer(String serverName) {
+        return (RemoteServer) serverMap.get(serverName);
+    }
+
+    // Implementation methods
+    // -------------------------------------------------------------------------
+    public synchronized void sessionDestroyed(LocalSessionLocation location) {
+        super.sessionDestroyed(location);
+        if (sessionListener != null) {
+            sessionListener.onSessionDestroy(getLocalServerName(), location.getSessionId());
+        }
+    }
+
+    protected synchronized SessionImpl getSession(String sessionId) throws NoSuchSessionException
{
+        LocalSessionLocation location = (LocalSessionLocation) getSessionLocation(sessionId);
+        if (location != null) {
+            return location.getLocalSession();
+        }
+        throw new NoSuchSessionException(sessionId);
+    }
+
+    protected LocalSessionLocation createSessionLocation(String sessionId) {
+        SessionImpl session = new SessionImpl(sessionId);
+        RemotableSessionLocation answer = new RemotableSessionLocation(this, session);
+        return answer;
+    }
+}

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteServer.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteServer.java?rev=369313&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteServer.java
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/RemoteServer.java
Sun Jan 15 18:28:48 2006
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.geronimo.session.remote;
+
+import org.apache.geronimo.session.Server;
+
+/**
+ * A remote server reference which uses a remote proxy for the
+ * {@link RemoteClient}
+ * 
+ * @version $Revision: $
+ */
+public class RemoteServer implements Server {
+
+    private final Server server;
+    private final RemoteClient remoteClient;
+
+    public RemoteServer(Server server, RemoteClient control) {
+        this.server = server;
+        this.remoteClient = control;
+    }
+
+    public boolean isLocalServer() {
+        return false;
+    }
+
+    public String[] getAddresses(String protocol) {
+        return server.getAddresses(protocol);
+    }
+
+    public String getName() {
+        return server.getName();
+    }
+
+    public void setAddresses(String string, String[] strings) {
+        server.setAddresses(string, strings);
+    }
+
+    public RemoteClient getRemoteClient() {
+        return remoteClient;
+    }
+}

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/SessionListener.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/SessionListener.java?rev=369313&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/SessionListener.java
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/SessionListener.java
Sun Jan 15 18:28:48 2006
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.geronimo.session.remote;
+
+import org.apache.geronimo.session.Server;
+
+/**
+ * Notifications of new sessions arriving, sessions being destroyed or sessions
+ * migrating.
+ * 
+ * @version $Revision: $
+ */
+public interface SessionListener {
+    public void onServerCreate(Server server, RemoteClient remoteServerCallback);
+
+    public void onServerRemove(String serverName);
+
+    public void onSessionCreate(String serverName, String sessionId);
+
+    public void onSessionMove(String serverName, String sessionId);
+
+    public void onSessionDestroy(String serverName, String sessionId);
+
+}

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/package.html
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/package.html?rev=369313&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/package.html
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/remote/package.html
Sun Jan 15 18:28:48 2006
@@ -0,0 +1,11 @@
+<html>
+<head>
+</head>
+<body>
+
+<p>
+An implementation which supports remote servers (possibly by discovery) together with remote
sessions
+</p>
+
+</body>
+</html>

Added: geronimo/trunk/modules/session/src/test/org/apache/geronimo/session/remote/RemoteSessionTest.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/test/org/apache/geronimo/session/remote/RemoteSessionTest.java?rev=369313&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/test/org/apache/geronimo/session/remote/RemoteSessionTest.java
(added)
+++ geronimo/trunk/modules/session/src/test/org/apache/geronimo/session/remote/RemoteSessionTest.java
Sun Jan 15 18:28:48 2006
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.geronimo.session.remote;
+
+import org.apache.geronimo.session.Locator;
+import org.apache.geronimo.session.Server;
+import org.apache.geronimo.session.Session;
+import org.apache.geronimo.session.SessionLocation;
+import org.apache.geronimo.session.SessionNotLocalException;
+import org.apache.geronimo.session.SessionTest;
+
+import java.util.Arrays;
+
+/**
+ * 
+ * @version $Revision: $
+ */
+public class RemoteSessionTest extends SessionTest {
+
+    public void testMove() throws Exception {
+        SessionLocation location = locator.getSessionLocation("remote");
+
+        assertEquals("state.isLocal", false, location.isLocal());
+
+        Session session = null;
+        try {
+            session = location.getSession();
+            fail("Cannot access the session on a remote session");
+        }
+        catch (SessionNotLocalException e) {
+            System.out.println("Caught expected exception: " + e);
+        }
+
+        // now lets move it
+        session = location.moveLocally();
+
+        assertEquals("state.isLocal", true, location.isLocal());
+        session = location.getSession();
+        useSession(session);
+    }
+
+    public void testRedirectOrProxy() throws Exception {
+        SessionLocation location = locator.getSessionLocation("remote");
+
+        assertEquals("state.isLocal", false, location.isLocal());
+
+        Session session = null;
+        try {
+            session = location.getSession();
+            fail("Cannot access the session on a remote session");
+        }
+        catch (SessionNotLocalException e) {
+            System.out.println("Caught expected exception: " + e);
+        }
+
+        // now lets access the server information as we are going to
+        // either redirect or proxy the request to where the session really is
+
+        Server server = location.getServer();
+        assertNotNull("server", server);
+
+        String[] addresses = server.getAddresses("ejb");
+        assertNotNull("addresses", addresses);
+    }
+
+    public void testServerList() throws Exception {
+        RemoteServer[] servers = getLocator().getRemoteServers();
+        assertNotNull("servers should not be null", servers);
+        assertTrue("should be at least one server always", servers.length > 0);
+
+        for (int i = 0; i < servers.length; i++) {
+            RemoteServer server = servers[i];
+            System.out.println("Server: " + server.getName() + " has addresses: "
+                    + Arrays.asList(server.getAddresses("ejb")));
+
+            assertNotNull("Should have a remote controller", server.getRemoteClient());
+        }
+    }
+
+    public RemoteLocator getLocator() {
+        return (RemoteLocator) locator;
+    }
+
+    protected Locator createLocator() throws Exception {
+        RemoteLocator locator = new RemoteLocator("localServer");
+        Server localServer = locator.getLocalServer();
+        localServer.setAddresses("ejb", new String[] { "http://localhost" });
+
+        RemoteLocator remoteLocator = new RemoteLocator("remoteServer");
+        Server remoteServer = remoteLocator.getLocalServer();
+        remoteServer.setAddresses("ejb", new String[] { "http://somehost" });
+
+        // lets wire the servers together
+        locator.onServerCreate(remoteServer, remoteLocator);
+        remoteLocator.onServerCreate(localServer, locator);
+
+        locator.setSessionListener(remoteLocator);
+        remoteLocator.setSessionListener(locator);
+
+        // add some sessions
+        locator.createSession("local").release();
+        remoteLocator.createSession("remote").release();
+
+        return locator;
+    }
+}



Mime
View raw message