View Javadoc

1   /*
2    * $Id: StrutsConfigRetriever.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.sitegraph;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.util.Collections;
27  import java.util.LinkedHashMap;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import org.apache.struts2.config.BeanSelectionProvider;
32  import org.apache.struts2.config.DefaultPropertiesProvider;
33  import org.apache.struts2.config.LegacyPropertiesConfigurationProvider;
34  import org.apache.struts2.config.StrutsXmlConfigurationProvider;
35  import org.apache.struts2.sitegraph.entities.FreeMarkerView;
36  import org.apache.struts2.sitegraph.entities.JspView;
37  import org.apache.struts2.sitegraph.entities.VelocityView;
38  import org.apache.struts2.sitegraph.entities.View;
39  
40  import com.opensymphony.xwork2.config.ConfigurationManager;
41  import com.opensymphony.xwork2.config.ConfigurationProvider;
42  import com.opensymphony.xwork2.config.entities.ActionConfig;
43  import com.opensymphony.xwork2.config.entities.ResultConfig;
44  import com.opensymphony.xwork2.util.logging.Logger;
45  import com.opensymphony.xwork2.util.logging.LoggerFactory;
46  
47  /***
48   * Initializes and retrieves XWork config elements
49   */
50  public class StrutsConfigRetriever {
51  
52      private static final Logger LOG = LoggerFactory.getLogger(StrutsConfigRetriever.class);
53      private static String configDir;
54      private static String[] views;
55      private static boolean isXWorkStarted = false;
56      private static Map viewCache = new LinkedHashMap();
57      private static ConfigurationManager cm;
58  
59      /***
60       * Returns a Map of all action names/configs
61       *
62       * @return Map of all action names/configs
63       */
64      public static Map getActionConfigs() {
65          if (!isXWorkStarted)
66              initXWork();
67          return cm.getConfiguration().getRuntimeConfiguration().getActionConfigs();
68      }
69  
70      private static void initXWork() {
71          String configFilePath = configDir + "/struts.xml";
72          File configFile = new File(configFilePath);
73          try {
74              ConfigurationProvider configProvider = new StrutsXmlConfigurationProvider(configFile.getCanonicalPath(), true, null);
75              cm = new ConfigurationManager();
76              cm.addConfigurationProvider(new DefaultPropertiesProvider());
77              cm.addConfigurationProvider(new StrutsXmlConfigurationProvider("struts-default.xml", false, null));
78              cm.addConfigurationProvider(configProvider);
79              cm.addConfigurationProvider(new LegacyPropertiesConfigurationProvider());
80              cm.addConfigurationProvider(new BeanSelectionProvider());
81              isXWorkStarted = true;
82          } catch (IOException e) {
83              LOG.error("IOException", e);
84          }
85      }
86  
87      public static Set getNamespaces() {
88          Set namespaces = Collections.EMPTY_SET;
89          Map allActionConfigs = getActionConfigs();
90          if (allActionConfigs != null) {
91              namespaces = allActionConfigs.keySet();
92          }
93          return namespaces;
94      }
95  
96      /***
97       * Return a Set of the action names for this namespace.
98       *
99       * @param namespace
100      * @return Set of the action names for this namespace.
101      */
102     public static Set getActionNames(String namespace) {
103         Set actionNames = Collections.EMPTY_SET;
104         Map allActionConfigs = getActionConfigs();
105         if (allActionConfigs != null) {
106             Map actionMappings = (Map) allActionConfigs.get(namespace);
107             if (actionMappings != null) {
108                 actionNames = actionMappings.keySet();
109             }
110         }
111         return actionNames;
112     }
113 
114     /***
115      * Returns the ActionConfig for this action name at this namespace.
116      *
117      * @param namespace
118      * @param actionName
119      * @return The ActionConfig for this action name at this namespace.
120      */
121     public static ActionConfig getActionConfig(String namespace, String actionName) {
122         ActionConfig config = null;
123         Map allActionConfigs = getActionConfigs();
124         if (allActionConfigs != null) {
125             Map actionMappings = (Map) allActionConfigs.get(namespace);
126             if (actionMappings != null) {
127                 config = (ActionConfig) actionMappings.get(actionName);
128             }
129         }
130         return config;
131     }
132 
133     public static ResultConfig getResultConfig(String namespace, String actionName,
134                                                String resultName) {
135         ResultConfig result = null;
136         ActionConfig actionConfig = getActionConfig(namespace, actionName);
137         if (actionConfig != null) {
138             Map resultMap = actionConfig.getResults();
139             result = (ResultConfig) resultMap.get(resultName);
140         }
141         return result;
142     }
143 
144     public static File getViewFile(String namespace, String actionName, String resultName) {
145         ResultConfig result = getResultConfig(namespace, actionName, resultName);
146         String location = (String) result.getParams().get("location");
147         for (int i = 0; i < views.length; i++) {
148             String viewRoot = views[i];
149             File viewFile = getViewFileInternal(viewRoot, location, namespace);
150             if (viewFile != null) {
151                 return viewFile;
152             }
153         }
154 
155         return null;
156     }
157 
158     private static File getViewFileInternal(String root, String location, String namespace) {
159         StringBuffer filePath = new StringBuffer(root);
160         if (!location.startsWith("/")) {
161             filePath.append(namespace + "/");
162         }
163         filePath.append(location);
164         File viewFile = new File(filePath.toString());
165         if (viewFile.exists()) {
166             return viewFile;
167         } else {
168             return null;
169         }
170     }
171 
172     public static View getView(String namespace, String actionName, String resultName, int type) {
173         String viewId = namespace + "/" + actionName + "/" + resultName;
174         View view = (View) viewCache.get(viewId);
175         if (view == null) {
176             File viewFile = StrutsConfigRetriever.getViewFile(namespace, actionName, resultName);
177             if (viewFile != null) {
178                 switch (type) {
179                     case View.TYPE_JSP:
180                         view = new JspView(viewFile);
181                         break;
182                     case View.TYPE_FTL:
183                         view = new FreeMarkerView(viewFile);
184                         break;
185                     case View.TYPE_VM:
186                         view = new VelocityView(viewFile);
187                         break;
188                     default:
189                         return null;
190                 }
191 
192                 viewCache.put(viewId, view);
193             }
194         }
195         return view;
196     }
197 
198     public static void setConfiguration(String configDir, String[] views) {
199         StrutsConfigRetriever.configDir = configDir;
200         StrutsConfigRetriever.views = views;
201         isXWorkStarted = false;
202         viewCache = new LinkedHashMap();
203     }
204 }