001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.j2ee.appclient;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.naming.*;
034: import com.caucho.util.L10N;
035:
036: import javax.annotation.PostConstruct;
037: import javax.naming.*;
038: import javax.rmi.PortableRemoteObject;
039: import java.util.Hashtable;
040: import java.util.logging.Logger;
041:
042: /**
043: * Configuration for the ejb-ref.
044: *
045: * An ejb-ref is used to make an ejb available within the environment
046: * in which the ejb-ref is declared.
047: */
048: public class ClientEjbRef implements ObjectProxy {
049: private static final L10N L = new L10N(ClientEjbRef.class);
050: private static final Logger log = Logger
051: .getLogger(ClientEjbRef.class.getName());
052:
053: private Context _ic;
054:
055: private String _ejbRefName;
056: private String _type;
057: private Class _home;
058: private Class _remote;
059: private String _jndiName;
060: private String _ejbLink;
061:
062: public ClientEjbRef(Context ic) {
063: _ic = ic;
064: }
065:
066: public void setId(String id) {
067: }
068:
069: public void setDescription(String description) {
070: }
071:
072: /**
073: * Sets the name to use in the local jndi context.
074: * This is the jndi lookup name that code uses to obtain the home for
075: * the bean when doing a jndi lookup.
076: *
077: * <pre>
078: * <ejb-ref-name>ejb/Gryffindor</ejb-ref-name>
079: * ...
080: * (new InitialContext()).lookup("java:comp/env/ejb/Gryffindor");
081: * </pre>
082: */
083: public void setEjbRefName(String name) {
084: _ejbRefName = name;
085: }
086:
087: /**
088: * Returns the ejb name.
089: */
090: public String getEjbRefName() {
091: return _ejbRefName;
092: }
093:
094: public void setEjbRefType(String type) {
095: _type = type;
096: }
097:
098: public void setHome(Class home) {
099: _home = home;
100: }
101:
102: /**
103: * Returns the home class.
104: */
105: public Class getHome() {
106: return _home;
107: }
108:
109: public void setRemote(Class remote) {
110: _remote = remote;
111: }
112:
113: /**
114: * Returns the remote class.
115: */
116: public Class getRemote() {
117: return _remote;
118: }
119:
120: /**
121: * Sets the canonical jndi name to use to find the bean that
122: * is the target of the reference.
123: * For remote beans, a <jndi-link> {@link com.caucho.naming.LinkProxy} is
124: * used to link the local jndi context referred to in this name to
125: * a remote context.
126: */
127: public void setJndiName(String jndiName) {
128: _jndiName = jndiName;
129: }
130:
131: /**
132: * Set the target of the reference, an alternative to {@link #setJndiName(String)}.
133: * The format of the ejbLink is "bean", or "jarname#bean", where <i>bean</i> is the
134: * ejb-name of a bean within the same enterprise application, and <i>jarname</i>
135: * further qualifies the identity of the target.
136: */
137: public void setEjbLink(String ejbLink) {
138: _ejbLink = ejbLink;
139: }
140:
141: @PostConstruct
142: public void init() throws Exception {
143: if (_ejbRefName == null)
144: throw new ConfigException(L.l("{0} is required",
145: "<ejb-ref-name>"));
146:
147: _ejbRefName = Jndi.getFullName(_ejbRefName);
148:
149: if (_ejbLink != null) {
150: // TCK: ejb30.persistence.annotations.entity
151: Jndi.bindDeepShort(_ejbRefName, this );
152: }
153:
154: ClientEjbRefContext context = ClientEjbRefContext.createLocal();
155:
156: context.add(this );
157: }
158:
159: /**
160: * Creates the object from the proxy.
161: *
162: * @param env the calling environment
163: *
164: * @return the object named by the proxy.
165: */
166: public Object createObject(Hashtable env) throws NamingException {
167: Context context = new InitialContext();
168:
169: Object value = context.lookup(Jndi.getFullName(_ejbLink));
170:
171: if (_home != null)
172: return PortableRemoteObject.narrow(value, _home);
173: else if (_remote != null)
174: return PortableRemoteObject.narrow(value, _remote);
175: else
176: return value;
177: }
178:
179: public String toString() {
180: return "ClientEjbRef[" + _ejbRefName + ", " + _ejbLink + ", "
181: + _jndiName + "]";
182: }
183: }
|