1 19 20 package org.netbeans.modules.web.struts; 21 22 import java.io.BufferedReader ; 23 import java.io.BufferedWriter ; 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.InputStreamReader ; 29 import java.io.OutputStreamWriter ; 30 import java.math.BigInteger ; 31 import java.util.ArrayList ; 32 import java.util.List ; 33 import java.util.Set ; 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 75 public class StrutsFrameworkProvider extends WebFrameworkProvider { 76 77 78 private static final String STRUTS_CONFIG ="nbres:/org/netbeans/modules/web/struts/resources/struts-config.xml"; 79 80 private StrutsConfigurationPanel panel; 81 private static String defaultAppResource ="com.myapp.struts.ApplicationResource"; 83 public StrutsFrameworkProvider(){ 84 super ( 85 NbBundle.getMessage(StrutsFrameworkProvider.class, "Sruts_Name"), NbBundle.getMessage(StrutsFrameworkProvider.class, "Sruts_Description")); } 88 89 public Set extend (WebModule wm) { 90 FileObject fo = wm.getDocumentBase(); 91 Project project = FileOwnerQuery.getOwner(fo); 92 93 Library lib = LibraryManager.getDefault().getLibrary("struts"); 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 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()); } 106 107 try { 108 FileSystem fs = wm.getWebInf().getFileSystem(); 109 fs.runAtomicAction(new CreateStrutsConfig(wm)); 110 111 } catch (FileNotFoundException exc) { 112 ErrorManager.getDefault().notify(exc); 113 return null; 114 } catch (IOException 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 readResource(InputStream is, String encoding) throws IOException { 124 StringBuffer sb = new StringBuffer (); 126 String lineSep = System.getProperty("line.separator"); BufferedReader br = new BufferedReader (new InputStreamReader (is, encoding)); 128 String 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 [] getConfigurationFiles(org.netbeans.modules.web.api.webmodule.WebModule wm) { 139 FileObject webinf = wm.getWebInf(); 140 List files = new ArrayList (); 141 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"); if (fo != null) files.add(FileUtil.toFile(fo)); 151 fo = webinf.getFileObject("validation.xml"); if (fo != null) files.add(FileUtil.toFile(fo)); 153 fo = webinf.getFileObject("validator-rules.xml"); if (fo != null) files.add(FileUtil.toFile(fo)); 155 } 156 157 File [] rFiles = new File [files.size()]; 158 files.toArray(rFiles); 159 return rFiles; 160 } 161 162 public boolean isInWebModule(org.netbeans.modules.web.api.webmodule.WebModule wm) { 163 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 panel.setAppResource(defaultAppResource); 175 } 176 else { 177 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 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 content, String encoding) throws IOException { 199 FileLock lock = target.lock(); 200 try { 201 BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (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 { 212 FileObject target; 213 String content; 214 if (canCreateNewFile(wm.getWebInf(), "struts-config.xml")) { content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/struts-config.xml").getInputStream (), "UTF-8"); content = content.replaceFirst("____ACTION_MAPPING___", StrutsConfigUtilities.getActionAsResource(panel.getURLPattern(), "/Welcome")); 219 content = content.replaceFirst("_____MESSAGE_RESOURCE____", panel.getAppResource().replace('.', '/')); 221 target = FileUtil.createData(wm.getWebInf(), "struts-config.xml"); createFile(target, content, "UTF-8"); } 224 if (canCreateNewFile(wm.getWebInf(), "tiles-defs.xml")) { content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/tiles-defs.xml").getInputStream (), "UTF-8"); target = FileUtil.createData(wm.getWebInf(), "tiles-defs.xml"); createFile(target, content, "UTF-8"); } 230 if (canCreateNewFile(wm.getWebInf(), "validation.xml")) { content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/validation.xml").getInputStream (), "UTF-8"); target = FileUtil.createData(wm.getWebInf(), "validation.xml"); createFile(target, content, "UTF-8"); } 236 if (canCreateNewFile(wm.getWebInf(), "validator-rules.xml")) { content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/validator-rules.xml").getInputStream (), "UTF-8"); target = FileUtil.createData(wm.getWebInf(), "validator-rules.xml"); createFile(target, content, "UTF-8"); } 242 243 Project project = FileOwnerQuery.getOwner(wm.getDocumentBase()); 245 SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 246 String sresource = panel.getAppResource(); 247 if (sresource != null && sresource.trim().length()>0) { 248 int index = sresource.lastIndexOf('.'); 249 String path = ""; 250 String name = sresource; 251 if (index > -1){ 252 path = sresource.substring(0, sresource.lastIndexOf(".")); name = sresource.substring(sresource.lastIndexOf(".")+1); } 255 name = name + ".properties"; FileObject targetFolder = sourceGroups[0].getRootFolder(); 257 String 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)) { content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/MessageResources.properties").getInputStream (), "UTF-8"); target = FileUtil.createData(targetFolder, name); createFile(target, content, "UTF-8"); } 269 } 270 271 if (panel.addTLDs()){ 272 if (canCreateNewFile(wm.getWebInf(), "struts-bean.tld")) { content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/struts-bean.tld").getInputStream (), "UTF-8"); target = FileUtil.createData(wm.getWebInf(), "struts-bean.tld"); createFile(target, content, "UTF-8"); } 278 if (canCreateNewFile(wm.getWebInf(), "struts-html.tld")) { content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/struts-html.tld").getInputStream (), "UTF-8"); target = FileUtil.createData(wm.getWebInf(), "struts-html.tld"); createFile(target, content, "UTF-8"); } 284 if (canCreateNewFile(wm.getWebInf(), "struts-logic.tld")) { content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/struts-logic.tld").getInputStream (), "UTF-8"); target = FileUtil.createData(wm.getWebInf(), "struts-logic.tld"); createFile(target, content, "UTF-8"); } 290 if (canCreateNewFile(wm.getWebInf(), "struts-nested.tld")) { content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/struts-nested.tld").getInputStream (), "UTF-8"); target = FileUtil.createData(wm.getWebInf(), "struts-nested.tld"); createFile(target, content, "UTF-8"); } 296 if (canCreateNewFile(wm.getWebInf(), "struts-tiles.tld")) { content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/struts-tiles.tld").getInputStream (), "UTF-8"); target = FileUtil.createData(wm.getWebInf(), "struts-tiles.tld"); createFile(target, content, "UTF-8"); } 302 } 303 304 FileObject dd = wm.getDeploymentDescriptor(); 306 WebApp ddRoot = DDProvider.getDefault().getDDRoot(dd); 307 if (ddRoot != null){ 308 try{ 309 Servlet servlet = (Servlet)ddRoot.createBean("Servlet"); servlet.setServletName("action"); servlet.setServletClass("org.apache.struts.action.ActionServlet"); 313 ddRoot.addServlet(servlet); 314 315 InitParam param = (InitParam)servlet.createBean("InitParam"); param.setParamName("config"); param.setParamValue("/WEB-INF/struts-config.xml"); servlet.addInitParam(param); 319 param = (InitParam)servlet.createBean("InitParam"); param.setParamName("debug"); param.setParamValue("2"); servlet.addInitParam(param); 323 param = (InitParam)servlet.createBean("InitParam"); param.setParamName("detail"); param.setParamValue("2"); servlet.addInitParam(param); 327 servlet.setLoadOnStartup(new BigInteger ("2")); 329 330 ServletMapping mapping = (ServletMapping)ddRoot.createBean("ServletMapping"); mapping.setServletName(panel.getServletName()); mapping.setUrlPattern(panel.getURLPattern()); 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")); ddRoot.addTaglib(createTaglib(create, "/WEB-INF/struts-html.tld", "/WEB-INF/struts-html.tld")); ddRoot.addTaglib(createTaglib(create, "/WEB-INF/struts-logic.tld", "/WEB-INF/struts-logic.tld")); ddRoot.addTaglib(createTaglib(create, "/WEB-INF/struts-nested.tld", "/WEB-INF/struts-nested.tld")); ddRoot.addTaglib(createTaglib(create, "/WEB-INF/struts-tiles.tld", "/WEB-INF/struts-tiles.tld")); } 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")); jspConfig.addTaglib(createTaglib(create, "/WEB-INF/struts-html.tld", "/WEB-INF/struts-html.tld")); jspConfig.addTaglib(createTaglib(create, "/WEB-INF/struts-logic.tld", "/WEB-INF/struts-logic.tld")); jspConfig.addTaglib(createTaglib(create, "/WEB-INF/struts-nested.tld", "/WEB-INF/struts-nested.tld")); jspConfig.addTaglib(createTaglib(create, "/WEB-INF/struts-tiles.tld", "/WEB-INF/struts-tiles.tld")); } 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 cnfe){ 371 ErrorManager.getDefault().notify(cnfe); 372 } 373 } 374 375 if (canCreateNewFile(wm.getDocumentBase(), "welcomeStruts.jsp")) { content = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-struts/welcome.jsp").getInputStream (), "UTF-8"); target = FileUtil.createData(wm.getDocumentBase(), "welcomeStruts.jsp"); createFile(target, content, "UTF-8"); FileObject documentBase = wm.getDocumentBase(); 382 FileObject indexjsp = documentBase.getFileObject("index.jsp"); if (indexjsp != null){ 384 changeIndexJSP(indexjsp); 385 } 386 } 387 } 388 389 private boolean canCreateNewFile(FileObject parent, String name){ 390 File fileToBe = new File (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 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 location, String uri) throws ClassNotFoundException { 405 Taglib taglib = (Taglib)createObject.createBean("Taglib"); taglib.setTaglibLocation(location); 407 taglib.setTaglibUri(uri); 408 return taglib; 409 } 410 411 413 private void changeIndexJSP(FileObject indexjsp) throws IOException { 414 String content = readResource(indexjsp.getInputStream(), "UTF-8"); String find = "<h1>JSP Page</h1>"; String endLine = System.getProperty("line.separator"); if ( content.indexOf(find) > 0){ 419 StringBuffer replace = new StringBuffer (); 420 replace.append(find); 421 replace.append(endLine); 422 replace.append(" <br/>"); replace.append(endLine); 424 replace.append(" <a HREF=\"."); replace.append(StrutsConfigUtilities.getActionAsResource(panel.getURLPattern(), "/Welcome")); replace.append("\">"); replace.append(NbBundle.getMessage(StrutsFrameworkProvider.class,"LBL_STRUTS_WELCOME_PAGE")); 428 replace.append("</a>"); content = content.replaceFirst(find, replace.toString()); 430 createFile(indexjsp, content, "UTF-8"); } 432 } 433 } 434 435 436 } 437 | Popular Tags |