components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/common/smf/PropertyGroupsManagedObject.java
changeset 827 0944d8c0158b
child 1410 ca9946e5736c
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     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, 2012, 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.util.*;
       
    30 import java.util.logging.*;
       
    31 import com.oracle.solaris.scf.common.ScfException;
       
    32 import com.oracle.solaris.vp.panel.common.api.smf_old.PropertyGroup;
       
    33 import com.oracle.solaris.vp.panel.common.model.AbstractManagedObject;
       
    34 import com.oracle.solaris.vp.util.misc.ObjectUtil;
       
    35 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    36 
       
    37 public class PropertyGroupsManagedObject
       
    38     extends AbstractManagedObject<PropertyGroupManagedObject> {
       
    39 
       
    40     //
       
    41     // Static data
       
    42     //
       
    43 
       
    44     public static final String ID = "properties";
       
    45     public static final String NAME = Finder.getString("properties.name");
       
    46 
       
    47     public static final Comparator<Object> COMPARATOR =
       
    48 	new Comparator<Object>() {
       
    49 	    @Override
       
    50 	    public int compare(Object o1, Object o2) {
       
    51 		if (o1 == null) {
       
    52 		    return o2 == null ? 0 : -1;
       
    53 		}
       
    54 
       
    55 		if (o2 == null) {
       
    56 		    return 1;
       
    57 		}
       
    58 
       
    59 		String name1 = o1 instanceof PropertyGroupManagedObject ?
       
    60                     ((PropertyGroupManagedObject)o1).getName() :
       
    61 		    o1 instanceof PropertyGroup ?
       
    62                     ((PropertyGroup)o1).getName() : null;
       
    63 
       
    64 		String name2 = o2 instanceof PropertyGroupManagedObject ?
       
    65                     ((PropertyGroupManagedObject)o2).getName() :
       
    66 		    o2 instanceof PropertyGroup ?
       
    67                     ((PropertyGroup)o2).getName() : null;
       
    68 
       
    69 		return ObjectUtil.compare(name1, name2);
       
    70 	    }
       
    71 	};
       
    72 
       
    73     private static final String PROPERTY_GROUP_APP = "application";
       
    74 
       
    75     //
       
    76     // Instance data
       
    77     //
       
    78 
       
    79     private ServiceTracker tracker;
       
    80 
       
    81     private PropertyChangeListener serviceListener =
       
    82 	new PropertyChangeListener() {
       
    83 	    @Override
       
    84 	    public void propertyChange(PropertyChangeEvent event) {
       
    85 		try {
       
    86 		    refresh(false);
       
    87 		} catch (ScfException e) {
       
    88 		    Logger log = Logger.getLogger(getClass().getName());
       
    89                     log.log(Level.WARNING, "error gathering SMF property info",
       
    90 			e);
       
    91 		}
       
    92 	    }
       
    93 	};
       
    94 
       
    95     //
       
    96     // Constructors
       
    97     //
       
    98 
       
    99     PropertyGroupsManagedObject(ServiceTracker tracker) throws ScfException {
       
   100 	super(ID);
       
   101 	this.tracker = tracker;
       
   102 	setName(NAME);
       
   103 	setComparator(COMPARATOR);
       
   104 
       
   105         tracker.addPropertyChangeListener(
       
   106 	    ServiceTracker.PROPERTY_SERVICE, serviceListener);
       
   107 	refresh(true);
       
   108     }
       
   109 
       
   110     //
       
   111     // PropertyGroupsManagedObject methods
       
   112     //
       
   113 
       
   114     public PropertyGroupManagedObject getChild(PropertyGroup group) {
       
   115 	synchronized (children) {
       
   116 	    int index = Collections.binarySearch(
       
   117 		getChildren(), group, COMPARATOR);
       
   118 
       
   119 	    if (index >= 0) {
       
   120 		return children.get(index);
       
   121 	    }
       
   122 
       
   123 	    return null;
       
   124 	}
       
   125     }
       
   126 
       
   127     public ServiceTracker getServiceTracker() {
       
   128 	return tracker;
       
   129     }
       
   130 
       
   131     public void refresh(boolean force) throws ScfException {
       
   132 	ServiceMXBean service = tracker.getService();
       
   133 	if (service == null) {
       
   134 	    return;
       
   135 	}
       
   136 
       
   137 	synchronized (children) {
       
   138 	    List<PropertyGroupManagedObject> toRemove =
       
   139 		new LinkedList<PropertyGroupManagedObject>(children);
       
   140 
       
   141 	    List<PropertyGroup> groups = service.getPropertyGroups();
       
   142 	    for (PropertyGroup group : groups) {
       
   143 		if (!group.getType().equals(PROPERTY_GROUP_APP)) {
       
   144 		    continue;
       
   145 		}
       
   146 
       
   147 		PropertyGroupManagedObject child = getChild(group);
       
   148 		if (child == null) {
       
   149 		    child = new PropertyGroupManagedObject(this, group);
       
   150 		    addChildren(child);
       
   151 		} else {
       
   152 		    toRemove.remove(child);
       
   153 		    child.refresh(force);
       
   154 		}
       
   155 	    }
       
   156 
       
   157 	    // Minimize IntervalEvents by removing en masse
       
   158 	    if (!toRemove.isEmpty()) {
       
   159 		removeChildren(toRemove.toArray(
       
   160 		    new PropertyGroupManagedObject[toRemove.size()]));
       
   161 	    }
       
   162 	}
       
   163     }
       
   164 }