KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > struts > StrutsFrameworkProvider


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.struts;
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.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Set JavaDoc;
34 import org.netbeans.api.java.project.JavaProjectConstants;
35 import org.netbeans.api.project.ProjectUtils;
36 import org.netbeans.api.project.SourceGroup;
37 import org.netbeans.modules.j2ee.dd.api.common.CreateCapability;
38 import org.netbeans.modules.j2ee.dd.api.common.InitParam;
39 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException;
40 import org.netbeans.modules.j2ee.dd.api.web.DDProvider;
41 import org.netbeans.modules.j2ee.dd.api.web.JspConfig;
42 import org.netbeans.modules.j2ee.dd.api.web.Servlet;
43 import org.netbeans.modules.j2ee.dd.api.web.ServletMapping;
44 import org.netbeans.modules.j2ee.dd.api.web.Taglib;
45 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
46 import org.netbeans.modules.web.struts.config.model.MessageResources;
47 import org.openide.ErrorManager;
48
49 import org.openide.filesystems.FileObject;
50 import org.openide.filesystems.FileSystem;
51 import org.openide.filesystems.FileUtil;
52 import org.openide.filesystems.FileLock;
53 import org.openide.filesystems.Repository;
54
55 import org.netbeans.api.project.FileOwnerQuery;
56 import org.netbeans.api.project.Project;
57 import org.netbeans.api.project.libraries.Library;
58 import org.netbeans.api.project.libraries.LibraryManager;
59
60 import org.netbeans.modules.web.spi.webmodule.WebFrameworkProvider;
61 import org.netbeans.modules.web.api.webmodule.WebModule;
62 import org.netbeans.modules.web.spi.webmodule.FrameworkConfigurationPanel;
63
64 import org.netbeans.modules.web.struts.ui.StrutsConfigurationPanel;
65
66 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender;
67 import org.openide.DialogDescriptor;
68 import org.openide.util.NbBundle;
69
70
71 /**
72  *
73  * @author petr
74  */

