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