1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * ProtectedProperties.java |
---|
19 | * Copyright (C) 2001 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.core; |
---|
24 | |
---|
25 | import java.io.InputStream; |
---|
26 | import java.util.Enumeration; |
---|
27 | import java.util.Map; |
---|
28 | import java.util.Properties; |
---|
29 | |
---|
30 | /** |
---|
31 | * Simple class that extends the Properties class so that the properties are |
---|
32 | * unable to be modified. |
---|
33 | * |
---|
34 | * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz) |
---|
35 | * @version $Revision: 5953 $ |
---|
36 | */ |
---|
37 | public class ProtectedProperties |
---|
38 | extends Properties |
---|
39 | implements RevisionHandler { |
---|
40 | |
---|
41 | /** for serialization */ |
---|
42 | private static final long serialVersionUID = 3876658672657323985L; |
---|
43 | |
---|
44 | /** the properties need to be open during construction of the object */ |
---|
45 | private boolean closed = false; |
---|
46 | |
---|
47 | /** |
---|
48 | * Creates a set of protected properties from a set of normal ones. |
---|
49 | * |
---|
50 | * @param props the properties to be stored and protected. |
---|
51 | */ |
---|
52 | public ProtectedProperties(Properties props) |
---|
53 | { |
---|
54 | |
---|
55 | Enumeration propEnum = props.propertyNames(); |
---|
56 | while (propEnum.hasMoreElements()) { |
---|
57 | String propName = (String) propEnum.nextElement(); |
---|
58 | String propValue = props.getProperty(propName); |
---|
59 | super.setProperty(propName, propValue); |
---|
60 | } |
---|
61 | closed = true; // no modifications allowed from now on |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Overrides a method to prevent the properties from being modified. |
---|
66 | * |
---|
67 | * @return never returns without throwing an exception. |
---|
68 | * @throws UnsupportedOperationException always. |
---|
69 | */ |
---|
70 | public Object setProperty(String key, String value) |
---|
71 | { |
---|
72 | |
---|
73 | if (closed) |
---|
74 | throw new |
---|
75 | UnsupportedOperationException("ProtectedProperties cannot be modified!"); |
---|
76 | else return super.setProperty(key, value); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Overrides a method to prevent the properties from being modified. |
---|
81 | * |
---|
82 | * @throws UnsupportedOperationException always. |
---|
83 | */ |
---|
84 | public void load(InputStream inStream) { |
---|
85 | |
---|
86 | throw new |
---|
87 | UnsupportedOperationException("ProtectedProperties cannot be modified!"); |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Overrides a method to prevent the properties from being modified. |
---|
92 | * |
---|
93 | * @throws UnsupportedOperationException always. |
---|
94 | */ |
---|
95 | public void clear() { |
---|
96 | |
---|
97 | throw new |
---|
98 | UnsupportedOperationException("ProtectedProperties cannot be modified!"); |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * Overrides a method to prevent the properties from being modified. |
---|
103 | * |
---|
104 | * @return never returns without throwing an exception. |
---|
105 | * @throws UnsupportedOperationException always. |
---|
106 | */ |
---|
107 | public Object put(Object key, |
---|
108 | Object value) { |
---|
109 | |
---|
110 | if (closed) |
---|
111 | throw new |
---|
112 | UnsupportedOperationException("ProtectedProperties cannot be modified!"); |
---|
113 | else return super.put(key, value); |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Overrides a method to prevent the properties from being modified. |
---|
118 | * |
---|
119 | * @throws UnsupportedOperationException always. |
---|
120 | */ |
---|
121 | public void putAll(Map t) { |
---|
122 | |
---|
123 | throw new |
---|
124 | UnsupportedOperationException("ProtectedProperties cannot be modified!"); |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Overrides a method to prevent the properties from being modified. |
---|
129 | * |
---|
130 | * @return never returns without throwing an exception. |
---|
131 | * @throws UnsupportedOperationException always. |
---|
132 | */ |
---|
133 | public Object remove(Object key) { |
---|
134 | |
---|
135 | throw new |
---|
136 | UnsupportedOperationException("ProtectedProperties cannot be modified!"); |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * Returns the revision string. |
---|
141 | * |
---|
142 | * @return the revision |
---|
143 | */ |
---|
144 | public String getRevision() { |
---|
145 | return RevisionUtils.extract("$Revision: 5953 $"); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|