1 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 ; 19 import java.io.IOException ; 20 import java.util.*; 21 22 25 public class XWorkConfigRetriever { 26 27 private static final Log LOG = LogFactory.getLog(XWorkConfigRetriever.class); 28 private static String configDir; 29 private static String [] views; 30 private static boolean isXWorkStarted = false; 31 private static Map viewCache = new TreeMap(); 32 33 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 configFilePath = configDir + "/xwork.xml"; 46 File configFile = new File (configFilePath); 47 try { 48 ConfigurationProvider configProvider = new ArbitraryXMLConfigurationProvider(configFile.getCanonicalPath()); 49 ConfigurationManager.addConfigurationProvider(configProvider); 50 isXWorkStarted = true; 51 } catch (IOException 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 71 public static Set getActionNames(String 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 90 public static ActionConfig getActionConfig(String namespace, String 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 namespace, String actionName, 103 String 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 getViewFile(String namespace, String actionName, String resultName) { 114 ResultConfig result = getResultConfig(namespace, actionName, resultName); 115 String location = (String ) result.getParams().get("location"); 116 for (int i = 0; i < views.length; i++) { 117 String viewRoot = views[i]; 118 File viewFile = getViewFileInternal(viewRoot, location, namespace); 119 if (viewFile != null) { 120 return viewFile; 121 } 122 } 123 124 return null; 125 } 126 127 private static File getViewFileInternal(String root, String location, String namespace) { 128 StringBuffer filePath = new StringBuffer (root); 129 if (!location.startsWith("/")) { 130 filePath.append(namespace + "/"); 131 } 132 filePath.append(location); 133 File viewFile = new File (filePath.toString()); 134 if (viewFile.exists()) { 135 return viewFile; 136 } else { 137 return null; 138 } 139 } 140 141 public static View getView(String namespace, String actionName, String resultName, int type) { 142 String viewId = namespace + "/" + actionName + "/" + resultName; 143 View view = (View) viewCache.get(viewId); 144 if (view == null) { 145 File 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 configDir, String [] views) { 168 XWorkConfigRetriever.configDir = configDir; 169 XWorkConfigRetriever.views = views; 170 isXWorkStarted = false; 171 viewCache = new TreeMap(); 172 } 173 } | Popular Tags |