|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.panel.common.smf; |
|
27 |
|
28 import java.util.*; |
|
29 import com.oracle.solaris.scf.common.*; |
|
30 import com.oracle.solaris.vp.panel.common.api.smf_old.*; |
|
31 import com.oracle.solaris.vp.util.misc.converter.*; |
|
32 import com.oracle.solaris.vp.util.misc.property.BasicMutableProperty; |
|
33 |
|
34 /** |
|
35 * The {@code BasicSmfMutableProperty} class is a {@link SmfMutableProperty} for |
|
36 * a standard SMF property within a property group. |
|
37 */ |
|
38 public abstract class BasicSmfMutableProperty<T> |
|
39 extends BasicMutableProperty<List<T>> |
|
40 implements SmfMutableProperty<List<T>> { |
|
41 |
|
42 // |
|
43 // Inner classes |
|
44 // |
|
45 |
|
46 private class MutablePropertyConverter |
|
47 implements DualConverter<String, List<T>> { |
|
48 |
|
49 // |
|
50 // Instance data |
|
51 // |
|
52 |
|
53 private StringArrayStringConverter sConverter = |
|
54 new StringArrayStringConverter(); |
|
55 |
|
56 // |
|
57 // Converter methods |
|
58 // |
|
59 |
|
60 @Override |
|
61 public List<T> convert(String s) { |
|
62 String[] array = sConverter.convert(s); |
|
63 List<T> list = new ArrayList<T>(array.length); |
|
64 |
|
65 for (int i = 0; i < array.length; i++) { |
|
66 T t = converter.convert(array[i]); |
|
67 list.add(t); |
|
68 } |
|
69 |
|
70 return list; |
|
71 } |
|
72 |
|
73 // |
|
74 // DualConverter methods |
|
75 // |
|
76 |
|
77 @Override |
|
78 public String revert(List<T> t) { |
|
79 String[] array = new String[t.size()]; |
|
80 for (int i = 0; i < array.length; i++) { |
|
81 array[i] = converter.revert(t.get(i)); |
|
82 } |
|
83 return sConverter.revert(array); |
|
84 } |
|
85 } |
|
86 |
|
87 // |
|
88 // Instance data |
|
89 // |
|
90 |
|
91 private DualConverter<String, List<T>> pConverter = |
|
92 new MutablePropertyConverter(); |
|
93 |
|
94 private DualConverter<String, T> converter; |
|
95 private PropertyType type; |
|
96 private SmfPropertyInfo info; |
|
97 private boolean readable_ = true; |
|
98 |
|
99 // |
|
100 // Constructors |
|
101 // |
|
102 |
|
103 /** |
|
104 * Constructs a {@code BasicSmfMutableProperty}. |
|
105 * </p> |
|
106 * Note: if {@link SmfPropertyInfo#getPropertyGroupName |
|
107 * info.getPropertyName} returns {@code null}, all read/write operations of |
|
108 * this class will use {@code name} as the SMF property name. |
|
109 * |
|
110 * @param name |
|
111 * the name of the property (may differ from the name of this |
|
112 * property in the SMF repository) |
|
113 * |
|
114 * @param converter |
|
115 * used to convert property elements to {@code String}s, to be |
|
116 * stored in the SMF repository |
|
117 * |
|
118 * @param type |
|
119 * the type of the property |
|
120 * |
|
121 * @param info |
|
122 * source of the service, property group name, and property |
|
123 * name |
|
124 */ |
|
125 public BasicSmfMutableProperty(String name, |
|
126 DualConverter<String, T> converter, PropertyType type, |
|
127 SmfPropertyInfo info) { |
|
128 |
|
129 super(name); |
|
130 |
|
131 setPropertyConverter(converter); |
|
132 this.type = type; |
|
133 this.info = info; |
|
134 } |
|
135 |
|
136 // |
|
137 // SmfMutableProperty methods |
|
138 // |
|
139 |
|
140 @Override |
|
141 public DualConverter<String, List<T>> getConverter() { |
|
142 return pConverter; |
|
143 } |
|
144 |
|
145 @Override |
|
146 public List<T> getRepoValue() throws ScfException { |
|
147 List<T> values = null; |
|
148 List<String> sValues; |
|
149 |
|
150 try { |
|
151 readable_ = true; |
|
152 sValues = getValuesFromRepository(); |
|
153 } catch (ScfException e) { |
|
154 // Does property or property group not yet exist? |
|
155 if (e.getError() == SmfErrorCode.NOT_FOUND) { |
|
156 sValues = Collections.emptyList(); |
|
157 } else if (e.getError() == SmfErrorCode.PERMISSION_DENIED) { |
|
158 readable_ = false; |
|
159 return values; |
|
160 } else { |
|
161 throw e; |
|
162 } |
|
163 } |
|
164 |
|
165 if (sValues != null) { |
|
166 int n = sValues.size(); |
|
167 |
|
168 values = new ArrayList<T>(n); |
|
169 for (int i = 0; i < n; i++) { |
|
170 String s = sValues.get(i); |
|
171 values.add(converter.convert(s)); |
|
172 } |
|
173 } |
|
174 |
|
175 return values; |
|
176 } |
|
177 |
|
178 @Override |
|
179 public void saveToRepo() throws ScfException { |
|
180 List<T> value = getValue(); |
|
181 setRepoValue(value); |
|
182 save(); |
|
183 } |
|
184 |
|
185 @Override |
|
186 public void setRepoValue(List<T> values) throws ScfException { |
|
187 List<String> array = new ArrayList<String>(values.size()); |
|
188 for (T v : values) |
|
189 array.add(converter.revert(v)); |
|
190 |
|
191 ServiceMXBean service = info.getService(); |
|
192 String group = info.getPropertyGroupName(); |
|
193 String name = getSmfPropertyName(); |
|
194 |
|
195 try { |
|
196 service.setPropertyValues(group, name, array); |
|
197 } catch (ScfException e) { |
|
198 // Does property not yet exist? |
|
199 if (e.getError() == SmfErrorCode.NOT_FOUND) { |
|
200 try { |
|
201 service.createProperty(group, name, type); |
|
202 } catch (ScfException e2) { |
|
203 // Does property group not yet exist? |
|
204 if (e2.getError() == SmfErrorCode.NOT_FOUND) { |
|
205 service.createPropertyGroup(group, |
|
206 ScfConstants.SCF_APPLICATION_PG); |
|
207 } else { |
|
208 throw e2; |
|
209 } |
|
210 |
|
211 // Retry, now that property group is created |
|
212 service.createProperty(group, name, type); |
|
213 } |
|
214 |
|
215 // Retry, now that property is created |
|
216 service.setPropertyValues(group, name, array); |
|
217 } else { |
|
218 throw e; |
|
219 } |
|
220 } |
|
221 |
|
222 if (info.getService().isInstance()) |
|
223 service.refresh(); |
|
224 } |
|
225 |
|
226 @Override |
|
227 public void updateFromRepo(boolean force) throws ScfException { |
|
228 update(getRepoValue(), force); |
|
229 } |
|
230 |
|
231 // |
|
232 // BasicSmfMutableProperty methods |
|
233 // |
|
234 |
|
235 public abstract T getDefaultValue(); |
|
236 |
|
237 public T getFirstRepoValue() throws ScfException { |
|
238 List<T> values = getRepoValue(); |
|
239 if (values != null && values.size() != 0) { |
|
240 return values.get(0); |
|
241 } |
|
242 return null; |
|
243 } |
|
244 |
|
245 public T getFirstSavedValue() { |
|
246 List<T> values = getSavedValue(); |
|
247 if (values != null && values.size() != 0) { |
|
248 return values.get(0); |
|
249 } |
|
250 return null; |
|
251 } |
|
252 |
|
253 public T getFirstValue() { |
|
254 List<T> values = getValue(); |
|
255 if (values != null && values.size() != 0) { |
|
256 return values.get(0); |
|
257 } |
|
258 return null; |
|
259 } |
|
260 |
|
261 public DualConverter<String, T> getPropertyConverter() { |
|
262 return converter; |
|
263 } |
|
264 |
|
265 protected String getSmfPropertyName() throws ScfException { |
|
266 String name = info.getPropertyName(); |
|
267 if (name == null) { |
|
268 name = getPropertyName(); |
|
269 } |
|
270 return name; |
|
271 } |
|
272 |
|
273 public PropertyType getType() { |
|
274 return type; |
|
275 } |
|
276 |
|
277 /** |
|
278 * Determine whether property currently exists in SMF repository. |
|
279 */ |
|
280 public boolean getExistsInRepo() throws ScfException { |
|
281 try { |
|
282 getValuesFromRepository(); |
|
283 return true; |
|
284 } catch (ScfException e) { |
|
285 // Does property or property group not yet exist? |
|
286 if (e.getError() == SmfErrorCode.NOT_FOUND) |
|
287 return false; |
|
288 else |
|
289 throw e; |
|
290 } |
|
291 } |
|
292 |
|
293 /** |
|
294 * Retrieves the property values from the SMF repository. |
|
295 */ |
|
296 protected List<String> getValuesFromRepository() throws ScfException { |
|
297 List<String> values = info.getService().isInstance() ? |
|
298 info.getService().getSnapshotPropertyValues( |
|
299 ScfConstants.SCF_RUNNING, info.getPropertyGroupName(), |
|
300 getSmfPropertyName()) : |
|
301 info.getService().getPropertyValues(info.getPropertyGroupName(), |
|
302 getSmfPropertyName()); |
|
303 return values; |
|
304 } |
|
305 |
|
306 public void removeFromRepo() throws ScfException { |
|
307 ServiceMXBean service = info.getService(); |
|
308 String group = info.getPropertyGroupName(); |
|
309 String name = getSmfPropertyName(); |
|
310 |
|
311 service.deleteProperty(group, name); |
|
312 if (info.getService().isInstance()) |
|
313 service.refresh(); |
|
314 } |
|
315 |
|
316 public void setPropertyConverter(DualConverter<String, T> converter) { |
|
317 this.converter = converter; |
|
318 } |
|
319 |
|
320 public void setFirstRepoValue(T value) throws ScfException { |
|
321 List<T> list = toList(value); |
|
322 setRepoValue(list); |
|
323 } |
|
324 |
|
325 public void setFirstSavedValue(T value) { |
|
326 List<T> list = toList(value); |
|
327 setSavedValue(list); |
|
328 } |
|
329 |
|
330 public void setFirstValue(T value) { |
|
331 List<T> list = toList(value); |
|
332 setValue(list); |
|
333 } |
|
334 |
|
335 public void setType(PropertyType type) { |
|
336 this.type = type; |
|
337 } |
|
338 |
|
339 public void updateFirst(T value, boolean force) { |
|
340 List<T> list = toList(value); |
|
341 update(list, force); |
|
342 } |
|
343 |
|
344 public boolean isReadable() { |
|
345 return readable_; |
|
346 } |
|
347 |
|
348 // |
|
349 // Private methods |
|
350 // |
|
351 |
|
352 private List<T> toList(T value) { |
|
353 List<T> list = null; |
|
354 if (value != null) { |
|
355 list = new ArrayList<T>(1); |
|
356 list.add(value); |
|
357 } |
|
358 return list; |
|
359 } |
|
360 } |