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.panel.common; |
|
27 |
|
28 import java.beans.PropertyChangeEvent; |
|
29 import java.io.IOException; |
|
30 import java.util.logging.*; |
|
31 import com.oracle.solaris.rad.client.ADRName; |
|
32 import com.oracle.solaris.rad.client.RadException; |
|
33 import com.oracle.solaris.rad.connect.Connection; |
|
34 import com.oracle.solaris.vp.util.misc.ObjectUtil; |
|
35 import com.oracle.solaris.vp.util.misc.event.PropertyChangeListeners; |
|
36 |
|
37 /** |
|
38 * The {@code BeanTracker} is a {@link ConnectionTracker} that automatically |
|
39 * creates and re-creates an {@link #getBean Bean} after changes in the {@link |
|
40 * #setServerConnection ServerConnection} or {@link #setObjectName |
|
41 * ObjectName}. |
|
42 * </p> |
|
43 * A property change notification is sent out when the tracked {@code Bean} |
|
44 * changes. |
|
45 */ |
|
46 public class BeanTracker<T> extends ConnectionTracker { |
|
47 // |
|
48 // Static data |
|
49 // |
|
50 |
|
51 /** |
|
52 * The name of the property that changes with {@link #setBean}. |
|
53 */ |
|
54 public static final String PROPERTY_BEAN = "bean"; |
|
55 |
|
56 // |
|
57 // Instance data |
|
58 // |
|
59 |
|
60 private Class<T> clazz; |
|
61 private T bean; |
|
62 |
|
63 // |
|
64 // Constructors |
|
65 // |
|
66 |
|
67 public BeanTracker(ADRName aName, Class<T> clazz) |
|
68 { |
|
69 super(aName); |
|
70 try { |
|
71 setClass(clazz); |
|
72 |
|
73 // Impossible because ServerConnection not yet set |
|
74 } catch (TrackerException impossible) { |
|
75 } |
|
76 } |
|
77 |
|
78 public BeanTracker(ADRName aName, Class<T> clazz, |
|
79 ClientContext context) throws TrackerException { |
|
80 |
|
81 this(aName, clazz); |
|
82 setClientContext(context); |
|
83 } |
|
84 |
|
85 // |
|
86 // ConnectionTracker methods |
|
87 // |
|
88 |
|
89 @Override |
|
90 public void setServerConnection(Connection conn) |
|
91 throws TrackerException { |
|
92 |
|
93 if (getServerConnection() != conn) { |
|
94 super.setServerConnection(conn); |
|
95 setBean(); |
|
96 } |
|
97 } |
|
98 |
|
99 @Override |
|
100 public void setObjectName(ADRName aName) |
|
101 throws TrackerException { |
|
102 |
|
103 if (!ObjectUtil.equals(getObjectName(), aName)) { |
|
104 super.setObjectName(aName); |
|
105 setBean(); |
|
106 } |
|
107 } |
|
108 |
|
109 // |
|
110 // BeanTracker methods |
|
111 // |
|
112 |
|
113 protected T createBean() throws RadException, IOException { |
|
114 |
|
115 T bean = null; |
|
116 Connection conn = getServerConnection(); |
|
117 ADRName aname = getObjectName(); |
|
118 if (conn != null && aname != null && clazz != null) { |
|
119 bean = conn.getObject(aname); |
|
120 } |
|
121 |
|
122 return bean; |
|
123 } |
|
124 |
|
125 public T getBean() { |
|
126 return bean; |
|
127 } |
|
128 |
|
129 public Class<T> getBeanClass() { |
|
130 return clazz; |
|
131 } |
|
132 |
|
133 public void setBean(T bean) { |
|
134 if (this.bean != bean) { |
|
135 PropertyChangeEvent e = new PropertyChangeEvent( |
|
136 this, PROPERTY_BEAN, this.bean, bean); |
|
137 this.bean = bean; |
|
138 getPropertyChangeListeners().propertyChange(e); |
|
139 } |
|
140 } |
|
141 |
|
142 // |
|
143 // Private methods |
|
144 // |
|
145 |
|
146 private void setClass(Class<T> clazz) |
|
147 throws TrackerException { |
|
148 |
|
149 this.clazz = clazz; |
|
150 setBean(); |
|
151 } |
|
152 |
|
153 private void setBean() throws TrackerException { |
|
154 T bean = null; |
|
155 String message = null; |
|
156 Throwable cause = null; |
|
157 |
|
158 try { |
|
159 bean = createBean(); |
|
160 } catch (RadException e) { |
|
161 cause = e; |
|
162 message = "Error getting object information for: " |
|
163 + getObjectName(); |
|
164 } catch (IOException e) { |
|
165 cause = e; |
|
166 message = "Error contacting server while " |
|
167 + "creating proxy for: " + clazz.getSimpleName(); |
|
168 } |
|
169 |
|
170 setBean(bean); |
|
171 |
|
172 if (cause != null) { |
|
173 Logger.getLogger(getClass().getName()).log( |
|
174 Level.SEVERE, message, cause); |
|
175 throw new TrackerException(cause); |
|
176 } |
|
177 } |
|
178 } |
|