KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > JSFFrameworkProvider


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.web.jsf;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.math.BigInteger JavaDoc;
31 import java.util.Set JavaDoc;
32 import org.netbeans.api.java.classpath.ClassPath;
33 import org.netbeans.modules.j2ee.dd.api.common.InitParam;
34 import org.netbeans.modules.j2ee.dd.api.web.*;
35 import org.netbeans.modules.web.jsf.api.ConfigurationUtils;
36 import org.netbeans.modules.web.jsf.wizards.JSFConfigurationPanel;
37 import org.openide.DialogDescriptor;
38 import org.openide.ErrorManager;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileSystem;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.filesystems.FileLock;
43 import org.openide.filesystems.Repository;
44 import org.netbeans.api.project.FileOwnerQuery;
45 import org.netbeans.api.project.Project;
46 import org.netbeans.api.project.libraries.Library;
47 import org.netbeans.api.project.libraries.LibraryManager;
48 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender;
49 import org.netbeans.modules.web.spi.webmodule.WebFrameworkProvider;
50 import org.netbeans.modules.web.api.webmodule.WebModule;
51 import org.netbeans.modules.web.spi.webmodule.FrameworkConfigurationPanel;
52 import org.openide.util.NbBundle;
53
54 /**
55  *
56  * @author Petr Pisl
57  */

