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, 2013, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.panel.swing.smf; |
|
27 |
|
28 import java.awt.*; |
|
29 import java.awt.event.*; |
|
30 import java.beans.*; |
|
31 import java.io.IOException; |
|
32 import java.util.*; |
|
33 import java.util.logging.*; |
|
34 import javax.swing.*; |
|
35 import com.oracle.solaris.scf.common.ScfException; |
|
36 import com.oracle.solaris.vp.panel.common.api.smf_old.*; |
|
37 import com.oracle.solaris.vp.panel.common.smf.*; |
|
38 import com.oracle.solaris.vp.util.misc.ArrayUtil; |
|
39 import com.oracle.solaris.vp.util.misc.finder.Finder; |
|
40 import com.oracle.solaris.vp.util.swing.*; |
|
41 import com.oracle.solaris.vp.util.swing.layout.*; |
|
42 |
|
43 @SuppressWarnings({"serial"}) |
|
44 public class SmfStatusPanel extends SettingsPanel { |
|
45 // |
|
46 // Inner classes |
|
47 // |
|
48 |
|
49 static class TaskComponent { |
|
50 private Component component_; |
|
51 private HyperlinkLabel label_; |
|
52 |
|
53 TaskComponent(Component c, HyperlinkLabel l) { |
|
54 component_ = c; |
|
55 label_ = l; |
|
56 } |
|
57 |
|
58 Component getComponent() { |
|
59 return component_; |
|
60 } |
|
61 |
|
62 HyperlinkLabel getLabel() { |
|
63 return label_; |
|
64 } |
|
65 } |
|
66 |
|
67 // |
|
68 // Instance data |
|
69 // |
|
70 |
|
71 private ServiceTracker tracker; |
|
72 |
|
73 private JLabel statusLabel; |
|
74 private JTextArea statusArea; |
|
75 private JTextArea recArea; |
|
76 private JLabel tasksLabel; |
|
77 private JPanel taskPanel; |
|
78 |
|
79 private TaskComponent clearMaint; |
|
80 private TaskComponent clearDegraded; |
|
81 private TaskComponent degrade; |
|
82 private TaskComponent disable; |
|
83 private TaskComponent enable; |
|
84 private TaskComponent maintain; |
|
85 private TaskComponent refresh; |
|
86 private TaskComponent restart; |
|
87 |
|
88 private JLabel nameLabel; |
|
89 private JTextArea fmriArea; |
|
90 |
|
91 private JLabel descLabel; |
|
92 private JTextArea descArea; |
|
93 |
|
94 private PropertyChangeListener serviceListener = |
|
95 new PropertyChangeListener() { |
|
96 @Override |
|
97 public void propertyChange(PropertyChangeEvent event) { |
|
98 serviceChanged(); |
|
99 } |
|
100 }; |
|
101 |
|
102 // |
|
103 // Constructors |
|
104 // |
|
105 |
|
106 public SmfStatusPanel() { |
|
107 JPanel panel = createForm(); |
|
108 setContent(panel, false, false); |
|
109 } |
|
110 |
|
111 // |
|
112 // SmfStatusPanel methods |
|
113 // |
|
114 |
|
115 protected JPanel createForm() { |
|
116 nameLabel = createSectionLabel(""); |
|
117 fmriArea = createTextArea(); |
|
118 |
|
119 descLabel = createSectionLabel( |
|
120 Finder.getString("service.status.label.description")); |
|
121 descArea = createTextArea(); |
|
122 |
|
123 statusLabel = createSectionLabel( |
|
124 Finder.getString("service.status.label.status")); |
|
125 statusArea = createTextArea(); |
|
126 recArea = createTextArea(); |
|
127 |
|
128 tasksLabel = createSectionLabel( |
|
129 Finder.getString("service.status.label.tasks")); |
|
130 |
|
131 clearMaint = createClearMaintTaskComponent(); |
|
132 clearDegraded = createClearDegradedTaskComponent(); |
|
133 degrade = createDegradeTaskComponent(); |
|
134 disable = createDisableTaskComponent(); |
|
135 enable = createEnableTaskComponent(); |
|
136 maintain = createMaintainTaskComponent(); |
|
137 refresh = createRefreshTaskComponent(); |
|
138 restart = createRestartTaskComponent(); |
|
139 |
|
140 taskPanel = new JPanel( |
|
141 new UniformLayout(UniformLayout.Orientation.VERTICAL)); |
|
142 taskPanel.setOpaque(false); |
|
143 taskPanel.add(clearMaint.getComponent()); |
|
144 taskPanel.add(clearDegraded.getComponent()); |
|
145 taskPanel.add(enable.getComponent()); |
|
146 taskPanel.add(disable.getComponent()); |
|
147 taskPanel.add(refresh.getComponent()); |
|
148 taskPanel.add(restart.getComponent()); |
|
149 taskPanel.add(maintain.getComponent()); |
|
150 taskPanel.add(degrade.getComponent()); |
|
151 |
|
152 JPanel panel = new JPanel(); |
|
153 panel.setOpaque(false); |
|
154 panel.setLayout(new ColumnLayout()); |
|
155 ColumnLayoutConstraint c = new ColumnLayoutConstraint( |
|
156 HorizontalAnchor.FILL, GUIUtil.getGap()); |
|
157 panel.add(nameLabel, c); |
|
158 panel.add(fmriArea, c); |
|
159 panel.add(descLabel, c); |
|
160 panel.add(descArea, c); |
|
161 panel.add(statusLabel, c); |
|
162 panel.add(statusArea, c); |
|
163 panel.add(recArea, c); |
|
164 panel.add(tasksLabel, c); |
|
165 panel.add(taskPanel, c); |
|
166 |
|
167 return panel; |
|
168 } |
|
169 |
|
170 public HyperlinkLabel getClearDegradedLink() { |
|
171 return clearDegraded.getLabel(); |
|
172 } |
|
173 |
|
174 public HyperlinkLabel getClearMaintLink() { |
|
175 return clearMaint.getLabel(); |
|
176 } |
|
177 |
|
178 public HyperlinkLabel getDegradeLink() { |
|
179 return degrade.getLabel(); |
|
180 } |
|
181 |
|
182 public HyperlinkLabel getDisableLink() { |
|
183 return disable.getLabel(); |
|
184 } |
|
185 |
|
186 public HyperlinkLabel getEnableLink() { |
|
187 return enable.getLabel(); |
|
188 } |
|
189 |
|
190 public HyperlinkLabel getMaintainLink() { |
|
191 return maintain.getLabel(); |
|
192 } |
|
193 |
|
194 public HyperlinkLabel getRefreshLink() { |
|
195 return refresh.getLabel(); |
|
196 } |
|
197 |
|
198 public HyperlinkLabel getRestartLink() { |
|
199 return restart.getLabel(); |
|
200 } |
|
201 |
|
202 public void init(ServiceTracker tracker) |
|
203 throws IOException { |
|
204 |
|
205 if (this.tracker != tracker) { |
|
206 if (this.tracker != null) { |
|
207 this.tracker.removePropertyChangeListener( |
|
208 ServiceTracker.PROPERTY_SERVICE, serviceListener); |
|
209 } |
|
210 |
|
211 this.tracker = tracker; |
|
212 |
|
213 if (tracker != null) { |
|
214 tracker.addPropertyChangeListener( |
|
215 ServiceTracker.PROPERTY_SERVICE, serviceListener); |
|
216 } |
|
217 |
|
218 serviceChanged(); |
|
219 } |
|
220 } |
|
221 |
|
222 // |
|
223 // Private methods |
|
224 // |
|
225 |
|
226 private TaskComponent createTaskComponent(String taskRes, String tipRes, |
|
227 final Runnable task) { |
|
228 |
|
229 HyperlinkLabel link = new HyperlinkLabel(Finder.getString(taskRes)); |
|
230 link.addActionListener( |
|
231 new ActionListener() { |
|
232 @Override |
|
233 public void actionPerformed(ActionEvent e) { |
|
234 task.run(); |
|
235 } |
|
236 }); |
|
237 |
|
238 if (tipRes == null) { |
|
239 return new TaskComponent(link, link); |
|
240 } |
|
241 |
|
242 String tip = Finder.getString(tipRes); |
|
243 if (tip == null) { |
|
244 return new TaskComponent(link, link); |
|
245 } |
|
246 |
|
247 InfoLabel info = new InfoLabel(tip); |
|
248 JPanel panel = new JPanel(new RowLayout()); |
|
249 RowLayoutConstraint r = new RowLayoutConstraint( |
|
250 VerticalAnchor.CENTER, GUIUtil.getHalfGap(), 1f); |
|
251 panel.setOpaque(false); |
|
252 panel.add(link, r); |
|
253 panel.add(info, r.setWeight(0f)); |
|
254 |
|
255 return new TaskComponent(panel, link); |
|
256 } |
|
257 |
|
258 abstract class SCFRunnable implements Runnable { |
|
259 public void run() { |
|
260 try { |
|
261 runScf(); |
|
262 } catch (ScfException t) { |
|
263 if (t.getError() == SmfErrorCode.PERMISSION_DENIED) { |
|
264 GUIUtil.showError(SmfStatusPanel.this, null, |
|
265 Finder.getString("error.scf.permission")); |
|
266 } else { |
|
267 Logger.getLogger(getClass().getPackage().getName()).log( |
|
268 Level.SEVERE, Finder.getString("error.scf.general"), t); |
|
269 } |
|
270 } |
|
271 } |
|
272 |
|
273 abstract protected void runScf() throws ScfException; |
|
274 } |
|
275 |
|
276 private TaskComponent createClearMaintTaskComponent() { |
|
277 return createTaskComponent( |
|
278 "service.task.long.clear.maint", |
|
279 "service.task.tip.clear.maint", |
|
280 new SCFRunnable() { |
|
281 @Override |
|
282 protected void runScf() throws ScfException { |
|
283 tracker.getService().clear(); |
|
284 } |
|
285 }); |
|
286 } |
|
287 |
|
288 private TaskComponent createClearDegradedTaskComponent() { |
|
289 return createTaskComponent( |
|
290 "service.task.long.clear.degraded", |
|
291 "service.task.tip.clear.degraded", |
|
292 new SCFRunnable() { |
|
293 @Override |
|
294 protected void runScf() throws ScfException { |
|
295 tracker.getService().clear(); |
|
296 } |
|
297 }); |
|
298 } |
|
299 |
|
300 private TaskComponent createDegradeTaskComponent() { |
|
301 return createTaskComponent( |
|
302 "service.task.long.degrade", |
|
303 "service.task.tip.degrade", |
|
304 new SCFRunnable() { |
|
305 @Override |
|
306 protected void runScf() throws ScfException { |
|
307 tracker.getService().degrade(true); |
|
308 } |
|
309 }); |
|
310 } |
|
311 |
|
312 private TaskComponent createDisableTaskComponent() { |
|
313 return createTaskComponent( |
|
314 "service.task.long.disable", |
|
315 "service.task.tip.disable", |
|
316 new SCFRunnable() { |
|
317 @Override |
|
318 protected void runScf() throws ScfException { |
|
319 tracker.getService().setPersistentlyEnabled(false); |
|
320 } |
|
321 }); |
|
322 } |
|
323 |
|
324 private TaskComponent createEnableTaskComponent() { |
|
325 return createTaskComponent( |
|
326 "service.task.long.enable", |
|
327 "service.task.tip.enable", |
|
328 new SCFRunnable() { |
|
329 @Override |
|
330 protected void runScf() throws ScfException { |
|
331 tracker.getService().setPersistentlyEnabled(true); |
|
332 } |
|
333 }); |
|
334 } |
|
335 |
|
336 private TaskComponent createMaintainTaskComponent() { |
|
337 return createTaskComponent( |
|
338 "service.task.long.maintain", |
|
339 "service.task.tip.maintain", |
|
340 new SCFRunnable() { |
|
341 @Override |
|
342 protected void runScf() throws ScfException { |
|
343 tracker.getService().maintain(true); |
|
344 } |
|
345 }); |
|
346 } |
|
347 |
|
348 private TaskComponent createRefreshTaskComponent() { |
|
349 return createTaskComponent( |
|
350 "service.task.long.refresh", |
|
351 "service.task.tip.refresh", |
|
352 new SCFRunnable() { |
|
353 @Override |
|
354 protected void runScf() throws ScfException { |
|
355 tracker.getService().refresh(); |
|
356 } |
|
357 }); |
|
358 } |
|
359 |
|
360 private TaskComponent createRestartTaskComponent() { |
|
361 return createTaskComponent( |
|
362 "service.task.long.restart", |
|
363 "service.task.tip.restart", |
|
364 new SCFRunnable() { |
|
365 @Override |
|
366 protected void runScf() throws ScfException { |
|
367 tracker.getService().restart(); |
|
368 } |
|
369 }); |
|
370 } |
|
371 |
|
372 private JLabel createSectionLabel(String text) { |
|
373 JLabel label = new JLabel(text); |
|
374 decorateTitle(label); |
|
375 |
|
376 return label; |
|
377 } |
|
378 |
|
379 private JTextArea createTextArea() { |
|
380 return new FlowTextArea(); |
|
381 } |
|
382 |
|
383 private void serviceChanged() { |
|
384 ServiceBean service = tracker == null ? null : tracker.getService(); |
|
385 |
|
386 Logger log = Logger.getLogger(getClass().getPackage().getName()); |
|
387 |
|
388 boolean isInst = false; |
|
389 if (service != null) { |
|
390 try { |
|
391 isInst = service.isInstance(); |
|
392 } catch (ScfException e) { |
|
393 log.log(Level.SEVERE, Finder.getString("error.scf.general"), e); |
|
394 } |
|
395 } |
|
396 |
|
397 SmfState state = null; |
|
398 SmfState nextState = null; |
|
399 String auxState = null; |
|
400 Date sTime = null; |
|
401 |
|
402 if (isInst) { |
|
403 try { |
|
404 state = service.getState(); |
|
405 nextState = service.getNextState(); |
|
406 auxState = service.getAuxiliaryState(); |
|
407 sTime = service.getSTime(); |
|
408 } catch (ScfException t) { |
|
409 log.log(Level.SEVERE, Finder.getString("error.scf.general"), t); |
|
410 } |
|
411 } |
|
412 |
|
413 // Can handle null state |
|
414 setServiceState(state, nextState, auxState, sTime); |
|
415 statusLabel.setVisible(isInst); |
|
416 statusArea.setVisible(isInst); |
|
417 |
|
418 String name = null; |
|
419 String fmri = null; |
|
420 String description = null; |
|
421 |
|
422 if (service != null) { |
|
423 try { |
|
424 String value = service.getCommonName( |
|
425 Locale.getDefault().getLanguage()); |
|
426 String resource = isInst ? |
|
427 "service.status.label.name.instance" : |
|
428 "service.status.label.name.service"; |
|
429 if (value == null) { |
|
430 resource += ".null"; |
|
431 } |
|
432 name = Finder.getString(resource, value); |
|
433 fmri = service.getFmri(); |
|
434 |
|
435 description = service.getDescription( |
|
436 Locale.getDefault().getLanguage()); |
|
437 } catch (ScfException e) { |
|
438 log.log(Level.SEVERE, Finder.getString("error.scf.general"), e); |
|
439 } |
|
440 } |
|
441 |
|
442 nameLabel.setText(name); |
|
443 fmriArea.setText(fmri); |
|
444 |
|
445 if (description == null) { |
|
446 description = Finder.getString( |
|
447 "service.status.label.description.unknown"); |
|
448 } |
|
449 description = description.trim().replaceAll("\\s+", " "); |
|
450 descArea.setText(description); |
|
451 } |
|
452 |
|
453 private void setServiceState(SmfState state, SmfState nextState, |
|
454 String auxState, Date sTime) { |
|
455 |
|
456 // Can handle null state |
|
457 statusArea.setText(ServiceUtil.getStateDetails( |
|
458 state, nextState, auxState, sTime)); |
|
459 |
|
460 // Can handle null state |
|
461 String rec = ServiceUtil.getRecommendation(state); |
|
462 if (rec == null) { |
|
463 recArea.setVisible(false); |
|
464 } else { |
|
465 recArea.setVisible(true); |
|
466 recArea.setText(rec); |
|
467 } |
|
468 |
|
469 TaskComponent[] tasks = {}; |
|
470 |
|
471 // No tasks if in transition |
|
472 if (state != null && |
|
473 (nextState == null || nextState == SmfState.NONE)) { |
|
474 |
|
475 switch (state) { |
|
476 case MAINT: |
|
477 tasks = new TaskComponent[] {clearMaint, disable}; |
|
478 break; |
|
479 |
|
480 case OFFLINE: |
|
481 tasks = new TaskComponent[] { |
|
482 disable, restart, maintain, degrade}; |
|
483 break; |
|
484 |
|
485 case DISABLED: |
|
486 tasks = new TaskComponent[] {enable}; |
|
487 break; |
|
488 |
|
489 case ONLINE: |
|
490 tasks = new TaskComponent[] { |
|
491 disable, refresh, restart, maintain, degrade}; |
|
492 break; |
|
493 |
|
494 case DEGRADED: |
|
495 tasks = new TaskComponent[] {clearDegraded, disable}; |
|
496 break; |
|
497 } |
|
498 } |
|
499 |
|
500 setTasks(tasks); |
|
501 } |
|
502 |
|
503 private void setTasks(TaskComponent... taskcomps) { |
|
504 Component[] comps = new Component[taskcomps.length]; |
|
505 for (int i = 0; i < taskcomps.length; i++) |
|
506 comps[i] = taskcomps[i].getComponent(); |
|
507 |
|
508 for (Component c : taskPanel.getComponents()) { |
|
509 c.setVisible(ArrayUtil.contains(comps, c)); |
|
510 } |
|
511 |
|
512 tasksLabel.setVisible(comps.length != 0); |
|
513 } |
|
514 } |
|