View Javadoc

1   /*
2    * Created on Aug 12, 2004 by mgreer
3    */
4   package org.apache.struts2.config;
5   
6   import java.io.File;
7   import java.io.IOException;
8   import java.net.MalformedURLException;
9   import java.net.URL;
10  import java.util.ArrayList;
11  import java.util.HashMap;
12  import java.util.Iterator;
13  import java.util.List;
14  import java.util.Map;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  import com.opensymphony.xwork2.ActionContext;
20  import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
21  
22  /***
23   * Override Xwork class so we can use an arbitrary config file
24   */
25  public class StrutsXmlConfigurationProvider extends XmlConfigurationProvider {
26  
27      private static final Log LOG = LogFactory.getLog(StrutsXmlConfigurationProvider.class);
28      private File baseDir = null;
29      private String filename;
30  
31      /*** 
32       * Constructs the configuration provider
33       * 
34       * @param errorIfMissing If we should throw an exception if the file can't be found
35       */
36      public StrutsXmlConfigurationProvider(boolean errorIfMissing) {
37          this("struts.xml", errorIfMissing);
38      }
39      
40      /*** 
41       * Constructs the configuration provider
42       * 
43       * @param filename The filename to look for
44       * @param errorIfMissing If we should throw an exception if the file can't be found
45       */
46      public StrutsXmlConfigurationProvider(String filename, boolean errorIfMissing) {
47          super(filename, errorIfMissing);
48          this.filename = filename;
49          Map<String,String> dtdMappings = new HashMap<String,String>(getDtdMappings());
50          dtdMappings.put("-//Apache Software Foundation//DTD Struts Configuration 2.0//EN", "struts-2.0.dtd");
51          setDtdMappings(dtdMappings);
52          File file = new File(filename);
53          if (file.getParent() != null) {
54              this.baseDir = file.getParentFile();
55          }
56      }
57  
58      /***
59       * Look for the configuration file on the classpath and in the file system
60       *
61       * @param fileName The file name to retrieve
62       * @see com.opensymphony.xwork2.config.providers.XmlConfigurationProvider#getInputStream(java.lang.String)
63       */
64      @Override
65      protected Iterator<URL> getConfigurationUrls(String fileName) throws IOException {
66          URL url = null;
67          if (baseDir != null) {
68              url = findInFileSystem(fileName);
69              if (url == null) {
70                  return super.getConfigurationUrls(fileName);
71              }
72          } 
73          if (url != null) {
74              List<URL> list = new ArrayList<URL>();
75              list.add(url);
76              return list.iterator();
77          } else {
78              return super.getConfigurationUrls(fileName);
79          }
80      }
81      
82      protected URL findInFileSystem(String fileName) throws IOException {
83          URL url = null;
84          File file = new File(fileName);
85          if (LOG.isDebugEnabled()) {
86              LOG.debug("Trying to load file " + file);
87          }
88          
89          // Trying relative path to original file
90          if (!file.exists()) {
91              file = new File(baseDir, fileName);
92          }
93          if (file.exists()) {
94              try {
95                  url = file.toURL();
96              } catch (MalformedURLException e) {
97                  throw new IOException("Unable to convert "+file+" to a URL");
98              }
99          } 
100         return url;
101     }
102 
103     /***
104      * Overrides needs reload to ensure it is only checked once per request
105      */
106     @Override
107     public boolean needsReload() {
108         ActionContext ctx = ActionContext.getContext();
109         String key = "configurationReload-"+filename;
110         if (ctx.get(key) == null) {
111             ctx.put(key, Boolean.TRUE);
112             return super.needsReload();
113         }
114         return false;
115         
116     }
117     
118     
119 }