KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import javax.management.Descriptor JavaDoc;
31 import javax.management.InstanceNotFoundException JavaDoc;
32 import javax.management.JMException JavaDoc;
33 import javax.management.MBeanException JavaDoc;
34 import javax.management.MBeanInfo JavaDoc;
35 import javax.management.MBeanServer JavaDoc;
36 import javax.management.MBeanServerFactory JavaDoc;
37 import javax.management.MalformedObjectNameException JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39 import javax.management.modelmbean.InvalidTargetObjectTypeException JavaDoc;
40 import javax.management.modelmbean.ModelMBean JavaDoc;
41 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
42
43 import org.jboss.mx.modelmbean.ModelMBeanConstants;
44 import org.jboss.mx.modelmbean.ModelMBeanInvoker;
45 import org.jboss.mx.modelmbean.RequiredModelMBeanInstantiator;
46 import org.jboss.mx.server.registry.MbeanInfoDb;
47
48 /**
49  * Class responsible for storing and loading the MBean info database.
50  * @author Matt Munz
51  */

52 public class MbeanInfoDbPm
53    extends MBeanInfoOdb
54    implements PersistenceManager
55 {
56
57    protected boolean fLoadedFromFs;
58    protected Vector JavaDoc fNamesToRestore;
59    protected Vector JavaDoc fInfosToRestore;
60    protected MBeanServer JavaDoc fMbeanServer;
61    protected MbeanInfoDb fMbeanInfoDb;
62    protected File JavaDoc fMbiDbRoot;
63
64    public MbeanInfoDbPm()
65    {
66       super();
67    }
68
69    /**
70     * @todo finish implementing
71     */

72    public void load(ModelMBeanInvoker mbean, MBeanInfo JavaDoc info)
73      throws MBeanException JavaDoc
74    {
75
76       logger().debug("Loading the MBI DB");
77       setMbeanInfoDb((MbeanInfoDb) mbean.getResource());
78       if(!loadedFromFs())
79       {
80
81          // get mbi db root from descriptor
82
Descriptor JavaDoc d = ((ModelMBeanInfo JavaDoc) info).getMBeanDescriptor();
83          String JavaDoc dir = (String JavaDoc) d.getFieldValue(
84                          ModelMBeanConstants.PERSIST_LOCATION);
85          if((dir != null) && (!dir.equals("")))
86          {
87
88             setMbiDbRoot(new File JavaDoc(dir));
89          }
90          String JavaDoc message = "Loading the MBI DB from the filesystem. location: ";
91          message += mbiDbRoot();
92          logger().debug(message);
93          File JavaDoc[] mbiFiles = mbiDbRoot().listFiles();
94          if(mbiFiles == null)
95          {
96
97             return;
98          }
99          // loop over each item in the mbeaninfo store
100
for(int index = 0; index < mbiFiles.length; index++)
101          {
102
103             /// for each, load, make a new mbean, and register that mbean
104
File JavaDoc curFile = mbiFiles[index];
105             ObjectName JavaDoc nameToRestore = null;
106             try
107             {
108
109                nameToRestore = objectName(curFile);
110             }
111             catch(MalformedObjectNameException JavaDoc cause)
112             {
113
114                throw new MBeanException JavaDoc(cause,
115                                         "Object Name was stored incorrectly.");
116             }
117             MBeanInfo JavaDoc infoToRestore = null;
118             try
119             {
120
121                infoToRestore = load(curFile);
122             }
123             catch(IOException JavaDoc cause)
124             {
125
126                throw new MBeanException JavaDoc(cause,
127                                         "Couldn't read the MBeanInfo file.");
128             }
129             catch(ClassNotFoundException JavaDoc cause2)
130             {
131
132                String JavaDoc message2 = "Couldn't find the Class specified in the object file.";
133                throw new MBeanException JavaDoc(cause2, message2);
134             }
135             if(infoToRestore == null)
136             {
137
138                throw new MBeanException JavaDoc(new Exception JavaDoc(
139                                            "Null loaded MBean info. Error on load."),
140                                         "Could not load");
141             }
142             namesToRestore().add(nameToRestore);
143             infosToRestore().add(infoToRestore);
144          }
145          setLoadedFromFs(true);
146       }
147       try
148       {
149
150          register();
151       }
152       catch(Exception JavaDoc cause)
153       {
154
155          throw new MBeanException JavaDoc(cause,
156                                   "Error trying to register loaded MBeans");
157       }
158    }
159
160    public void store(MBeanInfo JavaDoc info) throws MBeanException JavaDoc
161    {
162
163       logger().debug("storing MBI DB State");
164       Enumeration JavaDoc queue = mbeanInfoDb().mbiPersistenceQueue();
165       for(; queue.hasMoreElements();)
166       {
167
168          ObjectName JavaDoc curName = (ObjectName JavaDoc) queue.nextElement();
169          logger().debug("queue elem: " + curName);
170          MBeanInfo JavaDoc curInfo = null;
171          try
172          {
173
174             curInfo = getMBeanServer().getMBeanInfo(curName);
175          }
176          catch(InstanceNotFoundException JavaDoc cause)
177          {
178
179             throw new MBeanException JavaDoc(cause);
180          }
181          catch(JMException JavaDoc cause3)
182          {
183
184             throw new MBeanException JavaDoc(cause3);
185          }
186          if(curInfo == null)
187          {
188
189             throw new MBeanException JavaDoc(
190                new Exception JavaDoc("Current MBean Info object is null."),
191                "Could not store null object.");
192          }
193          try
194          {
195
196             store(curName, curInfo);
197          }
198          catch(IOException JavaDoc cause2)
199          {
200
201             throw new MBeanException JavaDoc(cause2);
202          }
203          mbeanInfoDb().removeFromMbiQueue(curName);
204          logger().info("Successfully stored mbi for " + curName);
205       }
206    }
207
208    protected void store(ObjectName JavaDoc name, MBeanInfo JavaDoc info)
209      throws IOException JavaDoc
210    {
211
212       File JavaDoc location = new File JavaDoc(mbiDbRoot(), fileName(name));
213       logger().debug("Storing mbi at: " + location);
214       mbiDbRoot().mkdirs();
215       store(info, location);
216    }
217
218    /**
219     * Iterates over the loaded MBean infos and ObjectNames, creates an MBean for each, and
220     * registers it with the server. This includes creating the resource object as well.
221     */

222    protected void register() throws JMException JavaDoc,
223                                     InvalidTargetObjectTypeException JavaDoc
224    {
225
226       // iterate over all loaded infos and register them.
227
logger().debug("registering...");
228       Enumeration JavaDoc names = namesToRestore().elements();
229       Enumeration JavaDoc infos = infosToRestore().elements();
230       try
231       {
232
233          while(names.hasMoreElements() && infos.hasMoreElements())
234          {
235
236             ObjectName JavaDoc curName = (ObjectName JavaDoc) names.nextElement();
237             logger().debug("curName: " + curName);
238             ModelMBeanInfo JavaDoc curInfo = (ModelMBeanInfo JavaDoc) infos.nextElement();
239             Descriptor JavaDoc mbeanDescriptor = curInfo.getMBeanDescriptor();
240             String JavaDoc fieldName = ModelMBeanConstants.RESOURCE_CLASS;
241             String JavaDoc className = (String JavaDoc) mbeanDescriptor.getFieldValue(fieldName);
242             logger().debug("className: " + className);
243             Object JavaDoc resource = getMBeanServer().instantiate(className);
244             ModelMBean JavaDoc modelmbean = RequiredModelMBeanInstantiator.instantiate();
245             modelmbean.setModelMBeanInfo(curInfo);
246             modelmbean.setManagedResource(resource, "ObjectReference");
247             getMBeanServer().registerMBean(modelmbean, curName);
248          }
249       }
250       finally
251       {
252
253          namesToRestore().removeAllElements();
254          infosToRestore().removeAllElements();
255       }
256    }
257
258    protected MBeanServer JavaDoc getMBeanServer() throws JMException JavaDoc
259    {
260
261       if(fMbeanServer == null)
262       {
263
264          Collection JavaDoc col = MBeanServerFactory.findMBeanServer(null);
265          if(col.isEmpty())
266          {
267
268             throw new JMException JavaDoc("No MBeanServer found");
269          }
270          fMbeanServer = (MBeanServer JavaDoc) col.iterator().next();
271       }
272       return fMbeanServer;
273    }
274    
275    /**
276     * JDK 1.3.1 substitute for jdk 1.4.1 String.replaceAll()
277     * Does not accept regular expressions...
278     * returns the object string where all instances of target are replaced with replacement
279     * @todo replace with a more appropriate string util if available
280     */

281    protected String JavaDoc replaceAll(String JavaDoc object, String JavaDoc target, String JavaDoc replacement)
282    {
283       return replaceAll(object, target, replacement, 0);
284    }
285    
286    /**
287     * Iterative
288     */

289    protected String JavaDoc replaceAll(String JavaDoc object, String JavaDoc target, String JavaDoc replacement, int curIndex)
290    {
291      int indexOfMatch = object.indexOf(target, curIndex);
292      if(indexOfMatch < curIndex)
293      {
294       
295         return object;
296      }
297      String JavaDoc prefix = "";
298      if(indexOfMatch > 0)
299      {
300          
301         prefix = object.substring(0, indexOfMatch);
302      }
303      String JavaDoc tail = object.substring(indexOfMatch + target.length());
304      String JavaDoc newObject = prefix + replacement + tail;
305      return replaceAll(newObject, target, replacement, indexOfMatch + replacement.length());
306    }
307
308    protected String JavaDoc objNameSeparator()
309    {
310
311       return ":";
312    }
313
314    protected String JavaDoc objNameSepRep()
315    {
316
317       return "___";
318    }
319
320    protected String JavaDoc fileName(ObjectName JavaDoc name)
321    {
322
323       String JavaDoc fileName = name.getCanonicalName();
324       fileName = replaceAll(fileName, objNameSeparator(), objNameSepRep());
325       return fileName;
326    }
327
328    protected ObjectName JavaDoc objectName(File JavaDoc fileName)
329      throws MalformedObjectNameException JavaDoc
330    {
331
332       String JavaDoc objectName = fileName.getName();
333       objectName = replaceAll(objectName, objNameSepRep(), objNameSeparator());
334       return new ObjectName JavaDoc(objectName);
335    }
336
337    protected boolean loadedFromFs()
338    {
339
340       return fLoadedFromFs;
341    }
342
343    protected void setLoadedFromFs(boolean newLoadedFromFs)
344    {
345
346       fLoadedFromFs = newLoadedFromFs;
347    }
348
349    protected Vector JavaDoc namesToRestore()
350    {
351
352       if(fNamesToRestore == null)
353       {
354
355          fNamesToRestore = new Vector JavaDoc(10);
356       }
357       return fNamesToRestore;
358    }
359
360    protected Vector JavaDoc infosToRestore()
361    {
362
363       if(fInfosToRestore == null)
364       {
365
366          fInfosToRestore = new Vector JavaDoc(10);
367       }
368       return fInfosToRestore;
369    }
370
371    protected File JavaDoc mbiDbRoot()
372    {
373
374       if(fMbiDbRoot == null)
375       {
376
377          fMbiDbRoot = new File JavaDoc("../conf/mbean-info-db/");
378       }
379       return fMbiDbRoot;
380    }
381
382    protected void setMbiDbRoot(File JavaDoc newMbiDbRoot)
383    {
384
385       fMbiDbRoot = newMbiDbRoot;
386    }
387
388    protected MbeanInfoDb mbeanInfoDb()
389    {
390
391       return fMbeanInfoDb;
392    }
393
394    protected void setMbeanInfoDb(MbeanInfoDb newMbeanInfoDb)
395    {
396
397       fMbeanInfoDb = newMbeanInfoDb;
398    }
399 }
Popular Tags