KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tutorial > jboss_resource_ref > bean > TestENCBean


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.tutorial.jboss_resource_ref.bean;
23
24 import javax.naming.Context JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27
28 import javax.ejb.Remove JavaDoc;
29
30 import org.apache.log4j.Logger;
31 import org.jboss.ejb3.Container;
32
33 /** A bean that does nothing but access resources from the ENC
34  to test ENC usage.
35
36  @author Scott.Stark@jboss.org
37  @version $Revision: 39990 $
38  */

39 public class TestENCBean implements TestENC
40 {
41    Logger log = Logger.getLogger(getClass());
42
43    public void accessENC() throws NamingException JavaDoc
44    {
45       // Obtain the enterprise beans environment naming context.
46
Context JavaDoc initCtx = new InitialContext JavaDoc();
47       Context JavaDoc myEnv = (Context JavaDoc) initCtx.lookup(Container.ENC_CTX_NAME + "/env");
48       
49       // This bean should have the full ENC setup of the ENCBean
50
testJdbcDataSource(initCtx, myEnv);
51       testMail(initCtx, myEnv);
52       testJMS(initCtx, myEnv);
53       testResourceEnvEntries(initCtx, myEnv);
54    }
55
56    private void testJdbcDataSource(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
57    {
58       // JDBC DataSource
59
Object JavaDoc obj = myEnv.lookup("jdbc/DefaultDS");
60       if ((obj instanceof javax.sql.DataSource JavaDoc) == false)
61          throw new NamingException JavaDoc("jdbc/DefaultDS is not a javax.sql.DataSource");
62       log.info("Found data source resource ref");
63    }
64
65    private void testMail(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
66    {
67       // JavaMail Session
68
Object JavaDoc obj = myEnv.lookup("mail/DefaultMail");
69       if ((obj instanceof javax.mail.Session JavaDoc) == false)
70          throw new NamingException JavaDoc("DefaultMail is not a javax.mail.Session");
71       log.info("Found mail resource ref");
72    }
73
74    private void testJMS(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
75    {
76       // JavaMail Session
77
Object JavaDoc obj = myEnv.lookup("jms/QueFactory");
78       if ((obj instanceof javax.jms.QueueConnectionFactory JavaDoc) == false)
79          throw new NamingException JavaDoc("mail/DefaultMail is not a javax.jms.QueueConnectionFactory");
80       log.info("Found jms queue resource ref");
81    }
82
83    private void testResourceEnvEntries(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
84    {
85       Object JavaDoc obj = myEnv.lookup("res/aQueue");
86       if ((obj instanceof javax.jms.Queue JavaDoc) == false)
87          throw new NamingException JavaDoc("res/aQueue is not a javax.jms.Queue");
88       log.info("Found jms queue resource env ref");
89    }
90    
91    @Remove JavaDoc
92    public void remove()
93    {
94       
95    }
96 }
97
Popular Tags