1 package org.jdiseq.filter;
2
3 import java.io.InputStream;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.util.List;
7
8 import org.jdom.Document;
9 import org.jdom.Element;
10 import org.jdom.JDOMException;
11 import org.jdom.input.SAXBuilder;
12
13 import org.jdiseq.util.MessageUtil;
14 import org.apache.commons.logging.LogFactory;
15 import org.apache.commons.logging.Log;
16
17 /***
18 * FilterPersistenceImpl holds all the filter configuration data used by jDiSeq.
19 * It will allow clients to ask for filter configuration data whenever needed.
20 *
21 * @author Trond Andersen <trondandersen@c2i.net>
22 * @version $Id: FilterPersistenceImpl.java,v 1.2 2003/08/17 13:15:53 trondandersen Exp $
23 * @since 1.0
24 */
25 public class FilterPersistenceImpl implements FilterPersistence, Persister {
26 /*** Logging reference */
27 private final static Log log = LogFactory.getLog(FilterPersistenceImpl.class);
28
29 /***Filter configuration on pacakges */
30 private Element root;
31
32 /***
33 * Reads configuration from an <code>InputStream</code> given by the client.
34 * @param inputStream <code>InputStream</code> to the configuration file
35 * @throws InvalidConfigurationException Failed to read configuration file
36 */
37 public FilterPersistenceImpl(InputStream inputStream) throws InvalidConfigurationException {
38 parseConfiguration(inputStream);
39 }
40
41 /***
42 * Initilized the configuration based on the filename given as a parameter.
43 * @param configFilename Name of the filter configuration file
44 * @throws InvalidConfigurationException Failed to read configuration file
45 * @see #FilterPersistenceImpl(InputStream)
46 * @precondition configFileName != null
47 * @precondition configFileName.length() > 0
48 */
49 public FilterPersistenceImpl(String configFilename) throws InvalidConfigurationException {
50 assert (configFilename != null);
51 assert (configFilename.trim().length() > 0);
52 root = parseConfiguration(getConfigurationInputStream(configFilename));
53 /*setPackageFilters(root.getChild("package-filters", NAMESPACE).getChildren("package-filter", NAMESPACE));
54 setMethodFilters(root.getChild("method-filters", NAMESPACE).getChildren("method-filter", NAMESPACE));*/
55 }
56
57 /***
58 * Initializes the configuration with the default filter configuration file name.
59 * This default filename is 'filterConfig.xml'.
60 * @see #FilterPersistenceImpl(String)
61 */
62 public FilterPersistenceImpl() throws InvalidConfigurationException {
63 this("config/filterConfig.xml");
64 }
65
66 /*** {@inheritDoc} */
67 public Filter[] load() throws InvalidConfigurationException {
68
69 return new MethodFilter[0];
70 }
71
72 /*** {@inheritDoc} */
73 public MethodFilter[] loadMethodFilter() throws InvalidConfigurationException {
74 List methodFilters = getMethodFilterParent().getChildren("method-filter", NAMESPACE);
75 MethodFilter[] mthFilter = new MethodFilter[methodFilters.size()];
76 for (int i = 0; i < methodFilters.size(); i++) {
77 mthFilter[i] = new MethodFilter((Element) methodFilters.get(i));
78 }
79 return mthFilter;
80 }
81
82 /*** {@inheritDoc} */
83 public PackageFilter[] loadPackageFilter() throws InvalidConfigurationException {
84 List packageFilters = getPackageFilterParent().getChildren("package-filter", NAMESPACE);
85 PackageFilter[] pckFilter = new PackageFilter[packageFilters/size()]/package-summary.html">PackageFilter[] pckFilter = new PackageFilter[packageFilters.size()];
86 for (int i = 0; i < packageFilters.size(); i++) {
87 PackageFilter((Element) packageFilters/get(i))/package-summary.html">pckFilter[i] = new PackageFilter((Element) packageFilters.get(i));
88 }
89 return pckFilter;
90 }
91
92 /*** {@inheritDoc} */
93 public Element getMethodFilterParent() {
94 return root.getChild("method-filters", NAMESPACE);
95 }
96
97 /*** {@inheritDoc} */
98 public Element getPackageFilterParent() {
99 return root.getChild("package-filters", NAMESPACE);
100 }
101
102 /*** {@inheritDoc} */
103 public Document getXMLDocmuent() {
104 return root.getDocument();
105 }
106
107
108 /***
109 * Reads the configuration file's content and sets the configuration settings.
110 *
111 * @param inputStream <code>InputStream</code> used to read the configuration file
112 * @return Root element of the jdom three representing the <code>XML</code> document
113 * @throws InvalidConfigurationException Failed to access the configuration file
114 */
115 protected Element parseConfiguration(InputStream inputStream) throws InvalidConfigurationException {
116 SAXBuilder builder = new SAXBuilder();
117
118 try {
119 Document document = builder.build(inputStream);
120 root = document.getRootElement();
121 } catch (JDOMException jdomEx) {
122 log.info(jdomEx.getMessage());
123 throw new InvalidConfigurationException(jdomEx);
124 } catch (IOException ioEx) {
125 log.info(ioEx.getMessage());
126 throw new InvalidConfigurationException(ioEx);
127 }
128 return root;
129 }
130
131 /***
132 * @param configFile name of the configuration file
133 * @return the stream to the configuration file
134 * @throws InvalidConfigurationException Failed to read the <code>InputStream</code> content
135 */
136 protected InputStream getConfigurationInputStream(String configFile) throws InvalidConfigurationException {
137 InputStream stream = null;
138
139 try {
140 stream = new FileInputStream(configFile);
141 } catch (IOException ioException) {
142 log.info(MessageUtil.getInstance().getMessage("filter.configuration.classpath.trying"));
143 stream = getConfigurationInputStreamFromClassloader(configFile);
144 }
145 if (stream == null) {
146 log.error(MessageUtil.getInstance().getMessage("filter.configuration.unavailable"));
147 throw new InvalidConfigurationException(MessageUtil.getInstance().getMessage("filter.configuration.unavailable"));
148 }
149 return stream;
150 }
151
152 /***
153 * Reads the configuration file from the classpath.
154 * @param configFile Name of the configuration file
155 * @return configuration file as stream read from classpath
156 */
157 private InputStream getConfigurationInputStreamFromClassloader(String configFile) {
158 return this.getClass().getResourceAsStream(configFile);
159 }
160 }
This page was automatically generated by Maven