KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > webFlow > XWorkConfigRetriever


1 /*
2  * Created on Aug 14, 2004 by mgreer
3  */

4 package com.opensymphony.webwork.webFlow;
5
6 import com.opensymphony.webwork.webFlow.collectors.ArbitraryXMLConfigurationProvider;
7 import com.opensymphony.webwork.webFlow.entities.FreeMarkerView;
8 import com.opensymphony.webwork.webFlow.entities.JspView;
9 import com.opensymphony.webwork.webFlow.entities.VelocityView;
10 import com.opensymphony.webwork.webFlow.entities.View;
11 import com.opensymphony.xwork.config.ConfigurationManager;
12 import com.opensymphony.xwork.config.ConfigurationProvider;
13 import com.opensymphony.xwork.config.entities.ActionConfig;
14 import com.opensymphony.xwork.config.entities.ResultConfig;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.*;
21
22 /**
23  * Initializes and retrieves XWork config elements
24  */

25 public class XWorkConfigRetriever {
26
27     private static final Log LOG = LogFactory.getLog(XWorkConfigRetriever.class);
28     private static String JavaDoc configDir;
29     private static String JavaDoc[] views;
30     private static boolean isXWorkStarted = false;
31     private static Map viewCache = new TreeMap();
32
33     /**
34      * Returns a Map of all action names/configs
35      *
36      * @return
37      */

38     public static Map getActionConfigs() {
39         if (!isXWorkStarted)
40             initXWork();
41         return ConfigurationManager.getConfiguration().getRuntimeConfiguration().getActionConfigs();
42     }
43
44     private static void initXWork() {
45         String JavaDoc configFilePath = configDir + "/xwork.xml";
46         File JavaDoc configFile = new File JavaDoc(configFilePath);
47         try {
48             ConfigurationProvider configProvider = new ArbitraryXMLConfigurationProvider(configFile.getCanonicalPath());
49             ConfigurationManager.addConfigurationProvider(configProvider);
50             isXWorkStarted = true;
51         } catch (IOException JavaDoc e) {
52             LOG.error("IOException", e);
53         }
54     }
55
56     public static Set getNamespaces() {
57         Set namespaces = Collections.EMPTY_SET;
58         Map allActionConfigs = getActionConfigs();
59         if (allActionConfigs != null) {
60             namespaces = allActionConfigs.keySet();
61         }
62         return namespaces;
63     }
64
65     /**
66      * Return a Map of the action names for this namespace
67      *
68      * @param namespace
69      * @return
70      */

71     public static Set getActionNames(String JavaDoc namespace) {
72         Set actionNames = Collections.EMPTY_SET;
73         Map allActionConfigs = getActionConfigs();
74         if (allActionConfigs != null) {
75             Map actionMappings = (Map) allActionConfigs.get(namespace);
76             if (actionMappings != null) {
77                 actionNames = actionMappings.keySet();
78             }
79         }
80         return actionNames;
81     }
82
83     /**
84      * Returns the ActionConfig for this action name at this namespace
85      *
86      * @param namespace
87      * @param actionName
88      * @return
89      */

90     public static ActionConfig getActionConfig(String JavaDoc namespace, String JavaDoc actionName) {
91         ActionConfig config = null;
92         Map allActionConfigs = getActionConfigs();
93         if (allActionConfigs != null) {
94             Map actionMappings = (Map) allActionConfigs.get(namespace);
95             if (actionMappings != null) {
96                 config = (ActionConfig) actionMappings.get(actionName);
97             }
98         }
99         return config;
100     }
101
102     public static ResultConfig getResultConfig(String JavaDoc namespace, String JavaDoc actionName,
103                                                String JavaDoc resultName) {
104         ResultConfig result = null;
105         ActionConfig actionConfig = getActionConfig(namespace, actionName);
106         if (actionConfig != null) {
107             Map resultMap = actionConfig.getResults();
108             result = (ResultConfig) resultMap.get(resultName);
109         }
110         return result;
111     }
112
113     public static File JavaDoc getViewFile(String JavaDoc namespace, String JavaDoc actionName, String JavaDoc resultName) {
114         ResultConfig result = getResultConfig(namespace, actionName, resultName);
115         String JavaDoc location = (String JavaDoc) result.getParams().get("location");
116         for (int i = 0; i < views.length; i++) {
117             String JavaDoc viewRoot = views[i];
118             File JavaDoc viewFile = getViewFileInternal(viewRoot, location, namespace);
119             if (viewFile != null) {
120                 return viewFile;
121             }
122         }
123
124         return null;
125     }
126
127     private static File JavaDoc getViewFileInternal(String JavaDoc root, String JavaDoc location, String JavaDoc namespace) {
128         StringBuffer JavaDoc filePath = new StringBuffer JavaDoc(root);
129         if (!location.startsWith("/")) {
130             filePath.append(namespace + "/");
131         }
132         filePath.append(location);
133         File JavaDoc viewFile = new File JavaDoc(filePath.toString());
134         if (viewFile.exists()) {
135             return viewFile;
136         } else {
137             return null;
138         }
139     }
140
141     public static View getView(String JavaDoc namespace, String JavaDoc actionName, String JavaDoc resultName, int type) {
142         String JavaDoc viewId = namespace + "/" + actionName + "/" + resultName;
143         View view = (View) viewCache.get(viewId);
144         if (view == null) {
145             File JavaDoc viewFile = XWorkConfigRetriever.getViewFile(namespace, actionName, resultName);
146             if (viewFile != null) {
147                 switch (type) {
148                     case View.TYPE_JSP:
149                         view = new JspView(viewFile);
150                         break;
151                     case View.TYPE_FTL:
152                         view = new FreeMarkerView(viewFile);
153                         break;
154                     case View.TYPE_VM:
155                         view = new VelocityView(viewFile);
156                         break;
157                     default:
158                         return null;
159                 }
160
161                 viewCache.put(viewId, view);
162             }
163         }
164         return view;
165     }
166
167     public static void setConfiguration(String JavaDoc configDir, String JavaDoc[] views) {
168         XWorkConfigRetriever.configDir = configDir;
169         XWorkConfigRetriever.views = views;
170         isXWorkStarted = false;
171         viewCache = new TreeMap();
172     }
173 }
Popular Tags