KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > EjbReferenceDescriptor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.deployment;
24
25 import com.sun.enterprise.deployment.types.EjbReference;
26 import com.sun.enterprise.util.LocalStringManagerImpl;
27
28 /**
29  * An object representing a link to another ejb.
30  *
31  * @author Jerome Dochez
32  *
33 */

34
35 public class EjbReferenceDescriptor extends EnvironmentProperty implements com.sun.enterprise.deployment.types.EjbReference, NamedDescriptor {
36
37     // In case the reference has been resolved, the ejbDescriptor will
38
// be the referenced ejb.
39
private EjbDescriptor ejbDescriptor;
40     
41     private static LocalStringManagerImpl localStrings =
42         new LocalStringManagerImpl(EjbReferenceDescriptor.class);
43     
44     // We need the referring bundle for what ?
45
private BundleDescriptor referringBundle;
46
47     // bean type and interfaces names
48
private String JavaDoc refType=null;
49     private String JavaDoc refHomeIntf=null;
50     private String JavaDoc refIntf=null;
51     
52     // local-ref or remote-ref
53
private boolean isLocal=false;
54
55     /**
56      * holds the ejb-link value associated to this ejb reference before the
57      * ejbs were resolved
58      */

59     private String JavaDoc ejbLink=null;
60
61     /**
62      * copy constructor
63      *
64      * @param other handle to other EjbReferenceDescriptor to clone
65      */

66     public EjbReferenceDescriptor(EjbReferenceDescriptor other) {
67     super(other);
68     isLocal = other.isLocal; // boolean
69
refType = other.refType; // String
70
refHomeIntf = other.refHomeIntf; // String
71
refIntf = other.refIntf; // String
72
ejbLink = other.ejbLink; // String
73
referringBundle = other.referringBundle; // copy as-is
74
ejbDescriptor = other.ejbDescriptor;
75     if (ejbDescriptor != null) {
76             ejbDescriptor.addEjbReferencer(this); // ???
77
}
78     }
79
80     /**
81      * Construct an remote ejb reference to the given ejb descriptor
82      * with the given name and descriptor of the reference.
83      *
84      * @param name the ejb-ref name as used in the referencing bean
85      * @param optional description
86      * @param ejbDescriptor of the referenced bean
87      */

88     public EjbReferenceDescriptor(String JavaDoc name, String JavaDoc description, EjbDescriptor ejbDescriptor) {
89     super(name, "", description);
90     this.setEjbDescriptor(ejbDescriptor);
91     }
92     
93     /**
94      * constructs an local or remote ejb reference to the given ejb descriptor,
95      * the desciption and the name of the reference
96      *
97      * @param name is the name of the reference
98      * @param description is a human readable description of the reference
99      * @param ejbDescriptor the referenced EJB
100      * @param isLocal true if the reference uses the local interfaces
101      */

102     public EjbReferenceDescriptor(String JavaDoc name, String JavaDoc description, EjbDescriptor ejbDescriptor, boolean isLocal) {
103         super(name, "", description);
104         this.isLocal = isLocal;
105         this.setEjbDescriptor(ejbDescriptor);
106     }
107     
108     /**
109     * Constructs a reference in the extrernal state.
110     */

111     
112     public EjbReferenceDescriptor() {
113     }
114
115     /**
116      * Set the referring bundle, i.e. the bundle within which this
117      * EJB reference is declared.
118      */

119     public void setReferringBundleDescriptor(BundleDescriptor referringBundle)
120     {
121     this.referringBundle = referringBundle;
122     }
123
124     /**
125      * Get the referring bundle, i.e. the bundle within which this
126      * EJB reference is declared.
127      */

128     public BundleDescriptor getReferringBundleDescriptor()
129     {
130     return referringBundle;
131     }
132     
133     /**
134      * Sets the ejb descriptor to which I refer.
135      * @param the ejb descriptor referenced, null if it is unknow at this time
136      */

137     public void setEjbDescriptor(EjbDescriptor ejbDescriptor) {
138     if (this.ejbDescriptor != null) {
139             this.ejbDescriptor.removeEjbReferencer(this); // remove previous referencer
140
}
141         this.ejbDescriptor=ejbDescriptor;
142     if (ejbDescriptor!=null) {
143             ejbDescriptor.addEjbReferencer(this);
144             if (isLocal()) {
145                 if (!ejbDescriptor.isLocalInterfacesSupported() &&
146                     !ejbDescriptor.isLocalBusinessInterfacesSupported()) {
147                      throw new RuntimeException JavaDoc(localStrings.getLocalString(
148                      "entreprise.deployment.invalidLocalInterfaceReference",
149                      "Trying to set an ejb-local-ref on an EJB while the EJB does not define local interfaces"));
150                 }
151             } else {
152                 if (!ejbDescriptor.isRemoteInterfacesSupported() &&
153                     !ejbDescriptor.isRemoteBusinessInterfacesSupported()) {
154                     throw new RuntimeException JavaDoc(localStrings.getLocalString(
155                     "entreprise.deployment.invalidRemoteInterfaceReference",
156                     "Trying to set an ejb-ref on an EJB, while the EJB does not define remote interfaces"));
157                 }
158             }
159     }
160     }
161
162     
163     /**
164     * Sets the jndi name of the bean tyo whoch I am referring.*/

165     
166     public void setJndiName(String JavaDoc jndiName) {
167     this.setValue(jndiName);
168     }
169     
170     /** return true if I know the name of the ejb to which I refer.
171     */

172     
173     public boolean isLinked() {
174     return ejbLink!=null;
175     }
176     
177     /**
178      * @return the name of the ejb to which I refer
179     */

180     
181     public String JavaDoc getLinkName() {
182     if (ejbDescriptor==null) {
183             return ejbLink;
184         } else {
185             if (ejbLink != null && ejbLink.length()!=0) {
186                 return ejbLink;
187             }
188         return ejbDescriptor.getName();
189     }
190     }
191
192     /**
193      * Sets the name of the ejb to which I refer.
194      */

195     public void setLinkName(String JavaDoc linkName) {
196         ejbLink = linkName;
197     }
198     /**
199      * return the jndi name of the bean to which I refer.
200      */

201     
202     public String JavaDoc getJndiName() {
203         String JavaDoc jndiName = this.getValue();
204         if( isLocal() ) {
205             // mapped-name has no meaning for the local ejb view. ejb-link
206
// should be used to resolve any ambiguities about the target
207
// local ejb.
208
return jndiName;
209         } else {
210             return (jndiName != null && ! jndiName.equals("")) ?
211                 jndiName : getMappedName();
212         }
213     }
214     
215     /**
216     * Return the jndi name of the bean to which I refer.
217     */

218     
219     public String JavaDoc getValue() {
220     if (ejbDescriptor == null) {
221             return super.getValue();
222     } else {
223             if (isLocal()) {
224                 return super.getValue();
225             } else {
226                 return ejbDescriptor.getJndiName();
227             }
228         }
229     }
230         
231     /** return the ejb to whoch I refer.
232     */

233     
234     public EjbDescriptor getEjbDescriptor() {
235     return ejbDescriptor;
236     }
237     
238     /**
239      * @return true if the EJB reference uses the local interfaces of the EJB
240      */

241     public boolean isLocal() {
242         return isLocal;
243     }
244     
245     /**
246      * Set whether this EJB Reference uses local interfaces or remote
247      * @param local true if the EJB reference use local interfaces
248      */

249     public void setLocal(boolean local) {
250         this.isLocal = local;
251     }
252     
253     /**
254     * Retusn the type of the ejb to whioch I refer.
255     */

256     
257     public String JavaDoc getType() {
258         if (ejbDescriptor==null) {
259             return refType;
260         } else {
261             return ejbDescriptor.getType();
262         }
263     }
264     
265     /** Assigns the type of the ejb to whcoih I refer.
266     */

267     public void setType(String JavaDoc type) {
268     refType=type;
269     }
270
271     public String JavaDoc getInjectResourceType() {
272         return isEJB30ClientView() ?
273             getEjbInterface() : getEjbHomeInterface();
274     }
275
276     public void setInjectResourceType(String JavaDoc resourceType) {
277         if (isEJB30ClientView()) {
278             setEjbInterface(resourceType);
279         } else {
280             setEjbHomeInterface(resourceType);
281         }
282     }
283     
284     /**
285       * Gets the home classname of the referee EJB.
286       */

287     public String JavaDoc getHomeClassName() {
288         return refHomeIntf;
289     }
290     
291     /**
292      * Sets the home classname of the bean to whcioh I refer.
293      */

294
295     public void setHomeClassName(String JavaDoc homeClassName) {
296         refHomeIntf = homeClassName;
297     }
298     
299     /**
300      * @return the bean instance interface classname of the referee EJB.
301      */

302     public String JavaDoc getBeanClassName() {
303         return refIntf;
304     }
305     
306     /** Sets the bean instance business interface classname of the bean to which I refer.
307      * this interface is the local object or the remote interfaces depending if the
308      * reference is local or not.
309     */

310     public void setBeanClassName(String JavaDoc remoteClassName) {
311         refIntf = remoteClassName;
312     }
313     
314     /**
315      * Gets the home classname of the referee EJB.
316      * @return the class name of the EJB home.
317      */

318     public String JavaDoc getEjbHomeInterface() {
319         return getHomeClassName();
320     }
321
322     /**
323      * Sets the local or remote home classname of the referee EJB.
324      * @param the class name of the EJB home.
325      */

326     public void setEjbHomeInterface(String JavaDoc homeClassName) {
327         setHomeClassName(homeClassName);
328     }
329     
330     /**
331      * Gets the local or remote interface classname of the referee EJB.
332      * @return the classname of the EJB remote object.
333      */

334     public String JavaDoc getEjbInterface() {
335         return getBeanClassName();
336     }
337     /**
338      * Sets the local or remote bean interface classname of the referee EJB.
339      * @param the classname of the EJB remote object.
340      */

341     public void setEjbInterface(String JavaDoc remoteClassName) {
342         setBeanClassName(remoteClassName);
343     }
344
345     /**
346      * @return true if the EJB reference is a 30 client view
347      */

348     public boolean isEJB30ClientView() {
349         return (getHomeClassName() == null);
350     }
351
352     /** returns a formatted string representing me.
353     */

354     
355     public void print(StringBuffer JavaDoc toStringBuffer) {
356     if (ejbDescriptor!=null) {
357         toStringBuffer.append("Resolved Ejb-Ref ").append(getName()).append("@jndi: ").append(getJndiName()).append(
358                     " - > ").append(ejbDescriptor.getName());
359     } else {
360         toStringBuffer.append("Unresolved Ejb-Ref ").append(getName()).append("@jndi: ").append(getJndiName()).append("@").append(
361                 getEjbHomeInterface()).append("@").append(getEjbInterface()).append("@").append(refType).append("@").append(ejbLink) ;
362     }
363     }
364     
365     /* Equality on name. */
366     public boolean equals(Object JavaDoc object) {
367     if (object instanceof EjbReference) {
368         EjbReference ejbReference = (EjbReference) object;
369         return ejbReference.getName().equals(this.getName());
370     }
371     return false;
372     }
373 }
374
Popular Tags