|
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, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.panel.common; |
|
27 |
|
28 import java.net.*; |
|
29 import javax.management.remote.JMXConnector; |
|
30 import com.oracle.solaris.vp.util.misc.*; |
|
31 |
|
32 /** |
|
33 * The {@code ConnectionInfo} class encapsulate an open {@code JMXConnector} and |
|
34 * some of its attributes. |
|
35 */ |
|
36 public class ConnectionInfo implements LoginInfo { |
|
37 // |
|
38 // Instance data |
|
39 // |
|
40 |
|
41 private String host; |
|
42 private String user; |
|
43 private String role; |
|
44 private String zone; |
|
45 private String zoneUser; |
|
46 private String zoneRole; |
|
47 private JMXConnector connector; |
|
48 private InetAddress[] addrs; |
|
49 private Boolean isLocalAddress; |
|
50 |
|
51 // |
|
52 // Constructors |
|
53 // |
|
54 |
|
55 public ConnectionInfo(String host, String user, String role, |
|
56 String zone, String zoneUser, String zoneRole, JMXConnector connector) { |
|
57 |
|
58 this.host = host; |
|
59 this.user = user; |
|
60 this.role = role; |
|
61 this.zone = zone; |
|
62 this.zoneUser = zoneUser; |
|
63 this.zoneRole = zoneRole; |
|
64 this.connector = connector; |
|
65 } |
|
66 |
|
67 public ConnectionInfo(String host, String user, String role, |
|
68 JMXConnector connector) { |
|
69 |
|
70 this(host, user, role, null, null, null, connector); |
|
71 } |
|
72 |
|
73 // |
|
74 // LoginInfo methods |
|
75 // |
|
76 |
|
77 @Override |
|
78 public String getHost() { |
|
79 return host; |
|
80 } |
|
81 |
|
82 @Override |
|
83 public String getRole() { |
|
84 return role; |
|
85 } |
|
86 |
|
87 @Override |
|
88 public String getUser() { |
|
89 return user; |
|
90 } |
|
91 |
|
92 @Override |
|
93 public String getZone() { |
|
94 return zone; |
|
95 } |
|
96 |
|
97 @Override |
|
98 public String getZoneUser() { |
|
99 return zoneUser; |
|
100 } |
|
101 |
|
102 @Override |
|
103 public String getZoneRole() { |
|
104 return zoneRole; |
|
105 } |
|
106 |
|
107 @Override |
|
108 public boolean matches(String host, String user, String role, String zone, |
|
109 String zoneUser, String zoneRole) { |
|
110 |
|
111 // Defer matchesHost to last |
|
112 return matchesUser(user) && matchesRole(role) && matchesZone(zone) && |
|
113 (zone == null || matchesZoneUser(zoneUser)) && |
|
114 (zone == null || matchesZoneRole(zoneRole)) && |
|
115 matchesHost(host); |
|
116 } |
|
117 |
|
118 @Override |
|
119 public boolean matches(LoginInfo info) { |
|
120 return matches(info.getHost(), info.getUser(), info.getRole(), |
|
121 info.getZone(), info.getZoneUser(), info.getZoneRole()); |
|
122 } |
|
123 |
|
124 // |
|
125 // Object methods |
|
126 // |
|
127 |
|
128 @Override |
|
129 public void finalize() throws Throwable { |
|
130 IOUtil.closeIgnore(connector); |
|
131 super.finalize(); |
|
132 } |
|
133 |
|
134 @Override |
|
135 public String toString() { |
|
136 StringBuilder buffer = new StringBuilder(); |
|
137 if (zone != null) { |
|
138 buffer.append(toString(zone, zoneUser, zoneRole)).append(", via "); |
|
139 } |
|
140 buffer.append(toString(host, user, role)); |
|
141 return buffer.toString(); |
|
142 } |
|
143 |
|
144 // |
|
145 // ConnectionInfo methods |
|
146 // |
|
147 |
|
148 public JMXConnector getConnector() { |
|
149 return connector; |
|
150 } |
|
151 |
|
152 public synchronized InetAddress[] getInetAddresses() { |
|
153 if (addrs == null) { |
|
154 try { |
|
155 addrs = NetUtil.getAllByName(getHost()); |
|
156 } catch (UnknownHostException e) { |
|
157 addrs = new InetAddress[0]; |
|
158 } |
|
159 } |
|
160 return addrs; |
|
161 } |
|
162 |
|
163 public boolean matchesHost(String host) { |
|
164 // Avoid IP resolution if possible |
|
165 if (ObjectUtil.equals(host, getHost())) { |
|
166 return true; |
|
167 } |
|
168 |
|
169 // Test for localhost |
|
170 if (isLocalAddress == null) { |
|
171 isLocalAddress = NetUtil.isLocalAddress(getHost()); |
|
172 } |
|
173 if (isLocalAddress && NetUtil.isLocalAddress(host)) { |
|
174 return true; |
|
175 } |
|
176 |
|
177 try { |
|
178 InetAddress[] addrs = getInetAddresses(); |
|
179 InetAddress[] addrs2 = NetUtil.getAllByName(host); |
|
180 |
|
181 for (InetAddress addr : addrs) { |
|
182 for (InetAddress addr2 : addrs2) { |
|
183 if (addr.equals(addr2)) { |
|
184 return true; |
|
185 } |
|
186 } |
|
187 } |
|
188 } catch (UnknownHostException stop) { |
|
189 } |
|
190 return false; |
|
191 } |
|
192 |
|
193 public boolean matchesRole(String role) { |
|
194 return ObjectUtil.equals(role, getRole()); |
|
195 } |
|
196 |
|
197 public boolean matchesUser(String user) { |
|
198 return ObjectUtil.equals(user, getUser()); |
|
199 } |
|
200 |
|
201 public boolean matchesZone(String zone) { |
|
202 return ObjectUtil.equals(zone, getZone()); |
|
203 } |
|
204 |
|
205 public boolean matchesZoneRole(String zoneRole) { |
|
206 return ObjectUtil.equals(zoneRole, getZoneRole()); |
|
207 } |
|
208 |
|
209 public boolean matchesZoneUser(String zoneUser) { |
|
210 return ObjectUtil.equals(zoneUser, getZoneUser()); |
|
211 } |
|
212 |
|
213 // |
|
214 // Static methods |
|
215 // |
|
216 |
|
217 private static String toString(String host, String user, String role) { |
|
218 String fmt = role == null ? |
|
219 "%2$s@%1$s" : "%3$s@%1$s (%2$s)"; |
|
220 return String.format(fmt, host, user, role); |
|
221 } |
|
222 } |