KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > j2ee > session > PublishingInterceptor


1 // ========================================================================
2
// $Id: PublishingInterceptor.java,v 1.5 2004/06/22 16:23:44 jules_gosnell Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.j2ee.session;
17
18 //----------------------------------------
19
import java.lang.reflect.InvocationHandler JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.lang.reflect.Proxy JavaDoc;
22 import java.rmi.RemoteException JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.jfox.ioc.logger.Logger;
26 //----------------------------------------
27

28 // at a later date, this needs to be able to batch up changes and
29
// flush to JG on various events e.g. immediately (no batching),
30
// end-of-request, idle, stop (migration)...
31

32 public class PublishingInterceptor
33   extends StateInterceptor
34 {
35   protected static final Logger _log=Logger.getLogger(PublishingInterceptor.class);
36
37   protected AbstractReplicatedStore
38     getStore()
39   {
40     AbstractReplicatedStore store=null;
41     try
42     {
43       store=(AbstractReplicatedStore)getManager().getStore();
44     }
45     catch (Exception JavaDoc e)
46     {
47       _log.error("could not get AbstractReplicatedStore");
48     }
49
50     return store;
51   }
52
53   // by using a Proxy, we can avoid having to allocate/deallocate lots
54
// of Class[]/Object[]s... Later it could be intelligent and not
55
// pass the methodName and Class[] across the wire...
56
public class PublishingInvocationHandler
57     implements InvocationHandler JavaDoc
58   {
59     public Object JavaDoc
60       invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
61       throws RemoteException JavaDoc
62     {
63       if (!AbstractReplicatedStore.getReplicating())
64       {
65         AbstractReplicatedStore store = getStore();
66         if (store != null)
67           store.publish(getId(), method, args);
68       }
69
70       return null;
71     }
72   }
73
74   public void
75     start()
76   {
77     _publisher=createProxy();
78   }
79
80   public void
81     stop()
82   {
83     _publisher=null;
84   }
85   
86   public State
87     createProxy()
88   {
89     AbstractReplicatedStore store=getStore();
90     if (store==null)
91       return null;
92     InvocationHandler JavaDoc handler = new PublishingInvocationHandler();
93     return (State) Proxy.newProxyInstance(store.getLoader(), new Class JavaDoc[] { State.class }, handler);
94   }
95
96   protected State _publisher;
97
98   //----------------------------------------
99
// writers - wrap-publish-n-delegate - these should be moved into a
100
// ReplicatingInterceptor...
101

102   public void
103     setLastAccessedTime(long time)
104     throws RemoteException JavaDoc
105   {
106     if (_publisher!=null) _publisher.setLastAccessedTime(time);
107     super.setLastAccessedTime(time);
108   }
109
110   public void
111     setMaxInactiveInterval(int interval)
112     throws RemoteException JavaDoc
113   {
114     if (_publisher!=null) _publisher.setMaxInactiveInterval(interval);
115     super.setMaxInactiveInterval(interval);
116   }
117
118   public Object JavaDoc
119     setAttribute(String JavaDoc name, Object JavaDoc value, boolean returnValue)
120     throws RemoteException JavaDoc
121   {
122     if (_publisher!=null) _publisher.setAttribute(name, value, returnValue);
123     return super.setAttribute(name, value, returnValue);
124   }
125
126   public void
127     setAttributes(Map JavaDoc attributes)
128     throws RemoteException JavaDoc
129   {
130     if (_publisher!=null) _publisher.setAttributes(attributes);
131     super.setAttributes(attributes);
132   }
133
134   public Object JavaDoc
135     removeAttribute(String JavaDoc name, boolean returnValue)
136     throws RemoteException JavaDoc
137   {
138     if (_publisher!=null) _publisher.removeAttribute(name, returnValue);
139     return super.removeAttribute(name, returnValue);
140   }
141 }
142
Popular Tags