|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.jconfig.handler; |
|
8 |
| |
|
9 |
| import java.io.IOException; |
|
10 |
| import java.io.InputStream; |
|
11 |
| import java.io.OutputStreamWriter; |
|
12 |
| import java.io.PrintWriter; |
|
13 |
| import java.util.Properties; |
|
14 |
| |
|
15 |
| import javax.xml.parsers.DocumentBuilder; |
|
16 |
| import javax.xml.parsers.DocumentBuilderFactory; |
|
17 |
| import javax.xml.parsers.ParserConfigurationException; |
|
18 |
| |
|
19 |
| import org.jconfig.Configuration; |
|
20 |
| import org.jconfig.ConfigurationManagerException; |
|
21 |
| import org.jconfig.URLFileWatcher; |
|
22 |
| import org.jconfig.error.ErrorReporter; |
|
23 |
| import org.jconfig.event.FileListener; |
|
24 |
| import org.jconfig.event.FileListenerEvent; |
|
25 |
| import org.jconfig.parser.ConfigurationParser; |
|
26 |
| import org.jconfig.parser.ConfigurationParserFactory; |
|
27 |
| import org.jconfig.utils.ConfigErrorHandler; |
|
28 |
| import org.w3c.dom.Document; |
|
29 |
| import org.xml.sax.SAXException; |
|
30 |
| import org.jconfig.utils.ConfigurationUtils; |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| public class URLHandler implements ConfigurationHandler, FileListener { |
|
45 |
| |
|
46 |
| private boolean validate = false; |
|
47 |
| private String url; |
|
48 |
| |
|
49 |
| |
|
50 |
| private URLFileWatcher watcher = null; |
|
51 |
| |
|
52 |
1
| public URLHandler() {
|
|
53 |
| } |
|
54 |
| |
|
55 |
1
| public void setURL( String url ) {
|
|
56 |
1
| this.url = url;
|
|
57 |
| } |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
0
| public void setValidation(boolean validate) {
|
|
66 |
0
| this.validate = validate;
|
|
67 |
| } |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
0
| public synchronized Configuration load() throws ConfigurationManagerException {
|
|
72 |
0
| Configuration c = load( url );
|
|
73 |
0
| addFileListener(this);
|
|
74 |
0
| return ( c );
|
|
75 |
| } |
|
76 |
| |
|
77 |
0
| public void fileChanged(FileListenerEvent event) {
|
|
78 |
0
| try {
|
|
79 |
| |
|
80 |
0
| load();
|
|
81 |
| } catch (ConfigurationManagerException e) { |
|
82 |
0
| ErrorReporter.getErrorHandler().reportError("Error while reloading",e);
|
|
83 |
| |
|
84 |
| } |
|
85 |
| } |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
1
| public synchronized Configuration load(String configName) throws ConfigurationManagerException {
|
|
95 |
1
| return load(getURL(), configName);
|
|
96 |
| } |
|
97 |
| |
|
98 |
1
| protected Configuration load(String theURL,String configName) throws ConfigurationManagerException {
|
|
99 |
1
| java.net.URL jcfURL = null;
|
|
100 |
1
| InputStream is = null;
|
|
101 |
1
| try {
|
|
102 |
| |
|
103 |
1
| ClassLoader cl = this.getClass().getClassLoader();
|
|
104 |
1
| InputStream jcf = cl.getResourceAsStream( "jconfig.properties" );
|
|
105 |
| |
|
106 |
1
| if ( jcf != null ) {
|
|
107 |
1
| Properties jcfProperties = new Properties();
|
|
108 |
1
| jcfProperties.load( jcf );
|
|
109 |
| |
|
110 |
| |
|
111 |
1
| Properties prop = System.getProperties();
|
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
1
| if ( jcfProperties.getProperty( "http.proxyHost" ) != null )
|
|
116 |
1
| prop.put( "http.proxyHost", jcfProperties.getProperty( "http.proxyHost" ) );
|
|
117 |
1
| if ( jcfProperties.getProperty( "http.proxyPort" ) != null )
|
|
118 |
1
| prop.put( "http.proxyPort", jcfProperties.getProperty( "http.proxyPort" ) );
|
|
119 |
| } |
|
120 |
| |
|
121 |
1
| jcfURL = new java.net.URL( theURL );
|
|
122 |
| |
|
123 |
1
| java.net.URLConnection con = jcfURL.openConnection();
|
|
124 |
1
| is = con.getInputStream();
|
|
125 |
| } |
|
126 |
| catch ( Exception e ) { |
|
127 |
0
| ErrorReporter.getErrorHandler().reportError("Problem with URL handling/connection/validating",e);
|
|
128 |
0
| throw new ConfigurationManagerException( "Problem with URL handling/connection/validating: "
|
|
129 |
| + e.getMessage() ); |
|
130 |
| } |
|
131 |
| |
|
132 |
1
| DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
|
133 |
1
| dbf.setValidating(validate);
|
|
134 |
1
| DocumentBuilder db = null;
|
|
135 |
1
| try {
|
|
136 |
1
| db = dbf.newDocumentBuilder();
|
|
137 |
| } catch (ParserConfigurationException pce) { |
|
138 |
0
| ErrorReporter.getErrorHandler().reportError("The parser cannot create a new document builder",pce);
|
|
139 |
0
| throw new ConfigurationManagerException("The parser cannot create a new document builder: " + pce.getMessage());
|
|
140 |
| } |
|
141 |
| |
|
142 |
1
| if (validate) {
|
|
143 |
0
| try {
|
|
144 |
| |
|
145 |
0
| OutputStreamWriter errorWriter =
|
|
146 |
| new OutputStreamWriter(System.err, "UTF-8"); |
|
147 |
0
| db.setErrorHandler(
|
|
148 |
| new ConfigErrorHandler(new PrintWriter(errorWriter, true))); |
|
149 |
| } catch (Exception e) { |
|
150 |
0
| ErrorReporter.getErrorHandler().reportError("The parser cannot set up the error handler",e);
|
|
151 |
0
| throw new ConfigurationManagerException("The parser cannot set up the error handler");
|
|
152 |
| } |
|
153 |
| } |
|
154 |
1
| Document doc = null;
|
|
155 |
1
| try {
|
|
156 |
1
| doc = db.parse(is);
|
|
157 |
| } catch (SAXException se) { |
|
158 |
0
| ErrorReporter.getErrorHandler().reportError("The parser cannot parse the XML",se);
|
|
159 |
0
| throw new ConfigurationManagerException("The parser cannot parse the XML: " + se.getMessage());
|
|
160 |
| } catch (IOException ioe) { |
|
161 |
0
| ErrorReporter.getErrorHandler().reportError("The parser cannot open the file",ioe);
|
|
162 |
0
| throw new ConfigurationManagerException("The parser cannot open the file: " + ioe.getMessage());
|
|
163 |
| } |
|
164 |
1
| ConfigurationParser parser = ConfigurationParserFactory.getParser(configName);
|
|
165 |
1
| Configuration config = parser.parse(doc, configName);
|
|
166 |
1
| config.resetCreated();
|
|
167 |
1
| return config;
|
|
168 |
| } |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
0
| public void store(Configuration configuration) throws ConfigurationManagerException {
|
|
174 |
| } |
|
175 |
| |
|
176 |
| |
|
177 |
0
| public void addFileListener( FileListener fileListener ) {
|
|
178 |
0
| try {
|
|
179 |
0
| String val = ConfigurationUtils.getConfigProperty("jconfig.filewatcher","true");
|
|
180 |
0
| if ( val.equalsIgnoreCase("true") ) {
|
|
181 |
0
| watcher = new URLFileWatcher( getURL() );
|
|
182 |
0
| watcher.addFileListener( fileListener );
|
|
183 |
0
| watcher.start();
|
|
184 |
| } |
|
185 |
| } |
|
186 |
| catch ( Exception e ) { |
|
187 |
0
| ErrorReporter.getErrorHandler().reportError("Problem while setting up the watcher",e);
|
|
188 |
| } |
|
189 |
| } |
|
190 |
| |
|
191 |
1
| public String getURL() {
|
|
192 |
1
| return ( url );
|
|
193 |
| } |
|
194 |
| } |