KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 import javax.enterprise.deploy.model.DDBean JavaDoc;
26 import javax.enterprise.deploy.model.DDBeanRoot JavaDoc;
27 import javax.enterprise.deploy.spi.DeploymentConfiguration JavaDoc;
28 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
29
30 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
31 import org.netbeans.modules.j2ee.sun.dd.api.DDProvider;
32 import org.netbeans.modules.j2ee.sun.dd.api.app.SunApplication;
33 import org.netbeans.modules.j2ee.sun.dd.api.app.Web;
34
35
36 /** This bean is contained by an AppRoot and represents a reference to a web
37  * module contained in that application
38  *
39  * @auther Peter Williams
40  */

41 public class WebAppRef extends BaseModuleRef {
42
43     /** -----------------------------------------------------------------------
44      * Initialization
45      */

46     
47     /** Creates new WebAppRef
48      */

49     public WebAppRef() {
50     }
51     
52     protected void init(DDBean JavaDoc dDBean, Base parent) throws ConfigurationException JavaDoc {
53         super.init(dDBean, parent);
54
55         contextRootDD = getNameDD("context-root");
56         loadFromPlanFile(getConfig());
57     }
58     
59     /** Called from BaseModuleRef.init() to get the correct module URI field
60      * for the reference object.
61      */

62     protected void initModuleUri(DDBean JavaDoc dDBean) {
63         DDBean JavaDoc[] uriBeans = dDBean.getChildBean("web-uri");
64         if(uriBeans.length > 0) {
65             setModuleUri(uriBeans[0]);
66         } else {
67             setModuleUri(null);
68         }
69     }
70     
71     /** Getter for customizer title fragment property
72      * @return String fragment for use in customizer title
73      *
74      */

75     public String JavaDoc getTitleFragment() {
76         return bundle.getString("LBL_WebTitleFragment"); // NOI18N
77
}
78     
79     /** -----------------------------------------------------------------------
80      * JSR-88 Interface support
81      */

82     
83     // All JSR-88 methods inherited from base
84

85     /** -----------------------------------------------------------------------
86      * Properties
87      */

88
89     /** Holds value of property webUri. */
90 // private DDBean webUri = null;
91

92     /** Holds value of property contextRoot. */
93     private DDBean JavaDoc contextRootDD = null;
94     private String JavaDoc contextRoot;
95     
96     /** Getter for property webURI.
97      * @return Value of property webURI.
98      *
99      */

100     public String JavaDoc getWebUri() {
101         return getModuleUri();
102     }
103     
104     /** Getter for property contextRoot.
105      * @return Value of property contextRoot.
106      *
107      */

108     public String JavaDoc getContextRoot() {
109         String JavaDoc result = null;
110         
111         /* if a contextRoot for this module has not been set, get the context
112          * root from the application.xml, otherwise, use the explicit setting
113          */

114         if(contextRoot == null || contextRoot.length() == 0 && contextRootDD != null) {
115             result = contextRootDD.getText();
116         } else {
117             result = contextRoot;
118         }
119         
120         return result;
121     }
122     
123     /** Setter for property contextRoot.
124      * @param newContextRoot New value of property contextRoot.
125      * @throws PropertyVetoException if the property change is vetoed
126      */

127     public void setContextRoot(String JavaDoc newContextRoot) throws java.beans.PropertyVetoException JavaDoc {
128         if (newContextRoot!=null){
129             newContextRoot = newContextRoot.replace (' ', '_'); //NOI18N
130
}
131         if (newContextRoot!=null){ //see bug 56280
132
try{
133                 String JavaDoc result="";
134                 String JavaDoc s[] = newContextRoot.split("/");
135                 for (int i=0;i<s.length;i++){
136                     result=result+java.net.URLEncoder.encode(s[i], "UTF-8");
137                     if (i!=s.length -1)
138                         result=result+"/";
139                 }
140                 newContextRoot= result;
141             }
142             catch (Exception JavaDoc e){
143
144             }
145         }
146         String JavaDoc oldContextRoot = contextRoot;
147         getVCS().fireVetoableChange("contextRoot", oldContextRoot, newContextRoot);
148         contextRoot = newContextRoot;
149         getPCS().firePropertyChange("contextRoot", oldContextRoot, contextRoot);
150     }
151
152
153         public String JavaDoc getHelpId() {
154             return "AS_CFG_WebAppRef"; //NOI18N
155
}
156
157
158     /** -----------------------------------------------------------------------
159      * Persistance support
160      */

161      Collection JavaDoc getSnippets() {
162         Collection JavaDoc snippets = new ArrayList JavaDoc();
163         Snippet snipOne = new DefaultSnippet() {
164             
165             public CommonDDBean getDDSnippet() {
166                 Web web = getConfig().getStorageFactory().createWeb();
167                 
168                 web.setWebUri(getWebUri());
169                 web.setContextRoot(getContextRoot());
170                 
171                 return web;
172             }
173             
174             public boolean hasDDSnippet() {
175                 if(Utils.notEmpty(contextRoot)) {
176                     return true;
177                 }
178
179                 return false;
180             }
181              
182             public String JavaDoc getPropertyName() {
183                 return SunApplication.WEB;
184             }
185         };
186         
187         snippets.add(snipOne);
188         return snippets;
189     }
190
191     private class WebAppRefFinder implements ConfigFinder {
192         public Object JavaDoc find(Object JavaDoc obj) {
193             Web result = null;
194             String JavaDoc webUri = getWebUri();
195             
196             if(obj instanceof SunApplication && webUri != null) {
197                 SunApplication sa = (SunApplication) obj;
198                 Web [] webModules = sa.getWeb();
199                 
200                 for(int i = 0; i < webModules.length; i++) {
201                     if(webUri.compareTo(webModules[i].getWebUri()) == 0) {
202                         result = webModules[i];
203                         break;
204                     }
205                 }
206             }
207             
208             return result;
209         }
210     }
211         
212     boolean loadFromPlanFile(SunONEDeploymentConfiguration config) {
213         String JavaDoc uriText = getUriText();
214         
215         Web beanGraph = (Web) config.getBeans(
216             uriText, constructFileName(), getParser(), new WebAppRefFinder());
217         
218         clearProperties();
219         
220         if(beanGraph != null) {
221             String JavaDoc cr = beanGraph.getContextRoot();
222             if(Utils.notEmpty(cr) && cr.compareTo(getContextRoot()) != 0) {
223                 contextRoot = cr;
224             }
225         } else {
226             setDefaultProperties();
227         }
228         
229         return (beanGraph != null);
230     }
231     
232     protected void clearProperties() {
233         contextRoot = null;
234     }
235     
236     protected void setDefaultProperties() {
237         // no defaults
238
}
239 }
240
Popular Tags