KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > ejb > TestDataSourceBean


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.test.jmx.ejb;
23
24 import java.util.Collection JavaDoc;
25 import javax.ejb.*;
26 import javax.sql.*;
27 import java.sql.*;
28 import javax.naming.*;
29
30
31 /**
32  * This is a session bean whose only purpose is to look for and test datasources. It is an example of how to use the EJBDoclet tags.
33  *
34  * @ejb:stateless-session
35  * @ejb:ejb-name test/jmx/TestDataSource
36  * @ejb:jndi-name ejb/test/jmx/TestDataSource
37  * @ejb:security-role-ref admin Administrator
38  * @ejb:permission Teller
39  * @ejb:permission Administrator
40  * @ejb:transaction Required
41  * @ejb:transaction-type Container
42  *
43  * JBoss specific
44  * @jboss:container-configuration Standard Stateless SessionBean
45  *
46  */

47 public class TestDataSourceBean
48    implements SessionBean
49 {
50    // Public --------------------------------------------------------
51
/**
52     * The <code>testDataSource</code> method looks for the datasource at the supplied name
53     * and tests if it can supply a working connection.
54     *
55     * @param dsName a <code>String</code> value
56     * @ejb:interface-method type="remote"
57     */

58    public void testDataSource(String JavaDoc dsName)
59    {
60       try
61       {
62          InitialContext ctx = new InitialContext();
63          DataSource ds = (DataSource)ctx.lookup(dsName);
64          if (ds == null) {
65             throw new Exception JavaDoc("DataSource lookup was null");
66          }
67          Connection c = ds.getConnection();
68          if (c == null) {
69              throw new Exception JavaDoc("Connection was null!!");
70          }
71          DatabaseMetaData dmd = c.getMetaData();
72          ResultSet rs = dmd.getTables(null, null, "%", null);
73          c.close();
74       } catch (Exception JavaDoc e)
75       {
76          throw new EJBException(e);
77       }
78    }
79
80    /**
81     * The <code>isBound</code> method checks to see if the supplied name is bound in jndi.
82     *
83     * @param name a <code>String</code> value
84     * @return a <code>boolean</code> value
85     * @ejb:interface-method type="remote"
86     */

87    public boolean isBound(String JavaDoc name)
88    {
89       try
90       {
91          InitialContext ctx = new InitialContext();
92          Object JavaDoc ds = ctx.lookup(name);
93          if (ds == null) {
94             return false;
95          }
96          return true;
97       }
98       catch (NamingException e)
99       {
100          return false;
101       } // end of catch
102

103    }
104    
105    /**
106     * Create.
107     */

108    public void ejbCreate()
109       throws CreateException
110    {
111    }
112    
113    // SessionBean implementation ------------------------------------
114
public void ejbActivate() {}
115    public void ejbPassivate() {}
116    public void setSessionContext(SessionContext ctx) {}
117    
118    /**
119     * Remove
120     *
121     */

122    public void ejbRemove() {}
123
124 }
125
Popular Tags