View Javadoc

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