58 public class JSFFrameworkProvider extends WebFrameworkProvider {
59     
60     private JSFConfigurationPanel panel;
61     /** Creates a new instance of JSFFrameworkProvider */
62     public JSFFrameworkProvider() {
63         super(
64                 NbBundle.getMessage(JSFFrameworkProvider.class, "JSF_Name"), // NOI18N
65
NbBundle.getMessage(JSFFrameworkProvider.class, "JSF_Description")); //NOI18N
66
}
67     
68     public Set JavaDoc extend(WebModule webModule) {
69         FileObject fileObject = webModule.getDocumentBase();Project project = FileOwnerQuery.getOwner(fileObject);
70         
71         try {
72             FileObject dd = webModule.getDeploymentDescriptor();
73             WebApp ddRoot = DDProvider.getDefault().getDDRoot(dd);
74             ClassPath cp = ClassPath.getClassPath(fileObject, ClassPath.COMPILE);
75             if (ddRoot != null){
76                 if (!WebApp.VERSION_2_5.equals(ddRoot.getVersion())) {
77                     Library jsfLibrary = LibraryManager.getDefault().getLibrary("jsf");
78                     if (jsfLibrary != null) {
79                         if (cp.findResource("javax/faces/FacesException.class") == null) { //NOI18N
80
ProjectClassPathExtender cpExtender = (ProjectClassPathExtender) project.getLookup().lookup(ProjectClassPathExtender.class);
81                             if (cpExtender != null) {
82                                 try {
83                                     cpExtender.addLibrary(jsfLibrary);
84                                     Library jstlLibrary = LibraryManager.getDefault().getLibrary("jstl11");
85                                     if (jstlLibrary != null){
86                                         cpExtender.addLibrary(jstlLibrary);
87                                     }
88                                 } catch (IOException JavaDoc ioe) {
89                                     // ErrorManager.getDefault().notify(ioe);
90
}
91                             }
92                         } else {
93                             // ErrorManager.getDefault().log ("WebProjectClassPathExtender not found in the project lookup of project: "+project.getProjectDirectory().getPath()); //NOI18N
94
}
95                     }
96                 }
97             }
98             boolean isMyFaces = cp.findResource("org/apache/myfaces/webapp/StartupServletContextListener.class") != null; //NOI18N
99
FileSystem fileSystem = webModule.getWebInf().getFileSystem();
100             fileSystem.runAtomicAction(new CreateFacesConfig(webModule, isMyFaces));
101         } catch (FileNotFoundException JavaDoc exc) {
102             ErrorManager.getDefault().notify(exc);
103         } catch (IOException JavaDoc exc) {
104             ErrorManager.getDefault().notify(exc);
105         }
106         return null;
107     }
108     
109     public static String JavaDoc readResource(InputStream JavaDoc is, String JavaDoc encoding) throws IOException JavaDoc {
110         // read the config from resource first
111
StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
112         String JavaDoc lineSep = System.getProperty("line.separator");//NOI18N
113
BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is, encoding));
114         String JavaDoc line = br.readLine();
115         while (line != null) {
116             sbuffer.append(line);
117             sbuffer.append(lineSep);
118             line = br.readLine();
119         }
120         br.close();
121         return sbuffer.toString();
122     }
123     
124     public java.io.File JavaDoc[] getConfigurationFiles(org.netbeans.modules.web.api.webmodule.WebModule wm) {
125         // The JavaEE 5 introduce web modules without deployment descriptor. In such wm can not be jsf used.
126
FileObject dd = wm.getDeploymentDescriptor();
127         if (dd != null){
128             FileObject[] filesFO = ConfigurationUtils.getFacesConfigFiles(wm);
129             File JavaDoc[] files = new File JavaDoc[filesFO.length];
130             for (int i = 0; i < filesFO.length; i++)
131                 files[i] = FileUtil.toFile(filesFO[i]);
132             if (files.length > 0)
133                 return files;
134         }
135         return null;
136     }
137     
138     public FrameworkConfigurationPanel getConfigurationPanel(WebModule webModule) {
139         boolean defaultValue = (webModule == null || !isInWebModule(webModule));
140         panel = new JSFConfigurationPanel(!defaultValue);
141         if (!defaultValue){
142             // get configuration panel with values from the wm
143
Servlet servlet = ConfigurationUtils.getFacesServlet(webModule);
144             panel.setServletName(servlet.getServletName());
145             panel.setURLPattern(ConfigurationUtils.getFacesServletMapping(webModule));
146             panel.setValidateXML(JSFConfigUtilities.validateXML(webModule.getDeploymentDescriptor()));
147             panel.setVerifyObjects(JSFConfigUtilities.verifyObjects(webModule.getDeploymentDescriptor()));
148         }
149         
150         return panel;
151     }
152     
153     public boolean isInWebModule(org.netbeans.modules.web.api.webmodule.WebModule webModule) {
154         // The JavaEE 5 introduce web modules without deployment descriptor. In such wm can not be jsf used.
155
FileObject dd = webModule.getDeploymentDescriptor();
156         return (dd != null && ConfigurationUtils.getFacesServlet(webModule) != null);
157     }
158     
159     public String JavaDoc getServletPath(FileObject file){
160         String JavaDoc url = null;
161         if (file == null) return url;
162         
163         WebModule wm = WebModule.getWebModule(file);
164         if (wm != null){
165             url = FileUtil.getRelativePath(wm.getDocumentBase(), file);
166             if (url.charAt(0)!='/')
167                 url = "/" + url;
168             String JavaDoc mapping = ConfigurationUtils.getFacesServletMapping(wm);
169             if (mapping != null && !"".equals(mapping)){
170                 if (mapping.endsWith("/*")){
171                     mapping = mapping.substring(0, mapping.length()-2);
172                     url = mapping + url;
173                 }
174             }
175         }
176         return url;
177     }
178     
179     public static void createFile(FileObject target, String JavaDoc content, String JavaDoc encoding) throws IOException JavaDoc{
180         FileLock lock = target.lock();
181         try {
182             BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(target.getOutputStream(lock), encoding));
183             bw.write(content);
184             bw.close();
185             
186         } finally {
187             lock.releaseLock();
188         }
189     }
190     
191     private class CreateFacesConfig implements FileSystem.AtomicAction{
192         WebModule webModule;
193         boolean isMyFaces;
194         
195         public CreateFacesConfig(WebModule webModule, boolean isMyFaces){
196             this.webModule = webModule;
197             this.isMyFaces = isMyFaces;
198         }
199         
200         public void run() throws IOException JavaDoc {
201             // Enter servlet into the deployment descriptor
202
FileObject dd = webModule.getDeploymentDescriptor();
203             WebApp ddRoot = DDProvider.getDefault().getDDRoot(dd);
204             if (ddRoot != null){
205                 try{
206                     Servlet servlet = (Servlet)ddRoot.createBean("Servlet"); //NOI18N
207
String JavaDoc servletName = panel == null ? "Faces Servlet" : panel.getServletName();
208                     servlet.setServletName(servletName); //NOI18N
209
servlet.setServletClass("javax.faces.webapp.FacesServlet"); //NOI18N
210
ddRoot.addServlet(servlet);
211                     
212                     servlet.setLoadOnStartup(new BigInteger JavaDoc("1"));//NOI18N
213

214                     
215                     ServletMapping mapping = (ServletMapping)ddRoot.createBean("ServletMapping"); //NOI18N
216
mapping.setServletName(servletName);//NOI18N
217
mapping.setUrlPattern(panel == null ? "/faces/*" : panel.getURLPattern());//NOI18N
218

219                     ddRoot.addServletMapping(mapping);
220                     
221                     InitParam contextParam = (InitParam)ddRoot.createBean("InitParam"); //NOI18N
222
contextParam.setParamName("com.sun.faces.verifyObjects"); //NOI18N
223
if (panel != null && panel.verifyObjects())
224                         contextParam.setParamValue("true"); //NOI18N
225
else
226                         contextParam.setParamValue("false"); //NOI18N
227
ddRoot.addContextParam(contextParam);
228                     
229                     contextParam = (InitParam)ddRoot.createBean("InitParam"); //NOI18N
230
contextParam.setParamName("com.sun.faces.validateXml"); //NOI18N
231
if(panel == null || panel.validateXML())
232                         contextParam.setParamValue("true"); //NOI18N
233
else
234                         contextParam.setParamValue("false"); //NOI18N
235
ddRoot.addContextParam(contextParam);
236                     
237                     contextParam = (InitParam)ddRoot.createBean("InitParam"); //NOI18N
238
contextParam.setParamName("javax.faces.STATE_SAVING_METHOD"); //NOI18N
239
contextParam.setParamValue("client"); //NOI18N
240
ddRoot.addContextParam(contextParam);
241                     
242                     if (isMyFaces) {
243                         Listener facesListener = (Listener) ddRoot.createBean("Listener");
244                         facesListener.setListenerClass("org.apache.myfaces.webapp.StartupServletContextListener");
245                         ddRoot.addListener(facesListener);
246                     }
247                     ddRoot.write(dd);
248                     
249                     
250                 } catch (ClassNotFoundException JavaDoc cnfe){
251                     ErrorManager.getDefault().notify(cnfe);
252                 }
253             }
254             
255             // copy faces-config.xml
256
if (canCreateNewFile(webModule.getWebInf(),"faces-config.xml")){
257                 String JavaDoc facesConfigTemplate = "faces-config.xml"; //NOI18N
258
if (ddRoot != null) {
259                     if (WebApp.VERSION_2_5.equals(ddRoot.getVersion())) {
260                         facesConfigTemplate = "faces-config_1_2.xml"; //NOI18N
261
}
262                 }
263                 String JavaDoc content = readResource(Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-jsf/" + facesConfigTemplate).getInputStream(), "UTF-8"); //NOI18N
264
FileObject target = FileUtil.createData(webModule.getWebInf(), "faces-config.xml");//NOI18N
265
createFile(target, content, "UTF-8"); //NOI18N
266
}
267             
268             //copy Welcome.jsp
269
if (canCreateNewFile(webModule.getDocumentBase(), "welcomeJSF.jsp")){
270                 String JavaDoc content = readResource(Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-jsf/welcomeJSF.jsp").getInputStream(), "UTF-8"); //NOI18N
271
FileObject target = FileUtil.createData(webModule.getDocumentBase(), "welcomeJSF.jsp");//NOI18N
272
createFile(target, content, "UTF-8"); //NOI18N
273

274                 FileObject documentBase = webModule.getDocumentBase();
275                 FileObject indexjsp = documentBase.getFileObject("index.jsp"); //NOI18N
276
if (indexjsp != null){
277                     changeIndexJSP(indexjsp);
278                 }
279             }
280             
281         }
282         
283         private boolean canCreateNewFile(FileObject parent, String JavaDoc name){
284             File JavaDoc fileToBe = new File JavaDoc(FileUtil.toFile(parent), name);
285             boolean create = true;
286             if (fileToBe.exists()){
287                 DialogDescriptor dialog = new DialogDescriptor(
288                         NbBundle.getMessage(JSFFrameworkProvider.class, "MSG_OverwriteFile", fileToBe.getAbsolutePath()),
289                         NbBundle.getMessage(JSFFrameworkProvider.class, "TTL_OverwriteFile"),
290                         true, DialogDescriptor.YES_NO_OPTION, DialogDescriptor.NO_OPTION, null);
291                 java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
292                 d.setVisible(true);
293                 create = (dialog.getValue() == org.openide.DialogDescriptor.NO_OPTION);
294             }
295             return create;
296         }
297         
298         /** Changes the index.jsp file. Only when there is <h1>JSP Page</h1> string.
299          */

300         private void changeIndexJSP(FileObject indexjsp) throws IOException JavaDoc {
301             
302             String JavaDoc content = readResource(indexjsp.getInputStream(), "UTF-8"); //NO18N
303

304             // what find
305
String JavaDoc find = "<h1>JSP Page</h1>"; // NOI18N
306
String JavaDoc endLine = System.getProperty("line.separator"); //NOI18N
307
if ( content.indexOf(find) > 0){
308                 StringBuffer JavaDoc replace = new StringBuffer JavaDoc();
309                 replace.append(find);
310                 replace.append(endLine);
311                 replace.append(" <br/>"); //NOI18N
312
replace.append(endLine);
313                 replace.append(" <a HREF=\"."); //NOI18N
314
replace.append(ConfigurationUtils.translateURI(panel == null ? "/faces/*" : panel.getURLPattern(),"/welcomeJSF.jsp")); //NOI18N
315
replace.append("\">"); //NOI18N
316
replace.append(NbBundle.getMessage(JSFFrameworkProvider.class,"LBL_JSF_WELCOME_PAGE"));
317                 replace.append("</a>"); //NOI18N
318
content = content.replaceFirst(find, new String JavaDoc(replace.toString().getBytes("UTF8"), "UTF-8")); //NOI18N
319
createFile(indexjsp, content, "UTF-8"); //NOI18N
320
}
321         }
322     }
323     
324 }
325
Popular Tags