Change nexus values to properties
[appc.git] / app-c / appc / appc-common / src / main / java / org / openecomp / appc / util / UnmodifiableProperties.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * 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, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22
23
24 package org.openecomp.appc.util;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.io.PrintStream;
30 import java.io.PrintWriter;
31 import java.io.Reader;
32 import java.io.Writer;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.Enumeration;
36 import java.util.InvalidPropertiesFormatException;
37 import java.util.Map;
38 import java.util.Properties;
39 import java.util.Set;
40
41 /**
42  * This utility class is used to wrap a properties object and to delegate all read operations to the property object,
43  * while disallowing any write or modification to the property object.
44  * 
45  */
46 public class UnmodifiableProperties extends Properties implements Cloneable {
47
48     /**
49      * Serial number
50      */
51     private static final long serialVersionUID = 1L;
52
53     private static final String PROPERTY_CANNOT_BE_MODIFIED_MSG = "Property cannot be modified!";
54
55     /**
56      * The properties object which we are wrapping
57      */
58     private Properties properties;
59
60     /**
61      * Create the unmodifiable wrapper around the provided properties object
62      * 
63      * @param properties
64      *            The properties to be wrapped and protected from modification
65      */
66     public UnmodifiableProperties(Properties properties) {
67         this.properties = properties;
68     }
69
70     /**
71      * @see java.util.Hashtable#clear()
72      */
73     @Override
74     public synchronized void clear() {
75         throw new UnsupportedOperationException(PROPERTY_CANNOT_BE_MODIFIED_MSG);
76     }
77
78     /**
79      * @see java.util.Hashtable#clone()
80      */
81     // @sonar:off
82     @Override
83     public synchronized Object clone() {
84         throw new UnsupportedOperationException(PROPERTY_CANNOT_BE_MODIFIED_MSG);
85     }
86
87     // @sonar:on
88
89     /**
90      * @see java.util.Hashtable#contains(java.lang.Object)
91      */
92     @Override
93     public synchronized boolean contains(Object value) {
94         return properties.contains(value);
95     }
96
97     /**
98      * @see java.util.Hashtable#containsKey(java.lang.Object)
99      */
100     @Override
101     public synchronized boolean containsKey(Object key) {
102         return properties.containsKey(key);
103     }
104
105     /**
106      * @see java.util.Hashtable#containsValue(java.lang.Object)
107      */
108     @Override
109     public boolean containsValue(Object value) {
110         return properties.containsValue(value);
111     }
112
113     /**
114      * @see java.util.Hashtable#elements()
115      */
116     @Override
117     public synchronized Enumeration<Object> elements() {
118         return properties.elements();
119     }
120
121     /**
122      * @see java.util.Hashtable#entrySet()
123      */
124     @Override
125     public Set<java.util.Map.Entry<Object, Object>> entrySet() {
126         return Collections.unmodifiableSet(properties.entrySet());
127     }
128
129     /**
130      * @see java.util.Hashtable#equals(java.lang.Object)
131      */
132     @Override
133     public synchronized boolean equals(Object o) {
134         return properties.equals(o);
135     }
136
137     /**
138      * @see java.util.Hashtable#get(java.lang.Object)
139      */
140     @Override
141     public synchronized Object get(Object key) {
142         return properties.get(key);
143     }
144
145     /**
146      * @see java.util.Properties#getProperty(java.lang.String)
147      */
148     @Override
149     public String getProperty(String key) {
150         return properties.getProperty(key);
151     }
152
153     /**
154      * @see java.util.Properties#getProperty(java.lang.String, java.lang.String)
155      */
156     @Override
157     public String getProperty(String key, String defaultValue) {
158         return properties.getProperty(key, defaultValue);
159     }
160
161     /**
162      * @see java.util.Hashtable#hashCode()
163      */
164     @Override
165     public synchronized int hashCode() {
166         return properties.hashCode();
167     }
168
169     /**
170      * @see java.util.Hashtable#isEmpty()
171      */
172     @Override
173     public synchronized boolean isEmpty() {
174         return properties.isEmpty();
175     }
176
177     /**
178      * @see java.util.Hashtable#keys()
179      */
180     @Override
181     public synchronized Enumeration<Object> keys() {
182         return properties.keys();
183     }
184
185     /**
186      * @see java.util.Hashtable#keySet()
187      */
188     @Override
189     public Set<Object> keySet() {
190         return Collections.unmodifiableSet(properties.keySet());
191     }
192
193     /**
194      * @see java.util.Properties#list(java.io.PrintStream)
195      */
196     @Override
197     public void list(PrintStream out) {
198         properties.list(out);
199     }
200
201     /**
202      * @see java.util.Properties#list(java.io.PrintWriter)
203      */
204     @Override
205     public void list(PrintWriter out) {
206         properties.list(out);
207     }
208
209     /**
210      * @see java.util.Properties#load(java.io.InputStream)
211      */
212     @Override
213     public synchronized void load(InputStream inStream) throws IOException {
214         throw new UnsupportedOperationException(PROPERTY_CANNOT_BE_MODIFIED_MSG);
215     }
216
217     /**
218      * @see java.util.Properties#load(java.io.Reader)
219      */
220     @Override
221     public synchronized void load(Reader reader) throws IOException {
222         throw new UnsupportedOperationException(PROPERTY_CANNOT_BE_MODIFIED_MSG);
223     }
224
225     /**
226      * @see java.util.Properties#loadFromXML(java.io.InputStream)
227      */
228     @Override
229     public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException {
230         throw new UnsupportedOperationException(PROPERTY_CANNOT_BE_MODIFIED_MSG);
231     }
232
233     /**
234      * @see java.util.Properties#propertyNames()
235      */
236     @Override
237     public Enumeration<?> propertyNames() {
238         return properties.propertyNames();
239     }
240
241     /**
242      * @see java.util.Hashtable#put(java.lang.Object, java.lang.Object)
243      */
244     @Override
245     public synchronized Object put(Object key, Object value) {
246         throw new UnsupportedOperationException(PROPERTY_CANNOT_BE_MODIFIED_MSG);
247     }
248
249     /**
250      * @see java.util.Hashtable#putAll(java.util.Map)
251      */
252     @Override
253     public synchronized void putAll(Map<? extends Object, ? extends Object> t) {
254         throw new UnsupportedOperationException(PROPERTY_CANNOT_BE_MODIFIED_MSG);
255     }
256
257     /**
258      * @see java.util.Hashtable#rehash()
259      */
260     @Override
261     protected void rehash() {
262         throw new UnsupportedOperationException(PROPERTY_CANNOT_BE_MODIFIED_MSG);
263     }
264
265     /**
266      * @see java.util.Hashtable#remove(java.lang.Object)
267      */
268     @Override
269     public synchronized Object remove(Object key) {
270         throw new UnsupportedOperationException(PROPERTY_CANNOT_BE_MODIFIED_MSG);
271     }
272
273     /**
274      * @see java.util.Properties#save(java.io.OutputStream, java.lang.String)
275      */
276     @Override
277     @Deprecated
278     public synchronized void save(OutputStream out, String comments) {
279         properties.save(out, comments);
280     }
281
282     /**
283      * @see java.util.Properties#setProperty(java.lang.String, java.lang.String)
284      */
285     @Override
286     public synchronized Object setProperty(String key, String value) {
287         throw new UnsupportedOperationException(PROPERTY_CANNOT_BE_MODIFIED_MSG);
288     }
289
290     /**
291      * @see java.util.Hashtable#size()
292      */
293     @Override
294     public synchronized int size() {
295         return properties.size();
296     }
297
298     /**
299      * @see java.util.Properties#store(java.io.OutputStream, java.lang.String)
300      */
301     @Override
302     public void store(OutputStream out, String comments) throws IOException {
303         properties.store(out, comments);
304     }
305
306     /**
307      * @see java.util.Properties#store(java.io.Writer, java.lang.String)
308      */
309     @Override
310     public void store(Writer writer, String comments) throws IOException {
311         properties.store(writer, comments);
312     }
313
314     /**
315      * @see java.util.Properties#storeToXML(java.io.OutputStream, java.lang.String)
316      */
317     @Override
318     public synchronized void storeToXML(OutputStream os, String comment) throws IOException {
319         properties.storeToXML(os, comment);
320     }
321
322     /**
323      * @see java.util.Properties#storeToXML(java.io.OutputStream, java.lang.String, java.lang.String)
324      */
325     @Override
326     public synchronized void storeToXML(OutputStream os, String comment, String encoding) throws IOException {
327         properties.storeToXML(os, comment, encoding);
328     }
329
330     /**
331      * @see java.util.Properties#stringPropertyNames()
332      */
333     @Override
334     public Set<String> stringPropertyNames() {
335         return properties.stringPropertyNames();
336     }
337
338     /**
339      * @see java.util.Hashtable#toString()
340      */
341     @Override
342     public synchronized String toString() {
343         return properties.toString();
344     }
345
346     /**
347      * @see java.util.Hashtable#values()
348      */
349     @Override
350     public Collection<Object> values() {
351         return Collections.unmodifiableCollection(properties.values());
352     }
353 }