1 package org.apache.tools.ant.taskdefs.optional.jmx; 2 3 52 53 54 55 import java.util.ArrayList ; 56 import javax.management.MalformedObjectNameException ; 57 import javax.management.ObjectName ; 58 import org.apache.tools.ant.BuildException; 59 60 61 62 73 public class CopyMBeanTask extends AbstractMBeanTask { 74 75 private String toName; 76 private String ifExists = IfExists.FAIL; 77 private String type = null; 78 79 84 public void setToName(String toName) { 85 this.toName = toName; 86 } 87 88 public void setType(String type) { 89 this.type = type; 90 } 91 92 100 public void setIfExists(IfExists ifExists) { 101 this.ifExists = ifExists.getValue(); 102 } 103 104 107 private String getToName() { 108 return toName; 109 } 110 111 117 private String getType() { 118 if (type == null) { 121 try { 122 ObjectName toObjectName = new ObjectName (getToName()); 123 type = toObjectName.getKeyProperty("Type"); 124 } catch (Exception eatMe) { 125 128 } 129 130 if (type == null) { 131 try { 132 ObjectName fromObjectName = getObjectName(); 133 type = fromObjectName.getKeyProperty("Type"); 134 } catch (Exception eatMe) { 135 } 138 } 139 } 140 return type; 141 } 142 143 147 protected ObjectName getToObjectName() throws MalformedObjectNameException { 148 ObjectName toObjectName = new ObjectName (getToName()); 149 150 StringBuffer tempToObjectName = new StringBuffer (getToName()); 151 ObjectName fromObjectName = getObjectName(); 152 153 if ((toObjectName.getDomain() == null) || (toObjectName.getDomain().length() == 0)) { 156 157 tempToObjectName.insert(0,fromObjectName.getDomain()); 158 } 159 return new ObjectName (tempToObjectName.toString()); 160 } 161 162 163 169 protected void execute(javax.management.MBeanServer mbserver) throws BuildException { 170 171 try { 172 ObjectName mbeanName = getObjectName(); 173 174 if (mbserver.isRegistered(mbeanName)) { 175 176 ObjectName toMBeanName = getToObjectName(); 177 178 removeMBeanIfExists(mbserver,toMBeanName,ifExists); 179 180 if (!mbserver.isRegistered(toMBeanName)) { 181 try { 182 toMBeanName = getJMXServer().createMBean(getType(),getToObjectName(),mbserver); 183 if (!mbserver.isRegistered(mbeanName)) { 184 throw new BuildException("Cannot create MBean. " + toString()); 185 } 186 } catch (Exception ex) { 187 String message = "Cannot create toMBean. " + toMBeanName; 188 throw new BuildException(message,ex); 189 } 190 191 } 192 193 try { 194 javax.management.MBeanAttributeInfo [] info = mbserver.getMBeanInfo(mbeanName).getAttributes(); 195 ArrayList attributeNames = new ArrayList (); 196 for (int counter = 0; counter < info.length; counter++) { 197 if (info[counter].isWritable()) { 200 attributeNames.add(info[counter].getName()); 201 } 202 } 203 204 String [] nameArray = new String [attributeNames.size()]; 205 java.util.Iterator nameIt = attributeNames.iterator(); 206 int counter = 0; 207 while (nameIt.hasNext()) { 208 nameArray[counter] = (String )nameIt.next(); 209 counter++; 210 } 211 javax.management.AttributeList attributeList = mbserver.getAttributes(mbeanName,nameArray); 212 213 mbserver.setAttributes(toMBeanName,attributeList); 214 215 try { 219 mbserver.setAttribute(toMBeanName,new javax.management.Attribute ("Name",getToObjectName().getKeyProperty("Name"))); 220 } catch (Exception eatMe) { 221 } 223 224 } catch (Exception x) { 225 String message = "Cannot copy mbean attributes to new mbean " + toMBeanName; 226 throw new BuildException(message,x); 227 } 228 229 } else { 230 throw new BuildException("Cannot find MBean. " + toString()); 231 } 232 233 } catch (MalformedObjectNameException x) { 234 throw new BuildException(x); 235 } 236 } 237 238 } 239 240 250 | Popular Tags |