KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Hashtable JavaDoc;
31 import java.util.Set JavaDoc;
32
33 /**
34  * Root class of the JBoss JSR-77 implementation of
35  * {@link org.jboss.management.j2ee.JDBCDataSource JDBCDataSource}.
36  *
37  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
38  * @version $Revision: 40550 $
39  */

40 public class JDBCDataSource extends J2EEManagedObject
41    implements JDBCDataSourceMBean
42 {
43    // Constants -----------------------------------------------------
44
private static Logger log = Logger.getLogger(JDBCDataSource.class);
45
46    // Attributes ----------------------------------------------------
47

48    private StateManagement mState;
49    private ObjectName JavaDoc mService;
50    private ObjectName JavaDoc mJdbcDriver;
51
52    // Static --------------------------------------------------------
53

54    public static ObjectName JavaDoc create(MBeanServer JavaDoc pServer, String JavaDoc pName, ObjectName JavaDoc pService)
55    {
56       ObjectName JavaDoc lServer = null;
57       try
58       {
59          lServer = (ObjectName JavaDoc) pServer.queryNames(new ObjectName JavaDoc(J2EEDomain.getDomainName() + ":" +
60                  J2EEManagedObject.TYPE + "=" + J2EETypeConstants.J2EEServer + "," +
61                  "*"),
62                  null).iterator().next();
63       }
64       catch (Exception JavaDoc e)
65       {
66          log.error("Could not locate JSR-77 Server: " + pName, e);
67          // Return because without the JDBC manager go on does not work
68
return null;
69       }
70       // First create its parent the JDBC resource
71
ObjectName JavaDoc lJDBC = null;
72       try
73       {
74          // Check if the JDBC Manager exists and if not create one
75
Set JavaDoc lNames = pServer.queryNames(new ObjectName JavaDoc(J2EEDomain.getDomainName() + ":" +
76                  J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JDBCResource + "," +
77                  "*"),
78                  null);
79          if (lNames.isEmpty())
80          {
81             // Now create the JDBC Manager
82
lJDBC = JDBCResource.create(pServer, "JDBC");
83          }
84          else
85          {
86             lJDBC = (ObjectName JavaDoc) lNames.iterator().next();
87          }
88       }
89       catch (Exception JavaDoc e)
90       {
91          log.error("Could not create JSR-77 JDBC Manager", e);
92          // Return because without the JDBC manager go on does not work
93
return null;
94       }
95
96       try
97       {
98          //AS ToDo: Replace any ':' by '~' do avoid ObjectName conflicts for now
99
//AS FixMe: look for a solution
100
pName = pName.replace(':', '~');
101          // Now create the JNDI Representant
102
return pServer.createMBean("org.jboss.management.j2ee.JDBCDataSource",
103                  null,
104                  new Object JavaDoc[]{
105                     pName,
106                     lJDBC,
107                     pService
108                  },
109                  new String JavaDoc[]{
110                     String JavaDoc.class.getName(),
111                     ObjectName JavaDoc.class.getName(),
112                     ObjectName JavaDoc.class.getName()
113                  }).getObjectName();
114       }
115       catch (Exception JavaDoc e)
116       {
117          log.error("Could not create JSR-77 JDBC DataSource: " + pName, e);
118          return null;
119       }
120    }
121
122    public static void destroy(MBeanServer JavaDoc pServer, String JavaDoc pName)
123    {
124       try
125       {
126          J2EEManagedObject.removeObject(pServer,
127                  J2EEDomain.getDomainName() + ":" +
128                  J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JDBCDataSource + "," +
129                  "name=" + pName + "," +
130                  "*");
131          // Now let us try to destroy the JDBC Manager
132
JDBCResource.destroy(pServer, "JDBC");
133       }
134       catch (Exception JavaDoc e)
135       {
136          log.error("Could not destroy JSR-77 JDBC DataSource: " + pName, e);
137       }
138    }
139
140    // Constructors --------------------------------------------------
141

142    /**
143     * @param pName Name of the JDBCDataSource
144     * @throws MalformedObjectNameException
145     * @throws InvalidParentException
146     */

147    public JDBCDataSource(String JavaDoc pName, ObjectName JavaDoc pServer, ObjectName JavaDoc pService)
148            throws
149            MalformedObjectNameException JavaDoc,
150            InvalidParentException
151    {
152       super(J2EETypeConstants.JDBCDataSource, pName, pServer);
153       mService = pService;
154       mState = new StateManagement(this);
155    }
156
157    // Public --------------------------------------------------------
158

159    // javax.managment.j2ee.EventProvider implementation -------------
160

161    public String JavaDoc[] getEventTypes()
162    {
163       return StateManagement.stateTypes;
164    }
165
166    public String JavaDoc getEventType(int pIndex)
167    {
168       if (pIndex >= 0 && pIndex < StateManagement.stateTypes.length)
169       {
170          return StateManagement.stateTypes[pIndex];
171       }
172       else
173       {
174          return null;
175       }
176    }
177
178    // javax.management.j2ee.StateManageable implementation ----------
179

180    public long getStartTime()
181    {
182       return mState.getStartTime();
183    }
184
185    public int getState()
186    {
187       return mState.getState();
188    }
189    public String JavaDoc getStateString()
190    {
191       return mState.getStateString();
192    }
193
194    public void mejbStart()
195    {
196       try
197       {
198          getServer().invoke(mService,
199                  "start",
200                  new Object JavaDoc[]{},
201                  new String JavaDoc[]{});
202       }
203       catch (Exception JavaDoc e)
204       {
205          getLog().error("start failed", e);
206       }
207    }
208
209    public void mejbStartRecursive()
210    {
211       mejbStart();
212    }
213
214    public void mejbStop()
215    {
216       try
217       {
218          getServer().invoke(mService,
219                  "stop",
220                  new Object JavaDoc[]{},
221                  new String JavaDoc[]{});
222       }
223       catch (Exception JavaDoc e)
224       {
225          getLog().error("Stop of JDBCDataSource failed", e);
226       }
227    }
228
229    public void postCreation()
230    {
231       try
232       {
233          getServer().addNotificationListener(mService, mState, null, null);
234       }
235       catch (JMException JavaDoc jme)
236       {
237          getLog().debug("Could not add listener at target service", jme);
238       }
239       sendNotification(NotificationConstants.OBJECT_CREATED, "JDBC DataSource Resource deleted");
240    }
241
242    public void preDestruction()
243    {
244       sendNotification(NotificationConstants.OBJECT_DELETED, "JDBC DataSource Resource deleted");
245       // Remove the listener of the target MBean
246
try
247       {
248          getServer().removeNotificationListener(mService, mState);
249       }
250       catch (JMException JavaDoc jme)
251       {
252          // When the service is not available anymore then just ignore the exception
253
}
254    }
255
256    // javax.management.j2ee.JDBCDataSource implementation -----------------
257

258    public ObjectName JavaDoc getJdbcDriver()
259    {
260       return mJdbcDriver;
261    }
262
263    // java.lang.Object overrides ------------------------------------
264

265    public String JavaDoc toString()
266    {
267       return "JDBCDatasource { " + super.toString() + " } [ " +
268               " ]";
269    }
270
271    // Package protected ---------------------------------------------
272

273    // Protected -----------------------------------------------------
274

275    /**
276     * @return A hashtable with the JDBC-Resource and J2EE-Server as parent
277     */

278    protected Hashtable JavaDoc getParentKeys(ObjectName JavaDoc pParent)
279    {
280       Hashtable JavaDoc lReturn = new Hashtable JavaDoc();
281       Hashtable JavaDoc lProperties = pParent.getKeyPropertyList();
282       lReturn.put(J2EETypeConstants.JDBCResource, lProperties.get("name"));
283       // J2EE-Server is already parent of J2EE-Application therefore lookup
284
// the name by the J2EE-Server type
285
lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get(J2EETypeConstants.J2EEServer));
286
287       return lReturn;
288    }
289
290    // Private -------------------------------------------------------
291

292    // Inner classes -------------------------------------------------
293
}
294
Popular Tags