1 package org.apache.tools.ant.taskdefs.optional.jmx; 2 3 52 53 import javax.management.MBeanAttributeInfo ; 54 import javax.management.MBeanServer ; 55 import javax.management.MalformedObjectNameException ; 56 import javax.management.ObjectName ; 57 import org.apache.tools.ant.BuildException; 58 import org.apache.tools.ant.taskdefs.optional.jmx.connector.JMXConnector; 59 import org.apache.tools.ant.types.optional.ContextType; 60 import org.apache.tools.ant.types.optional.MBeanType; 61 62 63 75 public abstract class AbstractMBeanTask extends AbstractManagementTask { 76 77 private ObjectName objectName = null; 78 private String name = null; 79 private String activeDomain = null; 80 81 private JMXConnector jmxServer = null; 82 83 public AbstractMBeanTask() { 84 super(); 85 setContext(new MBeanType()); 86 } 87 88 93 public void execute() throws BuildException { 94 try { 95 96 MBeanServer server = login(getProviderUrl(), getJndiName(), 97 getUser(),getPassword()); 98 execute(server); 99 } catch (Exception x) { 100 if (this.getFailOnError()) { 101 x.printStackTrace(); 102 throw new BuildException("Error! " + x, x); 103 } else { 104 log("Warning! " + x,org.apache.tools.ant.Project.MSG_WARN); 105 } 106 } 107 } 108 109 115 protected abstract void execute(MBeanServer mbserver) throws BuildException; 116 117 122 public String toString() { 123 try { 124 return getObjectName().toString(); 125 } catch (MalformedObjectNameException x) { 126 throw new BuildException(x); 127 } 128 } 129 130 138 protected ObjectName getObjectName() throws MalformedObjectNameException { 139 140 if (objectName == null) { 141 objectName = new ObjectName (getName()); 142 if ((activeDomain != null) && ((objectName.getDomain() == null) || (objectName.getDomain().length() == 0)) ) { 143 objectName = new ObjectName (activeDomain + getName()); 144 } 145 } 146 return objectName; 147 } 148 149 150 155 public void setName(String name) { 156 if ( (getContext() != null) && (getContext().getClass().isAssignableFrom(MBeanType.class)) ) { 157 ((MBeanType)getContext()).setName(name); 158 } else { 159 this.name = name; 160 } 161 } 162 163 168 protected String getName() { 169 if ( (getContext() != null) && (getContext().getClass().isAssignableFrom(MBeanType.class)) ) { 170 return ((MBeanType)getContext()).getName(); 171 } else { 172 return name; 173 } 174 } 175 176 177 public String getActiveDomain() { 178 return activeDomain; 179 } 180 181 public JMXConnector getJMXServer() { 182 return jmxServer; 183 } 184 185 public void setMBeanRef(org.apache.tools.ant.types.Reference ref) { 186 Object obj = ref.getReferencedObject(this.getProject()); 187 188 if (!(obj instanceof MBeanType)) { 189 String msg = "'" + ref.getRefId() + "' doesn't denote a " + MBeanType.DATA_TYPE_NAME + ". " + ref.getRefId() + " is a " + obj.getClass().getName(); 190 throw new BuildException(msg); 191 } 192 setContext((ContextType) obj); 193 } 194 195 202 protected MBeanAttributeInfo findAttributeInfo(javax.management.MBeanInfo beanInfo, String attributeName) { 203 MBeanAttributeInfo [] attributes = beanInfo.getAttributes(); 204 for (int counter = 0; counter < attributes.length; counter++) { 205 if (attributes[counter].getName().equals(attributeName)) { 206 return attributes[counter]; 207 } 208 } 209 return null; 210 } 211 212 218 protected java.util.Map getAttributes(javax.management.MBeanInfo beanInfo) { 219 return getAttributes(beanInfo,null); 220 } 221 222 229 protected java.util.Map getAttributes(javax.management.MBeanInfo beanInfo, java.util.Set attributeNames) { 230 java.util.Map result = new java.util.TreeMap (); 231 MBeanAttributeInfo [] attributes = beanInfo.getAttributes(); 232 for (int counter = 0; counter < attributes.length; counter++) { 233 234 if ( (attributeNames == null) || (attributeNames.contains(attributes[counter].getName()) ) ) { 235 result.put(attributes[counter].getName(), attributes[counter]); 236 } 237 } 238 return result; 239 } 240 241 248 protected String [] getFeatureNames(javax.management.MBeanFeatureInfo [] features) { 249 String [] result = new String [features.length]; 250 251 for (int counter = 0; counter < features.length; counter++) { 252 result[counter] = features[counter].getName(); 253 } 254 255 return result; 256 } 257 270 private MBeanServer login(String providerUrl, String jndiName, String user, String password) throws javax.naming.NamingException { 271 try { 272 273 if (getContext().getServerType() == null) { 274 log("Warning! serverType not specified or unrecognized type. Defaulting to WebLogic."); 275 getContext().setServerType("weblogic"); 276 } 277 278 jmxServer = org.apache.tools.ant.taskdefs.optional.jmx.connector.JMXConnectorFactory.createConnector(getContext().getServerType()); 279 java.util.Hashtable properties = jmxServer.getInitialContextProperties(providerUrl,user,password); 280 281 MBeanServer mbserver = jmxServer.getMBeanServer(properties,jndiName); 282 activeDomain = jmxServer.getActiveDomain(mbserver); 283 return mbserver; 284 285 } catch (Exception e) { 286 e.printStackTrace(); 287 throw new BuildException("JMX Error. " + e.getMessage(), e); 288 } 289 } 290 291 309 protected void removeMBeanIfExists(javax.management.MBeanServer mbserver, 310 javax.management.ObjectName mbean, String ifExists) 311 throws org.apache.tools.ant.BuildException { 312 313 if (mbserver.isRegistered(mbean)) { 314 String message = "Mbean " + mbean.getCanonicalName() + " already exists. "; 315 if (ifExists.equals(IfExists.FAIL)) { 316 throw new org.apache.tools.ant.BuildException(message); 317 } else if (ifExists.equals(IfExists.SKIP)) { 318 return; 319 } else if (ifExists.equals(IfExists.WARN)) { 320 log("Warning! " + message + "Skipping." + mbean); 321 return; 322 } else if (ifExists.equals(IfExists.REPLACE)) { 323 log("Warning! " + message + "It will be deleted and replaced. " + mbean); 324 try { 325 mbserver.unregisterMBean(mbean); 326 } catch (javax.management.InstanceNotFoundException eatMe) { 327 log("Warning! " + message + "However it cannot be properly removed."); 328 } catch (javax.management.MBeanRegistrationException eatMe) { 329 log("Warning! " + message + "However it cannot be properly removed."); 330 } 331 } else if (ifExists.equals(IfExists.REPLACE_ATTRIBUTES)) { 332 log("Warning! " + message + "Its attributes will be replaced. " + mbean); 333 } 334 } 335 } 336 } 337 338 | Popular Tags |