KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.Attribute JavaDoc;
25 import javax.management.Descriptor JavaDoc;
26 import javax.management.InvalidAttributeValueException JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28
29 import org.jboss.logging.Logger;
30 import org.jboss.mx.modelmbean.ModelMBeanConstants;
31 import org.jboss.mx.modelmbean.ModelMBeanInvoker;
32 import org.jboss.mx.server.Invocation;
33 import org.jboss.util.UnreachableStatementException;
34
35
36 /** This interceptor handles the ModelMBean attribute caching and setter
37  * and getter dispatches.
38  *
39  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
40  * @author Scott.Stark@jboss.org
41  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>.
42  * @version $Revision: 37459 $
43  */

44 public class ModelMBeanAttributeInterceptor
45       extends AbstractInterceptor
46       implements ModelMBeanConstants
47 {
48    // Constants -----------------------------------------------------
49

50    private static final Logger log = Logger.getLogger(ModelMBeanAttributeInterceptor.class);
51    
52    // Attributes ----------------------------------------------------
53

54    private boolean trace = log.isTraceEnabled();
55    
56    // Constructors --------------------------------------------------
57

58    public ModelMBeanAttributeInterceptor()
59    {
60       super("ModelMBean Attribute Interceptor");
61    }
62
63    
64    // Public --------------------------------------------------------
65

66    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
67    {
68       // get the attribute's descriptor
69
Descriptor JavaDoc d = invocation.getDescriptor();
70       Class JavaDoc clazz = invocation.getAttributeTypeClass();
71       
72       String JavaDoc name = null;
73       ObjectName JavaDoc objectName = null;
74       if (trace)
75       {
76          name = (String JavaDoc) d.getFieldValue(NAME);
77          objectName = invocation.getInvoker().getObjectName();
78       }
79       
80       // check the invocation access point: setAttribute()
81
if (invocation.getType().equals(Invocation.OP_SETATTRIBUTE))
82       {
83          // setAttribute always contains one arg
84
Object JavaDoc value = invocation.getArgs() [0];
85          if (trace)
86             log.trace("Setting objectName=" + objectName + " attr=" + name + " value=" + value);
87          
88          checkAssignable("Set attribute ", clazz, value);
89          
90          // remember the old value of this attribute for AVC notification
91
Object JavaDoc oldValue = d.getFieldValue(ATTRIBUTE_VALUE);
92          if (trace)
93             log.trace("Setting objectName=" + objectName + " attr=" + name + " oldValue=" + value);
94
95          // check if the attribute maps to a setter
96
String JavaDoc setMethod = (String JavaDoc) d.getFieldValue(SET_METHOD);
97          if (trace)
98             log.trace("Setting objectName=" + objectName + " attr=" + name + " setMethod=" + setMethod);
99
100          if (setMethod != null)
101          {
102             // if setter was found, invoke the corresponding setter operation
103
invocation.invoke();
104          }
105
106          // Don't cache the value or last update when not caching
107
String JavaDoc timeLimit = (String JavaDoc) d.getFieldValue(CURRENCY_TIME_LIMIT);
108          long limit = (timeLimit == null) ? CACHE_NEVER_LIMIT : Long.parseLong(timeLimit);
109          String JavaDoc timestamp = Long.toString(System.currentTimeMillis()/1000);
110          
111          if (limit != CACHE_NEVER_LIMIT)
112          {
113             if (trace)
114                log.trace("Setting objectName=" + objectName + " attr=" + name + " value=" + value + " timestamp=" + timestamp);
115             d.setField(CACHED_VALUE, value);
116             d.setField(LAST_UPDATED_TIME_STAMP, timestamp);
117          }
118          
119          // Always store the attribute value set, for persistence and AVC purposes mainly
120
// independent of whether caching is enabled or not.
121
// Note that if the resource updates its internal attribute value
122
// we will not know, unless set is performed through the ModelMBean
123
// interface, or the following descriptor gets updated somehow.
124
d.setField(ATTRIBUTE_VALUE, value);
125          d.setField(LAST_UPDATED_TIME_STAMP2, timestamp);
126          
127          // send AVC notification
128
ModelMBeanInvoker invoker = (ModelMBeanInvoker) invocation.getInvoker();
129          invoker.sendAttributeChangeNotification(
130             new Attribute JavaDoc(invocation.getName(), oldValue),
131             new Attribute JavaDoc(invocation.getName(), value)
132          );
133          return null;
134       }
135       else if (invocation.getType().equals(Invocation.OP_GETATTRIBUTE))
136       {
137          if (trace)
138             log.trace("Getting objectName=" + objectName + " attr=" + name);
139
140          String JavaDoc timeLimit = (String JavaDoc)d.getFieldValue(CURRENCY_TIME_LIMIT);
141          long limit = (timeLimit == null) ? CACHE_NEVER_LIMIT : Long.parseLong(timeLimit);
142
143          // We are never stale
144
if (limit == CACHE_ALWAYS_LIMIT)
145          {
146             String JavaDoc timeStamp = (String JavaDoc)d.getFieldValue(LAST_UPDATED_TIME_STAMP);
147             if (timeStamp != null)
148             {
149                Object JavaDoc value = d.getFieldValue(CACHED_VALUE);
150                if (trace)
151                   log.trace("Always cache objectName=" + objectName + " attr=" + name + " value=" + value);
152                checkAssignable("Cached value in descriptor ", clazz, value);
153                return value;
154             }
155          }
156
157          // is caching enabled
158
if (limit != CACHE_NEVER_LIMIT)
159          {
160             String JavaDoc timeStamp = (String JavaDoc)d.getFieldValue(LAST_UPDATED_TIME_STAMP);
161             long lastUpdate = (timeStamp == null) ? 0 : Long.parseLong(timeStamp);
162         
163             // if the value hasn't gone stale, return from the descriptor
164
long now = System.currentTimeMillis();
165             long expires = lastUpdate * 1000 + limit * 1000;
166             if (now < expires)
167             {
168                Object JavaDoc value = d.getFieldValue(CACHED_VALUE);
169                if (trace)
170                   log.trace("Using cache objectName=" + objectName + " attr=" + name + " value=" + value + " now=" + now + " expires=" + expires);
171                checkAssignable("Cached value in descriptor ", clazz, value);
172                return value;
173             }
174             else
175             {
176                if (trace)
177                   log.trace("Cache expired objectName=" + objectName + " attr=" + name + " now=" + now + " expires=" + expires);
178                d.removeField(CACHED_VALUE);
179             }
180          }
181          else
182          {
183             // Unfortunatley we have to cope with stupid users
184
if (trace)
185                log.trace("Removing any cached value objectName=" + objectName + " attr=" + name + " descriptor=" + d);
186             d.removeField(CACHED_VALUE);
187          }
188          
189          // get the attribute's descriptor
190
String JavaDoc getMethod = (String JavaDoc)d.getFieldValue(GET_METHOD);
191          if (trace)
192             log.trace("Get attribute objectName=" + objectName + " attr=" + name + " getMethod=" + getMethod);
193          
194          if (getMethod != null)
195          {
196             // we got here means either stale value in descriptior, or no caching
197
Object JavaDoc value = invocation.invoke();
198             if (trace)
199                log.trace("Got attribute objectName=" + objectName + " attr=" + name + " value=" + value);
200             
201             // update the descriptor (unless not caching)
202
if (limit != CACHE_NEVER_LIMIT)
203             {
204                String JavaDoc timestamp = Long.toString(System.currentTimeMillis()/1000);
205                if (trace)
206                   log.trace("Cache attribute objectName=" + objectName + " attr=" + name + " value=" + value + " timestamp=" + timestamp);
207                d.setField(CACHED_VALUE, value);
208                d.setField(LAST_UPDATED_TIME_STAMP, timestamp);
209             }
210             return value;
211          }
212          else
213          {
214             // There is no instance accessor so check for a default value
215
Object JavaDoc value = d.getFieldValue(DEFAULT);
216             if (trace)
217                log.trace("Get attribute use default objectName=" + objectName + " attr=" + name + " default=" + value);
218             checkAssignable("Default value ", clazz, value);
219             return value;
220          }
221       }
222       else
223          throw new UnreachableStatementException(invocation.getType());
224    }
225    
226    protected void checkAssignable(String JavaDoc context, Class JavaDoc clazz, Object JavaDoc value) throws InvalidAttributeValueException JavaDoc, ClassNotFoundException JavaDoc
227    {
228       if (value != null && clazz.isAssignableFrom(value.getClass()) == false)
229          throw new InvalidAttributeValueException JavaDoc(context + " has class " + value.getClass() + " loaded from " + value.getClass().getClassLoader() +
230             " that is not assignable to attribute class " + clazz + " loaded from " + clazz.getClassLoader());
231    }
232 }
233
234
235
236
237
Popular Tags