1 22 package org.jboss.mx.persistence; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.Collection ; 27 import java.util.Enumeration ; 28 import java.util.Vector ; 29 30 import javax.management.Descriptor ; 31 import javax.management.InstanceNotFoundException ; 32 import javax.management.JMException ; 33 import javax.management.MBeanException ; 34 import javax.management.MBeanInfo ; 35 import javax.management.MBeanServer ; 36 import javax.management.MBeanServerFactory ; 37 import javax.management.MalformedObjectNameException ; 38 import javax.management.ObjectName ; 39 import javax.management.modelmbean.InvalidTargetObjectTypeException ; 40 import javax.management.modelmbean.ModelMBean ; 41 import javax.management.modelmbean.ModelMBeanInfo ; 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 52 public class MbeanInfoDbPm 53 extends MBeanInfoOdb 54 implements PersistenceManager 55 { 56 57 protected boolean fLoadedFromFs; 58 protected Vector fNamesToRestore; 59 protected Vector fInfosToRestore; 60 protected MBeanServer fMbeanServer; 61 protected MbeanInfoDb fMbeanInfoDb; 62 protected File fMbiDbRoot; 63 64 public MbeanInfoDbPm() 65 { 66 super(); 67 } 68 69 72 public void load(ModelMBeanInvoker mbean, MBeanInfo info) 73 throws MBeanException 74 { 75 76 logger().debug("Loading the MBI DB"); 77 setMbeanInfoDb((MbeanInfoDb) mbean.getResource()); 78 if(!loadedFromFs()) 79 { 80 81 Descriptor d = ((ModelMBeanInfo ) info).getMBeanDescriptor(); 83 String dir = (String ) d.getFieldValue( 84 ModelMBeanConstants.PERSIST_LOCATION); 85 if((dir != null) && (!dir.equals(""))) 86 { 87 88 setMbiDbRoot(new File (dir)); 89 } 90 String message = "Loading the MBI DB from the filesystem. location: "; 91 message += mbiDbRoot(); 92 logger().debug(message); 93 File [] mbiFiles = mbiDbRoot().listFiles(); 94 if(mbiFiles == null) 95 { 96 97 return; 98 } 99 for(int index = 0; index < mbiFiles.length; index++) 101 { 102 103 File curFile = mbiFiles[index]; 105 ObjectName nameToRestore = null; 106 try 107 { 108 109 nameToRestore = objectName(curFile); 110 } 111 catch(MalformedObjectNameException cause) 112 { 113 114 throw new MBeanException (cause, 115 "Object Name was stored incorrectly."); 116 } 117 MBeanInfo infoToRestore = null; 118 try 119 { 120 121 infoToRestore = load(curFile); 122 } 123 catch(IOException cause) 124 { 125 126 throw new MBeanException (cause, 127 "Couldn't read the MBeanInfo file."); 128 } 129 catch(ClassNotFoundException cause2) 130 { 131 132 String message2 = "Couldn't find the Class specified in the object file."; 133 throw new MBeanException (cause2, message2); 134 } 135 if(infoToRestore == null) 136 { 137 138 throw new MBeanException (new Exception ( 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 cause) 153 { 154 155 throw new MBeanException (cause, 156 "Error trying to register loaded MBeans"); 157 } 158 } 159 160 public void store(MBeanInfo info) throws MBeanException 161 { 162 163 logger().debug("storing MBI DB State"); 164 Enumeration queue = mbeanInfoDb().mbiPersistenceQueue(); 165 for(; queue.hasMoreElements();) 166 { 167 168 ObjectName curName = (ObjectName ) queue.nextElement(); 169 logger().debug("queue elem: " + curName); 170 MBeanInfo curInfo = null; 171 try 172 { 173 174 curInfo = getMBeanServer().getMBeanInfo(curName); 175 } 176 catch(InstanceNotFoundException cause) 177 { 178 179 throw new MBeanException (cause); 180 } 181 catch(JMException cause3) 182 { 183 184 throw new MBeanException (cause3); 185 } 186 if(curInfo == null) 187 { 188 189 throw new MBeanException ( 190 new Exception ("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 cause2) 199 { 200 201 throw new MBeanException (cause2); 202 } 203 mbeanInfoDb().removeFromMbiQueue(curName); 204 logger().info("Successfully stored mbi for " + curName); 205 } 206 } 207 208 protected void store(ObjectName name, MBeanInfo info) 209 throws IOException 210 { 211 212 File location = new File (mbiDbRoot(), fileName(name)); 213 logger().debug("Storing mbi at: " + location); 214 mbiDbRoot().mkdirs(); 215 store(info, location); 216 } 217 218 222 protected void register() throws JMException , 223 InvalidTargetObjectTypeException 224 { 225 226 logger().debug("registering..."); 228 Enumeration names = namesToRestore().elements(); 229 Enumeration infos = infosToRestore().elements(); 230 try 231 { 232 233 while(names.hasMoreElements() && infos.hasMoreElements()) 234 { 235 236 ObjectName curName = (ObjectName ) names.nextElement(); 237 logger().debug("curName: " + curName); 238 ModelMBeanInfo curInfo = (ModelMBeanInfo ) infos.nextElement(); 239 Descriptor mbeanDescriptor = curInfo.getMBeanDescriptor(); 240 String fieldName = ModelMBeanConstants.RESOURCE_CLASS; 241 String className = (String ) mbeanDescriptor.getFieldValue(fieldName); 242 logger().debug("className: " + className); 243 Object resource = getMBeanServer().instantiate(className); 244 ModelMBean 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 getMBeanServer() throws JMException 259 { 260 261 if(fMbeanServer == null) 262 { 263 264 Collection col = MBeanServerFactory.findMBeanServer(null); 265 if(col.isEmpty()) 266 { 267 268 throw new JMException ("No MBeanServer found"); 269 } 270 fMbeanServer = (MBeanServer ) col.iterator().next(); 271 } 272 return fMbeanServer; 273 } 274 275 281 protected String replaceAll(String object, String target, String replacement) 282 { 283 return replaceAll(object, target, replacement, 0); 284 } 285 286 289 protected String replaceAll(String object, String target, String replacement, int curIndex) 290 { 291 int indexOfMatch = object.indexOf(target, curIndex); 292 if(indexOfMatch < curIndex) 293 { 294 295 return object; 296 } 297 String prefix = ""; 298 if(indexOfMatch > 0) 299 { 300 301 prefix = object.substring(0, indexOfMatch); 302 } 303 String tail = object.substring(indexOfMatch + target.length()); 304 String newObject = prefix + replacement + tail; 305 return replaceAll(newObject, target, replacement, indexOfMatch + replacement.length()); 306 } 307 308 protected String objNameSeparator() 309 { 310 311 return ":"; 312 } 313 314 protected String objNameSepRep() 315 { 316 317 return "___"; 318 } 319 320 protected String fileName(ObjectName name) 321 { 322 323 String fileName = name.getCanonicalName(); 324 fileName = replaceAll(fileName, objNameSeparator(), objNameSepRep()); 325 return fileName; 326 } 327 328 protected ObjectName objectName(File fileName) 329 throws MalformedObjectNameException 330 { 331 332 String objectName = fileName.getName(); 333 objectName = replaceAll(objectName, objNameSepRep(), objNameSeparator()); 334 return new ObjectName (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 namesToRestore() 350 { 351 352 if(fNamesToRestore == null) 353 { 354 355 fNamesToRestore = new Vector (10); 356 } 357 return fNamesToRestore; 358 } 359 360 protected Vector infosToRestore() 361 { 362 363 if(fInfosToRestore == null) 364 { 365 366 fInfosToRestore = new Vector (10); 367 } 368 return fInfosToRestore; 369 } 370 371 protected File mbiDbRoot() 372 { 373 374 if(fMbiDbRoot == null) 375 { 376 377 fMbiDbRoot = new File ("../conf/mbean-info-db/"); 378 } 379 return fMbiDbRoot; 380 } 381 382 protected void setMbiDbRoot(File 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 |