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) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.panels.apache.client.swing; |
|
27 |
|
28 import java.util.*; |
|
29 import javax.swing.Icon; |
|
30 import com.oracle.solaris.scf.common.ScfException; |
|
31 import com.oracle.solaris.vp.panel.common.model.*; |
|
32 import com.oracle.solaris.vp.panel.common.smf.*; |
|
33 import com.oracle.solaris.vp.util.misc.*; |
|
34 import com.oracle.solaris.vp.util.misc.finder.Finder; |
|
35 import com.oracle.solaris.vp.util.misc.property.*; |
|
36 import com.oracle.solaris.vp.util.swing.HasIcon; |
|
37 |
|
38 @SuppressWarnings({"serial"}) |
|
39 public class Modules extends AbstractManagedObject<Module> |
|
40 implements HasIcon { |
|
41 |
|
42 // |
|
43 // Static data |
|
44 // |
|
45 |
|
46 public static final String ID = "modules"; |
|
47 public static final String NAME = Finder.getString("modules.title"); |
|
48 public static final String PROPERTY_ADDED = "added Modules"; |
|
49 public static final String PROPERTY_REMOVED = "removed Modules"; |
|
50 |
|
51 // |
|
52 // Instance data |
|
53 // |
|
54 |
|
55 private ApacheInfo info; |
|
56 private SimpleHasId tmpHasId = new SimpleHasId(); |
|
57 |
|
58 private BasicMutableProperty<Integer> addedProperty = |
|
59 new IntegerProperty(PROPERTY_ADDED); |
|
60 |
|
61 private BasicMutableProperty<Integer> removedProperty = |
|
62 new IntegerProperty(PROPERTY_REMOVED); |
|
63 |
|
64 private List<Module> removed = new ArrayList<Module>(); |
|
65 |
|
66 // |
|
67 // Constructors |
|
68 // |
|
69 |
|
70 public Modules(ApacheInfo info) { |
|
71 super(ID); |
|
72 this.info = info; |
|
73 |
|
74 addedProperty.update(0, true); |
|
75 addProperties(addedProperty); |
|
76 |
|
77 removedProperty.update(0, true); |
|
78 addProperties(removedProperty); |
|
79 |
|
80 setComparator(SimpleHasId.COMPARATOR); |
|
81 } |
|
82 |
|
83 // |
|
84 // HasIcon methods |
|
85 // |
|
86 |
|
87 @Override |
|
88 public Icon getIcon(int height) { |
|
89 return IconUtil.getClosestIcon(Module.icons, height); |
|
90 } |
|
91 |
|
92 // |
|
93 // ManagedObject methods |
|
94 // |
|
95 |
|
96 @Override |
|
97 public String getName() { |
|
98 return NAME; |
|
99 } |
|
100 |
|
101 // |
|
102 // AbstractManagedObject methods |
|
103 // |
|
104 |
|
105 @Override |
|
106 public void addChildren(Module... toAdd) { |
|
107 super.addChildren(toAdd); |
|
108 |
|
109 ChangeableAggregator aggregator = getChangeableAggregator(); |
|
110 for (Module module : toAdd) { |
|
111 if (!module.isInRepo()) { |
|
112 addedProperty.setValue(addedProperty.getValue() + 1); |
|
113 } |
|
114 aggregator.addChangeables(module.getChangeableAggregator()); |
|
115 } |
|
116 } |
|
117 |
|
118 @Override |
|
119 public void removeChildren(Module... toRemove) { |
|
120 super.removeChildren(toRemove); |
|
121 |
|
122 ChangeableAggregator aggregator = getChangeableAggregator(); |
|
123 for (Module module : toRemove) { |
|
124 if (!module.isInRepo()) { |
|
125 addedProperty.setValue(addedProperty.getValue() - 1); |
|
126 } |
|
127 aggregator.removeChangeable(module.getChangeableAggregator()); |
|
128 } |
|
129 } |
|
130 |
|
131 // |
|
132 // Modules methods |
|
133 // |
|
134 |
|
135 public ApacheInfo getApacheInfo() { |
|
136 return info; |
|
137 } |
|
138 |
|
139 public Module getModule(String id) { |
|
140 synchronized (children) { |
|
141 tmpHasId.setId(id); |
|
142 int index = Collections.binarySearch( |
|
143 getChildren(), tmpHasId, SimpleHasId.COMPARATOR); |
|
144 |
|
145 if (index >= 0) { |
|
146 return children.get(index); |
|
147 } |
|
148 |
|
149 return null; |
|
150 } |
|
151 } |
|
152 |
|
153 public void refresh(boolean force) throws ScfException { |
|
154 ServiceBean service = info.getPanelDescriptor().getService(); |
|
155 String group = info.getPropertyGroupName(); |
|
156 |
|
157 // Build list of names of Module properties |
|
158 List<String> properties = new ArrayList<String>(); |
|
159 for (String property : service.getPropertyNames(group)) { |
|
160 if (property.startsWith(Module.SMF_PROPERTY_PREFIX)) { |
|
161 properties.add(property); |
|
162 } |
|
163 } |
|
164 Collections.sort(properties); |
|
165 |
|
166 // Remove names of properties that are pending removal on client |
|
167 for (Iterator<Module> i = removed.iterator(); i.hasNext(); ) { |
|
168 Module module = i.next(); |
|
169 String property = module.getPropertyName(); |
|
170 if (properties.remove(property)) { |
|
171 // Pending removal on client, still exists on server |
|
172 } else { |
|
173 // Pending removal on client, but already removed on server |
|
174 i.remove(); |
|
175 int n = removed.size(); |
|
176 assert n == removedProperty.getValue() - 1; |
|
177 removedProperty.setValue(n); |
|
178 } |
|
179 } |
|
180 |
|
181 // Remove names of properties already created on the client |
|
182 List<Module> children = new ArrayList<Module>(getChildren()); |
|
183 for (Module module : children) { |
|
184 if (module.isInRepo()) { |
|
185 String property = module.getPropertyName(); |
|
186 if (properties.remove(property)) { |
|
187 // Exists on server, already exists on client |
|
188 module.refresh(force); |
|
189 } else { |
|
190 // Deleted on server, now must be deleted on client |
|
191 removeChildren(module); |
|
192 } |
|
193 } |
|
194 } |
|
195 |
|
196 // The list now contains only properties that have been created on the |
|
197 // server but don't yet exist on the client |
|
198 for (String property : properties) { |
|
199 Module module = new Module(this, property); |
|
200 addChildren(module); |
|
201 } |
|
202 } |
|
203 |
|
204 public void scheduleRemove(Module... modules) { |
|
205 for (Module module : modules) { |
|
206 if (getChildren().contains(module)) { |
|
207 removeChildren(module); |
|
208 |
|
209 if (module.isInRepo()) { |
|
210 removed.add(module); |
|
211 int n = removed.size(); |
|
212 assert n == removedProperty.getValue() + 1; |
|
213 removedProperty.setValue(n); |
|
214 } |
|
215 } |
|
216 } |
|
217 } |
|
218 |
|
219 public void saveToRepo() throws ScfException { |
|
220 ApacheUtil.saveToRepo(info.getService(), |
|
221 new ScfRunnable() { |
|
222 @Override |
|
223 public void run() throws ScfException { |
|
224 for (Module module : getChildren()) { |
|
225 module.saveToRepo(); |
|
226 } |
|
227 addedProperty.setValue(0); |
|
228 |
|
229 for (Iterator<Module> i = removed.iterator(); i.hasNext(); ) |
|
230 { |
|
231 Module module = i.next(); |
|
232 module.removeFromRepo(); |
|
233 i.remove(); |
|
234 int n = removed.size(); |
|
235 assert n == removedProperty.getValue() - 1; |
|
236 removedProperty.setValue(n); |
|
237 } |
|
238 |
|
239 for (MutableProperty<?> property : getProperties()) { |
|
240 if (property.isChanged()) { |
|
241 if (property instanceof SmfMutableProperty) { |
|
242 ((SmfMutableProperty)property).saveToRepo(); |
|
243 } else { |
|
244 property.save(); |
|
245 } |
|
246 } |
|
247 } |
|
248 } |
|
249 }); |
|
250 } |
|
251 } |
|