KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > interceptor > PersistenceInterceptor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.interceptor;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Timer JavaDoc;
26 import java.util.TimerTask JavaDoc;
27
28 import javax.management.Descriptor JavaDoc;
29 import javax.management.PersistentMBean JavaDoc;
30 import javax.management.MBeanException JavaDoc;
31 import javax.management.InstanceNotFoundException JavaDoc;
32 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
33
34 import org.jboss.mx.modelmbean.ModelMBeanConstants;
35 import org.jboss.mx.service.ServiceConstants;
36 import org.jboss.mx.server.Invocation;
37 import org.jboss.mx.server.MBeanInvoker;
38
39 /** A peristence interceptor that uses the java.util.Timer framework for the
40  * scheculed peristence policies.
41  *
42  * @see javax.management.PersistentMBean
43  *
44  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
45  * @author Scott.Stark@jboss.org
46  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>.
47  *
48  * @version $Revision: 37459 $
49  */

50 public class PersistenceInterceptor
51    extends AbstractInterceptor
52    implements ModelMBeanConstants, ServiceConstants
53 {
54    /** The HashMap<name, policy> of attribute level policies */
55    private HashMap JavaDoc attrPersistencePolicies = new HashMap JavaDoc();
56    /** The HashMap<name, PersistenceTimerTask> of scheduled peristence */
57    private HashMap JavaDoc timerTaskMap = new HashMap JavaDoc();
58    /** The bean level peristence policy */
59    private String JavaDoc mbeanPersistencePolicy;
60    /** The PersistentMBean load/store callback interface */
61    private PersistentMBean JavaDoc callback;
62
63    public PersistenceInterceptor()
64    {
65       super("Default Persistence Interceptor");
66    }
67
68    // Public --------------------------------------------------------
69
public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
70    {
71       if( callback == null )
72       {
73          lazyInit(invocation);
74       }
75
76       Object JavaDoc returnValue = invocation.nextInterceptor().invoke(invocation);
77       String JavaDoc type = invocation.getType();
78       if (type != Invocation.OP_SETATTRIBUTE )
79          return returnValue;
80
81       String JavaDoc attrName = invocation.getName();
82       String JavaDoc policy = (String JavaDoc)attrPersistencePolicies.get(attrName);
83       if (policy == null)
84          policy = mbeanPersistencePolicy;
85
86       if (policy.equalsIgnoreCase(PP_ON_UPDATE) == true)
87       {
88          MBeanInvoker invoker = invocation.getInvoker();
89          Descriptor JavaDoc attrDesc = invocation.getDescriptor();
90          invoker.updateAttributeInfo(attrDesc);
91          callback.store();
92       }
93       else if(policy.equalsIgnoreCase(PP_NO_MORE_OFTEN_THAN) == true)
94       {
95          PersistenceTimerTask task = (PersistenceTimerTask) timerTaskMap.get(attrName);
96          if( task != null )
97             task.setHasUpdated(true);
98       }
99       return returnValue;
100    }
101
102    /**
103     *
104     * @param invocation
105     */

106    private synchronized void lazyInit(Invocation invocation) throws MBeanException JavaDoc
107    {
108       // This requires the invoker to implement PersistentMBean
109
MBeanInvoker invoker = invocation.getInvoker();
110       callback = (PersistentMBean JavaDoc) invocation.getInvoker();
111       ModelMBeanInfo JavaDoc info = (ModelMBeanInfo JavaDoc) invoker.getMetaData();
112       Descriptor JavaDoc mbeanDesc = info.getMBeanDescriptor();
113
114       String JavaDoc policy = (String JavaDoc) mbeanDesc.getFieldValue(PERSIST_POLICY);
115       String JavaDoc persistPeriod = (String JavaDoc)mbeanDesc.getFieldValue(PERSIST_PERIOD);
116
117       mbeanPersistencePolicy = PP_NEVER;
118       if (policy != null)
119       {
120          mbeanPersistencePolicy = policy;
121          if (mbeanPersistencePolicy.equalsIgnoreCase(PP_ON_TIMER) ||
122              mbeanPersistencePolicy.equalsIgnoreCase(PP_NO_MORE_OFTEN_THAN))
123          {
124             boolean isNoMoreOftenThan = mbeanPersistencePolicy.equalsIgnoreCase(PP_NO_MORE_OFTEN_THAN);
125             schedulePersistenceNotifications(Long.parseLong(persistPeriod), MBEAN_DESCRIPTOR, isNoMoreOftenThan);
126          }
127       }
128       
129       Descriptor JavaDoc[] attrDescs = info.getDescriptors(ATTRIBUTE_DESCRIPTOR);
130       for (int i = 0; i < attrDescs.length; ++i)
131       {
132          policy = (String JavaDoc) attrDescs[i].getFieldValue(PERSIST_POLICY);
133          persistPeriod = (String JavaDoc)attrDescs[i].getFieldValue(PERSIST_PERIOD);
134
135          if (policy != null)
136          {
137             String JavaDoc name = (String JavaDoc) attrDescs[i].getFieldValue(NAME);
138             attrPersistencePolicies.put(name, policy);
139
140             if(policy.equalsIgnoreCase(PP_ON_TIMER) ||
141                policy.equalsIgnoreCase(PP_NO_MORE_OFTEN_THAN))
142             {
143                boolean isNoMoreOftenThan = policy.equalsIgnoreCase(PP_NO_MORE_OFTEN_THAN);
144                schedulePersistenceNotifications(Long.parseLong(persistPeriod), name, isNoMoreOftenThan);
145             }
146          }
147       }
148    }
149
150    private void schedulePersistenceNotifications(long persistPeriod, String JavaDoc name,
151       boolean isNoMoreOftenThan)
152    {
153       // @todo: unschedule on unregistration/descriptor field change
154
PersistenceTimerTask task = new PersistenceTimerTask(name, isNoMoreOftenThan);
155       Timer JavaDoc timer = new Timer JavaDoc(true);
156       timer.scheduleAtFixedRate(task, 0, persistPeriod);
157       timerTaskMap.put(name, task);
158    }
159
160    // Inner classes -------------------------------------------------
161
private class PersistenceTimerTask extends TimerTask JavaDoc
162    {
163       boolean noMoreOftenThan;
164       boolean hasUpdated;
165       String JavaDoc name;
166       PersistenceTimerTask(String JavaDoc name, boolean noMoreOftenThan)
167       {
168          this.name = name;
169          this.noMoreOftenThan = noMoreOftenThan;
170       }
171       synchronized void setHasUpdated(boolean flag)
172       {
173          hasUpdated = flag;
174       }
175       public void run()
176       {
177          try
178          {
179             // @todo: add PersistenceContext field to MBean's descriptor to
180
// relay attribute name (and possibly other) info with the
181
// persistence callback
182
boolean doStore = (noMoreOftenThan == true && hasUpdated == true)
183                || noMoreOftenThan == false;
184             if( doStore == true )
185             {
186                callback.store();
187                setHasUpdated(false);
188             }
189          }
190          catch (MBeanException JavaDoc e)
191          {
192             // FIXME: log exception
193
}
194          catch (InstanceNotFoundException JavaDoc e)
195          {
196             // FIXME: log exception
197
}
198       }
199    }
200 }
201
Popular Tags