1 19 package org.netbeans.modules.web.jsf.api; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import org.netbeans.modules.j2ee.dd.api.web.DDProvider; 25 import org.netbeans.modules.j2ee.dd.api.web.Servlet; 26 import org.netbeans.modules.j2ee.dd.api.web.ServletMapping; 27 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 28 import org.netbeans.modules.web.api.webmodule.WebModule; 29 import org.netbeans.modules.web.jsf.JSFConfigUtilities; 30 import org.netbeans.modules.web.jsf.api.facesmodel.FacesConfig; 31 import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigModel; 32 import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigModelFactory; 33 import org.netbeans.modules.web.jsf.api.facesmodel.ManagedBean; 34 import org.netbeans.modules.xml.retriever.catalog.Utilities; 35 import org.netbeans.modules.xml.xam.ModelSource; 36 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 37 import org.openide.ErrorManager; 38 import org.openide.filesystems.FileObject; 39 40 44 public class ConfigurationUtils { 45 46 53 public static JSFConfigModel getConfigModel(FileObject confFile, boolean editable){ 54 try { 55 ModelSource modelSource = Utilities.createModelSource(confFile,editable); 56 JSFConfigModel configModel = JSFConfigModelFactory.getInstance().getModel(modelSource); 57 58 return configModel; 59 } catch (CatalogModelException ex) { 60 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 61 ex.getMessage(), ex); 62 } 63 return null; 64 } 65 66 73 public static Servlet getFacesServlet(WebModule webModule) { 74 FileObject deploymentDescriptor = webModule.getDeploymentDescriptor(); 75 if (deploymentDescriptor == null) { 76 return null; 77 } 78 try { 79 WebApp webApp = DDProvider.getDefault().getDDRoot(deploymentDescriptor); 80 81 return (Servlet) webApp 84 .findBeanByName("Servlet", "ServletClass", "javax.faces.webapp.FacesServlet"); } catch (java.io.IOException e) { 86 return null; 87 } 88 } 89 90 95 public static String getFacesServletMapping(WebModule webModule){ 96 FileObject deploymentDescriptor = webModule.getDeploymentDescriptor(); 97 Servlet servlet = getFacesServlet(webModule); 98 if (servlet != null){ 99 try{ 100 WebApp webApp = DDProvider.getDefault().getDDRoot(deploymentDescriptor); 101 ServletMapping[] mappings = webApp.getServletMapping(); 102 for (int i = 0; i < mappings.length; i++){ 103 if (mappings[i].getServletName().equals(servlet.getServletName())) 104 return mappings[i].getUrlPattern(); 105 } 106 } catch (java.io.IOException e) { 107 ErrorManager.getDefault().notify(e); 108 } 109 } 110 return null; 111 } 112 113 120 121 public static FileObject[] getFacesConfigFiles(WebModule webModule){ 122 String [] sFiles = JSFConfigUtilities.getConfigFiles(webModule.getDeploymentDescriptor()); 123 if (sFiles.length > 0){ 124 FileObject documentBase = webModule.getDocumentBase(); 125 ArrayList files = new ArrayList (); 126 FileObject file; 127 for (int i = 0; i < sFiles.length; i++){ 128 file = documentBase.getFileObject(sFiles[i]); 129 if (file != null) 130 files.add(file); 131 } 132 return (FileObject[])files.toArray(new FileObject[files.size()]); 133 } 134 return new FileObject [0]; 135 } 136 137 147 public static String translateURI(String mapping, String uri){ 148 String resource = ""; 149 if (mapping != null && mapping.length()>0){ 150 if (mapping.startsWith("*.")){ 151 if (uri.indexOf('.') > 0) 152 resource = uri.substring(0, uri.lastIndexOf('.'))+mapping.substring(1); 153 else 154 resource = uri + mapping.substring(1); 155 } else 156 if (mapping.endsWith("/*")) 157 resource = mapping.substring(0,mapping.length()-2) + uri; 158 } 159 return resource; 160 } 161 162 170 public static FileObject findFacesConfigForManagedBean(WebModule webModule, String name){ 171 FileObject[] configs = ConfigurationUtils.getFacesConfigFiles(webModule); 172 173 174 for (int i = 0; i < configs.length; i++) { 175 FacesConfig facesConfig = getConfigModel(configs[i], true).getRootComponent(); 177 Collection <ManagedBean>beans = facesConfig.getManagedBeans(); 178 for (Iterator <ManagedBean> it = beans.iterator(); it.hasNext();) { 179 ManagedBean managedBean = it.next(); 180 if(name.equals(managedBean.getManagedBeanName())) 181 return configs[i]; 182 } 183 184 } 185 return null; 186 } 187 188 189 } 190 | Popular Tags |