KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > helper > EJBHelper


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: EJBHelper.java 827 2006-07-10 14:12:17Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.helper;
26
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29
30 /**
31  * This helper is used to do ejb operations.
32  * @author Eduardo Studzinski Estima de Castro
33  * @author Gisele Pinheiro Souza
34  */

35 public final class EJBHelper {
36
37     /**
38      * Helper should have a private constructor.
39      */

40     private EJBHelper() {
41     }
42
43     /**
44      * Constant used to identify a remote interface.
45      */

46     public static final String JavaDoc ITF_REMOTE = "@Remote";
47
48     /**
49      * Constant used to identify a local interface.
50      */

51     public static final String JavaDoc ITF_LOCAL = "@Local";
52
53     /**
54      * Gets a remote instance of a bean.
55      * @param <E> bean element type
56      * @param beanClass class of the bean
57      * @param beanInterface interface of the bean
58      * @return bean instance
59      * @throws Exception if occurs a problem in the instance invocation
60      */

61     @SuppressWarnings JavaDoc("unchecked")
62     public static synchronized <E> E getBeanRemoteInstance(final Class JavaDoc beanClass, final Class JavaDoc<E> beanInterface) throws Exception JavaDoc {
63         System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
64                 "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
65
66         Context JavaDoc initialContext = new InitialContext JavaDoc();
67         E clBean = (E) initialContext.lookup(beanClass.getName() + "_" + beanInterface.getName() + ITF_REMOTE);
68         return clBean;
69     }
70
71     /**
72      * Gets a local instance of a bean.
73      * @param <E> bean element type
74      * @param beanClass class of the bean
75      * @param beanInterface interface of the bean
76      * @return bean instance
77      * @throws Exception if occurs a problem in the instance invocation.
78      */

79     @SuppressWarnings JavaDoc("unchecked")
80     public static synchronized <E> E getBeanLocalInstance(final Class JavaDoc beanClass, final Class JavaDoc<E> beanInterface) throws Exception JavaDoc {
81         System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
82                 "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
83
84         Context JavaDoc initialContext = new InitialContext JavaDoc();
85         E clBean = (E) initialContext.lookup(beanClass.getName() + "_" + beanInterface.getName() + ITF_LOCAL);
86         return clBean;
87     }
88
89     /**
90      * Gets an instance of a bean by the mappedName.
91      * @param <E> the bean element type.
92      * @param mappedName the mappedName.
93      * @return the bean.
94      * @throws Exception f occurs a problem in the instance invocation.
95      */

96     @SuppressWarnings JavaDoc("unchecked")
97     public static synchronized <E> E getBeanByMappedName(final String JavaDoc mappedName) throws Exception JavaDoc {
98         System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
99                 "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
100
101         Context JavaDoc initialContext = new InitialContext JavaDoc();
102         E clBean = (E) initialContext.lookup(mappedName);
103         return clBean;
104     }
105
106 }
107
Popular Tags