75 public class StrutsFrameworkProvider extends WebFrameworkProvider {
76     
77     
78     private static final String JavaDoc STRUTS_CONFIG ="nbres:/org/netbeans/modules/web/struts/resources/struts-config.xml";
79     
80     private StrutsConfigurationPanel panel;
81     private static String JavaDoc defaultAppResource ="com.myapp.struts.ApplicationResource"; //NOI18N
82

83     public StrutsFrameworkProvider(){
84         super (
85                 NbBundle.getMessage(StrutsFrameworkProvider.class, "Sruts_Name"), //NOI18N
86
NbBundle.getMessage(StrutsFrameworkProvider.class, "Sruts_Description")); //NOI18N
87
}
88
89     public Set JavaDoc extend (WebModule wm) {
90         FileObject fo = wm.getDocumentBase();
91         Project project = FileOwnerQuery.getOwner(fo);
92         
93         Library lib = LibraryManager.getDefault().getLibrary("struts"); //NOI18N
94
if (lib != null) {
95             ProjectClassPathExtender cpExtender = (ProjectClassPathExtender) project.getLookup().lookup(ProjectClassPathExtender.class);
96             if (cpExtender != null) {
97                 try {
98                     cpExtender.addLibrary(lib);
99                 } catch (IOException JavaDoc ioe) {
100                     ErrorManager.getDefault().notify(ioe);
101
102                 }
103             } else {
104                 ErrorManager.getDefault().log ("WebProjectClassPathExtender not found in the project lookup of project: "+project.getProjectDirectory().getPath()); //NOI18N
105
}
106
107             try {
108                 FileSystem fs = wm.getWebInf().getFileSystem();
109                 fs.runAtomicAction(new CreateStrutsConfig(wm));
110               
111             } catch (FileNotFoundException JavaDoc exc) {
112                 ErrorManager.getDefault().notify(exc);
113                 return null;
114             } catch (IOException JavaDoc exc) {
115                 ErrorManager.getDefault().notify(exc);
116                 return null;
117             }
118         }
119         FileObject welcomePage = wm.getDocumentBase().getFileObject("welcome.jsp");
120         return null;
121     }
122     
123     private static String JavaDoc readResource(InputStream JavaDoc is, String JavaDoc encoding) throws IOException JavaDoc {
124         // read the config from resource first
125
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
126         String JavaDoc lineSep = System.getProperty("line.separator");//NOI18N
127
BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is, encoding));
128         String JavaDoc line = br.readLine();
129         while (line != null) {
130             sb.append(line);
131             sb.append(lineSep);
132             line = br.readLine();
133         }
134         br.close();
135         return sb.toString();
136     }
137
138     public java.io.File JavaDoc[] getConfigurationFiles(org.netbeans.modules.web.api.webmodule.WebModule wm) {
139         FileObject webinf = wm.getWebInf();
140         List JavaDoc files = new ArrayList JavaDoc();
141         // The JavaEE 5 introduce web modules without deployment descriptor.
142
// In such wm can not be struts used.
143
FileObject dd = wm.getDeploymentDescriptor();
144         if (dd != null){
145             FileObject[] configs = StrutsConfigUtilities.getConfigFilesFO(dd);
146             for (int i = 0; i < configs.length; i ++){
147                 files.add(FileUtil.toFile(configs[i]));
148             }
149             FileObject fo = webinf.getFileObject("tiles-defs.xml"); //NOI18N
150
if (fo != null) files.add(FileUtil.toFile(fo));
151             fo = webinf.getFileObject("validation.xml"); //NOI18N
152
if (fo != null) files.add(FileUtil.toFile(fo));
153             fo = webinf.getFileObject("validator-rules.xml"); //NOI18N
154
if (fo != null) files.add(FileUtil.toFile(fo));
155         }
156         
157         File JavaDoc [] rFiles = new File JavaDoc [files.size()];
158         files.toArray(rFiles);
159         return rFiles;
160     }
161
162     public boolean isInWebModule(org.netbeans.modules.web.api.webmodule.WebModule wm) {
163         // The JavaEE 5 introduce web modules without deployment descriptor.
164
// In such wm can not be struts used.
165
FileObject dd = wm.getDeploymentDescriptor();
166         return (dd != null && StrutsConfigUtilities.getActionServlet(dd) != null);
167     }
168     
169     public FrameworkConfigurationPanel getConfigurationPanel(WebModule wm) {
170         boolean defaultValue = (wm == null || !isInWebModule(wm));
171         panel = new StrutsConfigurationPanel(!defaultValue);
172         if (defaultValue){
173             // get configuration panel with default value
174
panel.setAppResource(defaultAppResource);
175         }
176         else {
177             // get configuration panel with values from the wm
178
Servlet servlet = StrutsConfigUtilities.getActionServlet(wm.getDeploymentDescriptor());
179             panel.setServletName(servlet.getServletName());
180             panel.setURLPattern(StrutsConfigUtilities.getActionServletMapping(wm.getDeploymentDescriptor()));
181             MessageResources resource = StrutsConfigUtilities.getDefatulMessageResource(wm.getDeploymentDescriptor());
182             if (resource != null){
183                 String JavaDoc name = resource.getAttributeValue("parameter");
184                 name = name.replaceAll("/", ".");
185                 panel.setAppResource(name);
186             }
187         }
188         
189         return panel;
190     }
191     
192     private class CreateStrutsConfig implements FileSystem.AtomicAction{
193         WebModule wm;
194         public CreateStrutsConfig (WebModule wm){
195             this.wm = wm;
196         }
197         
198         private void createFile(FileObject target, String JavaDoc content, String JavaDoc encoding) throws IOException JavaDoc{
199             FileLock lock = target.lock();
200             try {
201                 BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(target.getOutputStream(lock), encoding));
202                 bw.write(content);
203                 bw.close();
204
205             }
206             finally {
207                 lock.releaseLock();
208             }
209         }
210         
211         public void run() throws IOException JavaDoc {
212             FileObject target;
213             String JavaDoc content;
214             // copy struts-config.xml
215
if (canCreateNewFile(wm.getWebInf(), "struts-config.xml")) { //NOI18N
216
content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/struts-config.xml").getInputStream (), "UTF-8"); //NOI18N
217
content = content.replaceFirst("____ACTION_MAPPING___", //NOI18N
218
StrutsConfigUtilities.getActionAsResource(panel.getURLPattern(), "/Welcome"));
219                 content = content.replaceFirst("_____MESSAGE_RESOURCE____", //NOI18N
220
panel.getAppResource().replace('.', '/'));
221                 target = FileUtil.createData(wm.getWebInf(), "struts-config.xml");//NOI18N
222
createFile(target, content, "UTF-8"); //NOI18N
223
}
224             //copy tiles-defs.xml
225
if (canCreateNewFile(wm.getWebInf(), "tiles-defs.xml")) { //NOI18N
226
content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/tiles-defs.xml").getInputStream (), "UTF-8"); //NOI18N
227
target = FileUtil.createData(wm.getWebInf(), "tiles-defs.xml");//NOI18N
228
createFile(target, content, "UTF-8"); //NOI18N
229
}
230             //copy validation.xml
231
if (canCreateNewFile(wm.getWebInf(), "validation.xml")) { //NOI18N
232
content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/validation.xml").getInputStream (), "UTF-8"); //NOI18N
233
target = FileUtil.createData(wm.getWebInf(), "validation.xml");//NOI18N
234
createFile(target, content, "UTF-8"); //NOI18N
235
}
236             //copy validator-rules.xml
237
if (canCreateNewFile(wm.getWebInf(), "validator-rules.xml")) { //NOI18N
238
content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/validator-rules.xml").getInputStream (), "UTF-8"); //NOI18N
239
target = FileUtil.createData(wm.getWebInf(), "validator-rules.xml");//NOI18N
240
createFile(target, content, "UTF-8"); //NOI18N
241
}
242             
243             //MessageResource.properties
244
Project project = FileOwnerQuery.getOwner(wm.getDocumentBase());
245             SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
246             String JavaDoc sresource = panel.getAppResource();
247             if (sresource != null && sresource.trim().length()>0) {
248                 int index = sresource.lastIndexOf('.');
249                 String JavaDoc path = "";
250                 String JavaDoc name = sresource;
251                 if (index > -1){
252                     path = sresource.substring(0, sresource.lastIndexOf(".")); //NOI18N
253
name = sresource.substring(sresource.lastIndexOf(".")+1); //NOI18N
254
}
255                 name = name + ".properties"; //NOI18N
256
FileObject targetFolder = sourceGroups[0].getRootFolder();
257                 String JavaDoc folders[] = path.split("\\.");
258                 for (int i = 0; i < folders.length; i++){
259                     if (targetFolder.getFileObject(folders[i])== null)
260                         targetFolder = targetFolder.createFolder(folders[i]);
261                     else
262                         targetFolder = targetFolder.getFileObject(folders[i]);
263                 }
264                 if (canCreateNewFile(targetFolder, name)) { //NOI18N
265
content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/MessageResources.properties").getInputStream (), "UTF-8"); //NOI18N
266
target = FileUtil.createData(targetFolder, name);//NOI18N
267
createFile(target, content, "UTF-8"); //NOI18N
268
}
269             }
270             
271             if (panel.addTLDs()){
272                 //copy struts-bean.tld
273
if (canCreateNewFile(wm.getWebInf(), "struts-bean.tld")) { //NOI18N
274
content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/struts-bean.tld").getInputStream (), "UTF-8"); //NOI18N
275
target = FileUtil.createData(wm.getWebInf(), "struts-bean.tld");//NOI18N
276
createFile(target, content, "UTF-8"); //NOI18N
277
}
278                 //copy struts-html.tld
279
if (canCreateNewFile(wm.getWebInf(), "struts-html.tld")) { //NOI18N
280
content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/struts-html.tld").getInputStream (), "UTF-8"); //NOI18N
281
target = FileUtil.createData(wm.getWebInf(), "struts-html.tld");//NOI18N
282
createFile(target, content, "UTF-8"); //NOI18N
283
}
284                 //copy struts-logic.tld
285
if (canCreateNewFile(wm.getWebInf(), "struts-logic.tld")) { //NOI18N
286
content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/struts-logic.tld").getInputStream (), "UTF-8"); //NOI18N
287
target = FileUtil.createData(wm.getWebInf(), "struts-logic.tld");//NOI18N
288
createFile(target, content, "UTF-8"); //NOI18N
289
}
290                 //copy struts-nested.tld
291
if (canCreateNewFile(wm.getWebInf(), "struts-nested.tld")) { //NOI18N
292
content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/struts-nested.tld").getInputStream (), "UTF-8"); //NOI18N
293
target = FileUtil.createData(wm.getWebInf(), "struts-nested.tld");//NOI18N
294
createFile(target, content, "UTF-8"); //NOI18N
295
}
296                 //copy struts-tiles.tld
297
if (canCreateNewFile(wm.getWebInf(), "struts-tiles.tld")) { //NOI18N
298
content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/struts-tiles.tld").getInputStream (), "UTF-8"); //NOI18N
299
target = FileUtil.createData(wm.getWebInf(), "struts-tiles.tld");//NOI18N
300
createFile(target, content, "UTF-8"); //NOI18N
301
}
302             }
303             
304             // Enter servlet into the deployment descriptor
305
FileObject dd = wm.getDeploymentDescriptor();
306             WebApp ddRoot = DDProvider.getDefault().getDDRoot(dd);
307             if (ddRoot != null){
308                 try{
309                     Servlet servlet = (Servlet)ddRoot.createBean("Servlet"); //NOI18N
310
servlet.setServletName("action"); //NOI18N
311
servlet.setServletClass("org.apache.struts.action.ActionServlet"); //NOI18N
312

313                     ddRoot.addServlet(servlet);
314
315                     InitParam param = (InitParam)servlet.createBean("InitParam"); //NOI18N
316
param.setParamName("config");//NOI18N
317
param.setParamValue("/WEB-INF/struts-config.xml");//NOI18N
318
servlet.addInitParam(param);
319                     param = (InitParam)servlet.createBean("InitParam"); //NOI18N
320
param.setParamName("debug");//NOI18N
321
param.setParamValue("2");//NOI18N
322
servlet.addInitParam(param);
323                     param = (InitParam)servlet.createBean("InitParam"); //NOI18N
324
param.setParamName("detail");//NOI18N
325
param.setParamValue("2");//NOI18N
326
servlet.addInitParam(param);
327                     servlet.setLoadOnStartup(new BigInteger JavaDoc("2"));//NOI18N
328

329
330                     ServletMapping mapping = (ServletMapping)ddRoot.createBean("ServletMapping"); //NOI18N
331
mapping.setServletName(panel.getServletName());//NOI18N
332
mapping.setUrlPattern(panel.getURLPattern());//NOI18N
333

334                     ddRoot.addServletMapping(mapping);
335                     
336                     if (panel.addTLDs()){
337                         try{
338                             CreateCapability create;
339                             if (ddRoot.getVersion().equals(ddRoot.VERSION_2_3)){
340                                 create = ddRoot;
341                                 ddRoot.addTaglib(createTaglib(create, "/WEB-INF/struts-bean.tld", "/WEB-INF/struts-bean.tld")); //NOI18N
342
ddRoot.addTaglib(createTaglib(create, "/WEB-INF/struts-html.tld", "/WEB-INF/struts-html.tld")); //NOI18N
343
ddRoot.addTaglib(createTaglib(create, "/WEB-INF/struts-logic.tld", "/WEB-INF/struts-logic.tld")); //NOI18N
344
ddRoot.addTaglib(createTaglib(create, "/WEB-INF/struts-nested.tld", "/WEB-INF/struts-nested.tld")); //NOI18N
345
ddRoot.addTaglib(createTaglib(create, "/WEB-INF/struts-tiles.tld", "/WEB-INF/struts-tiles.tld")); //NOI18N
346
}
347                             else {
348                                 JspConfig jspConfig = ddRoot.getSingleJspConfig();
349                                 if (jspConfig==null){
350                                     jspConfig = (JspConfig)ddRoot.createBean("JspConfig");
351                                     ddRoot.setJspConfig(jspConfig);
352                                 }
353                                 create = jspConfig;
354                                 jspConfig.addTaglib(createTaglib(create, "/WEB-INF/struts-bean.tld", "/WEB-INF/struts-bean.tld")); //NOI18N
355
jspConfig.addTaglib(createTaglib(create, "/WEB-INF/struts-html.tld", "/WEB-INF/struts-html.tld")); //NOI18N
356
jspConfig.addTaglib(createTaglib(create, "/WEB-INF/struts-logic.tld", "/WEB-INF/struts-logic.tld")); //NOI18N
357
jspConfig.addTaglib(createTaglib(create, "/WEB-INF/struts-nested.tld", "/WEB-INF/struts-nested.tld")); //NOI18N
358
jspConfig.addTaglib(createTaglib(create, "/WEB-INF/struts-tiles.tld", "/WEB-INF/struts-tiles.tld")); //NOI18N
359
}
360                             
361                         }
362                         catch (VersionNotSupportedException e){
363                             ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
364                         }
365                     }
366                     ddRoot.write(dd);
367                     
368                     
369                 }
370                 catch (ClassNotFoundException JavaDoc cnfe){
371                     ErrorManager.getDefault().notify(cnfe);
372                 }
373             }
374             
375             //copy Welcome.jsp
376
if (canCreateNewFile(wm.getDocumentBase(), "welcomeStruts.jsp")) { //NOI18N
377
content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/welcome.jsp").getInputStream (), "UTF-8"); //NOI18N
378
target = FileUtil.createData(wm.getDocumentBase(), "welcomeStruts.jsp");//NOI18N
379
createFile(target, content, "UTF-8"); //NOI18N
380
// changing index.jsp
381
FileObject documentBase = wm.getDocumentBase();
382                 FileObject indexjsp = documentBase.getFileObject("index.jsp"); //NOI18N
383
if (indexjsp != null){
384                     changeIndexJSP(indexjsp);
385                 }
386             }
387         }
388         
389         private boolean canCreateNewFile(FileObject parent, String JavaDoc name){
390             File JavaDoc fileToBe = new File JavaDoc(FileUtil.toFile(parent), name);
391             boolean create = true;
392             if (fileToBe.exists()){
393                 DialogDescriptor dialog = new DialogDescriptor(
394                         NbBundle.getMessage(StrutsFrameworkProvider.class, "MSG_OverwriteFile", fileToBe.getAbsolutePath()),
395                         NbBundle.getMessage(StrutsFrameworkProvider.class, "TTL_OverwriteFile"),
396                         true, DialogDescriptor.YES_NO_OPTION, DialogDescriptor.NO_OPTION, null);
397                 java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
398                 d.setVisible(true);
399                 create = (dialog.getValue() == org.openide.DialogDescriptor.NO_OPTION);
400             }
401             return create;
402         }
403         
404         private Taglib createTaglib(CreateCapability createObject, String JavaDoc location, String JavaDoc uri) throws ClassNotFoundException JavaDoc {
405             Taglib taglib = (Taglib)createObject.createBean("Taglib"); //NOI18N
406
taglib.setTaglibLocation(location);
407             taglib.setTaglibUri(uri);
408             return taglib;
409         }
410         
411         /** Changes the index.jsp file. Only when there is <h1>JSP Page</h1> string.
412          */

