001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jmx.ejb;
023:
024: import java.util.Collection;
025: import javax.ejb.*;
026: import javax.sql.*;
027: import java.sql.*;
028: import javax.naming.*;
029:
030: /**
031: * 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.
032: *
033: * @ejb:stateless-session
034: * @ejb:ejb-name test/jmx/TestDataSource
035: * @ejb:jndi-name ejb/test/jmx/TestDataSource
036: * @ejb:security-role-ref admin Administrator
037: * @ejb:permission Teller
038: * @ejb:permission Administrator
039: * @ejb:transaction Required
040: * @ejb:transaction-type Container
041: *
042: * JBoss specific
043: * @jboss:container-configuration Standard Stateless SessionBean
044: *
045: */
046: public class TestDataSourceBean implements SessionBean {
047: // Public --------------------------------------------------------
048: /**
049: * The <code>testDataSource</code> method looks for the datasource at the supplied name
050: * and tests if it can supply a working connection.
051: *
052: * @param dsName a <code>String</code> value
053: * @ejb:interface-method type="remote"
054: */
055: public void testDataSource(String dsName) {
056: try {
057: InitialContext ctx = new InitialContext();
058: DataSource ds = (DataSource) ctx.lookup(dsName);
059: if (ds == null) {
060: throw new Exception("DataSource lookup was null");
061: }
062: Connection c = ds.getConnection();
063: if (c == null) {
064: throw new Exception("Connection was null!!");
065: }
066: DatabaseMetaData dmd = c.getMetaData();
067: ResultSet rs = dmd.getTables(null, null, "%", null);
068: c.close();
069: } catch (Exception e) {
070: throw new EJBException(e);
071: }
072: }
073:
074: /**
075: * The <code>isBound</code> method checks to see if the supplied name is bound in jndi.
076: *
077: * @param name a <code>String</code> value
078: * @return a <code>boolean</code> value
079: * @ejb:interface-method type="remote"
080: */
081: public boolean isBound(String name) {
082: try {
083: InitialContext ctx = new InitialContext();
084: Object ds = ctx.lookup(name);
085: if (ds == null) {
086: return false;
087: }
088: return true;
089: } catch (NamingException e) {
090: return false;
091: } // end of catch
092:
093: }
094:
095: /**
096: * Create.
097: */
098: public void ejbCreate() throws CreateException {
099: }
100:
101: // SessionBean implementation ------------------------------------
102: public void ejbActivate() {
103: }
104:
105: public void ejbPassivate() {
106: }
107:
108: public void setSessionContext(SessionContext ctx) {
109: }
110:
111: /**
112: * Remove
113: *
114: */
115: public void ejbRemove() {
116: }
117:
118: }
|