01: /*
02: * CoadunationUtil: The coadunation util library.
03: * Copyright (C) 2007 Rift IT Contracting
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18: *
19: * ProxyConnection.java
20: */
21:
22: // package path
23: package com.rift.coad.util.connection;
24:
25: // java imports
26: import java.util.Map;
27: import java.util.concurrent.ConcurrentHashMap;
28: import javax.naming.Context;
29:
30: // logging import
31: import org.apache.log4j.Logger;
32:
33: /**
34: * This class manages a proxy connection.
35: *
36: * @author Brett Chaldecott
37: */
38: public class ProxyConnection implements Connection {
39:
40: // the logger reference
41: protected static Logger log = Logger
42: .getLogger(ProxyConnection.class.getName());
43:
44: // private member variables
45: private Context context = null;
46: private String jndiURL = null;
47: private Object ref = null;
48:
49: /**
50: * Creates a new instance of ProxyConnection
51: *
52: * @param context The context object responsible for returning information.
53: * @param jndiURL The url identifying the object.
54: */
55: public ProxyConnection(Context context, String jndiURL) {
56: this .context = context;
57: this .jndiURL = jndiURL;
58: }
59:
60: /**
61: * This object returns the connection to the object.
62: *
63: * @return The retrieve connection to the object.
64: * @param type The type of object to narrow.
65: * @exception ConnectionException
66: * @exception java.lang.ClassCastException
67: */
68: public synchronized Object getConnection(Class type)
69: throws ConnectionException, java.lang.ClassCastException {
70: try {
71: if (ref == null) {
72: ref = context.lookup(jndiURL);
73: }
74: return ref;
75: } catch (Exception ex) {
76: log.error("Failed to retrieve a connection : "
77: + ex.getMessage(), ex);
78: throw new ConnectionException(
79: "Failed to retrieve a connection : "
80: + ex.getMessage(), ex);
81: }
82: }
83:
84: /**
85: * This method returns true if the object being passed contains a local
86: * url.
87: *
88: * @return TRUE if a local URL, FALSE if not.
89: * @param jndiURL The string url to perform the check on.
90: */
91: public static boolean isLocalURL(String jndiURL) {
92: if (jndiURL.trim().indexOf("java:comp") == 0) {
93: return true;
94: }
95: return false;
96: }
97: }
|