KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > BaseEjb


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.sun.share.configbean;
20
21 import java.beans.PropertyVetoException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
26 import javax.enterprise.deploy.model.DDBean JavaDoc;
27 import javax.enterprise.deploy.model.XpathEvent JavaDoc;
28
29 import org.openide.ErrorManager;
30
31 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
32 import org.netbeans.modules.j2ee.sun.dd.api.ejb.Ejb;
33 import org.netbeans.modules.j2ee.sun.dd.api.ejb.EnterpriseBeans;
34 import org.netbeans.modules.j2ee.sun.dd.api.ejb.BeanCache;
35 import org.netbeans.modules.j2ee.sun.dd.api.ejb.BeanPool;
36 import org.netbeans.modules.j2ee.sun.dd.api.ejb.IorSecurityConfig;
37 import org.netbeans.modules.j2ee.sun.dd.api.ejb.Principal;
38
39 /** This is the base class for all Ejb related config beans. It should have
40  * properties to deal with all the "shared" deployment descriptor elements.
41  * @author vkraemer
42  */

43 public abstract class BaseEjb extends Base {
44     
45     /** property event names
46      */

47     public static final String JavaDoc EJB_NAME = "ejbName"; // NOI18N
48

49     /** Holds value of property ejbNameDD */
50     private DDBean JavaDoc ejbNameDD;
51
52     /** Holds value of property jndiName. */
53     private String JavaDoc jndiName;
54
55     /** Holds value of property passByReference. */
56     private String JavaDoc passByReference;
57
58     /** Holds value of property principalName. */
59     private String JavaDoc principalName;
60
61     /** Holds value of property iorSecurityConfig. */
62     private IorSecurityConfig iorSecurityConfig;
63
64     /** Holds value of property beanPool. */
65     private BeanPool beanPool;
66
67     /** Holds value of property beanCache. */
68     private BeanCache beanCache;
69
70
71     /** Creates a new instance of SunONEBaseEjbDConfigBean */
72     public BaseEjb() {
73         setDescriptorElement(bundle.getString("BDN_BaseEjb")); // NOI18N
74
}
75
76     protected void init(DDBean JavaDoc dDBean, Base parent) throws ConfigurationException JavaDoc {
77         super.init(dDBean,parent);
78
79         ejbNameDD = getNameDD("ejb-name"); // NOI18N
80

81         updateNamedBeanCache(EnterpriseBeans.EJB);
82         
83         loadFromPlanFile(getConfig());
84     }
85     
86     protected String JavaDoc getComponentName() {
87         return getEjbName();
88     }
89     
90     /** The DDBean (or one of it's children) that this DConfigBean is bound to
91      * has changed.
92      *
93      * @param xpathEvent
94      */

95     public void notifyDDChange(XpathEvent JavaDoc xpathEvent) {
96         super.notifyDDChange(xpathEvent);
97
98         if(ejbNameDD == xpathEvent.getBean()) {
99             // name changed...
100
getPCS().firePropertyChange(EJB_NAME, "", getEjbName());
101             getPCS().firePropertyChange(DISPLAY_NAME, "", getDisplayName());
102
103             updateNamedBeanCache(EnterpriseBeans.EJB);
104         }
105     }
106     
107     /* ------------------------------------------------------------------------
108      * Persistence support. Loads DConfigBeans from previously saved Deployment
109      * plan file.
110      */

111     protected class BaseEjbSnippet extends DefaultSnippet {
112         public CommonDDBean getDDSnippet() {
113             Ejb ejb = getConfig().getStorageFactory().createEjb();
114             String JavaDoc version = getAppServerVersion().getEjbJarVersionAsString();
115             
116             ejb.setEjbName(getEjbName());
117
118             if(null != jndiName){
119                 ejb.setJndiName(getJndiName());
120             }
121
122             if (null != passByReference) {
123                 ejb.setPassByReference(passByReference);
124             }
125
126             if (null != principalName) {
127                 Principal principal = ejb.newPrincipal();
128                 principal.setName(principalName);
129                 ejb.setPrincipal(principal);
130             }
131
132             IorSecurityConfig iorSecConf = getIorSecurityConfig();
133             if(null != iorSecConf){
134                 ejb.setIorSecurityConfig((IorSecurityConfig)iorSecConf.cloneVersion(version));
135             }
136
137             BeanPool beanPool = getBeanPool();
138             if(null != beanPool){
139                 ejb.setBeanPool((BeanPool)beanPool.cloneVersion(version));
140             }
141
142             BeanCache beanCache = getBeanCache();
143             if(null != beanCache){
144                 ejb.setBeanCache((BeanCache)beanCache.cloneVersion(version));
145             }
146             
147             /* IZ 84549, etc - add remaining saved named beans here. All entries that are represented
148              * by real DConfigBeans should have been removed by now. */

149             restoreAllNamedBeans(ejb, version);
150
151             return ejb;
152         }
153
154         public String JavaDoc getPropertyName() {
155             return EnterpriseBeans.EJB;
156         }
157
158         public boolean hasDDSnippet() {
159             if(null != jndiName){
160                 return true;
161             }
162
163             if (null != passByReference) {
164                 return true;
165             }
166
167             if (null != principalName) {
168                 return true;
169             }
170
171             if(null != getIorSecurityConfig()){
172                 return true;
173             }
174
175             if(null != getBeanPool()){
176                 return true;
177             }
178
179             if(null != getBeanCache()){
180                 return true;
181             }
182
183             //return snippet in case of any child DConfigBeans.
184
Collection JavaDoc childList = getChildren();
185             if(childList.size() > 0){
186                 return true;
187             }
188
189             return false;
190         }
191     }
192
193 /*
194     public class EjbFinder implements ConfigFinder {
195         private String beanName;
196
197         public EjbFinder(String beanName) {
198             this.beanName = beanName;
199         }
200
201         public Object find(Object obj) {
202             Ejb retVal = null;
203             SunEjbJar root = (SunEjbJar) obj;
204 // String[] attrs = root.findAttributeValue("ejb-name", beanName);
205             String[] props = root.findPropertyValue("ejb-name", beanName);
206             for (int i = 0; i < props.length; i++) {
207                 CommonDDBean candidate = root.graphManager().getPropertyParent(props[i]);
208                 if (candidate instanceof Ejb) {
209                     retVal = (Ejb) candidate;
210                 }
211             }
212 // String[] values = root.findValue(beanName);
213             return retVal;
214         }
215     }
216  */

217     private class EjbFinder extends NameBasedFinder {
218         public EjbFinder(String JavaDoc beanName) {
219             super(Ejb.EJB_NAME, beanName, Ejb.class);
220         }
221     }
222     
223     boolean loadFromPlanFile(SunONEDeploymentConfiguration config) {
224         String JavaDoc uriText = getUriText();
225
226         Ejb ejb = (Ejb) config.getBeans(uriText, constructFileName(), getParser(),
227             new EjbFinder(getEjbName()));
228             
229         clearProperties();
230         
231         if(null != ejb) {
232             loadEjbProperties(ejb);
233             
234             // IZ 84549, etc - cache the data for all named beans.
235
saveAllNamedBeans(ejb);
236         } else {
237             setDefaultProperties();
238         }
239         
240         return (ejb != null);
241     }
242     
243     protected void loadEjbProperties(Ejb savedEjb) {
244         String JavaDoc val = savedEjb.getJndiName();
245         if(null != val) {
246             this.jndiName = val.trim();
247         }
248
249         val = savedEjb.getPassByReference();
250         if(null != val) {
251             this.passByReference = val.trim();
252         }
253
254         Principal principal = savedEjb.getPrincipal();
255         if(null != principal){
256             String JavaDoc name = principal.getName();
257             assert(name != null);
258             this.principalName = name;
259         }
260
261         IorSecurityConfig iorSecurityConfig = savedEjb.getIorSecurityConfig();
262         if(null != iorSecurityConfig){
263             this.iorSecurityConfig = iorSecurityConfig;
264         }
265
266         BeanPool beanPool = savedEjb.getBeanPool();
267         if(null != beanPool){
268             this.beanPool = beanPool;
269         }
270
271         BeanCache beanCache = savedEjb.getBeanCache();
272         if(null != beanCache){
273             this.beanCache = beanCache;
274         }
275     }
276     
277     protected void clearProperties() {
278         jndiName = null;
279         passByReference = null;
280         principalName = null;
281         iorSecurityConfig = null;
282         beanPool = null;
283         beanCache = null;
284     }
285
286     protected void setDefaultProperties() {
287         // Default behavior - remote interface = has jndi name.
288
// MDB overrides this to always set the JNDI name.
289
if(requiresJndiName()) {
290             jndiName = getDefaultJndiName();
291             getConfig().getMasterDCBRoot().setDirty();
292         }
293     }
294     
295     protected String JavaDoc getDefaultJndiName() {
296         return "ejb/" + getEjbName(); // NOI18N // J2EE recommended jndiName
297
}
298     
299     protected boolean requiresJndiName() {
300         // For JavaEE5 and later spec bean, jndi name is optional.
301
boolean needsJndi = super.requiresJndiName();
302
303         if(needsJndi) {
304             // For J2EE 1.4 and previous beans, jndi name is only required for beans with
305
// remote interfaces. Note this does not apply message driven beans and
306
// MDEjb.java overrides this method with logic correct to that bean type.
307
DDBean JavaDoc [] remoteDDBeans = getDDBean().getChildBean("remote"); // NOI18N
308
if(!(remoteDDBeans.length > 0 && remoteDDBeans[0] != null)) {
309                 // remote interface is not present, return false.
310
needsJndi = false;
311             }
312         }
313         
314         return needsJndi;
315     }
316     
317     private static Collection JavaDoc ejbBeanSpecs = new ArrayList JavaDoc();
318     
319     static {
320         ejbBeanSpecs.addAll(getCommonNamedBeanSpecs());
321     }
322     
323     protected Collection JavaDoc getNamedBeanSpecs() {
324         return ejbBeanSpecs;
325     }
326
327     /* ------------------------------------------------------------------------
328      * XPath to Factory mapping support
329      */

330     private HashMap JavaDoc baseEjbFactoryMap;
331
332     /** Retrieve the XPathToFactory map common to all EJB baseed DConfigBean.
333      * So far, this is:
334      *
335      * EjbRef
336      * ResourceRef
337      * ResourceEnvRef
338      * ServiceRef
339      *
340      * @return
341      */

342     protected java.util.Map JavaDoc getXPathToFactoryMap() {
343         if(baseEjbFactoryMap == null) {
344             baseEjbFactoryMap = new HashMap JavaDoc(17);
345
346             baseEjbFactoryMap.put("ejb-ref", new DCBGenericFactory(EjbRef.class)); // NOI18N
347
baseEjbFactoryMap.put("resource-ref", new DCBGenericFactory(ResourceRef.class)); // NOI18N
348
baseEjbFactoryMap.put("resource-env-ref", new DCBGenericFactory(ResourceEnvRef.class)); // NOI18N
349

350             J2EEBaseVersion moduleVersion = getJ2EEModuleVersion();
351             if(moduleVersion.compareTo(EjbJarVersion.EJBJAR_2_1) >= 0) {
352                 baseEjbFactoryMap.put("service-ref", new DCBGenericFactory(ServiceRef.class)); // NOI18N
353

354                 if(moduleVersion.compareTo(EjbJarVersion.EJBJAR_3_0) >= 0) {
355                     baseEjbFactoryMap.put("message-destination-ref", new DCBGenericFactory(MessageDestinationRef.class));// NOI18N
356
}
357             }
358         }
359         return baseEjbFactoryMap;
360     }
361
362     /* ------------------------------------------------------------------------
363      * Property support -- methods to manipulate the properties maintained by
364      * this bean.
365      */

366
367     /** Get /sun-ejb-jar/enterprise-beans/ejb/ejb-name element value.
368      * @return Value of element /sun-ejb-jar/enterprise-beans/ejb/ejb-name
369      */

370     public String JavaDoc getEjbName() {
371         return cleanDDBeanText(ejbNameDD);
372     }
373
374     /** Get /sun-ejb-jar/enterprise-beans/ejb/jndi-name element value.
375      * @return Value of element /sun-ejb-jar/enterprise-beans/ejb/jndi-name
376      */

377     public String JavaDoc getJndiName() {
378             return this.jndiName;
379     }
380
381     /** Set /sun-ejb-jar/enterprise-beans/ejb/jndi-name element value.
382      * @param jndiName New value of property jndiName.
383      * @throws PropertyVetoException In cases where the jndi name is illegal
384      */

385     public void setJndiName(String JavaDoc jndiName) throws java.beans.PropertyVetoException JavaDoc {
386             String JavaDoc oldJndiName = this.jndiName;
387             getVCS().fireVetoableChange("jndiName", oldJndiName, jndiName);
388             this.jndiName = jndiName;
389             getPCS().firePropertyChange("jndiName", oldJndiName, jndiName);
390     }
391
392     /** Get /sun-ejb-jar/enterprise-beans/ejb/pass-by-reference element value
393      * @return Value /sun-ejb-jar/enterprise-beans/ejb/pass-by-reference.
394      */

395     public String JavaDoc getPassByReference() {
396             return this.passByReference;
397     }
398
399     /** Setter for property passByReference.
400      * @param passByReference New value of property passByReference.
401      *
402      * @throws PropertyVetoException
403      *
404      */

405     public void setPassByReference(String JavaDoc passByReference) throws java.beans.PropertyVetoException JavaDoc {
406             String JavaDoc oldPassByReference = this.passByReference;
407             getVCS().fireVetoableChange("passByReference", oldPassByReference, passByReference);
408             this.passByReference = passByReference;
409             getPCS().firePropertyChange("passByReference", oldPassByReference, passByReference);
410     }
411
412     /** Getter for property principalName.
413      * @return Value of property principalName.
414      *
415      */

416     public String JavaDoc getPrincipalName() {
417             return this.principalName;
418     }
419
420     /** Setter for property principalName.
421      * @param principalName New value of property principalName.
422      *
423      * @throws PropertyVetoException
424      *
425      */

426     public void setPrincipalName(String JavaDoc principalName) throws java.beans.PropertyVetoException JavaDoc {
427             String JavaDoc oldPrincipalName = this.principalName;
428             getVCS().fireVetoableChange("principalName", oldPrincipalName, principalName);
429             this.principalName = principalName;
430             getPCS().firePropertyChange("principalName", oldPrincipalName, principalName);
431     }
432
433     /** Getter for property iorSecurityConfig.
434      * @return Value of property iorSecurityConfig.
435      *
436      */

437     public IorSecurityConfig getIorSecurityConfig() {
438         return this.iorSecurityConfig;
439     }
440
441     /** Setter for property iorSecurityConfig.
442      * @param iorSecurityConfig New value of property iorSecurityConfig.
443      *
444      * @throws PropertyVetoException
445      *
446      */

447     public void setIorSecurityConfig(IorSecurityConfig iorSecurityConfig) throws java.beans.PropertyVetoException JavaDoc {
448         IorSecurityConfig oldIorSecurityConfig = this.iorSecurityConfig;
449         getVCS().fireVetoableChange("iorSecurityConfig", oldIorSecurityConfig, iorSecurityConfig);
450         this.iorSecurityConfig = iorSecurityConfig;
451         getPCS().firePropertyChange("iorSecurityConfig", oldIorSecurityConfig, iorSecurityConfig);
452     }
453
454     /** Getter for property beanPool.
455      * @return Value of property beanPool.
456      *
457      */

458     public BeanPool getBeanPool() {
459         return this.beanPool;
460     }
461
462     /** Setter for property beanPool.
463      * @param beanPool New value of property beanPool.
464      *
465      * @throws PropertyVetoException
466      *
467      */

468     public void setBeanPool(BeanPool beanPool) throws java.beans.PropertyVetoException JavaDoc {
469         BeanPool oldBeanPool = this.beanPool;
470         getVCS().fireVetoableChange("beanPool", oldBeanPool, beanPool);
471         this.beanPool = beanPool;
472         getPCS().firePropertyChange("beanPool", oldBeanPool, beanPool);
473     }
474
475     /** Getter for property beanCache.
476      * @return Value of property beanCache.
477      *
478      */

479     public BeanCache getBeanCache() {
480         return this.beanCache;
481     }
482
483     /** Setter for property beanCache.
484      * @param beanCache New value of property beanCache.
485      *
486      * @throws PropertyVetoException
487      *
488      */

489     public void setBeanCache(BeanCache beanCache) throws java.beans.PropertyVetoException JavaDoc {
490         BeanCache oldBeanCache = this.beanCache;
491         getVCS().fireVetoableChange("beanCache", oldBeanCache, beanCache);
492         this.beanCache = beanCache;
493         getPCS().firePropertyChange("beanCache", oldBeanCache, beanCache);
494     }
495         
496     /** Api to retrieve the interface definitions for this bean. Aids usability
497      * during configuration, as the editors can display the existing methds
498      * rather than have the user enter them manually.
499      */

500     public ConfigQuery.InterfaceData getEJBMethods() {
501         /* !PW FIXME Temporary implementation values until plumbing in j2eeserver is worked out.
502          */

503         java.util.List JavaDoc hi = new ArrayList JavaDoc();
504         hi.add(new ConfigQuery.MethodData("home_method1", java.util.Arrays.asList(new String JavaDoc [] { "arg1", "arg2" } )));
505         
506         java.util.List JavaDoc ri = new ArrayList JavaDoc();
507         ri.add(new ConfigQuery.MethodData("remote_method1", java.util.Arrays.asList(new String JavaDoc [] { "arg1", "arg2", "arg3" } )));
508         ri.add(new ConfigQuery.MethodData("remote_method2", java.util.Arrays.asList(new String JavaDoc [] { "arg1" } )));
509         
510         java.util.List JavaDoc lhi = new ArrayList JavaDoc();
511         lhi.add(new ConfigQuery.MethodData("local_home_method1", java.util.Arrays.asList(new String JavaDoc [] { "arg1", "arg2" } )));
512         
513         java.util.List JavaDoc li = new ArrayList JavaDoc();
514         li.add(new ConfigQuery.MethodData("local_method1", java.util.Arrays.asList(new String JavaDoc [] { "arg1", "arg2" } )));
515         li.add(new ConfigQuery.MethodData("local_method2", java.util.Arrays.asList(new String JavaDoc [] { "arg1" } )));
516         li.add(new ConfigQuery.MethodData("local_method3", java.util.Arrays.asList(new String JavaDoc [] { "arg1", "arg2", "arg3" } )));
517         
518         return new ConfigQuery.InterfaceData(hi, ri, lhi, li);
519     }
520 }
521
Popular Tags