KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > j2ee > JNDIResource


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.management.j2ee;
23
24 import org.jboss.logging.Logger;
25
26 import javax.management.JMException JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MalformedObjectNameException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 /**
32  * Root class of the JBoss JSR-77 implementation of JNDIResource.
33  *
34  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
35  * @version $Revision: 40550 $
36  */

37 public class JNDIResource extends J2EEResource
38    implements JNDIResourceMBean
39 {
40    // Constants -----------------------------------------------------
41
private static Logger log = Logger.getLogger(JNDIResource.class);
42
43    // Attributes ----------------------------------------------------
44

45    private StateManagement mState;
46    private ObjectName JavaDoc jndiServiceName;
47
48    // Static --------------------------------------------------------
49

50    public static ObjectName JavaDoc create(MBeanServer JavaDoc mbeanServer, String JavaDoc resName,
51                                    ObjectName JavaDoc jndiServiceName)
52    {
53       ObjectName JavaDoc j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer);
54       ObjectName JavaDoc 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 JavaDoc 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 JavaDoc mbeanServer, String JavaDoc 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 JavaDoc e)
80       {
81          log.debug("Could not destroy JSR-77 JNDIResource: " + resName, e);
82       }
83    }
84
85    // Constructors --------------------------------------------------
86

87    /**
88     * @param resName Name of the JNDIResource
89     */

90    public JNDIResource(String JavaDoc resName, ObjectName JavaDoc mbeanServer, ObjectName JavaDoc jndiServiceName)
91            throws
92            MalformedObjectNameException JavaDoc,
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    // Public --------------------------------------------------------
102

103    // javax.managment.j2ee.EventProvider implementation -------------
104

105    public String JavaDoc[] getEventTypes()
106    {
107       return StateManagement.stateTypes;
108    }
109
110    public String JavaDoc 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    // javax.management.j2ee.StateManageable implementation ----------
123

124    public long getStartTime()
125    {
126       return mState.getStartTime();
127    }
128
129    public int getState()
130    {
131       return mState.getState();
132    }
133    public String JavaDoc 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 JavaDoc[]{},
145                  new String JavaDoc[]{});
146       }
147       catch (Exception JavaDoc e)
148       {
149          log.debug("Start of JNDI Resource failed", e);
150       }
151    }
152
153    public void mejbStartRecursive()
154    {
155       // No recursive start here
156
mejbStart();
157    }
158
159    public void mejbStop()
160    {
161       try
162       {
163          server.invoke(jndiServiceName,
164                  "stop",
165                  new Object JavaDoc[]{},
166                  new String JavaDoc[]{});
167       }
168       catch (Exception JavaDoc 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 JavaDoc 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       // Remove the listener of the target MBean
191
try
192       {
193          server.removeNotificationListener(jndiServiceName, mState);
194       }
195       catch (JMException JavaDoc jme)
196       {
197          // When the service is not available anymore then just ignore the exception
198
}
199    }
200
201    // java.lang.Object overrides ------------------------------------
202

203    public String JavaDoc toString()
204    {
205       return "JNDIResource { " + super.toString() + " } [ " +
206               " ]";
207    }
208
209 }
210
Popular Tags