1 23 24 25 package com.sun.enterprise.admin.mbeans.custom; 26 import com.sun.enterprise.admin.server.core.CustomMBeanRegistration; 27 import com.sun.enterprise.admin.common.MBeanServerFactory; 28 import com.sun.enterprise.admin.mbeans.custom.loading.CustomMBeanRegistrationImpl; 29 import com.sun.enterprise.admin.meta.MBeanRegistryFactory; 30 import com.sun.enterprise.config.ConfigContext; 31 import com.sun.enterprise.config.serverbeans.Mbean; 32 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 33 import com.sun.enterprise.admin.target.TargetBuilder; 34 import com.sun.enterprise.admin.target.Target; 35 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Collections ; 39 import java.util.HashMap ; 40 import javax.management.MBeanInfo ; 41 import javax.management.MalformedObjectNameException ; 42 import javax.management.ObjectName ; 43 import javax.management.MBeanInfo ; 44 import javax.management.MBeanServerConnection ; 45 import javax.management.MBeanServer ; 46 47 48 public class BasicCustomMBeanOperations implements CustomMBeanOperationsMBean { 49 50 51 protected final ConfigContext acc; 52 53 public BasicCustomMBeanOperations() { 54 this.acc = MBeanRegistryFactory.getAdminContext().getAdminConfigContext(); 55 } 56 57 public String createMBean(final String target, final String className) throws CustomMBeanException { 58 final Map <String , String > params = CustomMBeanConstants.unmodifiableMap(CustomMBeanConstants.IMPL_CLASS_NAME_KEY, className); 59 final Map <String , String > attributes = Collections.emptyMap(); 60 return ( this.createMBean(target, params, attributes) ); 61 } 62 63 public String createMBean(String target, Map <String , String > params) throws CustomMBeanException { 64 final Map <String , String > attributes = Collections.emptyMap(); 65 return ( this.createMBean(target, params, attributes) ); 66 } 67 68 public String createMBean(final String target, final Map <String , String > params, final Map <String , String > attributes) throws CustomMBeanException { 69 if (params == null || attributes == null) 70 throw new IllegalArgumentException (CMBStrings.get("InternalError", "null argument")); 71 Target t = null; 72 try { 73 t = TargetBuilder.INSTANCE.createTarget(target, this.acc); 74 } catch (final Exception e) { 75 throw new RuntimeException (e); 76 } 77 return ( this.createMBeanDefinitionAddingReferenceToServer(t.getName(), params, attributes) ); 78 } 79 80 public void createMBeanRef(final String target, final String ref) throws CustomMBeanException { 81 throw new UnsupportedOperationException (CMBStrings.get("InternalError", "Not to be called on PE")); 82 } 83 84 public String deleteMBean(final String target, final String name) throws CustomMBeanException { 85 if (name == null) 86 throw new IllegalArgumentException (CMBStrings.get("InternalError", "null argument")); 87 88 Target t = null; 89 try { 90 t = TargetBuilder.INSTANCE.createTarget(target, this.acc); 91 } catch (final Exception e) { 92 throw new RuntimeException (e); 93 } 94 95 return ( this.deleteMBeanDefinitionRemovingReferenceFromDomain(t.getName(), name) ); 96 } 97 98 public void deleteMBeanRef(final String target, final String ref) throws CustomMBeanException { 99 throw new UnsupportedOperationException (CMBStrings.get("InternalError", "Not to be called on PE")); 100 } 101 102 116 public MBeanInfo getMBeanInfo(String classname) throws CustomMBeanException 117 { 118 try 119 { 120 final String oname = "user:getMBeanInfo=" + System.nanoTime(); 122 final String name = "getMBeanInfo" + System.nanoTime(); 123 124 Mbean mbean = new Mbean(); 126 mbean.setImplClassName(classname); 127 mbean.setName(name); 128 mbean.setObjectName(oname); 129 mbean.setEnabled(true); 130 131 final MBeanServer mbs = MBeanServerFactory.getMBeanServer(); 133 134 CustomMBeanRegistrationImpl cmr = new CustomMBeanRegistrationImpl(mbs); 136 137 cmr.registerMBean(mbean); 139 140 ObjectName ron = cmr.getCascadingAwareObjectName(mbean); 142 143 MBeanInfo info = mbs.getMBeanInfo(ron); 145 146 mbs.unregisterMBean(ron); 148 149 return info; 150 } 151 catch(Exception e) 152 { 153 throw new CustomMBeanException(e); 154 } 155 } 156 157 166 protected String createMBeanDefinitionAddingReferenceToServer(final String s, final Map <String , String > params, final Map <String , String > attributes) throws CustomMBeanException { 167 try { 168 final Mbean mbean = this.createMBeanDefinitionInDomain(params, attributes); 169 final ObjectName onPostReg = new ObjectName (mbean.getObjectName()); 170 171 if (onExists(s, onPostReg)) { 175 ServerBeansFactory.removeMbeanDefinition(acc, mbean.getName()); 176 177 throw new CustomMBeanException(CMBStrings.get("AlreadyExists", onPostReg.toString())); 178 } 179 ServerBeansFactory.addMbeanReference(this.acc, mbean.getName(), s); 180 return ( mbean.getName() ); 181 } catch (final Exception e) { 182 throw new CustomMBeanException(e); 183 } 184 } 185 186 protected Mbean createMBeanDefinitionInDomain(final Map <String , String > params, final Map <String , String > attributes) { 187 final Map <String , String > mm = checkAndModifyParamsForName(params); 188 final String name = mm.get(CustomMBeanConstants.NAME_KEY); 189 if (definitionExists(name)) 190 throw new CustomMBeanException(CMBStrings.get("AlreadyExists", name)); 191 final ObjectName onPostReg = selectObjectName(mm, attributes); 192 if (!CustomMBeanConstants.CUSTOM_MBEAN_DOMAIN.equals(onPostReg.getDomain())) { 193 throw new IllegalArgumentException (CMBStrings.get("BadDomainName", onPostReg.getDomain(), CustomMBeanConstants.CUSTOM_MBEAN_DOMAIN)); 194 } 195 mm.put(CustomMBeanConstants.OBJECT_NAME_KEY, onPostReg.toString()); final Mbean mbean = MBeanValidator.toMbean(mm, attributes, true); 198 try { 199 ServerBeansFactory.addMbeanDefinition(this.acc, mbean); 200 } catch (final Exception e) { 201 throw new CustomMBeanException(e); 202 } 203 return ( mbean ); 204 } 205 protected Map <String , String > checkAndModifyParamsForName(final Map <String , String > params) throws CustomMBeanException { 206 checkParams(params); 207 final Map <String , String > paramsWithNameKey = putNameIfAbsent(params); final String name = paramsWithNameKey.get(CustomMBeanConstants.NAME_KEY); 210 return ( paramsWithNameKey ); 211 } 212 213 protected ObjectName selectObjectName(final Map <String , String > mm, final Map <String , String > attributes) throws CustomMBeanException { 214 final ObjectName onPreReg = ObjectNameSelectionAlgorithm.select(mm); 215 final MBeanValidator mv = new MBeanValidator(); 217 final ObjectName onPostReg = mv.registerTestMBean(createTestMap(Collections.unmodifiableMap(mm), onPreReg), attributes); 218 final String className = mm.get(CustomMBeanConstants.IMPL_CLASS_NAME_KEY); 219 final boolean selfReg = ObjectNameSelectionAlgorithm.implementsMBeanRegistrationInterface(className); 220 if (onPostReg != null) { final ObjectName cascadedON = CustomMBeanRegistrationImpl.getCascadingAwareObjectName(onPreReg); 223 224 if (!onPostReg.equals(cascadedON) && !selfReg) { 226 throw new CustomMBeanException(CMBStrings.get("ObjectNameMismatch", onPreReg, CustomMBeanRegistrationImpl.getCascadingUnawareObjectName(onPostReg))); 227 } 228 } 229 try { 230 mv.unregisterTestMBean(onPostReg); } catch (final Exception e) { 232 e.printStackTrace(); 233 } 234 final ObjectName onInConfig = CustomMBeanRegistrationImpl.getCascadingUnawareObjectName(onPostReg); 236 return ( onInConfig ); 237 } 238 protected String deleteMBeanDefinitionRemovingReferenceFromDomain(final String s, final String name) throws RuntimeException { 239 boolean refd; 240 try { 241 refd = ServerBeansFactory.isReferencedMBean(acc, s, name); 242 } catch(final Exception e) { 243 throw new RuntimeException (e); 244 } 245 if (!refd) { 246 throw new RuntimeException (CMBStrings.get("RefsNotFound", s, name)); 247 } 248 removeMBeanDefinitionAndReferenceFromConfigTree(s, name); 249 return ( name ); 250 } 251 252 protected Map <String , String > putNameIfAbsent(final Map <String , String > m) { 253 final Map <String , String > nm = new HashMap <String , String >(m); 254 final String c = nm.get(CustomMBeanConstants.IMPL_CLASS_NAME_KEY); assert (c != null); 256 if (!nm.containsKey(CustomMBeanConstants.NAME_KEY)) 257 nm.put(CustomMBeanConstants.NAME_KEY, c); 258 return ( nm ); 259 } 260 261 protected void checkParams(final Map <String , String > params) throws IllegalArgumentException { 262 if (!params.containsKey(CustomMBeanConstants.IMPL_CLASS_NAME_KEY)) 263 throw new IllegalArgumentException (CMBStrings.get("NoImplClass")); 264 checkValidIfPresent(params); 265 checkValidObjectNameIfPresent(params); 266 } 267 268 protected boolean definitionExists(final String name) throws CustomMBeanException { 269 boolean exists = false; 270 try { 271 final List <Mbean> mbeans = ServerBeansFactory.getAllMBeanDefinitions(acc); 272 for (Mbean m : mbeans) { 273 if (m.getName().equals(name)) { 274 exists = true; 275 break; 276 } 277 278 } 279 return (exists); 280 } catch (final Exception e) { 281 throw new CustomMBeanException(e); 282 } 283 } 284 285 protected boolean onExists(final String server, final ObjectName on) throws RuntimeException { 286 try { 287 boolean exists = false; 288 final List <Mbean> mbeans = ServerBeansFactory.getReferencedMBeans(acc, server); 289 for (Mbean m : mbeans) { 290 final String onsFromConfig = m.getObjectName(); 291 final ObjectName onFromConfig = new ObjectName (onsFromConfig); 292 if (onFromConfig.equals(on)) { 293 exists = true; 294 break; 295 } 296 } 297 return ( exists ) ; 298 } catch (final Exception e) { 299 throw new RuntimeException (e); 300 } 301 } 302 304 private void checkValidIfPresent(final Map <String , String > params) throws IllegalArgumentException { 305 final String nameKey = CustomMBeanConstants.NAME_KEY; 306 final String name = params.get(nameKey); 307 final String cKey = CustomMBeanConstants.IMPL_CLASS_NAME_KEY; 308 final String cVal = params.get(cKey); 309 final String onKey = CustomMBeanConstants.OBJECT_NAME_KEY; 310 final String onVal = params.get(onKey); 311 if (params.containsKey(nameKey) && name == null) 312 313 throw new IllegalArgumentException (CMBStrings.get("MapHasNullParam", "Name")); 314 if (params.containsKey(cKey) && cVal == null) 315 throw new IllegalArgumentException (CMBStrings.get("MapHasNullParam", "ClassName")); 316 if (params.containsKey(onKey) && onVal == null) 317 throw new IllegalArgumentException (CMBStrings.get("MapHasNullParam", "ObjectName")); 318 } 319 320 private void checkValidObjectNameIfPresent(final Map <String , String > params) throws IllegalArgumentException { 321 final String onKey = CustomMBeanConstants.OBJECT_NAME_KEY; 322 final String onVal = params.get(onKey); 323 boolean onSpecified = params.containsKey(onKey) && onVal != null; 324 ObjectName on = null; 325 if (onSpecified) { 326 try { 327 on = new ObjectName (onVal); } catch(final MalformedObjectNameException me) { 329 throw new IllegalArgumentException (me); 330 } 331 if (on.isPattern()) 332 throw new IllegalArgumentException (CMBStrings.get("ObjectNamePattern")); 333 final String d = on.getDomain(); 334 if (!CustomMBeanConstants.CUSTOM_MBEAN_DOMAIN.equals(d)) { 335 throw new IllegalArgumentException (CMBStrings.get("BadDomainName", d, CustomMBeanConstants.CUSTOM_MBEAN_DOMAIN)); 336 } 337 if (on.getKeyProperty(CustomMBeanConstants.SERVER_KEY) != null) { 338 throw new IllegalArgumentException (CMBStrings.get("ObjectNameReserved", on.toString(), CustomMBeanConstants.SERVER_KEY)); 339 } 340 } 341 } 342 343 private void removeMBeanDefinitionAndReferenceFromConfigTree(final String server, final String ref) throws RuntimeException { 344 try { 345 ServerBeansFactory.removeMbeanReference(acc, ref, server); 346 ServerBeansFactory.removeMbeanDefinition(acc, ref); 347 } catch (final Exception e) { 348 throw new RuntimeException (e); 349 } 350 } 351 private Map <String , String > createTestMap(final Map <String , String > params, final ObjectName on) { 352 final Map <String , String > nm = new HashMap <String , String > (params); 353 nm.put(CustomMBeanConstants.OBJECT_NAME_KEY, on.toString()); 354 return ( nm ); 355 } 356 } 358 | Popular Tags |