1 22 package org.jboss.mx.loading; 23 24 import java.io.Serializable ; 25 import java.lang.reflect.Constructor ; 26 import java.util.HashMap ; 27 import javax.management.InstanceNotFoundException ; 28 import javax.management.JMException ; 29 import javax.management.MalformedObjectNameException ; 30 import javax.management.MBeanServer ; 31 import javax.management.ObjectInstance ; 32 import javax.management.ObjectName ; 33 34 import org.jboss.logging.Logger; 35 import org.jboss.mx.server.ServerConstants; 36 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.NodeList ; 39 import org.w3c.dom.Node ; 40 41 48 public class LoaderRepositoryFactory 49 { 50 51 public static ObjectName DEFAULT_LOADER_REPOSITORY; 52 private static Logger log = Logger.getLogger(LoaderRepositoryFactory.class); 53 54 private static HashMap referenceCountMap = new HashMap (); 55 56 static 57 { 58 try 59 { 60 DEFAULT_LOADER_REPOSITORY = new ObjectName (ServerConstants.DEFAULT_LOADER_NAME); 62 } 63 catch(Exception e) 64 { 65 log.error("Failed to init DEFAULT_LOADER_REPOSITORY name", e); 66 } 67 } 68 69 74 public static class LoaderRepositoryConfig implements Serializable 75 { 76 static final long serialVersionUID = 4226952985429700362L; 77 78 79 public ObjectName repositoryName = DEFAULT_LOADER_REPOSITORY; 80 public String repositoryClassName; 81 public String configParserClassName; 82 public String repositoryConfig; 83 84 public String toString() 85 { 86 StringBuffer tmp = new StringBuffer ("LoaderRepositoryConfig("); 87 tmp.append("repositoryName: "); 88 tmp.append(repositoryName); 89 tmp.append(", repositoryClassName: "); 90 tmp.append(repositoryClassName); 91 tmp.append(", configParserClassName: "); 92 tmp.append(configParserClassName); 93 tmp.append(", repositoryConfig: "); 94 tmp.append(repositoryConfig); 95 tmp.append(")"); 96 return tmp.toString(); 97 } 98 } 99 100 104 static public interface LoaderRepositoryConfigParser 105 { 106 public void configure(LoaderRepository repository, String config) 107 throws Exception ; 108 } 109 110 121 public static LoaderRepositoryConfig parseRepositoryConfig(Element config) 122 throws MalformedObjectNameException 123 { 124 LoaderRepositoryConfig repositoryConfig = new LoaderRepositoryConfig(); 125 repositoryConfig.repositoryClassName = config.getAttribute("loaderRepositoryClass"); 126 if( repositoryConfig.repositoryClassName.length() == 0 ) 127 repositoryConfig.repositoryClassName = ServerConstants.DEFAULT_SCOPED_REPOSITORY_CLASS; 128 129 NodeList children = config.getChildNodes(); 131 int count = children.getLength(); 132 if( count > 0 ) 133 { 134 for(int n = 0; n < count; n ++) 135 { 136 Node node = children.item(n); 137 int type = node.getNodeType(); 138 if( type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE ) 140 { 141 String objectName = node.getNodeValue().trim(); 142 repositoryConfig.repositoryName = new ObjectName (objectName); 143 break; 144 } 145 } 146 147 children = config.getElementsByTagName("loader-repository-config"); 149 count = children.getLength(); 150 if( count > 0 ) 151 { 152 Element loaderRepositoryConfig = (Element ) children.item(0); 153 children = loaderRepositoryConfig.getChildNodes(); 154 count = children.getLength(); 155 repositoryConfig.configParserClassName = loaderRepositoryConfig.getAttribute("configParserClass"); 156 if( repositoryConfig.configParserClassName.length() == 0 ) 157 repositoryConfig.configParserClassName = ServerConstants.DEFAULT_SCOPED_REPOSITORY_PARSER_CLASS; 158 StringBuffer configData = new StringBuffer (); 159 for(int n = 0; n < count; n ++) 160 { 161 Node node = children.item(n); 162 int type = node.getNodeType(); 163 if( type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE ) 164 { 165 configData.append(node.getNodeValue()); 166 } 167 } 168 repositoryConfig.repositoryConfig = configData.toString().trim(); 169 } 170 } 171 return repositoryConfig; 172 } 173 174 183 public static synchronized void createLoaderRepository(MBeanServer server, 184 String repositoryClassName, 185 ObjectName repositoryName) throws JMException 186 { 187 LoaderRepositoryConfig config = new LoaderRepositoryConfig(); 188 config.repositoryClassName = repositoryClassName; 189 config.repositoryName = repositoryName; 190 createLoaderRepository(server, config); 191 } 192 200 public static synchronized void createLoaderRepository(MBeanServer server, 201 LoaderRepositoryConfig config) throws JMException 202 { 203 if( config == null ) 204 config = new LoaderRepositoryConfig(); 205 String repositoryClassName = config.repositoryClassName; 206 ObjectName repositoryName = config.repositoryName; 207 208 try 209 { 210 ObjectInstance oi = server.getObjectInstance(repositoryName); 211 if ( (repositoryClassName != null) && 212 !oi.getClassName().equals(repositoryClassName) ) 213 { 214 throw new JMException ("Inconsistent LoaderRepository class specification in repository: " + repositoryName); 215 } } 217 catch (InstanceNotFoundException e) 218 { 219 if( repositoryClassName == null ) 221 repositoryClassName = ServerConstants.DEFAULT_SCOPED_REPOSITORY_CLASS; 222 223 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 224 LoaderRepository repository = null; 225 try 226 { 227 Class repositoryClass = loader.loadClass(repositoryClassName); 229 Class [] ctorSig = {MBeanServer .class, ObjectName .class}; 230 Constructor ctor = repositoryClass.getConstructor(ctorSig); 231 Object [] args = {server, DEFAULT_LOADER_REPOSITORY}; 232 repository = (LoaderRepository) ctor.newInstance(args); 233 server.registerMBean(repository, repositoryName); 234 } 235 catch(Exception e2) 236 { 237 log.debug("Failed to create loader repository: ", e2); 238 throw new JMException ("Failed to create loader repository:" + e2); 239 } 240 241 try 242 { 243 if( config.configParserClassName != null && config.repositoryConfig != null ) 245 { 246 Class parserClass = loader.loadClass(config.configParserClassName); 247 LoaderRepositoryConfigParser parser = (LoaderRepositoryConfigParser) 248 parserClass.newInstance(); 249 parser.configure(repository, config.repositoryConfig); 250 } 251 } 252 catch(Exception e2) 253 { 254 log.debug("Failed to configure loader repository: ", e2); 255 throw new JMException ("Failed to configure loader repository: "+e2); 256 } 257 } 259 Integer activeCount = (Integer ) referenceCountMap.get(repositoryName); 260 if( activeCount == null ) 261 activeCount = new Integer (1); 262 else 263 activeCount = new Integer (activeCount.intValue() + 1); 264 referenceCountMap.put(repositoryName, activeCount); 265 } 266 267 public static synchronized void destroyLoaderRepository(MBeanServer server, 268 ObjectName repositoryName) 269 { 270 if( repositoryName.equals(DEFAULT_LOADER_REPOSITORY) == false ) 271 { 272 try 273 { 274 Integer activeCount = (Integer ) referenceCountMap.get(repositoryName); 275 if( activeCount != null ) 276 { 277 if( activeCount.intValue() == 1 ) 278 { 279 server.unregisterMBean(repositoryName); 280 referenceCountMap.remove(repositoryName); 281 log.debug("Unregistered repository: "+repositoryName); 282 } 283 else 284 { 285 activeCount = new Integer (activeCount.intValue() - 1); 286 referenceCountMap.put(repositoryName, activeCount); 287 } 288 } 289 } 290 catch(Exception e) 291 { 292 log.warn("Failed to unregister ear loader repository", e); 293 } 294 } 295 } 296 297 private LoaderRepositoryFactory() 298 { 299 } 300 } | Popular Tags |