KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
26 import com.sun.enterprise.deployment.*;
27 import com.sun.enterprise.deployment.web.InitializationParameter;
28 import com.sun.enterprise.deployment.web.SecurityRoleReference;
29
30     /**
31     * I am a superclass representing the common data nad behavior of the deployment
32     * information about a JSP or JavaServlet in J2EE.
33     *
34     *@author Danny Coward
35     */

36 public class WebComponentDescriptor extends Descriptor {
37     
38     private WebBundleDescriptor webBundleDescriptor = null;
39     /** Constant for Basic authentication.*/
40     public static String JavaDoc BASIC_AUTHENTICATION = "basic";
41     /** Constant for Form authentication.*/
42     public static String JavaDoc FORM_AUTHENTICATION = "form";
43     /** Constant for Secure authentication.*/
44     public static String JavaDoc SSL_AUTHENTICATION = "ssl";
45     /** Constant for the htpp GET method. */
46     public static String JavaDoc GET = "GET";
47     /** Constant for the http PUT method.*/
48     public static String JavaDoc PUT = "PUT";
49     /** Constant for the http POST method.*/
50     public static String JavaDoc POST = "POST";
51     /** Constant for the http DELET method.*/
52     public static String JavaDoc DELETE = "DELETE";
53     
54     private Set initializationParameters;
55     private Set urlPatterns;
56     private String JavaDoc canonicalName;
57     private int loadOnStartUp = -1;
58     private Set securityRoleReferences;
59     
60     private RunAsIdentityDescriptor runAs;
61     
62     /**
63     * The default constructor.
64     */

65
66     public WebComponentDescriptor() {}
67     
68     private Set getInitializationParameterSet() {
69     if (this.initializationParameters == null) {
70         this.initializationParameters = new OrderedSet();
71     }
72     return this.initializationParameters = new OrderedSet(this.initializationParameters);
73     }
74     
75     /**
76     * Return the Set of servlet initialization parameters.
77     */

78     public Enumeration getInitializationParameters() {
79     return (new Vector(this.getInitializationParameterSet())).elements();
80     }
81     
82     /**
83     * Return a matching initialization parameter by its name if there is one.
84     */

85     public InitializationParameter getInitializationParameterByName(String JavaDoc name) {
86     for (Iterator itr = this.getInitializationParameterSet().iterator(); itr.hasNext();) {
87         InitializationParameter next = (InitializationParameter) itr.next();
88         if (next.getName().equals(name)) {
89         return next;
90         }
91     }
92     return null;
93     }
94     
95     /**
96     * Adds a servlet initialization parameter to this component.
97     */

98     public void addInitializationParameter(InitializationParameter initializationParameter) {
99     this.getInitializationParameterSet().add(initializationParameter);
100     this.changed();
101     }
102     /**
103     * Removes the given servlet initialization parameter from this component.
104     */

105     public void removeInitializationParameter(InitializationParameter initializationParameter) {
106     this.getInitializationParameterSet().remove(initializationParameter);
107     this.changed();
108     }
109
110     /**
111     * Return the set of URL pattern aliases for this component.
112     */

113     public Set getUrlPatternsSet() {
114     if (this.urlPatterns == null) {
115         this.urlPatterns = new OrderedSet();
116     }
117     return this.urlPatterns;
118     }
119
120     /**
121     * Returns an enumeration of (String) URL pattern aliases for this component.
122     */

123     public Enumeration getUrlPatterns() {
124     return (new Vector(this.getUrlPatternsSet())).elements();
125     }
126     
127     /**
128     * Adds an alias to this web component.
129     */

130     public void addUrlPattern(String JavaDoc urlPattern) {
131     this.getUrlPatternsSet().add(urlPattern);
132     this.changed();
133     }
134     
135     /**
136     * Removes a URL pattern from this web component.
137     */

138     public void removeUrlPattern(String JavaDoc urlPattern) {
139     this.getUrlPatternsSet().remove(urlPattern);
140     this.changed();
141     }
142     
143     void setWebBundleDescriptor(WebBundleDescriptor webBundleDescriptor) {
144     this.webBundleDescriptor = webBundleDescriptor;
145     }
146     
147     /**
148     * Return the web app object to which I belong or null
149     */

150     public WebBundleDescriptor getWebBundleDescriptor() {
151     return this.webBundleDescriptor;
152     }
153     
154     /**
155     * The canonical name for the web component.
156     */

157     public String JavaDoc getCanonicalName() {
158     if (this.canonicalName == null) {
159         this.canonicalName = this.getName();
160     }
161     return this.canonicalName;
162     }
163     
164     /**
165     * Sets the canonical name of this web component.
166     */

167     public void setCanonicalName(String JavaDoc canonicalName) {
168     this.canonicalName = canonicalName;
169     this.changed();
170     }
171     
172     /**
173     * Return the order on which this component will be loaded by the web server.
174     */

175     public int getLoadOnStartUp() {
176     return this.loadOnStartUp;
177     }
178     
179     /**
180     * Sets the order on which this component will be loaded by the web server.
181     */

182     public void setLoadOnStartUp(int loadOnStartUp) {
183     this.loadOnStartUp = loadOnStartUp;
184     this.changed();
185     }
186     
187     /**
188     * Sets the order on which this component will be loaded by the web server.
189     */

190     public void setLoadOnStartUp(String JavaDoc loadOnStartUp) throws NumberFormatException JavaDoc {
191         this.loadOnStartUp = Integer.decode(loadOnStartUp).intValue();
192         this.changed();
193     }
194     
195     Set getSecurityRoleReferenceSet() {
196     if (this.securityRoleReferences == null) {
197         this.securityRoleReferences = new OrderedSet();
198     }
199     return this.securityRoleReferences = new OrderedSet(this.securityRoleReferences);
200     }
201     
202     /**
203     * Return the Set of security role references that I have.
204     */

205     public Enumeration getSecurityRoleReferences() {
206     return (new Vector(this.getSecurityRoleReferenceSet())).elements();
207     }
208     
209     /**
210     * Return a matching role reference by name or null if there is none matching.
211     */

212     public SecurityRoleReference getSecurityRoleReferenceByName(String JavaDoc roleReferenceName) {
213     for (Enumeration e = this.getSecurityRoleReferences(); e.hasMoreElements();) {
214         SecurityRoleReference nextRR = (SecurityRoleReference) e.nextElement();
215         if (nextRR.getRolename().equals( roleReferenceName )) {
216         return nextRR;
217         }
218     }
219     return null;
220     }
221     
222     /**
223     * Adds a security role reference to this web component.
224     */

225     public void addSecurityRoleReference(SecurityRoleReference securityRoleReference) {
226     this.getSecurityRoleReferenceSet().add(securityRoleReference);
227     this.changed();
228     }
229     
230     /**
231     * Removes the given security role reference from this web component.
232     */

233     public void removeSecurityRoleReference(SecurityRoleReference securityRoleReference) {
234     this.getSecurityRoleReferenceSet().remove(securityRoleReference);
235     this.changed();
236     }
237     
238      /**
239       * Gets the run-as of the referee EJB.
240       * @return the value of run-as.
241       */

242      public void setRunAsIdentity(RunAsIdentityDescriptor runAs) {
243          if (this.runAs == null) {
244              this.runAs = runAs;
245          }
246      }
247      
248     /**
249       * Sets the run-as of the referee EJB.
250       * @param the value of run-as
251       */

252      public RunAsIdentityDescriptor getRunAsIdentity() {
253          return this.runAs;
254      }
255
256      public boolean getUsesCallerIdentity() {
257     return (this.runAs==null);
258      }
259
260      public void setUsesCallerIdentity(boolean isCallerID) {
261     if (isCallerID) {
262         this.runAs = null;
263     } else {
264         this.runAs = new RunAsIdentityDescriptor("");
265         }
266      }
267
268      public Application getApplication() {
269     if (this.getWebBundleDescriptor()!=null) {
270         return this.getWebBundleDescriptor().getApplication();
271         }
272     return null;
273      }
274      
275      private String JavaDoc implFile="";
276      private boolean isServlet=false;
277      
278      /**
279       * sets the implementation file for this web component, the
280       * implementation file is either a servlet class name of a jsp
281       * file name.
282       * @param implFile the servlet class name or the jsp file
283       */

284      public void setWebComponentImplementation(String JavaDoc implFile) {
285          if (!isServlet && !implFile.startsWith("/")) {
286         implFile = "/" + implFile;
287      }
288          this.implFile = implFile;
289          changed();
290      }
291      
292      public String JavaDoc getWebComponentImplementation() {
293          return implFile;
294      }
295      
296      public boolean isServlet() {
297          return isServlet;
298      }
299      
300      public void setServlet(boolean isServlet) {
301          this.isServlet=isServlet;
302      }
303      
304     /* -----------
305     */

306
307     /**
308     * A formatted string representing my state.
309     */

310     public void print(StringBuffer JavaDoc toStringBuffer) {
311     super.print(toStringBuffer);
312         toStringBuffer.append("WebComponentDescriptor\n");
313     toStringBuffer.append( "\n initializationParameters ").append(initializationParameters);
314     toStringBuffer.append( "\n urlPatterns ").append(urlPatterns);
315     toStringBuffer.append( "\n canonicalName ").append(canonicalName);
316     toStringBuffer.append( "\n loadOnStartUp ").append(loadOnStartUp);
317     toStringBuffer.append( "\n securityRoleReferences ").append(securityRoleReferences);
318         if (isServlet()) {
319             toStringBuffer.append( "\n servlet className ").append(getWebComponentImplementation());
320         } else {
321             toStringBuffer.append( "\n jspFileName ").append(getWebComponentImplementation());
322         }
323     }
324
325         
326     public void changed() {
327     if (this.getWebBundleDescriptor() != null) {
328         this.getWebBundleDescriptor().changed();
329     } else {
330         super.changed();
331     }
332     }
333
334     public boolean equals(Object JavaDoc other) {
335         if (other instanceof WebComponentDescriptor &&
336             this.getCanonicalName().equals(((
337             WebComponentDescriptor)other).getCanonicalName())) {
338                 return true;
339         }
340         return false;
341     }
342
343     // this method will combine the information from this "other"
344
// WebComponentDescriptor with current WebComponentDescriptor
345
//
346
// when there are conflicts between the contents of the two,
347
// the value from current WebComponentDescriptor will override
348
// the value in "other"
349
//
350
// Note: in the Set API, we only add value when such value
351
// is not existed in the Set already
352
public void add(WebComponentDescriptor other) {
353         // do not do anything if the canonical name of the two web
354
// components are different
355
if (! getCanonicalName().equals(other.getCanonicalName())) {
356             return;
357         }
358         // do not do anything if the type of the two web
359
// components are different
360
if ( (isServlet() && !other.isServlet()) ||
361              (!isServlet() && other.isServlet()) ) {
362             return;
363         }
364
365         // for simple String types, we can rely on Set API
366
getUrlPatternsSet().addAll(other.getUrlPatternsSet());
367
368         // for complex types, only added it if the complex type with same
369
// name is not in the set yet
370

371         for (Iterator initParamIter =
372             other.getInitializationParameterSet().iterator();
373             initParamIter.hasNext();) {
374             InitializationParameter initParam =
375                 (InitializationParameter)initParamIter.next();
376             if (getInitializationParameterByName(initParam.getName()) == null) {
377                 getInitializationParameterSet().add(initParam);
378             }
379         }
380         for (Iterator secRoleRefIter =
381             other.getSecurityRoleReferenceSet().iterator();
382             secRoleRefIter.hasNext();) {
383             SecurityRoleReference secRoleRef =
384                 (SecurityRoleReference) secRoleRefIter.next();
385             if (getSecurityRoleReferenceByName(secRoleRef.getRolename())
386                 == null) {
387                 getSecurityRoleReferenceSet().add(secRoleRef);
388             }
389         }
390
391         // only set these values if they are not set in the current
392
// web component already
393

394         if (getLoadOnStartUp() == -1) {
395             setLoadOnStartUp(other.getLoadOnStartUp());
396         }
397         if (getRunAsIdentity() == null) {
398             setRunAsIdentity(other.getRunAsIdentity());
399         }
400         if (getWebComponentImplementation() == null) {
401             setWebComponentImplementation(
402                 other.getWebComponentImplementation());
403         }
404     }
405 }
406
407
Popular Tags