geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r369309 - in /geronimo/trunk/modules/session/src: java/org/apache/geronimo/session/ java/org/apache/geronimo/session/local/ test/org/apache/geronimo/session/
Date Mon, 16 Jan 2006 02:09:46 GMT
Author: jstrachan
Date: Sun Jan 15 18:09:43 2006
New Revision: 369309

URL: http://svn.apache.org/viewcvs?rev=369309&view=rev
Log:
a simple local implementation of the Session to allow single-JVM session management which
can offer optional persistence but no remote sessions

Added:
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalLocator.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalServerImpl.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalSessionLocation.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionHandle.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionImpl.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionStateListener.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionStateTracker.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/package.html
    geronimo/trunk/modules/session/src/test/org/apache/geronimo/session/SessionTest.java
Modified:
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Locator.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Server.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Session.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/SessionAlreadyExistsException.java
    geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/SessionLocation.java

Modified: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Locator.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Locator.java?rev=369309&r1=369308&r2=369309&view=diff
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Locator.java (original)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Locator.java Sun Jan
15 18:09:43 2006
@@ -16,8 +16,8 @@
 package org.apache.geronimo.session;
 
 /**
- * This is the main API to locating where sessions are located, to create
- * new sessions and to view the available servers. There is a single Locator per
+ * This is the main API to locating where sessions are located, to create new
+ * sessions and to view the available servers. There is a single Locator per
  * local {@link Server}.
  * 
  * @version $Revision: $
@@ -30,8 +30,6 @@
     public SessionLocation getSessionLocation(String clientID);
 
     Session createSession(String sessionId) throws SessionAlreadyExistsException;
-
-    public Server[] getRemoteServers();
 
     public Server getLocalServer();
 }

Modified: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Server.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Server.java?rev=369309&r1=369308&r2=369309&view=diff
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Server.java (original)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Server.java Sun Jan
15 18:09:43 2006
@@ -29,13 +29,14 @@
     String getName();
 
     /**
-     * Returns the addresses on which you can communicate with the server which is
-     * required for redirecting or proxying requests for remote sessions
+     * Returns the addresses on which you can communicate with the server which
+     * is required for redirecting or proxying requests for remote sessions
      */
     String[] getAddresses(String protocol);
 
     /**
-     * Configures the available addresses that can be used to connect to this server
+     * Configures the available addresses that can be used to connect to this
+     * server
      */
     void setAddresses(String string, String[] strings);
 
@@ -43,5 +44,4 @@
      * Is this the local in-JVM server or a remote server
      */
     public boolean isLocalServer();
-
 }

Modified: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Session.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Session.java?rev=369309&r1=369308&r2=369309&view=diff
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Session.java (original)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/Session.java Sun Jan
15 18:09:43 2006
@@ -16,7 +16,8 @@
 package org.apache.geronimo.session;
 
 /**
- * The state for a stateful session client which always exists in the local JVM; it is never
remote.
+ * The state for a stateful session client which always exists in the local JVM;
+ * it is never remote.
  * 
  * There is only one instance of this object in the system for a given session
  * ID; even when using buddy groups to replicate session state there will only

Modified: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/SessionAlreadyExistsException.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/SessionAlreadyExistsException.java?rev=369309&r1=369308&r2=369309&view=diff
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/SessionAlreadyExistsException.java
(original)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/SessionAlreadyExistsException.java
Sun Jan 15 18:09:43 2006
@@ -16,8 +16,8 @@
 package org.apache.geronimo.session;
 
 /**
- * An attempt was made to create a new session which was already created by another
- * process or thread.
+ * An attempt was made to create a new session which was already created by
+ * another process or thread.
  * 
  * @version $Revision: $
  */

