1 22 package org.jboss.management.j2ee; 23 24 import org.jboss.logging.Logger; 25 import org.w3c.dom.Document ; 26 27 import javax.management.Attribute ; 28 import javax.management.JMException ; 29 import javax.management.MBeanServer ; 30 import javax.management.MalformedObjectNameException ; 31 import javax.management.ObjectName ; 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 import java.io.ByteArrayInputStream ; 35 import java.io.InputStream ; 36 37 45 public class JavaMailResource extends J2EEResource 46 implements JavaMailResourceMBean 47 { 48 private static Logger log = Logger.getLogger(JavaMailResource.class); 50 51 53 private StateManagement mState; 54 private ObjectName mailServiceName; 55 56 58 public static ObjectName create(MBeanServer mbeanServer, String resName, 59 ObjectName mailServiceName) 60 { 61 ObjectName j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer); 62 ObjectName jsr77Name = null; 63 try 64 { 65 JavaMailResource mailRes = new JavaMailResource(resName, j2eeServerName, mailServiceName); 66 jsr77Name = mailRes.getObjectName(); 67 mbeanServer.registerMBean(mailRes, jsr77Name); 68 log.debug("Created JSR-77 JavaMailResource: " + resName); 69 } 70 catch (Exception e) 71 { 72 log.debug("Could not create JSR-77 JavaMailResource: " + resName, e); 73 } 74 return jsr77Name; 75 } 76 77 public static void destroy(MBeanServer mbeanServer, String resName) 78 { 79 try 80 { 81 J2EEManagedObject.removeObject(mbeanServer, 82 J2EEDomain.getDomainName() + ":" + 83 J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JavaMailResource + "," + 84 "name=" + resName + "," + 85 "*"); 86 } 87 catch (Exception e) 88 { 89 log.debug("Could not destroy JSR-77 JNDIResource: " + resName, e); 90 } 91 } 92 93 95 98 public JavaMailResource(String resName, ObjectName j2eeServerName, 99 ObjectName mailServiceName) 100 throws MalformedObjectNameException , 101 InvalidParentException 102 { 103 super(J2EETypeConstants.JavaMailResource, resName, j2eeServerName); 104 this.mailServiceName = mailServiceName; 105 mState = new StateManagement(this); 106 } 107 108 110 113 public String getuserName() 114 throws Exception 115 { 116 return (String ) server.getAttribute(mailServiceName, "User"); 117 } 118 119 122 public void setuserName(String pName) 123 throws Exception 124 { 125 server.setAttribute(mailServiceName, new Attribute ("User", pName)); 126 } 127 128 131 public void setpassword(String pPassword) 132 throws Exception 133 { 134 server.setAttribute(mailServiceName, new Attribute ("Password", pPassword)); 135 } 136 137 140 public String getjndiName() 141 throws Exception 142 { 143 return (String ) server.getAttribute(mailServiceName, "JNDIName"); 144 } 145 146 149 public void setjndiName(String pName) 150 throws Exception 151 { 152 server.setAttribute(mailServiceName, new Attribute ("JNDIName", pName)); 153 } 154 155 158 public String getconfiguration() 159 throws Exception 160 { 161 return server.getAttribute(mailServiceName, "Configuration") + ""; 162 } 163 164 167 public void setconfiguration(String pConfigurationElement) 168 throws Exception 169 { 170 if (pConfigurationElement == null || pConfigurationElement.length() == 0) 171 { 172 pConfigurationElement = "<configuration/>"; 173 } 174 DocumentBuilder lParser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 175 InputStream lInput = new ByteArrayInputStream (pConfigurationElement.getBytes()); 176 Document lDocument = lParser.parse(lInput); 177 server.setAttribute(mailServiceName, new Attribute ("Configuration", lDocument.getDocumentElement())); 178 } 179 180 182 public String [] getEventTypes() 183 { 184 return StateManagement.stateTypes; 185 } 186 187 public String getEventType(int pIndex) 188 { 189 if (pIndex >= 0 && pIndex < StateManagement.stateTypes.length) 190 { 191 return StateManagement.stateTypes[pIndex]; 192 } 193 else 194 { 195 return null; 196 } 197 } 198 199 201 public long getStartTime() 202 { 203 return mState.getStartTime(); 204 } 205 206 public int getState() 207 { 208 return mState.getState(); 209 } 210 public String getStateString() 211 { 212 return mState.getStateString(); 213 } 214 215 public void mejbStart() 216 { 217 try 218 { 219 server.invoke(mailServiceName, 220 "start", 221 new Object []{}, 222 new String []{}); 223 } 224 catch (Exception e) 225 { 226 log.error("start failed", e); 227 } 228 } 229 230 public void mejbStartRecursive() 231 { 232 try 234 { 235 mejbStart(); 236 } 237 catch (Exception e) 238 { 239 log.error("start failed", e); 240 } 241 } 242 243 public void mejbStop() 244 { 245 try 246 { 247 server.invoke(mailServiceName, 248 "stop", 249 new Object []{}, 250 new String []{}); 251 } 252 catch (Exception e) 253 { 254 log.error("Stop of JavaMailResource failed", e); 255 } 256 } 257 258 public void postCreation() 259 { 260 try 261 { 262 server.addNotificationListener(mailServiceName, mState, null, null); 263 } 264 catch (JMException e) 265 { 266 log.debug("Failed to add notification listener", e); 267 } 268 sendNotification(NotificationConstants.OBJECT_CREATED, "Java Mail Resource created"); 269 } 270 271 public void preDestruction() 272 { 273 sendNotification(NotificationConstants.OBJECT_DELETED, "Java Mail Resource deleted"); 274 try 276 { 277 server.removeNotificationListener(mailServiceName, mState); 278 } 279 catch (JMException jme) 280 { 281 } 283 } 284 285 287 public String toString() 288 { 289 return "JavaMailResource { " + super.toString() + " } [ " + 290 " ]"; 291 } 292 } 293 | Popular Tags |