KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.netbeans.modules.j2ee.sun.share.configbean;
22
23 import java.util.Collection JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26
27 import org.netbeans.modules.schema2beans.BaseBean;
28
29 import javax.enterprise.deploy.spi.DConfigBean JavaDoc;
30 import javax.enterprise.deploy.spi.DeploymentConfiguration JavaDoc;
31 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
32 import javax.enterprise.deploy.model.DDBean JavaDoc;
33 import javax.enterprise.deploy.model.XpathEvent JavaDoc;
34
35 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
36 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp;
37 import org.netbeans.modules.j2ee.sun.dd.api.common.DefaultResourcePrincipal;
38
39
40 /**
41  *
42  * @author Peter Williams
43  */

44 public class ResourceRef extends Base {
45     
46     /** property event names
47      */

48     public static final String JavaDoc RES_REF_NAME = "resRefName"; // NOI18N
49

50     /** Holds value of property resourceRefName. */
51     private DDBean JavaDoc resRefNameDD;
52     
53     /** Holds value of property jndiName. */
54     private String JavaDoc jndiName;
55     
56     /** Holds value of property principalName. */
57     private String JavaDoc principalName;
58     
59     /** Holds value of property principalPassword. */
60     private String JavaDoc principalPassword;
61     
62     /** Creates a new instance of ResourceRef */
63     public ResourceRef() {
64         setDescriptorElement(bundle.getString("BDN_ResourceRef")); // NOI18N
65
}
66
67     /** Override init to enable grouping support for this bean
68      * @param dDBean DDBean matching this bean
69      * @param parent Parent DConfigBean in the tree
70      */

71     protected void init(DDBean JavaDoc dDBean, Base parent) throws ConfigurationException JavaDoc {
72         super.init(dDBean, parent);
73 // !PW Disable grouping code for now, spec non-compliance.
74
// initGroup(dDBean, parent);
75

76         resRefNameDD = getNameDD("res-ref-name");
77
78         updateNamedBeanCache(SunWebApp.RESOURCE_REF);
79             
80         loadFromPlanFile(getConfig());
81     }
82     
83     protected String JavaDoc getComponentName() {
84         return getResRefName();
85     }
86     
87     /** -----------------------------------------------------------------------
88      * Validation implementation
89      */

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

133     public String JavaDoc getHelpId() {
134         return "AS_CFG_ResourceRef";
135     }
136     
137     /** The DDBean (or one of it's children) that this DConfigBean is bound to
138      * has changed.
139      *
140      * @param xpathEvent
141      */

142     public void notifyDDChange(XpathEvent JavaDoc xpathEvent) {
143         super.notifyDDChange(xpathEvent);
144
145         if(resRefNameDD == xpathEvent.getBean()) {
146             // name changed...
147
getPCS().firePropertyChange(RES_REF_NAME, "", getResRefName());
148             getPCS().firePropertyChange(DISPLAY_NAME, "", getDisplayName());
149
150             updateNamedBeanCache(SunWebApp.RESOURCE_REF);
151         }
152     }
153
154     /** Getter for property resRefName.
155      * @return Value of property resRefName.
156      *
157      */

158     public String JavaDoc getResRefName() {
159         return cleanDDBeanText(resRefNameDD);
160     }
161     
162     /** Getter for property jndiName.
163      * @return Value of property jndiName.
164      *
165      */

166     public String JavaDoc getJndiName() {
167         return this.jndiName;
168     }
169     
170     /** Setter for property jndiName.
171      * @param jndiName New value of property jndiName.
172      *
173      * @throws PropertyVetoException
174      *
175      */

176     public void setJndiName(String JavaDoc jndiName) throws java.beans.PropertyVetoException JavaDoc {
177         String JavaDoc oldJndiName = this.jndiName;
178         getVCS().fireVetoableChange("jndiName", oldJndiName, jndiName);
179         this.jndiName = jndiName;
180         getPCS().firePropertyChange("jndiName", oldJndiName, jndiName);
181     }
182     
183     /** Getter for property principalName.
184      * @return Value of property principalName.
185      *
186      */

187     public String JavaDoc getPrincipalName() {
188         return this.principalName;
189     }
190     
191     /** Setter for property principalName.
192      * @param principalName New value of property principalName.
193      *
194      * @throws PropertyVetoException
195      *
196      */

197     public void setPrincipalName(String JavaDoc principalName) throws java.beans.PropertyVetoException JavaDoc {
198         String JavaDoc oldPrincipalName = this.principalName;
199         getVCS().fireVetoableChange("principalName", oldPrincipalName, principalName);
200         this.principalName = principalName;
201         getPCS().firePropertyChange("principalName", oldPrincipalName, principalName);
202     }
203     
204     /** Getter for property principalPassword.
205      * @return Value of property principalPassword.
206      *
207      */

208     public String JavaDoc getPrincipalPassword() {
209         return this.principalPassword;
210     }
211     
212     /** Setter for property principalPassword.
213      * @param principalPassword New value of property principalPassword.
214      *
215      * @throws PropertyVetoException
216      *
217      */

218     public void setPrincipalPassword(String JavaDoc principalPassword) throws java.beans.PropertyVetoException JavaDoc {
219         String JavaDoc oldPrincipalPassword = this.principalPassword;
220         getVCS().fireVetoableChange("principalPassword", oldPrincipalPassword, principalPassword);
221         this.principalPassword = principalPassword;
222         getPCS().firePropertyChange("principalPassword", oldPrincipalPassword, principalPassword);
223     }
224     
225     /* ------------------------------------------------------------------------
226      * Persistence support. Loads DConfigBeans from previously saved Deployment
227      * plan file.
228      *
229     BaseBean getDDSnippet() {
230         return null;
231     }
232     
233     String getFileName() {
234         return getParent().getFileName();
235     }
236     */

237     Collection JavaDoc getSnippets() {
238         Collection JavaDoc snippets = new ArrayList JavaDoc();
239         Snippet snipOne = new DefaultSnippet() {
240             
241             public CommonDDBean getDDSnippet() {
242                 org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef ref =
243                     getConfig().getStorageFactory().createResourceRef();
244
245                 // write properties into Servlet bean
246
String JavaDoc resRefName = getResRefName();
247                 if(resRefName != null) {
248                     ref.setResRefName(resRefName);
249                 }
250
251                 if(jndiName != null && jndiName.length() > 0) {
252                     ref.setJndiName(jndiName);
253                 }
254
255                 boolean hasPrincipalName = (principalName != null && principalName.length() > 0);
256                 boolean hasPrincipalPassword = (principalPassword != null && principalPassword.length() > 0);
257                 
258                 if(hasPrincipalName || hasPrincipalPassword) {
259                     DefaultResourcePrincipal drp = ref.getDefaultResourcePrincipal();
260                     if(drp == null) {
261                         drp = ref.newDefaultResourcePrincipal();
262                     }
263
264                     if(hasPrincipalName) {
265                         drp.setName(principalName);
266                     }
267
268                     if(hasPrincipalPassword) {
269                         drp.setPassword(principalPassword);
270                     }
271
272                     ref.setDefaultResourcePrincipal(drp);
273                 }
274                 
275                 return ref;
276             }
277             
278             public boolean hasDDSnippet() {
279                 if(jndiName != null && jndiName.length() > 0) {
280                     return true;
281                 }
282                 
283                 if(principalName != null && principalName.length() > 0) {
284                     return true;
285                 }
286                 
287                 if(principalPassword != null && principalPassword.length() > 0) {
288                     return true;
289                 }
290                 
291                 return false;
292             }
293             
294
295             public String JavaDoc getPropertyName() {
296                 return SunWebApp.RESOURCE_REF;
297             }
298             
299         };
300         
301         snippets.add(snipOne);
302         return snippets;
303     }
304     
305 /*
306     private class ResourceRefFinder implements ConfigFinder {
307         private String beanName;
308
309         public ResourceRefFinder(String beanName) {
310             this.beanName = beanName;
311         }
312
313         public Object find(Object obj) {
314             org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef resEnvRef = null;
315             CommonDDBean root = (CommonDDBean) obj;
316             
317             String[] props = root.findPropertyValue(
318                 org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef.RES_REF_NAME, beanName);
319             
320             for(int i = 0; i < props.length; i++) {
321                 CommonDDBean candidate = root.graphManager().getPropertyParent(props[i]);
322                 if(candidate instanceof org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef) {
323                     resEnvRef = (org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef) candidate;
324                     break;
325                 }
326             }
327             
328             return resEnvRef;
329         }
330     }
331 */

332
333     private class ResourceRefFinder extends NameBasedFinder {
334         public ResourceRefFinder(String JavaDoc beanName) {
335             super(org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef.RES_REF_NAME,
336                 beanName, org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef.class);
337         }
338     }
339     
340     boolean loadFromPlanFile(SunONEDeploymentConfiguration config) {
341         String JavaDoc uriText = getUriText();
342
343         org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef beanGraph =
344             (org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef) config.getBeans(uriText,
345             constructFileName(), getParser(), new ResourceRefFinder(getResRefName()));
346         
347         clearProperties();
348         
349         if(beanGraph != null) {
350             jndiName = beanGraph.getJndiName();
351             
352             DefaultResourcePrincipal drp = beanGraph.getDefaultResourcePrincipal();
353             if(drp != null) {
354                 principalName = drp.getName();
355                 principalPassword = drp.getPassword();
356             }
357         } else {
358             setDefaultProperties();
359         }
360         
361         return (beanGraph != null);
362     }
363     
364     protected void clearProperties() {
365         jndiName = null;
366         principalName = null;
367         principalPassword = null;
368     }
369     
370     protected void setDefaultProperties() {
371         if(requiresJndiName()) {
372             jndiName = getResRefName();
373             getConfig().getMasterDCBRoot().setDirty();
374         }
375     }
376 }
377
Popular Tags