Modified: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/SessionLocation.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/SessionLocation.java?rev=369309&r1=369308&r2=369309&view=diff
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/SessionLocation.java
(original)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/SessionLocation.java
Sun Jan 15 18:09:43 2006
@@ -23,8 +23,8 @@
 public interface SessionLocation {
 
     /**
-     * Is the session local to this JVM so that it can be used directly or
-     * must the user redirect, proxy or move.
+     * Is the session local to this JVM so that it can be used directly or must
+     * the user redirect, proxy or move.
      */
     boolean isLocal();
 

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalLocator.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalLocator.java?rev=369309&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalLocator.java
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalLocator.java
Sun Jan 15 18:09:43 2006
@@ -0,0 +1,99 @@
+/*
+ * 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.local;
+
+import org.apache.geronimo.session.Locator;
+import org.apache.geronimo.session.Server;
+import org.apache.geronimo.session.Session;
+import org.apache.geronimo.session.SessionAlreadyExistsException;
+import org.apache.geronimo.session.SessionLocation;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A {@link Locator} which can only be run on a single server and only supports
+ * local sessions.
+ * 
+ * @version $Revision: $
+ */
+public class LocalLocator implements Locator {
+
+    private final Server localServer;
+    private final Map map = new HashMap();
+    private SessionStateListener stateListener;
+
+    public LocalLocator(String localServerName) {
+        localServer = new LocalServerImpl(localServerName);
+    }
+
+    public synchronized SessionLocation getSessionLocation(String sessionId) {
+        return (SessionLocation) map.get(sessionId);
+    }
+
+    public synchronized Session createSession(String sessionId) throws SessionAlreadyExistsException
{
+        if (map.containsKey(sessionId)) {
+            throw new SessionAlreadyExistsException(sessionId);
+        }
+        LocalSessionLocation location = createSessionLocation(sessionId);
+        if (stateListener != null) {
+            location.setStateListener(stateListener);
+        }
+        addSession(location);
+        return new SessionHandle(location);
+    }
+
+    public Server getLocalServer() {
+        return localServer;
+    }
+
+    public synchronized void addSession(LocalSessionLocation location) {
+        String sessionId = location.getSessionId();
+        map.put(sessionId, location);
+    }
+
+    public synchronized void removeMovedSession(SessionImpl session) {
+        String sessionId = session.getSessionId();
+        map.remove(sessionId);
+    }
+
+    // Properties
+    // -------------------------------------------------------------------------
+    public String getLocalServerName() {
+        return getLocalServer().getName();
+    }
+
+    public SessionStateListener getStateListener() {
+        return stateListener;
+    }
+
+    public void setStateListener(SessionStateListener stateListener) {
+        this.stateListener = stateListener;
+    }
+
+    // Implementation methods
+    // -------------------------------------------------------------------------
+    public synchronized void sessionDestroyed(LocalSessionLocation location) {
+        String sessionId = location.getSessionId();
+        map.remove(sessionId);
+    }
+
+    protected LocalSessionLocation createSessionLocation(String sessionId) {
+        SessionImpl session = new SessionImpl(sessionId);
+        LocalSessionLocation answer = new LocalSessionLocation(this, session);
+        return answer;
+    }
+}

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalServerImpl.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalServerImpl.java?rev=369309&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalServerImpl.java
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalServerImpl.java
Sun Jan 15 18:09:43 2006
@@ -0,0 +1,52 @@
+/*
+ * 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.local;
+
+import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.geronimo.session.Server;
+
+import java.util.Map;
+
+/**
+ * The server instance which is actually in this local JVM
+ * 
+ * @version $Revision: $
+ */
+public class LocalServerImpl implements Server {
+    private final String name;
+    private final Map addressMap = new ConcurrentHashMap();
+
+    public LocalServerImpl(String name) {
+        this.name = name;
+    }
+
+    public boolean isLocalServer() {
+        return true;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public String[] getAddresses(String protocol) {
+        return (String[]) addressMap.get(protocol);
+    }
+
+    public void setAddresses(String protocol, String[] addresses) {
+        addressMap.put(protocol, addresses);
+    }
+}

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalSessionLocation.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalSessionLocation.java?rev=369309&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalSessionLocation.java
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/LocalSessionLocation.java
Sun Jan 15 18:09:43 2006
@@ -0,0 +1,149 @@
+/*
+ * 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.local;
+
+import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
+import edu.emory.mathcs.backport.java.util.concurrent.locks.ReadWriteLock;
+import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantReadWriteLock;
+
+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;
+
+/**
+ * An implementation of {@link SessionLocation} which supports only local
+ * sessions.
+ * 
+ * @version $Revision: $
+ */
+public class LocalSessionLocation implements SessionLocation {
+
+    private LocalLocator locator;
+    protected SessionImpl session;
+    protected final ReadWriteLock lock = new ReentrantReadWriteLock();
+    private SessionStateTracker stateTracker;
+
+    /**
+     * Creates a local session location
+     */
+    public LocalSessionLocation(LocalLocator locator, SessionImpl session) {
+        this.locator = locator;
+        this.session = session;
+    }
+
+    public boolean isLocal() {
+        return true;
+    }
+
+    public String getSessionId() {
+        return session.getSessionId();
+    }
+
+    public Server getServer() {
+        return locator.getLocalServer();
+    }
+
+    public boolean isMovable() {
+        return false;
+    }
+
+    public Session getSession() throws SessionNotLocalException {
+        Lock readLock = lock.readLock();
+        readLock.lock();
+        try {
+            if (session == null) {
+                throw new SessionNotLocalException(getSessionId());
+            }
+            return new SessionHandle(this);
+        }
+        finally {
+            readLock.unlock();
+        }
+    }
+
+    public SessionImpl getLocalSession() {
+        return session;
+    }
+
+    public ReadWriteLock getLock() {
+        return lock;
+    }
+
+    public Session moveLocally() throws NoSuchSessionException, WriteLockTimedOutException,
SessionNotLocalException,
+            SessionNotMovableException {
+        throw new SessionNotMovableException(getSessionId());
+    }
+
+    public void destroy() throws SessionNotLocalException {
+        Lock writeLock = lock.writeLock();
+        writeLock.lock();
+        try {
+            if (session == null) {
+                throw new SessionNotLocalException(getSessionId());
+            }
+            locator.sessionDestroyed(this);
+            session.setDestroyed(true);
+        }
+        finally {
+            writeLock.unlock();
+        }
+    }
+
+    public SessionImpl getSessionAndHoldReadLock() {
+        lock.readLock().lock();
+        return session;
+    }
+
+    public void release() {
+        lock.readLock().unlock();
+        if (stateTracker != null) {
+            Lock writeLock = lock.writeLock();
+            if (writeLock.tryLock()) {
+                try {
+                    stateTracker.checkpoint();
+                }
+                finally {
+                    writeLock.unlock();
+                }
+            }
+        }
+    }
+
+    protected SessionStateTracker getStateTracker() {
+        return stateTracker;
+    }
+
+    protected void setStateTracker(SessionStateTracker stateTracker) {
+        this.stateTracker = stateTracker;
+        if (session != null) {
+            session.setStateTracker(stateTracker);
+        }
+    }
+
+    public void setStateListener(SessionStateListener stateListener) {
+        SessionStateTracker stateTracker = new SessionStateTracker(this, session, stateListener);
+        setStateTracker(stateTracker);
+    }
+
+    public LocalLocator getLocator() {
+        return locator;
+    }
+
+}

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionHandle.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionHandle.java?rev=369309&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionHandle.java
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionHandle.java
Sun Jan 15 18:09:43 2006
@@ -0,0 +1,70 @@
+/*
+ * 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.local;
+
+import org.apache.geronimo.session.Session;
+
+/**
+ * Represents a single threads access to a session. NOTE this class is not
+ * thread safe.
+ * 
+ * @version $Revision: $
+ */
+public class SessionHandle implements Session {
+
+    private SessionImpl session;
+    private boolean released;
+    private LocalSessionLocation locator;
+
+    public SessionHandle(LocalSessionLocation locator) {
+        this.locator = locator;
+        this.session = locator.getSessionAndHoldReadLock();
+    }
+
+    public void addState(String key, Object value) {
+        checkNotReleased();
+        session.addState(key, value);
+    }
+
+    public String getSessionId() {
+        return session.getSessionId();
+    }
+
+    public Object getState(String key) {
+        checkNotReleased();
+        return session.getState(key);
+    }
+
+    public Object removeState(String key) {
+        checkNotReleased();
+        return session.removeState(key);
+    }
+
+    public void release() {
+        if (!released) {
+            session = null;
+            released = true;
+            locator.release();
+        }
+    }
+
+    protected void checkNotReleased() {
+        if (released) {
+            throw new IllegalArgumentException("Cannot use this session handle as it has
already been released");
+        }
+    }
+
+}

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionImpl.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionImpl.java?rev=369309&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionImpl.java
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionImpl.java
Sun Jan 15 18:09:43 2006
@@ -0,0 +1,124 @@
+/*
+ * 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.local;
+
+import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.geronimo.session.Session;
+import org.apache.geronimo.session.SessionDestroyedException;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * 
+ * @version $Revision: $
+ */
+public class SessionImpl implements Session {
+
+    private final String sessionId;
+    private final Map map;
+    private boolean destroyed;
+    private SessionStateTracker stateTracker;
+
+    public SessionImpl(String sessionId) {
+        this.sessionId = sessionId;
+        this.map = new ConcurrentHashMap();
+    }
+
+    /**
+     * Must be a concurrent map
+     */
+    public SessionImpl(String sessionId, Map map) {
+        this.sessionId = sessionId;
+        this.map = map;
+    }
+
+    public String getSessionId() {
+        return sessionId;
+    }
+
+    public void release() {
+        checkDestroyed();
+    }
+
+    public void addState(String key, Object value) {
+        checkDestroyed();
+        map.put(key, value);
+        if (stateTracker != null) {
+            stateTracker.onDeltaChange(key);
+        }
+    }
+
+    public Object getState(String key) {
+        checkDestroyed();
+        Object answer = map.get(key);
+        if (stateTracker != null) {
+            stateTracker.onDeltaChange(key);
+        }
+        return answer;
+    }
+
+    public Object removeState(String key) {
+        checkDestroyed();
+        Object answer = map.remove(key);
+        if (answer != null) {
+            if (stateTracker != null) {
+                stateTracker.onDeltaChange(key);
+            }
+        }
+        return answer;
+    }
+
+    protected void checkDestroyed() throws SessionDestroyedException {
+        if (destroyed) {
+            throw new SessionDestroyedException(sessionId);
+        }
+    }
+
+    public Map getStateMap() {
+        return map;
+    }
+
+    public boolean isDestroyed() {
+        return destroyed;
+    }
+
+    public void setDestroyed(boolean destroyed) {
+        this.destroyed = destroyed;
+    }
+
+    public Map createDeltas(Set deltaKeys) {
+        Map deltas = new HashMap();
+        for (Iterator iter = deltaKeys.iterator(); iter.hasNext();) {
+            String key = (String) iter.next();
+            Object value = map.get(key);
+            deltas.put(key, value);
+        }
+        return deltas;
+    }
+
+    protected SessionStateTracker getStateTracker() {
+        return stateTracker;
+    }
+
+    protected void setStateTracker(SessionStateTracker stateTracker) {
+        this.stateTracker = stateTracker;
+    }
+
+}

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionStateListener.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionStateListener.java?rev=369309&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionStateListener.java
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionStateListener.java
Sun Jan 15 18:09:43 2006
@@ -0,0 +1,46 @@
+/*
+ * 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.local;
+
+import java.util.Map;
+
+/**
+ * A listener to be notified of changes in a session state so that it can be
+ * stored in some backup such as a file, a database or a buddy group.
+ * 
+ * @version $Revision: $
+ */
+public interface SessionStateListener {
+
+    /**
+     * Notifies a complete change of the state; typically when a session moves
+     */
+    void onCompleteChange(String sessionId, Map state);
+
+    /**
+     * Notifies a set of deltas have changed, only a fraction of the complete
+     * session state
+     */
+    void onDeltaChange(String sessionId, Map deltas);
+
+    /**
+     * The session has been destroyed so remove all state for this session Id
+     * 
+     * @param sessionId
+     */
+    void onDestroy(String sessionId);
+
+}

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionStateTracker.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionStateTracker.java?rev=369309&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionStateTracker.java
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/SessionStateTracker.java
Sun Jan 15 18:09:43 2006
@@ -0,0 +1,96 @@
+/*
+ * 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.local;
+
+import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArraySet;
+
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * A strategy to decide how to aggregate changes to the sessions together into
+ * complete units of work so that when checkpointed, the changes are replicated
+ * to some backup.
+ * 
+ * This class by default uses deltas where possible, though it would be possible
+ * to change this class to never use deltas if required; or to suspend
+ * checkpoints until later on etc.
+ * 
+ * @version $Revision: $
+ */
+public class SessionStateTracker {
+
+    private final LocalSessionLocation location;
+    private final SessionStateListener stateListener;
+    private final SessionImpl session;
+    private Set deltaKeys = new CopyOnWriteArraySet();
+    private boolean change;
+    private boolean destroy;
+
+    public SessionStateTracker(LocalSessionLocation location, SessionImpl session, SessionStateListener
listener) {
+        this.location = location;
+        this.session = session;
+        this.stateListener = listener;
+    }
+
+    /**
+     * When the session decides to broadcast the entire state; often done after
+     * a move
+     */
+    public void onChange() {
+        change = true;
+        deltaKeys.clear();
+    }
+
+    /**
+     * Notifies only the changed entry in the Session
+     */
+    public void onDeltaChange(String key) {
+        deltaKeys.add(key);
+    }
+
+    /**
+     * Broadcasts that the session has been removed
+     */
+    public void onDestroy() {
+        destroy = true;
+        deltaKeys.clear();
+    }
+
+    /**
+     * Checkpoints any pending state changes
+     */
+    public void checkpoint() {
+        if (stateListener != null) {
+            String sessionId = location.getSessionId();
+            if (change) {
+                Map state = session.getStateMap();
+                stateListener.onCompleteChange(sessionId, state);
+                change = false;
+            }
+            else if (destroy) {
+                stateListener.onDestroy(sessionId);
+                destroy = false;
+            }
+            else {
+                Map deltaState = session.createDeltas(deltaKeys);
+                stateListener.onDeltaChange(sessionId, deltaState);
+                deltaKeys.clear();
+            }
+        }
+    }
+
+}

Added: geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/package.html
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/package.html?rev=369309&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/package.html
(added)
+++ geronimo/trunk/modules/session/src/java/org/apache/geronimo/session/local/package.html
Sun Jan 15 18:09:43 2006
@@ -0,0 +1,12 @@
+<html>
+<head>
+</head>
+<body>
+
+<p>
+A single JVM implementation of the Session API where there are no remote servers or remote
sessions
+but there can be a variety of persistence mechanisms.
+</p>
+
+</body>
+</html>

Added: geronimo/trunk/modules/session/src/test/org/apache/geronimo/session/SessionTest.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/session/src/test/org/apache/geronimo/session/SessionTest.java?rev=369309&view=auto
==============================================================================
--- geronimo/trunk/modules/session/src/test/org/apache/geronimo/session/SessionTest.java (added)
+++ geronimo/trunk/modules/session/src/test/org/apache/geronimo/session/SessionTest.java Sun
Jan 15 18:09:43 2006
@@ -0,0 +1,69 @@
+/*
+ * 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;
+
+import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.geronimo.session.local.LocalLocator;
+
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+/**
+ * 
+ * @version $Revision: $
+ */
+public class SessionTest extends TestCase {
+
+    protected Locator locator;
+
+    public void testLocalAccess() throws Exception {
+        SessionLocation location = locator.getSessionLocation("local");
+
+        // the state should be locally in this test
+        assertEquals("state.isLocal", true, location.isLocal());
+
+        Session session = location.getSession();
+        useSession(session);
+    }
+
+    protected void useSession(Session session) throws SessionNotLocalException {
+
+        // lets make some state
+        session.addState("ejb:123", new ConcurrentHashMap());
+
+        // now lets use it
+        Map state = (Map) session.getState("ejb:123");
+
+        state.put("foo", "123");
+
+        session.release();
+    }
+
+    protected void setUp() throws Exception {
+        this.locator = createLocator();
+    }
+
+    protected Locator createLocator() throws Exception {
+        LocalLocator locator = new LocalLocator("localServer");
+
+        locator.createSession("local").release();
+
+        return locator;
+    }
+
+}



Mime
View raw message