KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.modules.j2ee.sun.share.configbean;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.text.MessageFormat 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.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
30 import org.netbeans.modules.j2ee.sun.dd.api.ejb.Ejb;
31 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp;
32 import org.netbeans.modules.j2ee.sun.share.configbean.Base.DefaultSnippet;
33
34
35 /**
36  *
37  * @author Peter Williams
38  */

39 public class EjbRef extends Base {
40     
41     /** property event names
42      */

43     public static final String JavaDoc EJB_REF_NAME = "ejbRefName"; // NOI18N
44

45     /** Holds value of property ejbRefName. */
46     private DDBean JavaDoc ejbRefNameDD;
47     
48     /** Holds value of property jndiName. */
49     private String JavaDoc jndiName;
50     
51     /** Creates a new instance of EjbRef */
52     public EjbRef() {
53         setDescriptorElement(bundle.getString("BDN_EjbRef")); // NOI18N
54
}
55
56     /** Override init to enable grouping support for this bean
57      * @param dDBean DDBean matching this bean
58      * @param parent Parent DConfigBean in the tree
59      */

60     protected void init(DDBean JavaDoc dDBean, Base parent) throws ConfigurationException JavaDoc {
61         super.init(dDBean, parent);
62 // !PW Disable grouping code for now, spec non-compliance.
63
// initGroup(dDBean, parent);
64

65         ejbRefNameDD = getNameDD("ejb-ref-name");
66         
67         updateNamedBeanCache(SunWebApp.EJB_REF);
68         
69         loadFromPlanFile(getConfig());
70     }
71     
72     protected String JavaDoc getComponentName() {
73         return getEjbRefName();
74     }
75
76     /** -----------------------------------------------------------------------
77      * Validation implementation
78      */

79     
80     // relative xpaths (double as field id's)
81
public static final String JavaDoc FIELD_JNDI_NAME="jndi-name";
82     
83     protected void updateValidationFieldList() {
84         super.updateValidationFieldList();
85         validationFieldList.add(FIELD_JNDI_NAME);
86     }
87     
88     public boolean validateField(String JavaDoc fieldId) {
89         ValidationError error = null;
90         boolean result = true;
91
92         if(fieldId.equals(FIELD_JNDI_NAME)) {
93             // validation version will be:
94
// expand relative field id to full xpath id based on current context
95
// lookup validator for this field in field validator DB
96
// execute validator
97
J2EEBaseVersion moduleVersion = getJ2EEModuleVersion();
98             if(moduleVersion.compareSpecification(J2EEVersion.JAVAEE_5_0) < 0) {
99                 String JavaDoc absoluteFieldXpath = getAbsoluteXpath(fieldId);
100                 if(!Utils.notEmpty(jndiName)) {
101                     Object JavaDoc [] args = new Object JavaDoc[1];
102                     args[0] = FIELD_JNDI_NAME;
103                     String JavaDoc message = MessageFormat.format(bundle.getString("ERR_SpecifiedFieldIsEmpty"), args); // NOI18N
104
error = ValidationError.getValidationError(absoluteFieldXpath, message);
105                 } else {
106                     error = ValidationError.getValidationErrorMask(absoluteFieldXpath);
107                 }
108             }
109         }
110         
111         if(error != null) {
112             getMessageDB().updateError(error);
113         }
114         
115         // return true if there was no error added
116
return (error == null || !Utils.notEmpty(error.getMessage()));
117     }
118     
119     /** Getter for helpId property
120      * @return Help context ID for this DConfigBean
121      */

122     public String JavaDoc getHelpId() {
123         return "AS_CFG_EjbRef";
124     }
125     
126     /** The DDBean (or one of it's children) that this DConfigBean is bound to
127      * has changed.
128      *
129      * @param xpathEvent
130      */

131     public void notifyDDChange(XpathEvent JavaDoc xpathEvent) {
132         super.notifyDDChange(xpathEvent);
133
134         if(ejbRefNameDD == xpathEvent.getBean()) {
135             // name changed...
136
getPCS().firePropertyChange(EJB_REF_NAME, "", getEjbRefName());
137             getPCS().firePropertyChange(DISPLAY_NAME, "", getDisplayName());
138             
139             updateNamedBeanCache(SunWebApp.EJB_REF);
140         }
141     }
142
143     /** Getter for property ejbRefName.
144      * @return Value of property ejbRefName.
145      *
146      */

147     public String JavaDoc getEjbRefName() {
148         return cleanDDBeanText(ejbRefNameDD);
149     }
150     
151     /** Getter for property jndiName.
152      * @return Value of property jndiName.
153      *
154      */

155     public String JavaDoc getJndiName() {
156         return this.jndiName;
157     }
158     
159     /** Setter for property jndiName.
160      * @param jndiName New value of property jndiName.
161      *
162      * @throws PropertyVetoException
163      *
164      */

165     public void setJndiName(String JavaDoc jndiName) throws java.beans.PropertyVetoException JavaDoc {
166         String JavaDoc oldJndiName = this.jndiName;
167         getVCS().fireVetoableChange("jndiName", oldJndiName, jndiName);
168         this.jndiName = jndiName;
169         getPCS().firePropertyChange("jndiName", oldJndiName, jndiName);
170     }
171     
172     /* ------------------------------------------------------------------------
173      * Persistence support. Loads DConfigBeans from previously saved Deployment
174      * plan file.
175      */

176     Collection JavaDoc getSnippets() {
177         Collection JavaDoc snippets = new ArrayList JavaDoc();
178         Snippet snipOne = new DefaultSnippet() {
179             public CommonDDBean getDDSnippet() {
180                 org.netbeans.modules.j2ee.sun.dd.api.common.EjbRef ref =
181                     getConfig().getStorageFactory().createEjbRef();
182
183                 // write properties into Servlet bean
184
String JavaDoc ejbRefName = getEjbRefName();
185                 if(ejbRefName != null) {
186                     ref.setEjbRefName(ejbRefName);
187                 }
188
189                 if(jndiName != null && jndiName.length() > 0) {
190                     ref.setJndiName(jndiName);
191                 }
192
193                 return ref;
194             }
195             
196             public boolean hasDDSnippet() {
197                 if(jndiName != null && jndiName.length() > 0) {
198                     return true;
199                 }
200                 
201                 return false;
202             }
203             
204             public String JavaDoc getPropertyName() {
205                 return Ejb.EJB_REF;
206             }
207         };
208         
209         snippets.add(snipOne);
210         return snippets;
211     }
212
213     private class EjbRefFinder extends NameBasedFinder {
214         public EjbRefFinder(String JavaDoc beanName) {
215             super(org.netbeans.modules.j2ee.sun.dd.api.common.EjbRef.EJB_REF_NAME,
216                   beanName, org.netbeans.modules.j2ee.sun.dd.api.common.EjbRef.class);
217         }
218     }
219     
220     boolean loadFromPlanFile(SunONEDeploymentConfiguration config) {
221         String JavaDoc uriText = getUriText();
222
223         org.netbeans.modules.j2ee.sun.dd.api.common.EjbRef beanGraph =
224             (org.netbeans.modules.j2ee.sun.dd.api.common.EjbRef) config.getBeans(uriText,
225             constructFileName(), getParser(), new EjbRefFinder(getEjbRefName()));
226         
227         clearProperties();
228         
229         if(beanGraph != null) {
230             jndiName = beanGraph.getJndiName();
231         } else {
232             setDefaultProperties();
233         }
234         
235         return (beanGraph != null);
236     }
237     
238     protected void clearProperties() {
239         jndiName = null;
240     }
241     
242     protected void setDefaultProperties() {
243         if(requiresJndiName()) {
244             // Set default JNDI name
245
jndiName = ejbRefNameDD.getText();
246
247             // Prepend "ejb/" only if, the Ref name does not start with "ejb/"
248
// By blue prints convention, Ref name always starts with "ejb/"
249
if (!jndiName.startsWith("ejb/")) { // NOI18N
250
jndiName = "ejb/" + jndiName; // NOI18N
251
}
252
253             getConfig().getMasterDCBRoot().setDirty();
254         }
255     }
256 }
257
Popular Tags