KickJava   Java API By Example, From Geeks To Geeks.

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


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.MessageDestinationReferencer;
26 import java.util.Set JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 /**
30  * Shared implementation for deployment descriptor entities that can refer
31  * to a message destination. Each MessageDestinationReferencer has an
32  * owner. The owner can either be a MessageDestinationReference
33  * or a MessageDrivenBean.
34  *
35  * @author Kenneth Saks
36  *
37 */

38
39 public class MessageDestinationReferencerImpl implements
40     MessageDestinationReferencer {
41
42     // holds the name of the message destination link.
43
private String JavaDoc messageDestinationLinkName=null;
44
45     // In case the reference has been resolved, this points to the
46
// referenced message destination.
47
private MessageDestinationDescriptor messageDestination=null;
48
49     private MessageDestinationReferenceDescriptor ownerMsgDestRef = null;
50     private EjbMessageBeanDescriptor ownerMsgBean = null;
51
52     public MessageDestinationReferencerImpl(MessageDestinationReferencerImpl other) {
53     //super(other);
54
messageDestinationLinkName = other.messageDestinationLinkName; // immutable String
55
messageDestination = other.messageDestination; // copy as-is
56
ownerMsgDestRef = other.ownerMsgDestRef; // copy as-is
57
ownerMsgBean = other.ownerMsgBean; // copy as-is
58
}
59
60     public MessageDestinationReferencerImpl(Descriptor desc) {
61         if( desc instanceof MessageDestinationReferenceDescriptor ) {
62             ownerMsgDestRef = (MessageDestinationReferenceDescriptor) desc;
63         } else if( desc instanceof EjbMessageBeanDescriptor ) {
64             ownerMsgBean = (EjbMessageBeanDescriptor) desc;
65         } else {
66             throw new IllegalArgumentException JavaDoc("Invalid desc = " + desc);
67         }
68     }
69
70     /**
71      * True if the owner is a message destination reference.
72      */

73     public boolean ownedByMessageDestinationRef() {
74         return (ownerMsgDestRef != null);
75     }
76
77     /**
78      * Get the descriptor for the message destination reference owner.
79      */

80     public MessageDestinationReferenceDescriptor getMessageDestinationRefOwner
81         () {
82         return ownerMsgDestRef;
83     }
84
85     /**
86      * True if the owner is a message-driven bean.
87      */

88     public boolean ownedByMessageBean() {
89         return (ownerMsgBean != null);
90     }
91
92
93     /**
94      * Get the descriptor for the message-driven bean owner.
95      */

96     public EjbMessageBeanDescriptor getMessageBeanOwner() {
97         return ownerMsgBean;
98     }
99
100     /**
101      * True if this reference has been resolved to a valid MessageDestination
102      * object.
103      */

104     public boolean isLinkedToMessageDestination() {
105     return (messageDestination != null);
106     }
107     
108     private BundleDescriptor getBundleDescriptor() {
109         return ownedByMessageDestinationRef() ?
110             ownerMsgDestRef.getReferringBundleDescriptor() :
111             ownerMsgBean.getEjbBundleDescriptor();
112     }
113
114     /**
115      * @return the link name of the message destination to which I refer
116      * NOTE that this "link name" is potentially different from the actual
117      * name of the target message destination, since the message destination
118      * could be defined in a different module.
119      */

120     public String JavaDoc getMessageDestinationLinkName() {
121         return messageDestinationLinkName;
122     }
123
124     /**
125      * Sets the name of the message destination to which I refer.
126      * NOTE : Does *NOT* attempt to resolve link name. Use
127      * alternate version of setMessageDestinationLinkName or resolveLink
128      * if link resolution is required.
129      */

130     public void setMessageDestinationLinkName(String JavaDoc linkName) {
131         setMessageDestinationLinkName(linkName, false);
132     }
133
134
135     /**
136      * Sets the name of the message destination to which I refer.
137      * @param resolve if true, *try* to resolve link to the target message
138      * destination.
139      *
140      * @return MessageDestination to which link was resolved, or null if
141      * link name resolution failed.
142      */

143     public MessageDestinationDescriptor setMessageDestinationLinkName
144         (String JavaDoc linkName, boolean resolve) {
145                                                             
146         messageDestinationLinkName = linkName;
147         MessageDestinationDescriptor msgDest = null;
148
149         if( resolve ) {
150             msgDest = resolveLinkName();
151         }
152         return msgDest;
153     }
154
155     /**
156      * Try to resolve the current link name value to a MessageDestination
157      * object.
158      *
159      * @return MessageDestination to which link was resolved, or null if
160      * link name resolution failed.
161      */

162     public MessageDestinationDescriptor resolveLinkName() {
163         MessageDestinationDescriptor msgDest = null;
164
165         String JavaDoc linkName = messageDestinationLinkName;
166
167         if( (linkName != null) && (linkName.length() > 0) ) {
168             int hashIndex = linkName.indexOf('#');
169
170             BundleDescriptor bundleDescriptor = getBundleDescriptor();
171             Application app = bundleDescriptor.getApplication();
172             BundleDescriptor targetBundle = bundleDescriptor;
173             String JavaDoc msgDestName = linkName;
174             
175             if( app != null ) {
176
177                 // explicit reference to another module
178
if( hashIndex != -1 ) {
179                     String JavaDoc relativeModuleUri = linkName.substring(0, hashIndex);
180                     msgDestName = linkName.substring(hashIndex + 1);
181                     targetBundle = app.getRelativeBundle(bundleDescriptor,
182                                                          relativeModuleUri);
183                 } else {
184                     // Default is to find message destination within this
185
// module. If it's not there, try searching the other
186
// modules. NOTE that it's up to the deployer to ensure
187
// that any message destinations that are referred to
188
// from outside the defining module without an explicit
189
// reference have names that are unique within all the
190
// message destinations in the .ear. There is no
191
// required search ordering.
192
if( !bundleDescriptor.hasMessageDestinationByName
193                           (msgDestName) ) {
194                         Set JavaDoc modules = app.getBundleDescriptors();
195                         for(Iterator JavaDoc iter = modules.iterator(); iter.hasNext();)
196                         {
197                             BundleDescriptor next=(BundleDescriptor)iter.next();
198                             if( next.hasMessageDestinationByName(msgDestName) ) {
199                                 targetBundle = next;
200                                 break;
201                             }
202                         }
203                     }
204                 }
205             }
206             try {
207                 if( targetBundle != null ) {
208                     msgDest = targetBundle.getMessageDestinationByName
209                         (msgDestName);
210                 }
211             } catch(IllegalArgumentException JavaDoc iae) {}
212         }
213         if( msgDest != null ) {
214             setMessageDestination(msgDest);
215         }
216
217         return msgDest;
218     }
219         
220     /**
221      * @return the message destination to which I refer. Can be NULL.
222     */

223     public MessageDestinationDescriptor getMessageDestination() {
224     return messageDestination;
225     }
226
227     /**
228      * @param messageDestiation the message destination to which I refer.
229      */

230     public void setMessageDestination(MessageDestinationDescriptor newMsgDest) {
231         if( messageDestination != null ) {
232             messageDestination.removeReferencer(this);
233         }
234         if( newMsgDest != null ) {
235             newMsgDest.addReferencer(this);
236
237             // Keep message destination link name in synch with message
238
// destination object.
239
BundleDescriptor bundleDescriptor = getBundleDescriptor();
240             BundleDescriptor targetBundleDescriptor =
241                 newMsgDest.getBundleDescriptor();
242             String JavaDoc linkName = newMsgDest.getName();
243             if( bundleDescriptor != targetBundleDescriptor ) {
244                 Application app = bundleDescriptor.getApplication();
245                 String JavaDoc relativeUri = app.getRelativeUri(bundleDescriptor,
246                                                         targetBundleDescriptor);
247                 linkName = relativeUri + "#" + linkName;
248             }
249             messageDestinationLinkName = linkName;
250         }
251         messageDestination = newMsgDest;
252     }
253
254 }
255
Popular Tags