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