1 19 20 package org.netbeans.modules.web.jsf; 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.Set ; 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 58 public class JSFFrameworkProvider extends WebFrameworkProvider { 59 60 private JSFConfigurationPanel panel; 61 62 public JSFFrameworkProvider() { 63 super( 64 NbBundle.getMessage(JSFFrameworkProvider.class, "JSF_Name"), NbBundle.getMessage(JSFFrameworkProvider.class, "JSF_Description")); } 67 68 public Set 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) { 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 ioe) { 89 } 91 } 92 } else { 93 } 95 } 96 } 97 } 98 boolean isMyFaces = cp.findResource("org/apache/myfaces/webapp/StartupServletContextListener.class") != null; FileSystem fileSystem = webModule.getWebInf().getFileSystem(); 100 fileSystem.runAtomicAction(new CreateFacesConfig(webModule, isMyFaces)); 101 } catch (FileNotFoundException exc) { 102 ErrorManager.getDefault().notify(exc); 103 } catch (IOException exc) { 104 ErrorManager.getDefault().notify(exc); 105 } 106 return null; 107 } 108 109 public static String readResource(InputStream is, String encoding) throws IOException { 110 StringBuffer sbuffer = new StringBuffer (); 112 String lineSep = System.getProperty("line.separator"); BufferedReader br = new BufferedReader (new InputStreamReader (is, encoding)); 114 String 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 [] getConfigurationFiles(org.netbeans.modules.web.api.webmodule.WebModule wm) { 125 FileObject dd = wm.getDeploymentDescriptor(); 127 if (dd != null){ 128 FileObject[] filesFO = ConfigurationUtils.getFacesConfigFiles(wm); 129 File [] files = new File [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 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 FileObject dd = webModule.getDeploymentDescriptor(); 156 return (dd != null && ConfigurationUtils.getFacesServlet(webModule) != null); 157 } 158 159 public String getServletPath(FileObject file){ 160 String 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 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 content, String encoding) throws IOException { 180 FileLock lock = target.lock(); 181 try { 182 BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (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 { 201 FileObject dd = webModule.getDeploymentDescriptor(); 203 WebApp ddRoot = DDProvider.getDefault().getDDRoot(dd); 204 if (ddRoot != null){ 205 try{ 206 Servlet servlet = (Servlet)ddRoot.createBean("Servlet"); String servletName = panel == null ? "Faces Servlet" : panel.getServletName(); 208 servlet.setServletName(servletName); servlet.setServletClass("javax.faces.webapp.FacesServlet"); ddRoot.addServlet(servlet); 211 212 servlet.setLoadOnStartup(new BigInteger ("1")); 214 215 ServletMapping mapping = (ServletMapping)ddRoot.createBean("ServletMapping"); mapping.setServletName(servletName); mapping.setUrlPattern(panel == null ? "/faces/*" : panel.getURLPattern()); 219 ddRoot.addServletMapping(mapping); 220 221 InitParam contextParam = (InitParam)ddRoot.createBean("InitParam"); contextParam.setParamName("com.sun.faces.verifyObjects"); if (panel != null && panel.verifyObjects()) 224 contextParam.setParamValue("true"); else 226 contextParam.setParamValue("false"); ddRoot.addContextParam(contextParam); 228 229 contextParam = (InitParam)ddRoot.createBean("InitParam"); contextParam.setParamName("com.sun.faces.validateXml"); if(panel == null || panel.validateXML()) 232 contextParam.setParamValue("true"); else 234 contextParam.setParamValue("false"); ddRoot.addContextParam(contextParam); 236 237 contextParam = (InitParam)ddRoot.createBean("InitParam"); contextParam.setParamName("javax.faces.STATE_SAVING_METHOD"); contextParam.setParamValue("client"); 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 cnfe){ 251 ErrorManager.getDefault().notify(cnfe); 252 } 253 } 254 255 if (canCreateNewFile(webModule.getWebInf(),"faces-config.xml")){ 257 String facesConfigTemplate = "faces-config.xml"; if (ddRoot != null) { 259 if (WebApp.VERSION_2_5.equals(ddRoot.getVersion())) { 260 facesConfigTemplate = "faces-config_1_2.xml"; } 262 } 263 String content = readResource(Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-jsf/" + facesConfigTemplate).getInputStream(), "UTF-8"); FileObject target = FileUtil.createData(webModule.getWebInf(), "faces-config.xml"); createFile(target, content, "UTF-8"); } 267 268 if (canCreateNewFile(webModule.getDocumentBase(), "welcomeJSF.jsp")){ 270 String content = readResource(Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-jsf/welcomeJSF.jsp").getInputStream(), "UTF-8"); FileObject target = FileUtil.createData(webModule.getDocumentBase(), "welcomeJSF.jsp"); createFile(target, content, "UTF-8"); 274 FileObject documentBase = webModule.getDocumentBase(); 275 FileObject indexjsp = documentBase.getFileObject("index.jsp"); if (indexjsp != null){ 277 changeIndexJSP(indexjsp); 278 } 279 } 280 281 } 282 283 private boolean canCreateNewFile(FileObject parent, String name){ 284 File fileToBe = new File (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 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 300 private void changeIndexJSP(FileObject indexjsp) throws IOException { 301 302 String content = readResource(indexjsp.getInputStream(), "UTF-8"); 304 String find = "<h1>JSP Page</h1>"; String endLine = System.getProperty("line.separator"); if ( content.indexOf(find) > 0){ 308 StringBuffer replace = new StringBuffer (); 309 replace.append(find); 310 replace.append(endLine); 311 replace.append(" <br/>"); replace.append(endLine); 313 replace.append(" <a HREF=\"."); replace.append(ConfigurationUtils.translateURI(panel == null ? "/faces/*" : panel.getURLPattern(),"/welcomeJSF.jsp")); replace.append("\">"); replace.append(NbBundle.getMessage(JSFFrameworkProvider.class,"LBL_JSF_WELCOME_PAGE")); 317 replace.append("</a>"); content = content.replaceFirst(find, new String (replace.toString().getBytes("UTF8"), "UTF-8")); createFile(indexjsp, content, "UTF-8"); } 321 } 322 } 323 324 } 325 | Popular Tags |