001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.server.j2ee.ejb;
046:
047: import org.obe.client.api.rmi.JNDIHelper;
048:
049: import javax.ejb.EJBHome;
050: import javax.ejb.EJBLocalHome;
051: import javax.naming.Context;
052: import javax.naming.NamingException;
053: import javax.rmi.PortableRemoteObject;
054: import javax.sql.DataSource;
055: import java.sql.Connection;
056: import java.sql.SQLException;
057: import java.util.Collections;
058: import java.util.HashMap;
059: import java.util.Map;
060:
061: /**
062: * This class defines EJBHelper methods for server side use.
063: *
064: * @author Adrian Price
065: */
066: public class EJBHelper {
067: private static final Map JNDI_OBJECTS = Collections
068: .synchronizedMap(new HashMap());
069: private static final Map EJB_HOMES = Collections
070: .synchronizedMap(new HashMap());
071: private static final ThreadLocal JNDI_CONTEXT = new ThreadLocal();
072: private static SequenceLocalHome _seqHome;
073: private static JMSMessageProducerLocalHome _msgProdHome;
074: private static DataSource dataSource;
075:
076: public static Object getJndiObject(String jndiName) {
077: Object obj = JNDI_OBJECTS.get(jndiName);
078: if (obj == null) {
079: try {
080: obj = getInitialContext().lookup(jndiName);
081: } catch (NamingException e) {
082: throw new WMRuntimeException(e);
083: }
084: JNDI_OBJECTS.put(jndiName, obj);
085: }
086: return obj;
087: }
088:
089: public static Connection getConnection() throws SQLException {
090: return getDataSource().getConnection();
091: }
092:
093: public static DataSource getDataSource() {
094: if (dataSource == null) {
095: try {
096: dataSource = (DataSource) getInitialContext().lookup(
097: JNDIHelper.EJB_DATA_SOURCE);
098: } catch (NamingException e) {
099: try {
100: dataSource = (DataSource) getInitialContext()
101: .lookup(JNDIHelper.DEFAULT_DATA_SOURCE);
102: } catch (NamingException e2) {
103: throw new WMRuntimeException(e2);
104: }
105: }
106: }
107: return dataSource;
108: }
109:
110: public static EJBHome getEJBHome(String jndiName, Class homeClass) {
111: EJBHome home = (EJBHome) EJB_HOMES.get(jndiName);
112: if (home == null) {
113: try {
114: Object obj = getInitialContext().lookup(jndiName);
115: home = (EJBHome) PortableRemoteObject.narrow(obj,
116: homeClass);
117: } catch (NamingException e) {
118: throw new WMRuntimeException(e);
119: } catch (ClassCastException e) {
120: throw new WMRuntimeException(e);
121: }
122: EJB_HOMES.put(jndiName, home);
123: }
124: return home;
125: }
126:
127: public static EJBLocalHome getEJBLocalHome(String jndiName) {
128: EJBLocalHome home = (EJBLocalHome) EJB_HOMES.get(jndiName);
129: if (home == null) {
130: try {
131: home = (EJBLocalHome) getInitialContext().lookup(
132: jndiName);
133: } catch (NamingException e) {
134: throw new WMRuntimeException(e);
135: } catch (ClassCastException e) {
136: throw new WMRuntimeException(e);
137: }
138: EJB_HOMES.put(jndiName, home);
139: }
140: return home;
141: }
142:
143: public static JMSMessageProducerLocalHome getJMSMessageProducerHome() {
144: if (_msgProdHome == null) {
145: try {
146: _msgProdHome = (JMSMessageProducerLocalHome) getEJBLocalHome(JMSMessageProducerLocalHome.COMP_NAME);
147: } catch (Exception e) {
148: _msgProdHome = (JMSMessageProducerLocalHome) getEJBLocalHome(JMSMessageProducerLocalHome.JNDI_NAME);
149: }
150: }
151: return _msgProdHome;
152: }
153:
154: public static SequenceLocalHome getSequenceHome() {
155: if (_seqHome == null) {
156: try {
157: _seqHome = (SequenceLocalHome) getEJBLocalHome(SequenceLocalHome.COMP_NAME);
158: } catch (Exception e) {
159: _seqHome = (SequenceLocalHome) getEJBLocalHome(SequenceLocalHome.JNDI_NAME);
160: }
161: }
162: return _seqHome;
163: }
164:
165: private static Context getInitialContext() throws NamingException {
166: Context ctx;
167: if ((ctx = (Context) JNDI_CONTEXT.get()) == null)
168: JNDI_CONTEXT.set(ctx = JNDIHelper.getInitialContext());
169: return ctx;
170: }
171:
172: // Prevent instantiation.
173: protected EJBHelper() {
174: }
175: }
|