KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.MessageFormat JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
27 import javax.enterprise.deploy.model.DDBean JavaDoc;
28 import javax.enterprise.deploy.model.XpathEvent JavaDoc;
29
30 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
31 import org.netbeans.modules.j2ee.sun.dd.api.VersionNotSupportedException;
32 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp;
33 import org.netbeans.modules.j2ee.sun.dd.api.web.Servlet;
34 import org.netbeans.modules.j2ee.sun.share.configbean.Base.DefaultSnippet;
35
36
37 /** This DConfigBean is a child of SunWebApp.
38  *
39  * Property structure of ServletRef from sun-web-app DTD:
40  *
41  * servlet <servlet> : Servlet[0,n]
42  * servletName <servlet-name> : String
43  * principalName <principal-name> : String[0,1]
44  * [attr: class-name CDATA #IMPLIED ] *
45  *
46  * @author Peter Williams
47  */

48 public class ServletRef extends Base {
49     
50     /** property event names
51      */

52     public static final String JavaDoc SERVLET_NAME = "servletName"; // NOI18N
53
public static final String JavaDoc RUN_AS_ROLE_NAME = "runAsRoleName"; // NOI18N
54

55     
56     /** DDBean that refers to "servlet-name" child of bound DDBean. */
57     private DDBean JavaDoc servletNameDD;
58     
59     /** Holds value of property principalName. */
60     private String JavaDoc principalName;
61     
62     /** Holds value of property className. */
63     private String JavaDoc className;
64     
65     /** Creates a new instance of ServletRef */
66     public ServletRef() {
67         setDescriptorElement(bundle.getString("BDN_Servlet")); // NOI18N
68
}
69
70     /** Override init to enable grouping support for this bean and load name
71      * field from related DDBean.
72      * @param dDBean DDBean matching this bean
73      * @param parent Parent DConfigBean in the tree
74      */

75     protected void init(DDBean JavaDoc dDBean, Base parent) throws ConfigurationException JavaDoc {
76         super.init(dDBean, parent);
77         
78 // !PW Disable grouping code for now, spec non-compliance.
79
// initGroup(dDBean, parent);
80

81         servletNameDD = getNameDD("servlet-name");
82
83         updateNamedBeanCache(SunWebApp.SERVLET);
84
85         loadFromPlanFile(getConfig());
86     }
87
88     protected String JavaDoc getComponentName() {
89         return getServletName();
90     }
91     
92     /** -----------------------------------------------------------------------
93      * Validation implementation
94      */

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

135     public String JavaDoc getHelpId() {
136         return "AS_CFG_Servlet";
137     }
138     
139     /** Getter for property servlet-name.
140      * @return Value of property servlet-name.
141      *
142      */

143     public String JavaDoc getServletName() {
144         return cleanDDBeanText(servletNameDD);
145     }
146     
147     public String JavaDoc getRunAsRoleName() {
148         String JavaDoc roleName = null;
149         
150         DDBean JavaDoc[] beans = getDDBean().getChildBean("run-as/role-name"); // NOI18N
151
if(beans.length > 0) {
152             // beans[0] is the run-as element & it's role-name DD
153
roleName = beans[0].getText();
154         }
155         
156         return roleName;
157     }
158     
159     /** The DDBean (or one of it's children) that this DConfigBean is bound to
160      * has changed.
161      *
162      * @param xpathEvent
163      */

164     public void notifyDDChange(XpathEvent JavaDoc xpathEvent) {
165         super.notifyDDChange(xpathEvent);
166
167         DDBean JavaDoc eventBean = xpathEvent.getBean();
168         
169         if(eventBean == servletNameDD) {
170             // name changed...
171
getPCS().firePropertyChange(SERVLET_NAME, GenericOldValue, getServletName());
172             getPCS().firePropertyChange(DISPLAY_NAME, GenericOldValue, getDisplayName());
173             
174             updateNamedBeanCache(SunWebApp.SERVLET);
175         } else if(eventBean.getXpath().indexOf("run-as") != -1) {
176             getPCS().firePropertyChange(RUN_AS_ROLE_NAME, GenericOldValue, getRunAsRoleName());
177         }
178     }
179     
180     /** Getter for property principalName.
181      * @return Value of property principalName.
182      *
183      */

184     public String JavaDoc getPrincipalName() {
185         return principalName;
186     }
187     
188     /** Setter for property principalName.
189      * @param newPrincipalName New value of property principalName.
190      *
191      * @throws PropertyVetoException
192      *
193      */

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

205     public String JavaDoc getClassName() {
206         return className;
207     }
208     
209     /** Setter for property className.
210      * @param newClassName New value of property className.
211      *
212      * @throws PropertyVetoException
213      *
214      */

215     public void setClassName(String JavaDoc newClassName) throws java.beans.PropertyVetoException JavaDoc {
216         String JavaDoc oldClassName = className;
217         getVCS().fireVetoableChange("className", oldClassName, newClassName);
218         className = newClassName;
219         getPCS().firePropertyChange("className", oldClassName, className);
220     }
221     
222     /* ------------------------------------------------------------------------
223      * Persistence support. Loads DConfigBeans from previously saved Deployment
224      * plan file.
225      */

226     Collection JavaDoc getSnippets() {
227         Collection JavaDoc snippets = new ArrayList JavaDoc();
228         Snippet snipOne = new DefaultSnippet() {
229             public CommonDDBean getDDSnippet() {
230                 Servlet sg = getConfig().getStorageFactory().createServlet();
231
232                 // write properties into Servlet bean
233
String JavaDoc sn = getServletName();
234                 if(sn != null) {
235                     sg.setServletName(sn);
236                 }
237
238                 if(Utils.notEmpty(principalName)) {
239                     sg.setPrincipalName(principalName);
240                     if(Utils.notEmpty(className)) {
241                         try {
242                             sg.setPrincipalNameClassName(className);
243                         } catch (VersionNotSupportedException ex) {
244                             // Should not happen at runtime.
245
}
246                     }
247                 }
248                 
249                 return sg;
250             }
251             
252             public boolean hasDDSnippet() {
253                 // No need to check className, as principal name must be filled in that case.
254
if(principalName != null && principalName.length() > 0) {
255                     return true;
256                 }
257                 
258                 return false;
259             }
260             
261 /*
262             public BaseBean mergeIntoRootDD(BaseBean ddRoot) {
263                 SunWebApp swa = SunWebApp.createGraph();
264                 BaseBean newBean = getDDSnippet();
265                 swa.addValue(SunWebApp.SERVLET, newBean);
266                 ddRoot.merge(swa, BaseBean.MERGE_UNION);
267                 return newBean;
268             }
269  */

270
271 /*
272             public BaseBean mergeIntoRovingDD(BaseBean ddParent) {
273                 BaseBean newBean = getDDSnippet();
274                 ddParent.addValue(SunWebApp.SERVLET, newBean);
275                 return newBean;
276             }
277  */

278             public String JavaDoc getPropertyName() {
279                 return SunWebApp.SERVLET;
280             }
281         };
282         
283         snippets.add(snipOne);
284         return snippets;
285     }
286     
287     private class ServletRefFinder extends NameBasedFinder {
288         public ServletRefFinder(String JavaDoc beanName) {
289             super(Servlet.SERVLET_NAME, beanName, Servlet.class);
290         }
291     }
292     
293     boolean loadFromPlanFile(SunONEDeploymentConfiguration config) {
294         String JavaDoc uriText = getUriText();
295
296         Servlet beanGraph = (Servlet) config.getBeans(uriText, constructFileName(),
297             getParser(), new ServletRefFinder(getServletName()));
298         
299         clearProperties();
300         
301         if(beanGraph != null) {
302             principalName = beanGraph.getPrincipalName();
303             try {
304                 className = beanGraph.getPrincipalNameClassName();
305             } catch(VersionNotSupportedException ex) {
306                 // Should not happen at runtime.
307
}
308         } else {
309             setDefaultProperties();
310         }
311         
312         return (beanGraph != null);
313     }
314     
315     protected void clearProperties() {
316         principalName = null;
317         className = null;
318     }
319     
320     protected void setDefaultProperties() {
321     }
322 }
323
Popular Tags