KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xfire > SessionScopePolicy


1 package jfun.yan.xfire;
2
3 import jfun.yan.factory.Factory;
4 import jfun.yan.factory.Pool;
5 import jfun.yan.factory.PooledFactory;
6 import jfun.yan.factory.SimplePool;
7
8 import org.codehaus.xfire.MessageContext;
9 import org.codehaus.xfire.service.Service;
10 import org.codehaus.xfire.transport.Session;
11
12 /**
13  * This scope policy implements one servant instance per session.
14  * <p>
15  * @author Ben Yu
16  * Feb 6, 2006 11:41:08 AM
17  */

18 public class SessionScopePolicy implements ScopePolicy {
19   /**
20    * Get the key for caching a service.
21    * @param service the service.
22    * @return the key.
23    */

24   protected Object JavaDoc getServiceKey(Service service){
25     return "service." + service.getSimpleName();
26   }
27   public Factory applyScope(Factory f, MessageContext ctxt) {
28     return new PooledFactory(f,
29         getSessionScope(getServiceKey(ctxt.getService()), ctxt.getSession()));
30   }
31   public String JavaDoc toString(){
32     return "session scope";
33   }
34   private static Pool getSessionScope(final Object JavaDoc key, final Session session){
35     return new SimplePool(){
36       public Object JavaDoc get() {
37         return session.get(key);
38       }
39       public void set(Object JavaDoc val) {
40         session.put(key, val);
41       }
42       public String JavaDoc toString(){
43         return "session scope";
44       }
45       /* This is not guaranteed to be safe with concurrent access to HttpSession.
46        * But better than nothing.
47        * @see jfun.yan.SimplePool#getMutex()
48        */

49       protected Object JavaDoc getMutex(){
50         return Service.class;
51       }
52     };
53   }
54   protected SessionScopePolicy(){}
55   private static final SessionScopePolicy singleton = new SessionScopePolicy();
56   /**
57    * To get an instance of session scope policy.
58    */

59   public static ScopePolicy instance(){
60     return singleton;
61   }
62 }
63
Popular Tags