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.scf.common; |
|
27 |
|
28 import java.net.URI; |
|
29 import java.net.URISyntaxException; |
|
30 import java.beans.ConstructorProperties; |
|
31 import java.util.Map; |
|
32 import java.util.EnumMap; |
|
33 |
|
34 /** |
|
35 * A basic FMRI class. Probably should be named "SmfFMRI", as |
|
36 * legacy-run services aren't an SCF concept. |
|
37 */ |
|
38 public class FMRI implements Comparable<FMRI> |
|
39 { |
|
40 /* |
|
41 * Static data |
|
42 */ |
|
43 public static final String SCF_SCOPE_LOCAL = "localhost"; |
|
44 private static final String FORMAT = |
|
45 "([A-Za-z][_A-Za-z0-9.-]*,)?[_A-Za-z0-9-]*"; |
|
46 private static final Map<FMRIScheme, String> scheme_strings = |
|
47 new EnumMap<FMRIScheme, String>(FMRIScheme.class); |
|
48 |
|
49 static { |
|
50 scheme_strings.put(FMRIScheme.FILE, "file"); |
|
51 scheme_strings.put(FMRIScheme.LEGACY, "lrc"); |
|
52 scheme_strings.put(FMRIScheme.SERVICE, "svc"); |
|
53 } |
|
54 |
|
55 /* |
|
56 * FMRI components |
|
57 */ |
|
58 private FMRIScheme scheme_; |
|
59 private String scope_ = null; |
|
60 private String service_ = null; |
|
61 private String instance_ = null; |
|
62 private String pg_ = null; |
|
63 private String property_ = null; |
|
64 private String name_ = null; |
|
65 |
|
66 /* |
|
67 * Derivative information |
|
68 */ |
|
69 private FMRIType svctype_; |
|
70 private String canonicalStr_; |
|
71 |
|
72 /** |
|
73 * Creates an FMRI from a URI. |
|
74 */ |
|
75 public FMRI(URI uri) throws URISyntaxException |
|
76 { |
|
77 if (!uri.isAbsolute() || uri.isOpaque()) |
|
78 throw (new URISyntaxException(uri.toString(), |
|
79 "Bad FMRI")); |
|
80 |
|
81 String scheme = uri.getScheme(); |
|
82 scope_ = uri.getAuthority(); |
|
83 if (scope_ == null || scope_.isEmpty() || |
|
84 scope_.equals(SCF_SCOPE_LOCAL)) |
|
85 scope_ = null; |
|
86 canonicalStr_ = new URI(uri.getScheme(), scope_, uri.getPath(), |
|
87 null, null).toString(); |
|
88 |
|
89 if (scheme.equals("svc")) { |
|
90 scheme_ = FMRIScheme.SERVICE; |
|
91 svctype_ = FMRIType.SCOPE; |
|
92 |
|
93 String[] top = uri.getPath().split("/:properties/", 2); |
|
94 |
|
95 String[] next = top[0].split(":", 2); |
|
96 /* |
|
97 * We can assume the path starts with a slash |
|
98 * because we reject relative URIs above. |
|
99 */ |
|
100 service_ = next[0].substring(1); |
|
101 svctype_ = FMRIType.SERVICE; |
|
102 if (next.length == 2) { |
|
103 instance_ = next[1]; |
|
104 if (!instance_.matches(FORMAT)) |
|
105 throw (new URISyntaxException( |
|
106 uri.toString(), |
|
107 "Invalid instance: " + instance_)); |
|
108 svctype_ = FMRIType.INSTANCE; |
|
109 } |
|
110 |
|
111 if (top.length == 2) { |
|
112 next = top[1].split("/", 2); |
|
113 pg_ = next[0]; |
|
114 if (!pg_.matches(FORMAT)) |
|
115 throw (new URISyntaxException( |
|
116 uri.toString(), |
|
117 "Invalid property group: " + pg_)); |
|
118 svctype_ = FMRIType.PGROUP; |
|
119 if (next.length == 2) { |
|
120 property_ = next[1]; |
|
121 if (!property_.matches(FORMAT)) |
|
122 throw (new URISyntaxException( |
|
123 uri.toString(), |
|
124 "Invalid property: " + |
|
125 property_)); |
|
126 svctype_ = FMRIType.PROPERTY; |
|
127 } |
|
128 } |
|
129 } else if (scheme.equals("file")) { |
|
130 scheme_ = FMRIScheme.FILE; |
|
131 name_ = uri.getPath(); |
|
132 } else if (scheme.equals("lrc")) { |
|
133 scheme_ = FMRIScheme.LEGACY; |
|
134 name_ = uri.getPath(); |
|
135 } else { |
|
136 throw (new URISyntaxException(uri.toString(), |
|
137 "Invalid FMRI scheme: " + scheme)); |
|
138 } |
|
139 } |
|
140 |
|
141 /** |
|
142 * Creates an FMRI from a string. |
|
143 */ |
|
144 public FMRI(String fmri) throws URISyntaxException |
|
145 { |
|
146 /* Work around bug 6504439 in scf_scope_to_fmri */ |
|
147 this(new URI(fmri.equals("svc:") ? "svc:///" : fmri)); |
|
148 } |
|
149 |
|
150 /** |
|
151 * Constucts an FMRI from its attributes. Needed for the class to be |
|
152 * reconstructable. |
|
153 */ |
|
154 @SuppressWarnings({"fallthrough"}) |
|
155 @ConstructorProperties({"scheme", "svcType", "scope", "name", "service", |
|
156 "instance", "propertyGroup", "property"}) |
|
157 public FMRI(FMRIScheme scheme, FMRIType type, String scope, String name, |
|
158 String service, String instance, String pg, String property) |
|
159 { |
|
160 scheme_ = scheme; |
|
161 |
|
162 scope_ = scope; |
|
163 if (scope_ == null || scope_.isEmpty() || |
|
164 scope_.equals(SCF_SCOPE_LOCAL)) |
|
165 scope_ = null; |
|
166 |
|
167 String path; |
|
168 if (scheme != FMRIScheme.SERVICE) { |
|
169 svctype_ = FMRIType.NONE; |
|
170 path = name_ = name; |
|
171 } else { |
|
172 svctype_ = type; |
|
173 name_ = null; |
|
174 |
|
175 path = ""; |
|
176 switch (svctype_) { |
|
177 case PROPERTY: |
|
178 property_ = property; |
|
179 path = "/" + property + path; |
|
180 case PGROUP: |
|
181 pg_ = pg; |
|
182 path = "/:properties/" + pg + path; |
|
183 case INSTANCE: |
|
184 instance_ = instance; |
|
185 path = ":" + instance + path; |
|
186 case SERVICE: |
|
187 service_ = service; |
|
188 path = "/" + service + path; |
|
189 } |
|
190 } |
|
191 |
|
192 try { |
|
193 URI uri = new URI(scheme_strings.get(scheme), scope_, |
|
194 path, null, null); |
|
195 canonicalStr_ = uri.toString(); |
|
196 } catch (URISyntaxException ex) { |
|
197 canonicalStr_ = null; |
|
198 } |
|
199 } |
|
200 |
|
201 /** |
|
202 * Returns the FMRI's type (scheme). |
|
203 */ |
|
204 public FMRIScheme getScheme() |
|
205 { |
|
206 return (scheme_); |
|
207 } |
|
208 |
|
209 /** |
|
210 * Returns a svc: FMRI's type. |
|
211 */ |
|
212 public FMRIType getSvcType() |
|
213 { |
|
214 if (scheme_ != FMRIScheme.SERVICE) |
|
215 throw (new FMRIException(this, "not a service FMRI")); |
|
216 return (svctype_); |
|
217 } |
|
218 |
|
219 /** |
|
220 * Returns the FMRI's scope. |
|
221 */ |
|
222 public String getScope() |
|
223 { |
|
224 return (scope_ == null ? SCF_SCOPE_LOCAL : scope_); |
|
225 } |
|
226 |
|
227 /** |
|
228 * Returns the name of a legacy-run or file FMRI. |
|
229 */ |
|
230 public String getName() |
|
231 { |
|
232 if (scheme_ == FMRIScheme.SERVICE) |
|
233 throw (new FMRIException(this, |
|
234 "not a file or legacy FMRI")); |
|
235 return (name_); |
|
236 } |
|
237 |
|
238 /** |
|
239 * Throws an FMRIException from a function which may only be |
|
240 * called on a svc: FMRI. Takes the FMRIType required by the |
|
241 * caller. |
|
242 */ |
|
243 private void checktype(FMRIType svctype) |
|
244 { |
|
245 String prefix = "unable to read " + svctype; |
|
246 if (scheme_ != FMRIScheme.SERVICE) |
|
247 throw (new FMRIException(this, prefix + |
|
248 " from non-service FMRI")); |
|
249 if (svctype_.compareTo(svctype) < 0) |
|
250 throw (new FMRIException(this, prefix + |
|
251 " from " + svctype_ + "FMRI")); |
|
252 } |
|
253 |
|
254 /** |
|
255 * Returns the service component of a service FMRI. |
|
256 */ |
|
257 public String getService() |
|
258 { |
|
259 checktype(FMRIType.SERVICE); |
|
260 return (service_); |
|
261 } |
|
262 |
|
263 /** |
|
264 * Returns the instance component of a service FMRI, or {@code |
|
265 * null} if there is none. |
|
266 */ |
|
267 public String getInstance() |
|
268 { |
|
269 checktype(FMRIType.INSTANCE); |
|
270 return (instance_); |
|
271 } |
|
272 |
|
273 /** |
|
274 * Returns the property group component of a service FMRI. |
|
275 */ |
|
276 public String getPropertyGroup() |
|
277 { |
|
278 checktype(FMRIType.PGROUP); |
|
279 return (pg_); |
|
280 } |
|
281 |
|
282 /** |
|
283 * Returns the property component of a service FMRI. |
|
284 */ |
|
285 public String getProperty() |
|
286 { |
|
287 checktype(FMRIType.PROPERTY); |
|
288 return (property_); |
|
289 } |
|
290 |
|
291 public FMRI toServiceFMRI() |
|
292 { |
|
293 checktype(FMRIType.SERVICE); |
|
294 if (svctype_ == FMRIType.SERVICE) |
|
295 return this; |
|
296 return (new FMRI(scheme_, FMRIType.SERVICE, scope_, null, |
|
297 service_, null, null, null)); |
|
298 } |
|
299 |
|
300 public FMRI toInstanceFMRI() |
|
301 { |
|
302 checktype(FMRIType.INSTANCE); |
|
303 if (svctype_ == FMRIType.INSTANCE) |
|
304 return this; |
|
305 return (new FMRI(scheme_, FMRIType.INSTANCE, scope_, null, |
|
306 service_, instance_, null, null)); |
|
307 } |
|
308 |
|
309 public FMRI toInstanceFMRI(String instanceName) |
|
310 { |
|
311 checktype(FMRIType.SERVICE); |
|
312 return (new FMRI(scheme_, FMRIType.INSTANCE, scope_, null, |
|
313 service_, instanceName, null, null)); |
|
314 } |
|
315 |
|
316 |
|
317 /* |
|
318 * CompositeDataView methods |
|
319 */ |
|
320 |
|
321 private static final String[] itemNames = new String[] { "scheme", |
|
322 "svcType", "scope", "name", "service", "instance", "propertyGroup", |
|
323 "property" }; |
|
324 |
|
325 |
|
326 // Comparable methods |
|
327 |
|
328 private static int strcmp(String a, String b) |
|
329 { |
|
330 if (a == null) |
|
331 return (b == null ? 0 : 1); |
|
332 if (b == null) |
|
333 return (-1); |
|
334 return (a.compareTo(b)); |
|
335 } |
|
336 |
|
337 public int compareTo(FMRI f) |
|
338 { |
|
339 int result = scheme_.compareTo(f.scheme_); |
|
340 |
|
341 if (result == 0) |
|
342 result = strcmp(scope_, f.scope_); |
|
343 if (scheme_ != FMRIScheme.SERVICE) |
|
344 result = strcmp(name_, f.name_); |
|
345 else { |
|
346 if (result == 0) |
|
347 result = strcmp(service_, f.service_); |
|
348 if (result == 0) |
|
349 result = strcmp(instance_, f.instance_); |
|
350 if (result == 0) |
|
351 result = strcmp(pg_, f.pg_); |
|
352 if (result == 0) |
|
353 result = strcmp(property_, f.property_); |
|
354 } |
|
355 |
|
356 return (result); |
|
357 } |
|
358 |
|
359 /* |
|
360 * Object methods |
|
361 */ |
|
362 |
|
363 /** |
|
364 * Returns the FMRI as a string. |
|
365 */ |
|
366 @Override |
|
367 public String toString() |
|
368 { |
|
369 return (canonicalStr_); |
|
370 } |
|
371 |
|
372 @Override |
|
373 public boolean equals(Object o) |
|
374 { |
|
375 if (o == null) |
|
376 return (false); |
|
377 |
|
378 FMRI f; |
|
379 try { |
|
380 f = (FMRI)o; |
|
381 } catch (ClassCastException e) { |
|
382 return (false); |
|
383 } |
|
384 |
|
385 return (compareTo(f) == 0); |
|
386 } |
|
387 |
|
388 @Override |
|
389 public int hashCode() |
|
390 { |
|
391 int hash = 7; |
|
392 hash = 79 * hash + |
|
393 (this.scope_ != null ? this.scope_.hashCode() : 0); |
|
394 hash = 79 * hash + |
|
395 (this.service_ != null ? this.service_.hashCode() : 0); |
|
396 hash = 79 * hash + |
|
397 (this.instance_ != null ? this.instance_.hashCode() : 0); |
|
398 hash = 79 * hash + |
|
399 (this.pg_ != null ? this.pg_.hashCode() : 0); |
|
400 hash = 79 * hash + |
|
401 (this.property_ != null ? this.property_.hashCode() : 0); |
|
402 hash = 79 * hash + |
|
403 (this.name_ != null ? this.name_.hashCode() : 0); |
|
404 hash = 79 * hash + |
|
405 (this.scheme_ != null ? this.scheme_.hashCode() : 0); |
|
406 hash = 79 * hash + |
|
407 (this.svctype_ != null ? this.svctype_.hashCode() : 0); |
|
408 return hash; |
|
409 } |
|
410 } |
|