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