Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PropertiesURLReader |
|
| 3.0;3 |
1 | package org.apache.onami.configuration; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import java.io.IOException; | |
23 | import java.io.InputStream; | |
24 | import java.net.HttpURLConnection; | |
25 | import java.net.URL; | |
26 | import java.net.URLConnection; | |
27 | import java.util.Properties; | |
28 | ||
29 | import org.apache.onami.configuration.binder.XMLPropertiesFormatBindingBuilder; | |
30 | ||
31 | /** | |
32 | * {@link Properties} reader implementation able to read configuration files from classpath, file system or URLs. | |
33 | * | |
34 | * This reader implementation support both {@code .properties} and {@code .xml} properties format. | |
35 | */ | |
36 | public final class PropertiesURLReader | |
37 | implements XMLPropertiesFormatBindingBuilder | |
38 | { | |
39 | ||
40 | /** | |
41 | * The URL has to be open. | |
42 | */ | |
43 | private final URL url; | |
44 | ||
45 | /** | |
46 | * Flag to mark properties are in XML format. | |
47 | */ | |
48 | 20 | private boolean isXML = false; |
49 | ||
50 | /** | |
51 | * Create a new {@code .properties} reader of a resource located in the given URL. | |
52 | * | |
53 | * @param url the URL that locates the configuration resource. | |
54 | * @param isXML to mark if the properties file is in XML format or not. | |
55 | */ | |
56 | public PropertiesURLReader( URL url ) | |
57 | 20 | { |
58 | 20 | if ( url == null) |
59 | { | |
60 | 0 | throw new IllegalArgumentException( "'url' argument can't be null" ); |
61 | } | |
62 | ||
63 | 20 | this.url = url; |
64 | 20 | } |
65 | ||
66 | /** | |
67 | * {@inheritDoc} | |
68 | */ | |
69 | public void inXMLFormat() | |
70 | { | |
71 | 8 | isXML = true; |
72 | 8 | } |
73 | ||
74 | /** | |
75 | * | |
76 | * @return | |
77 | */ | |
78 | public URL getUrl() | |
79 | { | |
80 | 0 | return url; |
81 | } | |
82 | ||
83 | /** | |
84 | * | |
85 | * | |
86 | * @return | |
87 | * @throws Exception | |
88 | */ | |
89 | public final Properties readConfiguration() | |
90 | throws Exception | |
91 | { | |
92 | 20 | URLConnection connection = null; |
93 | 20 | InputStream input = null; |
94 | try | |
95 | { | |
96 | 20 | connection = url.openConnection(); |
97 | 20 | connection.setUseCaches( false ); |
98 | 20 | input = connection.getInputStream(); |
99 | ||
100 | 20 | Properties properties = new Properties(); |
101 | 20 | if ( isXML ) |
102 | { | |
103 | 8 | properties.loadFromXML( input ); |
104 | } | |
105 | else | |
106 | { | |
107 | 12 | properties.load( input ); |
108 | } | |
109 | ||
110 | 20 | return properties; |
111 | } | |
112 | finally | |
113 | { | |
114 | 20 | if ( connection != null && ( connection instanceof HttpURLConnection ) ) |
115 | { | |
116 | 0 | ( (HttpURLConnection) connection ).disconnect(); |
117 | } | |
118 | 20 | if ( input != null ) |
119 | { | |
120 | try | |
121 | { | |
122 | 20 | input.close(); |
123 | } | |
124 | 0 | catch ( IOException e ) |
125 | { | |
126 | // close quietly | |
127 | 40 | } |
128 | } | |
129 | } | |
130 | } | |
131 | ||
132 | } |