KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > persistence > DelegatingPersistenceManager


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.persistence;
23
24 import javax.management.Attribute JavaDoc;
25 import javax.management.AttributeList JavaDoc;
26 import javax.management.Descriptor JavaDoc;
27 import javax.management.InstanceNotFoundException JavaDoc;
28 import javax.management.MBeanAttributeInfo JavaDoc;
29 import javax.management.MBeanException JavaDoc;
30 import javax.management.MBeanInfo JavaDoc;
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.MalformedObjectNameException JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.management.ReflectionException JavaDoc;
35 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
36 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
37
38 import org.jboss.logging.Logger;
39 import org.jboss.mx.modelmbean.ModelMBeanConstants;
40 import org.jboss.mx.modelmbean.ModelMBeanInvoker;
41
42 /**
43  * DelegatingPersistenceManager.
44  *
45  * An XMBean Persistence Manager that delegates to an external
46  * MBean-controlled implementation the actual persistence of
47  * MBean attributes.
48  *
49  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
50  * @version $Revision: 37459 $
51  */

52 public class DelegatingPersistenceManager
53    implements PersistenceManager
54 {
55    // Private Data --------------------------------------------------
56

57    private static Logger log = Logger.getLogger(DelegatingPersistenceManager.class);
58    
59    /** where calls are delegated */
60    private AttributePersistenceManager persistor;
61    
62    /** the associated name to use at load/store */
63    private String JavaDoc persistName;
64    
65    /** load operation triggers save, so we want to prevent this */
66    private boolean isLoading;
67
68    // Constructors --------------------------------------------------
69

70    public DelegatingPersistenceManager()
71    {
72       // emtpy
73
}
74    
75    // PersistenceManager overrides ----------------------------------
76

77    /**
78     * Called initialy when the XMBean is constructed in order
79     * to load and set the attributes of the MBean,
80     * if their persistent image exists.
81     */

82    public void load(ModelMBeanInvoker invoker, MBeanInfo JavaDoc metadata)
83       throws MBeanException JavaDoc
84    {
85       if (this.persistor == null) {
86          // lazy initialization on first load - couldn't do
87
// otherwise with this PersistenceManager interface
88
init(invoker, metadata);
89       }
90
91       if (log.isDebugEnabled())
92          log.debug("load() called for: '" + this.persistName + "'");
93       
94       AttributeList JavaDoc attrs = null;
95       
96       // load from the persistor
97
try {
98           attrs = this.persistor.load(this.persistName);
99       }
100       catch (Exception JavaDoc e) {
101          // problem while loading
102
log.warn("Caught exception while loading", e);
103          throw new MBeanException JavaDoc(e);
104       }
105
106       if (attrs != null) {
107          // a persistent attribute list image was found so restore it
108

109          try {
110             // need to mark we are loading because setting the attributes
111
// will triger a store() that should be ignored
112
setIsLoading(true);
113            
114             if (log.isDebugEnabled())
115                log.debug("loading attributes: " + attrs);
116             invoker.setAttributes(attrs);
117          }
118          finally {
119             setIsLoading(false);
120          }
121       }
122       else {
123          if (log.isDebugEnabled())
124             log.debug("No attributes to load");
125       }
126    }
127       
128    /**
129     * store() is triggered by the PersistenceInterceptor based
130     * on the persistence policy.
131     *
132     * In the simple case, it will be called for every attribute set.
133     *
134     * store() will save *all* attributes that:
135     * (a) are writable (so we can re-load them later on)
136     * (b) their value exists in the ATTRIBUTE_VALUE descriptor
137     * (c) are not marked as PM_NEVER
138     */

139    public void store(MBeanInfo JavaDoc metadata)
140       throws MBeanException JavaDoc
141    {
142       if (this.persistor == null) {
143          // shouln't happen
144
throw new MBeanException JavaDoc(new Exception JavaDoc("store() called before instance initialized"));
145       }
146       
147       // while loading store() is triggered
148
if (isLoading()) {
149          return; // ignore call
150
}
151       else {
152          if (log.isDebugEnabled())
153             log.debug("store() called for: '" + this.persistName + "'");
154          
155          // placehold for attributes to be persisted
156
AttributeList JavaDoc attributes = new AttributeList JavaDoc();
157
158          // iterate over all attributes in metadata
159
MBeanAttributeInfo JavaDoc[] attrs = metadata.getAttributes();
160          
161          if (log.isDebugEnabled() && attrs.length > 0)
162             log.debug("store() --- ModelMBeanAttributeInfo[] ---");
163          
164          for (int i = 0; i < attrs.length; i++)
165          {
166             /// for each (a) writable attribute (b) in the model cache,
167
// create a new Attribute object and add it to the collection.
168
ModelMBeanAttributeInfo JavaDoc attributeInfo = (ModelMBeanAttributeInfo JavaDoc)attrs[i];
169             
170             if (log.isDebugEnabled())
171                log.debug(" attr (#" + i + ") - " + attributeInfo);
172             
173             if (attributeInfo.isWritable()) {
174                 Descriptor JavaDoc attrDesc = attributeInfo.getDescriptor();
175     
176                 Object JavaDoc name = attrDesc.getFieldValue(ModelMBeanConstants.NAME);
177                 Object JavaDoc value = attrDesc.getFieldValue(ModelMBeanConstants.ATTRIBUTE_VALUE);
178                 Object JavaDoc updated = attrDesc.getFieldValue(ModelMBeanConstants.LAST_UPDATED_TIME_STAMP2);
179                 Object JavaDoc pPolicy = attrDesc.getFieldValue(ModelMBeanConstants.PERSIST_POLICY);
180                 
181                 boolean noPersistPolicy =
182                    pPolicy != null &&
183                    ((String JavaDoc)pPolicy).equalsIgnoreCase(ModelMBeanConstants.PP_NEVER) ? true : false;
184                 
185                 // to persist the attribute:
186
//
187
// (a) must be writable (so we can re-load it later on)
188
// (b) its value must be set in the ATTRIBUTE_VALUE descriptor
189
// (c) must not be marked as PM_NEVER
190
if (updated != null && noPersistPolicy == false) {
191                    attributes.add(new Attribute JavaDoc(name.toString(), value));
192                 }
193             }
194          }
195          try {
196             if (!attributes.isEmpty()) {
197                
198                if (log.isDebugEnabled())
199                   log.debug("calling persistor.store(" + this.persistName + ") attrs=" + attributes);
200                
201                persistor.store(this.persistName, attributes);
202             }
203             else {
204                if (log.isDebugEnabled())
205                   log.debug("nothing to persist");
206             }
207          }
208          catch (Exception JavaDoc e) {
209             log.warn("cought exception during store()", e);
210          }
211       }
212    }
213
214    // Protected -----------------------------------------------------
215

216    /**
217     * Lazy initialization
218     *
219     * Gets the external persistor to use and decides on the
220     * persistName to use for this MBean load()/store() calls.
221     */

222    protected void init(ModelMBeanInvoker invoker, MBeanInfo JavaDoc metadata)
223          throws MBeanException JavaDoc
224    {
225       Descriptor JavaDoc desc = ((ModelMBeanInfo JavaDoc)metadata).getMBeanDescriptor();
226       
227       if (log.isDebugEnabled()) {
228          log.debug("init() --- ModelMBeanInfo Descriptor --- ");
229          log.debug(desc);
230       }
231       
232       // Decide what to use as a persistent name (id) for this MBean
233

234       // If the user has explicitly specified a "persistName", use it
235
String JavaDoc name = (String JavaDoc)desc.getFieldValue(ModelMBeanConstants.PERSIST_NAME);
236       
237       if (name != null) {
238          this.persistName = name;
239       }
240       else {
241          // Try to find ObjectName stored (or lets say hidden) there by ModelMBeanInvoker
242
ObjectName JavaDoc objectName = (ObjectName JavaDoc)desc.getFieldValue(ModelMBeanConstants.OBJECT_NAME);
243          
244          if (objectName != null) {
245             this.persistName = objectName.toString();
246          }
247          else {
248             throw new MBeanException JavaDoc(new Exception JavaDoc("must specify a value for: " + ModelMBeanConstants.PERSIST_NAME));
249          }
250       }
251       
252       if (log.isDebugEnabled())
253          log.debug("chosen persistent id: '" + this.persistName + "'");
254
255       // get the name of the MBean factory service that creates
256
// the AttributePersistenceManager implementation
257
String JavaDoc service = (String JavaDoc)desc.getFieldValue(ModelMBeanConstants.DELEGATING_PM_SERVICE_DESCRIPTOR);
258       if (service == null)
259       {
260          // use default
261
service = ModelMBeanConstants.DELEGATING_PM_SERVICE_DEFAULT_VALUE;
262       }
263
264       // get the name of the operation to call on the MBean service
265
String JavaDoc operation = (String JavaDoc)desc.getFieldValue(ModelMBeanConstants.DELEGATING_PM_OPERATION_DESCRIPTOR);
266       if (operation == null)
267       {
268          // use default
269
operation = ModelMBeanConstants.DELEGATING_PM_OPERATION_DEFAULT_VALUE;
270       }
271
272       // Create the AttributePersistenceManager
273
try {
274          ObjectName JavaDoc objName = new ObjectName JavaDoc(service);
275          MBeanServer JavaDoc server = invoker.getServer();
276          
277          this.persistor = (AttributePersistenceManager)server.invoke(objName,
278                                                                      operation,
279                                                                      new Object JavaDoc[] {},
280                                                                      new String JavaDoc[] {});
281          if (this.persistor == null) {
282             throw new MBeanException JavaDoc(new NullPointerException JavaDoc("null AttributePersistenceManager from: " + service));
283          }
284       }
285       catch (MalformedObjectNameException JavaDoc e) {
286          throw new MBeanException JavaDoc(e, "not a valid ObjectName: " + service);
287       }
288       catch (InstanceNotFoundException JavaDoc e) {
289          throw new MBeanException JavaDoc(e, "service not registered: " + service);
290       }
291       catch (ReflectionException JavaDoc e) {
292          throw new MBeanException JavaDoc(e);
293       }
294       
295       if (log.isDebugEnabled())
296          log.debug("using AttributePersistenceManager: " + this.persistor.getClass().getName());
297    }
298    
299    /**
300     * Check if we are loading state
301     */

302    protected boolean isLoading()
303    {
304       return isLoading;
305    }
306
307    /**
308     * Set the loading status
309     *
310     * @param newIsLoading
311     */

312    protected void setIsLoading(boolean newIsLoading)
313    {
314       isLoading = newIsLoading;
315    }
316
317 }
318
Popular Tags