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