KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.management.JMException JavaDoc;
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33
34 import org.jboss.logging.Logger;
35 import org.jboss.system.ServiceMBean;
36
37 /**
38  * Root class of the JBoss JSR-77 implementation of
39  * {@link javax.management.j2ee.JDBCResource JDBCResource}.
40  *
41  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
42  * @author <a HREF="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
43  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
44  * @version $Revision: 40550 $
45  *
46  * @todo This resource should not implement state manageable because it
47  * has no MBean/Service associated but codes stays.
48  */

49 public class JDBCResource extends J2EEResource
50    implements JDBCResourceMBean
51 {
52    // Constants -----------------------------------------------------
53
private static Logger log = Logger.getLogger(JDBCResource.class);
54
55    // Attributes ----------------------------------------------------
56

57    private StateManagement mState;
58    private ObjectName JavaDoc mService;
59
60    // list of object names as strings
61
private List JavaDoc mDataSources = new ArrayList JavaDoc();
62
63    // Static --------------------------------------------------------
64

65    public static ObjectName JavaDoc create(MBeanServer JavaDoc mbeanServer, String JavaDoc resName)
66    {
67       ObjectName JavaDoc lServer = null;
68       try
69       {
70          lServer = (ObjectName JavaDoc) mbeanServer.queryNames(new ObjectName JavaDoc(J2EEDomain.getDomainName() + ":" +
71                  J2EEManagedObject.TYPE + "=" + J2EETypeConstants.J2EEServer + "," +
72                  "*"),
73                  null).iterator().next();
74       }
75       catch (Exception JavaDoc e)
76       {
77          log.error("Could not find parent J2EEServer", e);
78          return null;
79       }
80       try
81       {
82          JDBCResource jdbcRes = new JDBCResource(resName, lServer);
83          ObjectName JavaDoc jsr77Name = jdbcRes.getObjectName();
84          mbeanServer.registerMBean(jdbcRes, jsr77Name);
85          log.debug("Created JSR-77 JDBC Manager: " + resName);
86          
87          return jsr77Name;
88       }
89       catch (Exception JavaDoc e)
90       {
91          log.error("Could not create JSR-77 JDBC Manager", e);
92          return null;
93       }
94    }
95
96    public static void destroy(MBeanServer JavaDoc mbeanServer, String JavaDoc resName)
97    {
98       try
99       {
100          // Find the Object to be destroyed
101
ObjectName JavaDoc lSearch = new ObjectName JavaDoc(J2EEDomain.getDomainName() + ":" +
102                  J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JDBCResource + "," +
103                  "name=" + resName + "," +
104                  "*");
105          Set JavaDoc lNames = mbeanServer.queryNames(lSearch,
106                  null);
107          if (!lNames.isEmpty())
108          {
109             ObjectName JavaDoc lJDBCResource = (ObjectName JavaDoc) lNames.iterator().next();
110             // Now check if the JDBCResource Manager does not contains another DataSources
111
String JavaDoc[] lDataSources = (String JavaDoc[]) mbeanServer.getAttribute(lJDBCResource,
112                     "jdbcDataSources");
113             if (lDataSources.length == 0)
114             {
115                // Remove it because it does not reference any JDBC DataSources
116
mbeanServer.unregisterMBean(lJDBCResource);
117             }
118          }
119       }
120       catch (Exception JavaDoc e)
121       {
122          log.error("Could not destroy JSR-77 JDBC Manager", e);
123       }
124    }
125
126    // Constructors --------------------------------------------------
127

128    /**
129     * @param pName Name of the JDBCResource
130     */

131    public JDBCResource(String JavaDoc resName, ObjectName JavaDoc pServer)
132            throws
133            MalformedObjectNameException JavaDoc,
134            InvalidParentException
135    {
136       super(J2EETypeConstants.JDBCResource, resName, pServer);
137       mState = new StateManagement(this);
138    }
139
140    // Public --------------------------------------------------------
141

142    // javax.managment.j2ee.EventProvider implementation -------------
143

144    public String JavaDoc[] getEventTypes()
145    {
146       return StateManagement.stateTypes;
147    }
148
149    public String JavaDoc getEventType(int pIndex)
150    {
151       if (pIndex >= 0 && pIndex < StateManagement.stateTypes.length)
152       {
153          return StateManagement.stateTypes[pIndex];
154       }
155       else
156       {
157          return null;
158       }
159    }
160
161    // javax.management.j2ee.StateManageable implementation ----------
162

163    public long getStartTime()
164    {
165       return mState.getStartTime();
166    }
167
168    public int getState()
169    {
170       return mState.getState();
171    }
172    public String JavaDoc getStateString()
173    {
174       return mState.getStateString();
175    }
176
177    public void mejbStart()
178    {
179       // No component behind therefore just do it as it is started
180
mState.setState(ServiceMBean.STARTING + 2);
181       mState.setState(ServiceMBean.STARTED + 2);
182    }
183
184    public void mejbStartRecursive()
185    {
186       mState.setState(ServiceMBean.STOPPING + 2);
187       Iterator JavaDoc i = mDataSources.iterator();
188       String JavaDoc lDataSource = null;
189       while (i.hasNext())
190       {
191          lDataSource = (String JavaDoc) i.next();
192          try
193          {
194             getServer().invoke(newObjectName(lDataSource),
195                     "mejbStart",
196                     new Object JavaDoc[]{},
197                     new String JavaDoc[]{});
198          }
199          catch (JMException JavaDoc jme)
200          {
201             getLog().error("Could not start JSR-77 JDBC-DataSource: " + lDataSource, jme);
202          }
203       }
204       mState.setState(ServiceMBean.STOPPED + 2);
205    }
206
207    public void mejbStop()
208    {
209       // No component behind therefore just do it as it is started
210
mState.setState(3);
211       Iterator JavaDoc i = mDataSources.iterator();
212       while (i.hasNext())
213       {
214          String JavaDoc lDataSource = (String JavaDoc) i.next();
215          try
216          {
217             getServer().invoke(newObjectName(lDataSource),
218                     "mejbStop",
219                     new Object JavaDoc[]{},
220                     new String JavaDoc[]{});
221          }
222          catch (JMException JavaDoc jme)
223          {
224             getLog().error("Could not stop JSR-77 JDBC-DataSource: " + lDataSource, jme);
225          }
226       }
227       // No component behind therefore just do it as it is started
228
mState.setState(2);
229    }
230
231    /**
232     * @todo Listener cannot be used right now because there is no MBean associated
233     * to it and therefore no state management possible but currently it stays
234     * StateManageable to save the code.
235     */

236    public void postCreation()
237    {
238       sendNotification(NotificationConstants.OBJECT_DELETED, "JDBC Resource Resource deleted");
239    }
240
241    /**
242     * @todo Listener cannot be used right now because there is no MBean associated
243     * to it and therefore no state management possible but currently it stays
244     * StateManageable to save the code.
245     */

246    public void preDestruction()
247    {
248       sendNotification(NotificationConstants.OBJECT_CREATED, "JDBC Resource Resource created");
249    }
250
251    // javax.management.j2ee.JDBCResource implementation ---------------------
252

253    /**
254     * @jmx:managed-attribute
255     */

256    public String JavaDoc[] getjdbcDataSources()
257    {
258       return (String JavaDoc[]) mDataSources.toArray(new String JavaDoc[mDataSources.size()]);
259    }
260
261    /**
262     * @jmx:managed-operation
263     */

264    public String JavaDoc getjdbcDataSource(int pIndex)
265    {
266       if (pIndex >= 0 && pIndex < mDataSources.size())
267       {
268          return (String JavaDoc) mDataSources.get(pIndex);
269       }
270       else
271       {
272          return null;
273       }
274    }
275
276    // J2EEManagedObjectMBean implementation -------------------------
277

278    public void addChild(ObjectName JavaDoc pChild)
279    {
280       String JavaDoc lType = J2EEManagedObject.getType(pChild);
281       if (J2EETypeConstants.JDBCDataSource.equals(lType))
282       {
283          mDataSources.add(pChild.getCanonicalName());
284       }
285    }
286
287    public void removeChild(ObjectName JavaDoc pChild)
288    {
289       String JavaDoc lType = J2EEManagedObject.getType(pChild);
290       if (J2EETypeConstants.JDBCDataSource.equals(lType))
291       {
292          mDataSources.remove(pChild.getCanonicalName());
293       }
294    }
295
296    // java.lang.Object overrides ------------------------------------
297

298    public String JavaDoc toString()
299    {
300       return "JDBCResource { " + super.toString() + " } [ " +
301               "Datasources: " + mDataSources +
302               " ]";
303    }
304
305    // Package protected ---------------------------------------------
306

307    // Protected -----------------------------------------------------
308

309    // Private -------------------------------------------------------
310

311    // Inner classes -------------------------------------------------
312
}
313
Popular Tags