001: /*
002: * Created on Aug 14, 2004 by mgreer
003: */
004: package com.opensymphony.webwork.sitegraph;
005:
006: import com.opensymphony.webwork.sitegraph.collectors.ArbitraryXMLConfigurationProvider;
007: import com.opensymphony.webwork.sitegraph.entities.FreeMarkerView;
008: import com.opensymphony.webwork.sitegraph.entities.JspView;
009: import com.opensymphony.webwork.sitegraph.entities.VelocityView;
010: import com.opensymphony.webwork.sitegraph.entities.View;
011: import com.opensymphony.xwork.config.ConfigurationManager;
012: import com.opensymphony.xwork.config.ConfigurationProvider;
013: import com.opensymphony.xwork.config.entities.ActionConfig;
014: import com.opensymphony.xwork.config.entities.ResultConfig;
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.util.*;
021:
022: /**
023: * Initializes and retrieves XWork config elements
024: */
025: public class XWorkConfigRetriever {
026:
027: private static final Log LOG = LogFactory
028: .getLog(XWorkConfigRetriever.class);
029: private static String configDir;
030: private static String[] views;
031: private static boolean isXWorkStarted = false;
032: private static Map viewCache = new LinkedHashMap();
033:
034: /**
035: * Returns a Map of all action names/configs
036: *
037: * @return Map of all action names/configs
038: */
039: public static Map getActionConfigs() {
040: if (!isXWorkStarted)
041: initXWork();
042: return ConfigurationManager.getConfiguration()
043: .getRuntimeConfiguration().getActionConfigs();
044: }
045:
046: private static void initXWork() {
047: String configFilePath = configDir + "/xwork.xml";
048: File configFile = new File(configFilePath);
049: try {
050: ConfigurationProvider configProvider = new ArbitraryXMLConfigurationProvider(
051: configFile.getCanonicalPath());
052: ConfigurationManager
053: .addConfigurationProvider(configProvider);
054: isXWorkStarted = true;
055: } catch (IOException e) {
056: LOG.error("IOException", e);
057: }
058: }
059:
060: public static Set getNamespaces() {
061: Set namespaces = Collections.EMPTY_SET;
062: Map allActionConfigs = getActionConfigs();
063: if (allActionConfigs != null) {
064: namespaces = allActionConfigs.keySet();
065: }
066: return namespaces;
067: }
068:
069: /**
070: * Return a Set of the action names for this namespace.
071: *
072: * @param namespace
073: * @return Set of the action names for this namespace.
074: */
075: public static Set getActionNames(String namespace) {
076: Set actionNames = Collections.EMPTY_SET;
077: Map allActionConfigs = getActionConfigs();
078: if (allActionConfigs != null) {
079: Map actionMappings = (Map) allActionConfigs.get(namespace);
080: if (actionMappings != null) {
081: actionNames = actionMappings.keySet();
082: }
083: }
084: return actionNames;
085: }
086:
087: /**
088: * Returns the ActionConfig for this action name at this namespace.
089: *
090: * @param namespace
091: * @param actionName
092: * @return The ActionConfig for this action name at this namespace.
093: */
094: public static ActionConfig getActionConfig(String namespace,
095: String actionName) {
096: ActionConfig config = null;
097: Map allActionConfigs = getActionConfigs();
098: if (allActionConfigs != null) {
099: Map actionMappings = (Map) allActionConfigs.get(namespace);
100: if (actionMappings != null) {
101: config = (ActionConfig) actionMappings.get(actionName);
102: }
103: }
104: return config;
105: }
106:
107: public static ResultConfig getResultConfig(String namespace,
108: String actionName, String resultName) {
109: ResultConfig result = null;
110: ActionConfig actionConfig = getActionConfig(namespace,
111: actionName);
112: if (actionConfig != null) {
113: Map resultMap = actionConfig.getResults();
114: result = (ResultConfig) resultMap.get(resultName);
115: }
116: return result;
117: }
118:
119: public static File getViewFile(String namespace, String actionName,
120: String resultName) {
121: ResultConfig result = getResultConfig(namespace, actionName,
122: resultName);
123: String location = (String) result.getParams().get("location");
124: for (int i = 0; i < views.length; i++) {
125: String viewRoot = views[i];
126: File viewFile = getViewFileInternal(viewRoot, location,
127: namespace);
128: if (viewFile != null) {
129: return viewFile;
130: }
131: }
132:
133: return null;
134: }
135:
136: private static File getViewFileInternal(String root,
137: String location, String namespace) {
138: StringBuffer filePath = new StringBuffer(root);
139: if (!location.startsWith("/")) {
140: filePath.append(namespace + "/");
141: }
142: filePath.append(location);
143: File viewFile = new File(filePath.toString());
144: if (viewFile.exists()) {
145: return viewFile;
146: } else {
147: return null;
148: }
149: }
150:
151: public static View getView(String namespace, String actionName,
152: String resultName, int type) {
153: String viewId = namespace + "/" + actionName + "/" + resultName;
154: View view = (View) viewCache.get(viewId);
155: if (view == null) {
156: File viewFile = XWorkConfigRetriever.getViewFile(namespace,
157: actionName, resultName);
158: if (viewFile != null) {
159: switch (type) {
160: case View.TYPE_JSP:
161: view = new JspView(viewFile);
162: break;
163: case View.TYPE_FTL:
164: view = new FreeMarkerView(viewFile);
165: break;
166: case View.TYPE_VM:
167: view = new VelocityView(viewFile);
168: break;
169: default:
170: return null;
171: }
172:
173: viewCache.put(viewId, view);
174: }
175: }
176: return view;
177: }
178:
179: public static void setConfiguration(String configDir, String[] views) {
180: XWorkConfigRetriever.configDir = configDir;
181: XWorkConfigRetriever.views = views;
182: isXWorkStarted = false;
183: viewCache = new LinkedHashMap();
184: }
185: }
|