KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > api > ConfigurationUtils


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.web.jsf.api;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
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 /**
41  *
42  * @author Petr Pisl
43  */

44 public class ConfigurationUtils {
45     
46     /**
47      * This methods returns the model source for the faces config file.
48      * @param confFile - the faces config file
49      * @param editable - if the source will be editable. Clients should use true.
50      * @return The ModelSource for the configuration file. If the file is not faces config file
51      * or a version which is not handled, then returns null.
52      */

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     /**
67      * The methods finds the definition of the Faces Servlet in the deployment descriptor
68      * of the given web module.
69      * @param webModule the given web module, where the Faces Servlet is.
70      * @return Faces Servlet definition or null if the Faces Servlet definition is not
71      * found in the given web module.
72      */

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             // Try to find according the servlet class name. The javax.faces.webapp.FacesServlet is final, so
82
// it can not be extended.
83
return (Servlet) webApp
84                     .findBeanByName("Servlet", "ServletClass", "javax.faces.webapp.FacesServlet"); //NOI18N;
85
} catch (java.io.IOException JavaDoc e) {
86             return null;
87         }
88     }
89     
90     /** Returns the mapping for the Faces Servlet.
91      * @param webModule web module, where the JSF framework should be defined
92      * @return The maping for the faces servlet. Null if the web module doesn't
93      * contains definition of faces servlet.
94      */

95     public static String JavaDoc 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 JavaDoc e) {
107                 ErrorManager.getDefault().notify(e);
108             }
109         }
110         return null;
111     }
112     
113     /**
114      * The method returns all faces configuration files in the web module
115      * @param webModule - the web module, where you want to find the faces
116      * configuration files
117      * @return array of all faces configuration files. If there are not any
118      * configuration file, then empty array is returned.
119      **/

120     
121     public static FileObject[] getFacesConfigFiles(WebModule webModule){
122         String JavaDoc[] sFiles = JSFConfigUtilities.getConfigFiles(webModule.getDeploymentDescriptor());
123         if (sFiles.length > 0){
124             FileObject documentBase = webModule.getDocumentBase();
125             ArrayList JavaDoc files = new ArrayList JavaDoc();
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     /**
138      * Translates an URI to be executed with faces serlvet with the given mapping.
139      * For example, the servlet has mapping <i>*.jsf</i> then uri <i>/hello.jps</i> will be
140      * translated to <i>/hello.jsf</i>. In the case where the mapping is <i>/faces/*</i>
141      * will be translated to <i>/faces/hello.jsp<i>.
142      *
143      * @param mapping The servlet mapping
144      * @param uri The original URI
145      * @return The translated URI
146      */

147     public static String JavaDoc translateURI(String JavaDoc mapping, String JavaDoc uri){
148         String JavaDoc 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     /**
163      * Helper method which finds the faces configuration file, where is the managed bean
164      * defined.
165      * @param webModule the web module, wher the managed bean is defined.
166      * @param name Name of the managed bean.
167      * @return faces configuration file, where the managed bean is defined. Null, if a bean
168      * with the given name is not defined in the web module.
169      */

170     public static FileObject findFacesConfigForManagedBean(WebModule webModule, String JavaDoc name){
171         FileObject[] configs = ConfigurationUtils.getFacesConfigFiles(webModule);
172         
173         
174         for (int i = 0; i < configs.length; i++) {
175             //DataObject dObject = DataObject.find(configs[i]);
176
FacesConfig facesConfig = getConfigModel(configs[i], true).getRootComponent();
177             Collection JavaDoc<ManagedBean>beans = facesConfig.getManagedBeans();
178             for (Iterator JavaDoc<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