1 22 package org.jboss.management.j2ee; 23 24 import org.jboss.logging.Logger; 25 26 import javax.management.JMException ; 27 import javax.management.MBeanServer ; 28 import javax.management.MalformedObjectNameException ; 29 import javax.management.ObjectName ; 30 31 37 public class JNDIResource extends J2EEResource 38 implements JNDIResourceMBean 39 { 40 private static Logger log = Logger.getLogger(JNDIResource.class); 42 43 45 private StateManagement mState; 46 private ObjectName jndiServiceName; 47 48 50 public static ObjectName create(MBeanServer mbeanServer, String resName, 51 ObjectName jndiServiceName) 52 { 53 ObjectName j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer); 54 ObjectName jsr77Name = null; 55 try 56 { 57 JNDIResource jndiRes = new JNDIResource(resName, j2eeServerName, jndiServiceName); 58 jsr77Name = jndiRes.getObjectName(); 59 mbeanServer.registerMBean(jndiRes, jsr77Name); 60 log.debug("Created JSR-77 JNDIResource: " + resName); 61 } 62 catch (Exception e) 63 { 64 log.debug("Could not create JSR-77 JNDIResource: " + resName, e); 65 } 66 return jsr77Name; 67 } 68 69 public static void destroy(MBeanServer mbeanServer, String resName) 70 { 71 try 72 { 73 J2EEManagedObject.removeObject(mbeanServer, 74 J2EEDomain.getDomainName() + ":" + 75 J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JNDIResource + "," + 76 "name=" + resName + "," + 77 "*"); 78 } 79 catch (Exception e) 80 { 81 log.debug("Could not destroy JSR-77 JNDIResource: " + resName, e); 82 } 83 } 84 85 87 90 public JNDIResource(String resName, ObjectName mbeanServer, ObjectName jndiServiceName) 91 throws 92 MalformedObjectNameException , 93 InvalidParentException 94 { 95 super(J2EETypeConstants.JNDIResource, resName, mbeanServer); 96 log.debug("Service name: " + jndiServiceName); 97 this.jndiServiceName = jndiServiceName; 98 mState = new StateManagement(this); 99 } 100 101 103 105 public String [] getEventTypes() 106 { 107 return StateManagement.stateTypes; 108 } 109 110 public String getEventType(int pIndex) 111 { 112 if (pIndex >= 0 && pIndex < StateManagement.stateTypes.length) 113 { 114 return StateManagement.stateTypes[pIndex]; 115 } 116 else 117 { 118 return null; 119 } 120 } 121 122 124 public long getStartTime() 125 { 126 return mState.getStartTime(); 127 } 128 129 public int getState() 130 { 131 return mState.getState(); 132 } 133 public String getStateString() 134 { 135 return mState.getStateString(); 136 } 137 138 public void mejbStart() 139 { 140 try 141 { 142 server.invoke(jndiServiceName, 143 "start", 144 new Object []{}, 145 new String []{}); 146 } 147 catch (Exception e) 148 { 149 log.debug("Start of JNDI Resource failed", e); 150 } 151 } 152 153 public void mejbStartRecursive() 154 { 155 mejbStart(); 157 } 158 159 public void mejbStop() 160 { 161 try 162 { 163 server.invoke(jndiServiceName, 164 "stop", 165 new Object []{}, 166 new String []{}); 167 } 168 catch (Exception e) 169 { 170 log.debug("Stop of JNDI Resource failed", e); 171 } 172 } 173 174 public void postCreation() 175 { 176 try 177 { 178 server.addNotificationListener(jndiServiceName, mState, null, null); 179 } 180 catch (JMException e) 181 { 182 log.debug("Failed to add notification listener", e); 183 } 184 sendNotification(NotificationConstants.OBJECT_CREATED, "JNDI Resource created"); 185 } 186 187 public void preDestruction() 188 { 189 sendNotification(NotificationConstants.OBJECT_DELETED, "JNDI Resource destroyed"); 190 try 192 { 193 server.removeNotificationListener(jndiServiceName, mState); 194 } 195 catch (JMException jme) 196 { 197 } 199 } 200 201 203 public String toString() 204 { 205 return "JNDIResource { " + super.toString() + " } [ " + 206 " ]"; 207 } 208 209 } 210 | Popular Tags |