413         private void changeIndexJSP(FileObject indexjsp) throws IOException JavaDoc {
414             String JavaDoc content = readResource(indexjsp.getInputStream(), "UTF-8"); //NOI18N
415
// what find
416
String JavaDoc find = "<h1>JSP Page</h1>"; // NOI18N
417
String JavaDoc endLine = System.getProperty("line.separator"); //NOI18N
418
if ( content.indexOf(find) > 0){
419                 StringBuffer JavaDoc replace = new StringBuffer JavaDoc();
420                 replace.append(find);
421                 replace.append(endLine);
422                 replace.append(" <br/>"); //NOI18N
423
replace.append(endLine);
424                 replace.append(" <a HREF=\"."); //NOI18N
425
replace.append(StrutsConfigUtilities.getActionAsResource(panel.getURLPattern(), "/Welcome")); //NOI18N
426
replace.append("\">"); //NOI18N
427
replace.append(NbBundle.getMessage(StrutsFrameworkProvider.class,"LBL_STRUTS_WELCOME_PAGE"));
428                 replace.append("</a>"); //NOI18N
429
content = content.replaceFirst(find, replace.toString());
430                 createFile(indexjsp, content, "UTF-8"); //NOI18N
431
}
432         }
433     }
434     
435     
436 }
437
Popular Tags