KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.LocalStringManagerImpl;
26 import java.util.*;
27     
28     /**
29     * Objects of this kind represent the deployment information describing a single
30     * Session Ejb, either stateful or stateless.
31     *@author Danny Coward
32     */

33
34 public final class EjbSessionDescriptor extends EjbDescriptor {
35     private boolean isStateless = false;
36     private int timeout = 0;
37
38     private Set<LifecycleCallbackDescriptor> postActivateDescs =
39         new HashSet<LifecycleCallbackDescriptor>();
40     private Set<LifecycleCallbackDescriptor> prePassivateDescs =
41         new HashSet<LifecycleCallbackDescriptor>();
42
43     // For EJB 3.0 stateful session beans, information about the assocation
44
// between a business method and bean removal.
45
private Map<MethodDescriptor, EjbRemovalInfo> removeMethods
46         = new HashMap<MethodDescriptor, EjbRemovalInfo>();
47
48     // For EJB 3.0 stateful session beans with adapted homes, list of
49
// business methods corresponding to Home/LocalHome create methods.
50
private Set<EjbInitInfo> initMethods=new HashSet<EjbInitInfo>();
51
52     /** The Session type String.*/
53     public final static String JavaDoc TYPE = "Session";
54     /** The String to indicate stalessness. */
55     public final static String JavaDoc STATELESS = "Stateless";
56     /** Idicates statefullness of a session ejb.*/
57     public final static String JavaDoc STATEFUL = "Stateful";
58     
59     private static LocalStringManagerImpl localStrings =
60         new LocalStringManagerImpl(EjbSessionDescriptor.class);
61
62     /**
63     * Default constructor.
64     */

65     public EjbSessionDescriptor() {
66     }
67     
68     /**
69     * The copy constructor.
70     */

71     
72     public EjbSessionDescriptor(EjbDescriptor other) {
73     super(other);
74     if (other instanceof EjbSessionDescriptor) {
75         EjbSessionDescriptor session = (EjbSessionDescriptor) other;
76         this.isStateless = session.isStateless;
77         this.timeout = session.timeout;
78     }
79     }
80     
81     /**
82     * Returns the type of this bean - always "Session".
83     */

84     public String JavaDoc getType() {
85     return TYPE;
86     }
87     
88     /**
89     * Returns the string STATELESS or STATEFUL according as to whether
90     * the bean is stateless or stateful.
91     **/

92     
93     public String JavaDoc getSessionType() {
94     if (this.isStateless()) {
95         return STATELESS;
96     } else {
97         return STATEFUL;
98     }
99     }
100     
101     /**
102     * Accepts the Strings STATELESS or STATEFUL.
103     */

104     public void setSessionType(String JavaDoc sessionType) {
105     if (STATELESS.equals(sessionType)) {
106         this.setStateless(true);
107         return;
108     }
109     if (STATEFUL.equals(sessionType)) {
110         this.setStateless(false);
111         return;
112     }
113     if (this.isBoundsChecking()) {
114         throw new IllegalArgumentException JavaDoc(localStrings.getLocalString(
115                                        "enterprise.deployment.exceptionsessiontypenotlegaltype",
116                                        "{0} is not a legal session type for session ejbs. The type must be {1} or {2}", new Object JavaDoc[] {sessionType, STATEFUL, STATELESS}));
117     }
118     }
119     
120     /**
121     * Sets my type
122     */

123     public void setType(String JavaDoc type) {
124     throw new IllegalArgumentException JavaDoc(localStrings.getLocalString(
125                                    "enterprise.deployment.exceptioncannotsettypeofsessionbean",
126                                    "Cannot set the type of a session bean"));
127     }
128     
129     /**
130     * Sets the timeout value for this session bean.
131     */

132
133     public void setTimeout(int timeout) {
134     this.timeout = timeout;
135     }
136     
137     /**
138     * Returns the timeout value of this bean.
139     */

140     public int getTimeout() {
141     return this.timeout;
142     }
143     
144     /**
145     * Sets the transaction type for this bean. Must be either BEAN_TRANSACTION_TYPE or CONTAINER_TRANSACTION_TYPE.
146     */

147     public void setTransactionType(String JavaDoc transactionType) {
148     boolean isValidType = (BEAN_TRANSACTION_TYPE.equals(transactionType) ||
149                 CONTAINER_TRANSACTION_TYPE.equals(transactionType));
150                 
151     if (!isValidType && this.isBoundsChecking()) {
152         throw new IllegalArgumentException JavaDoc(localStrings.getLocalString(
153                                        "enterprise.deployment..exceptointxtypenotlegaltype",
154                                        "{0} is not a legal transaction type for session beans", new Object JavaDoc[] {transactionType}));
155     } else {
156         super.transactionType = transactionType;
157         super.setMethodContainerTransactions(new Hashtable());
158         super.changed();
159     }
160     }
161     
162     /**
163     * Returns true if I am describing a stateless session bean.
164     */

165     public boolean isStateless() {
166     return isStateless;
167     }
168     
169     public boolean isStateful() {
170         return !isStateless();
171     }
172     
173     /**
174     * Sets the isStateless attribute of this session bean.
175     */

176     public void setStateless(boolean isStateless) {
177     this.isStateless = isStateless;
178     super.changed();
179     }
180
181     public boolean hasRemoveMethods() {
182         return (!removeMethods.isEmpty());
183     }
184
185     /**
186      * @return remove method info for the given method or null if the
187      * given method is not a remove method for this stateful session bean.
188      */

189     public EjbRemovalInfo getRemovalInfo(MethodDescriptor method) {
190         return removeMethods.get(method);
191     }
192
193     public Set<EjbRemovalInfo> getAllRemovalInfo() {
194         return new HashSet<EjbRemovalInfo>(removeMethods.values());
195     }
196
197     public void addRemoveMethod(EjbRemovalInfo removalInfo) {
198         removeMethods.put(removalInfo.getRemoveMethod(), removalInfo);
199     }
200
201     public boolean hasInitMethods() {
202         return (!initMethods.isEmpty());
203     }
204
205     public Set<EjbInitInfo> getInitMethods() {
206         return new HashSet<EjbInitInfo>(initMethods);
207     }
208
209     public void addInitMethod(EjbInitInfo initInfo) {
210         initMethods.add(initInfo);
211     }
212     
213     public Set<LifecycleCallbackDescriptor> getPostActivateDescriptors() {
214         if (postActivateDescs == null) {
215             postActivateDescs =
216                 new HashSet<LifecycleCallbackDescriptor>();
217         }
218         return postActivateDescs;
219     }
220             
221     public void addPostActivateDescriptor(LifecycleCallbackDescriptor
222         postActivateDesc) {
223         String JavaDoc className = postActivateDesc.getLifecycleCallbackClass();
224         boolean found = false;
225         for (LifecycleCallbackDescriptor next :
226              getPostActivateDescriptors()) {
227             if (next.getLifecycleCallbackClass().equals(className)) {
228                 found = true;
229                 break;
230             }
231         }
232         if (!found) {
233             getPostActivateDescriptors().add(postActivateDesc);
234         }
235     }
236
237     public LifecycleCallbackDescriptor
238         getPostActivateDescriptorByClass(String JavaDoc className) {
239
240         for (LifecycleCallbackDescriptor next :
241                  getPostActivateDescriptors()) {
242             if (next.getLifecycleCallbackClass().equals(className)) {
243                 return next;
244             }
245         }
246         return null;
247     }
248
249     public boolean hasPostActivateMethod() {
250         return (getPostActivateDescriptors().size() > 0);
251     }
252
253     public Set<LifecycleCallbackDescriptor> getPrePassivateDescriptors() {
254         if (prePassivateDescs == null) {
255             prePassivateDescs =
256                 new HashSet<LifecycleCallbackDescriptor>();
257         }
258         return prePassivateDescs;
259     }
260             
261     public void addPrePassivateDescriptor(LifecycleCallbackDescriptor
262         prePassivateDesc) {
263         String JavaDoc className = prePassivateDesc.getLifecycleCallbackClass();
264         boolean found = false;
265         for (LifecycleCallbackDescriptor next :
266              getPrePassivateDescriptors()) {
267             if (next.getLifecycleCallbackClass().equals(className)) {
268                 found = true;
269                 break;
270             }
271         }
272         if (!found) {
273             getPrePassivateDescriptors().add(prePassivateDesc);
274         }
275     }
276
277     public LifecycleCallbackDescriptor
278         getPrePassivateDescriptorByClass(String JavaDoc className) {
279
280         for (LifecycleCallbackDescriptor next :
281                  getPrePassivateDescriptors()) {
282             if (next.getLifecycleCallbackClass().equals(className)) {
283                 return next;
284             }
285         }
286         return null;
287     }
288
289     public boolean hasPrePassivateMethod() {
290         return (getPrePassivateDescriptors().size() > 0);
291     }
292
293     public Vector getPossibleTransactionAttributes() {
294         Vector txAttributes = super.getPossibleTransactionAttributes();
295
296         // Session beans that implement SessionSynchronization interface
297
// have a limited set of possible transaction attributes.
298
if( isStateful() ) {
299             try {
300                 EjbBundleDescriptor ejbBundle = getEjbBundleDescriptor();
301
302                 ClassLoader JavaDoc classLoader = ejbBundle.getClassLoader();
303                 Class JavaDoc ejbClass = classLoader.loadClass(getEjbClassName());
304                 Class JavaDoc sessionSynchClass =
305                     javax.ejb.SessionSynchronization JavaDoc.class;
306                 if( sessionSynchClass.isAssignableFrom(ejbClass) ) {
307                     txAttributes = new Vector();
308                     txAttributes.add(new ContainerTransaction
309                         (ContainerTransaction.REQUIRED, ""));
310                     txAttributes.add(new ContainerTransaction
311                         (ContainerTransaction.REQUIRES_NEW, ""));
312                     txAttributes.add(new ContainerTransaction
313                         (ContainerTransaction.MANDATORY, ""));
314                 }
315             } catch(Exception JavaDoc e) {
316                 // Don't treat this as a fatal error. Just return full
317
// set of possible transaction attributes.
318
}
319         }
320         return txAttributes;
321     }
322     
323     
324     /**
325     * Returns a formatted String of the attributes of this object.
326     */

327     public void print(StringBuffer JavaDoc toStringBuffer) {
328     toStringBuffer.append("Session descriptor");
329     toStringBuffer.append("\n isStateless ").append(isStateless);
330     super.print(toStringBuffer);
331     }
332
333
334 }
335
Popular Tags