components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/common/smf/ServicePanelDescriptor.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     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.common.smf;
       
    27 
       
    28 import java.beans.*;
       
    29 import java.io.IOException;
       
    30 import java.util.Locale;
       
    31 import java.util.logging.Level;
       
    32 import com.oracle.solaris.scf.common.ScfException;
       
    33 import com.oracle.solaris.vp.panel.common.*;
       
    34 import com.oracle.solaris.vp.panel.common.api.smf_old.*;
       
    35 import com.oracle.solaris.vp.panel.common.model.*;
       
    36 import com.oracle.solaris.vp.util.misc.Enableable;
       
    37 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    38 
       
    39 @SuppressWarnings({"serial"})
       
    40 public abstract class ServicePanelDescriptor<C extends ManagedObject>
       
    41     extends AbstractPanelDescriptor<C> implements HasService, HasServiceTracker,
       
    42     Enableable {
       
    43 
       
    44     //
       
    45     // Static data
       
    46     //
       
    47 
       
    48     public static final String PROPERTY_STATE = "state";
       
    49 
       
    50     //
       
    51     // Instance data
       
    52     //
       
    53 
       
    54     private ServiceTracker tracker;
       
    55     private String serviceName;
       
    56     private String instanceName;
       
    57 
       
    58     private SmfEnabledProperty enabledProperty =
       
    59 	new SmfEnabledProperty(PROPERTY_ENABLED, this);
       
    60 
       
    61     private PropertyChangeListener serviceListener =
       
    62 	new PropertyChangeListener() {
       
    63 	    @Override
       
    64 	    public void propertyChange(PropertyChangeEvent event) {
       
    65 		serviceChanged();
       
    66 	    }
       
    67 	};
       
    68 
       
    69     //
       
    70     // Constructors
       
    71     //
       
    72 
       
    73     public ServicePanelDescriptor(String id, ClientContext context,
       
    74 	String serviceName, String instanceName) throws IOException,
       
    75 	TrackerException {
       
    76 
       
    77 	super(id, context);
       
    78 
       
    79 	this.serviceName = serviceName;
       
    80 	this.instanceName = instanceName;
       
    81 
       
    82 	tracker = new ServiceTracker(serviceName, instanceName, context);
       
    83 	tracker.addPropertyChangeListener(ServiceTracker.PROPERTY_SERVICE,
       
    84 	    serviceListener);
       
    85 
       
    86 	serviceChanged();
       
    87 	setName();
       
    88     }
       
    89 
       
    90     //
       
    91     // ManagedObject methods
       
    92     //
       
    93 
       
    94     /**
       
    95      * Stops monitoring the connection to the remote host.
       
    96      */
       
    97     @Override
       
    98     public void dispose() {
       
    99 	tracker.dispose();
       
   100 	super.dispose();
       
   101     }
       
   102 
       
   103     /**
       
   104      * Default implementation that returns {@code null}.
       
   105      */
       
   106     @Override
       
   107     public String getDescription() {
       
   108 	String locale = Locale.getDefault().getLanguage();
       
   109 
       
   110 	try {
       
   111 	    return getService().getDescription(locale);
       
   112 	} catch (ScfException e) {
       
   113 	    getLog().log(Level.SEVERE, Finder.getString("error.scf.general"),
       
   114 		 e);
       
   115 	}
       
   116 
       
   117 	return null;
       
   118     }
       
   119 
       
   120     //
       
   121     // PanelDescriptor methods
       
   122     //
       
   123 
       
   124     @Override
       
   125     public boolean getHasFullPrivileges() {
       
   126 	ConnectionInfo info = getClientContext().getConnectionInfo();
       
   127 	String user = info.getRole();
       
   128 	if (user == null) {
       
   129 	    user = info.getUser();
       
   130 	}
       
   131 
       
   132 	// XXX Provide a better diagnostic
       
   133 	return "root".equals(user);
       
   134     }
       
   135 
       
   136     //
       
   137     // HasService methods
       
   138     //
       
   139 
       
   140     @Override
       
   141     public AggregatedRefreshService getService() {
       
   142 	// This may be called indirectly by the superclass before tracker has
       
   143 	// been instantiated
       
   144 	return tracker == null ? null : tracker.getService();
       
   145     }
       
   146 
       
   147     //
       
   148     // HasServiceTracker methods
       
   149     //
       
   150 
       
   151     @Override
       
   152     public ServiceTracker getServiceTracker() {
       
   153 	return tracker;
       
   154     }
       
   155 
       
   156     //
       
   157     // Enableable methods
       
   158     //
       
   159 
       
   160     /**
       
   161      * Gets the enabled status from the {@link #getEnabledProperty enabled
       
   162      * property}.
       
   163      *
       
   164      * @return	    {@code true} if enabled, {@code false} if not enabled or if
       
   165      *		    the {@link #getEnabledProperty enabled property} has not
       
   166      *		    been initialized
       
   167      *
       
   168      * @see	    #refresh
       
   169      */
       
   170     @Override
       
   171     public boolean isEnabled() {
       
   172 	Boolean enabled = enabledProperty.getSavedValue();
       
   173 	return enabled == null ? false : enabled;
       
   174     }
       
   175 
       
   176     //
       
   177     // AbstractManagedObject methods
       
   178     //
       
   179 
       
   180     /**
       
   181      * Downgrades this {@code PanelDescriptor}'s state if the underlying service
       
   182      * is degraded.
       
   183      */
       
   184     @Override
       
   185     protected ManagedObjectStatus getCalculatedStatus() {
       
   186 	ManagedObjectStatus status = super.getCalculatedStatus();
       
   187 
       
   188 	ServiceBean service = getService();
       
   189 	if (service != null) {
       
   190 	    ManagedObjectStatus sStatus = null;
       
   191 
       
   192 	    try {
       
   193 		SmfState state = service.getState();
       
   194 		sStatus = ServiceUtil.getPanelStatus(state);
       
   195 	    } catch (ScfException e) {
       
   196 		getLog().log(Level.SEVERE, Finder.getString(
       
   197 		    "error.scf.general"), e);
       
   198 		sStatus = ManagedObjectStatus.ERROR;
       
   199 	    }
       
   200 
       
   201 	    status = status.compareTo(sStatus) > 0 ? status : sStatus;
       
   202 	}
       
   203 
       
   204 	return status;
       
   205     }
       
   206 
       
   207     @Override
       
   208     protected String getCalculatedStatusText() {
       
   209 	SmfState state = null;
       
   210 	SmfState nextState = null;
       
   211 	ServiceBean service = getService();
       
   212 
       
   213 	try {
       
   214 	    state = service.getState();
       
   215 	    nextState = service.getNextState();
       
   216 	} catch (ScfException e) {
       
   217 	    getLog().log(Level.SEVERE, Finder.getString("error.scf.general"),
       
   218 		 e);
       
   219 	}
       
   220 
       
   221 	// Can handle null states
       
   222 	return ServiceUtil.getStateSynopsis(state, nextState);
       
   223     }
       
   224 
       
   225     //
       
   226     // ServicePanelDescriptor methods
       
   227     //
       
   228 
       
   229     public SmfEnabledProperty getEnabledProperty() {
       
   230 	return enabledProperty;
       
   231     }
       
   232 
       
   233     public String getInstanceName() {
       
   234 	return instanceName;
       
   235     }
       
   236 
       
   237     public String getServiceName() {
       
   238 	return serviceName;
       
   239     }
       
   240 
       
   241     /**
       
   242      * Updates this {@code PanelDescriptor} with service/repository data.  This
       
   243      * default implementation updates the {@link #getEnabledProperty enabled
       
   244      * property} of this object.
       
   245      *
       
   246      * @param	    force
       
   247      *		    {@code true} to override any values that have been changed
       
   248      *		    but not yet saved, {@code false} otherwise
       
   249      *
       
   250      * @exception   ScfException
       
   251      *		    if an error occurs while accessing the SMF repository
       
   252      *
       
   253      * @exception   InvalidScfDataException
       
   254      *		    if data in the repository is invalid
       
   255      *
       
   256      * @exception   MissingScfDataException
       
   257      *		    if data in the repository is missing
       
   258      */
       
   259     public void refresh(boolean force) throws ScfException,
       
   260 	InvalidScfDataException, MissingScfDataException {
       
   261 
       
   262 	enabledProperty.updateFromRepo(force);
       
   263     }
       
   264 
       
   265     /**
       
   266      * Sets the name of this {@code ServicePanelDescriptor}.
       
   267      */
       
   268     protected void setName() {
       
   269 	String name = null;
       
   270 	String locale = Locale.getDefault().getLanguage();
       
   271 
       
   272 	try {
       
   273 	    name = getService().getCommonName(locale);
       
   274 	} catch (ScfException e) {
       
   275 	    getLog().log(Level.SEVERE, Finder.getString("error.scf.general"),
       
   276 		 e);
       
   277 	}
       
   278 
       
   279 	if (name == null) {
       
   280 	    name = serviceName.substring(serviceName.indexOf('/') + 1);
       
   281 	}
       
   282 
       
   283 	setName(name);
       
   284     }
       
   285 
       
   286     //
       
   287     // Private methods
       
   288     //
       
   289 
       
   290     private void serviceChanged() {
       
   291 	setStatus();
       
   292 	setStatusText();
       
   293     }
       
   294 }