components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/common/smf/RepoManagedObject.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) 2010, 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.io.IOException;
       
    30 import java.util.*;
       
    31 import java.util.logging.*;
       
    32 import javax.management.*;
       
    33 import com.oracle.solaris.adr.Stability;
       
    34 import com.oracle.solaris.rad.ObjectException;
       
    35 import com.oracle.solaris.rad.jmx.RadNotification;
       
    36 import com.oracle.solaris.vp.panel.common.*;
       
    37 import com.oracle.solaris.vp.panel.common.api.panel.MBeanUtil;
       
    38 import com.oracle.solaris.vp.panel.common.api.file.*;
       
    39 import com.oracle.solaris.vp.panel.common.api.smf_old.*;
       
    40 import com.oracle.solaris.vp.panel.common.model.*;
       
    41 
       
    42 public class RepoManagedObject
       
    43     extends AbstractManagedObject<InstanceManagedObject> {
       
    44 
       
    45     //
       
    46     // Static data
       
    47     //
       
    48 
       
    49     private static final String DOMAIN = MBeanUtil.VP_DOMAIN + ".smf_old";
       
    50     private static final ObjectName oName =
       
    51 	MBeanUtil.makeObjectName(DOMAIN, "Aggregator");
       
    52 
       
    53     //
       
    54     // Instance data
       
    55     //
       
    56 
       
    57     private Set<Service> services;
       
    58     private Set<Instance> instances;
       
    59     private Map<ObjectName, SmfManagedObject> objects;
       
    60     private AbstractManagedObject<ServiceManagedObject> serviceMo;
       
    61 
       
    62     private MXBeanTracker<AggregatorMXBean> beanTracker;
       
    63     private MXBeanTracker<FileBrowserMXBean> filebeanTracker;
       
    64 
       
    65     private NotificationListener stateListener =
       
    66 	new NotificationListener() {
       
    67 	    @Override
       
    68 	    public void handleNotification(Notification notification,
       
    69 		Object handback) {
       
    70 
       
    71 		StateChange sc = ((RadNotification)notification).getPayload(
       
    72 		    StateChange.class);
       
    73 		stateChanged(sc);
       
    74 	    }
       
    75 	};
       
    76 
       
    77     private PropertyChangeListener beanListener =
       
    78 	new PropertyChangeListener() {
       
    79 	    @Override
       
    80 	    public void propertyChange(PropertyChangeEvent event) {
       
    81 		beanChanged();
       
    82 	    }
       
    83 	};
       
    84 
       
    85     //
       
    86     // Constructors
       
    87     //
       
    88 
       
    89     public RepoManagedObject(String id, ClientContext context)
       
    90 	throws InstanceNotFoundException, IOException, TrackerException {
       
    91 
       
    92 	super(id);
       
    93 
       
    94 	beanTracker = new MXBeanTracker<AggregatorMXBean>(
       
    95 	    oName, AggregatorMXBean.class, Stability.PRIVATE, context);
       
    96 
       
    97 	beanTracker.addNotificationListener(stateListener,
       
    98 	    SmfUtil.NOTIFY_FILTER_STATE_CHANGE, null);
       
    99 
       
   100 	beanTracker.addPropertyChangeListener(
       
   101 	    MXBeanTracker.PROPERTY_BEAN, beanListener);
       
   102 
       
   103 	filebeanTracker = new MXBeanTracker<FileBrowserMXBean>(
       
   104             FileBrowserUtil.OBJECT_NAME, FileBrowserMXBean.class,
       
   105             Stability.PRIVATE, context);
       
   106 
       
   107 	objects = Collections.emptyMap();
       
   108 	serviceMo = new AbstractManagedObject<ServiceManagedObject>() {};
       
   109 
       
   110 	beanChanged();
       
   111     }
       
   112 
       
   113     //
       
   114     // ManagedObject methods
       
   115     //
       
   116 
       
   117     /**
       
   118      * Stops monitoring the connection to the remote host.
       
   119      */
       
   120     @Override
       
   121     public void dispose() {
       
   122 	beanTracker.dispose();
       
   123 	filebeanTracker.dispose();
       
   124 	super.dispose();
       
   125     }
       
   126 
       
   127     //
       
   128     // RepoManagedObject methods
       
   129     //
       
   130 
       
   131     public boolean hasInstance(String service) {
       
   132 	ObjectName pattern;
       
   133 
       
   134 	try {
       
   135 	    pattern = ServiceUtil.getServiceObjectName(service, "*");
       
   136 	} catch (MalformedObjectNameException ex) {
       
   137 	    return false;
       
   138 	}
       
   139 
       
   140 	for (Instance instance : instances)
       
   141 	    if (pattern.apply(instance.getObjectName())) {
       
   142 		return true;
       
   143 	    }
       
   144 
       
   145 	return false;
       
   146     }
       
   147 
       
   148     public ConnectionTracker getConnectionTracker() {
       
   149 	return beanTracker;
       
   150     }
       
   151 
       
   152     public ManagedObject<ServiceManagedObject> getServices() {
       
   153 	return serviceMo;
       
   154     }
       
   155 
       
   156     public SmfManagedObject getSMO(String fmri) {
       
   157 	for (SmfManagedObject smo : children)
       
   158 	    if (smo.getId().equals(fmri)) {
       
   159 		return smo;
       
   160 	    }
       
   161 
       
   162 	return null;
       
   163     }
       
   164 
       
   165     public SmfManagedObject getServiceSMO(String fmri) {
       
   166 	for (SmfManagedObject smo : serviceMo.getChildren())
       
   167 	    if (smo.getId().equals(fmri)) {
       
   168 		return smo;
       
   169 	    }
       
   170 
       
   171 	return null;
       
   172     }
       
   173 
       
   174     public boolean fileExists(String filename) {
       
   175 	try {
       
   176 	    return filebeanTracker.getBean().getFile(filename).isExists();
       
   177 	} catch (ObjectException e) {
       
   178 	}
       
   179 	return false;
       
   180     }
       
   181 
       
   182     //
       
   183     // Private methods
       
   184     //
       
   185 
       
   186     private void beanChanged() {
       
   187 	services = Collections.emptySet();
       
   188 	instances = Collections.emptySet();
       
   189 
       
   190 	AggregatorMXBean bean = beanTracker.getBean();
       
   191 	if (bean != null) {
       
   192 	    try {
       
   193 		services = new HashSet<Service>(bean.getservices());
       
   194 		instances = new HashSet<Instance>(bean.getinstances());
       
   195 	    } catch (ObjectException e) {
       
   196 		Logger.getLogger(getClass().getName()).log(Level.SEVERE,
       
   197 		    "could not retrieve smf services/instances", e);
       
   198 	    }
       
   199 	}
       
   200 
       
   201 	/*
       
   202 	 * Ideally, this would preserve those children that remain after a
       
   203 	 * relogin, and throw away children after a host change.
       
   204 	 *
       
   205 	 * Right now, it preserves objects but not the child list on both
       
   206 	 * relogin and host changes.
       
   207 	 */
       
   208 
       
   209 	clearChildren();
       
   210 	serviceMo.clearChildren();
       
   211 	Map<ObjectName, SmfManagedObject> oldObjects = objects;
       
   212 	objects = new HashMap<ObjectName, SmfManagedObject>(objects);
       
   213 
       
   214 	for (Service svc : services) {
       
   215 	    ObjectName oName = svc.getObjectName();
       
   216             ServiceManagedObject smo =
       
   217 		(ServiceManagedObject)oldObjects.get(oName);
       
   218 	    if (smo == null) {
       
   219 		try {
       
   220 		    smo = new ServiceManagedObject(this,
       
   221 			beanTracker.getClientContext(), svc);
       
   222 		} catch (TrackerException e) {
       
   223 		    smo = null;
       
   224 		}
       
   225 	    } else {
       
   226 		oldObjects.remove(oName);
       
   227 	    }
       
   228 
       
   229 	    if (smo != null) {
       
   230 		objects.put(oName, smo);
       
   231 		serviceMo.addChildren(smo);
       
   232 	    }
       
   233 	}
       
   234 
       
   235 	for (Instance inst : instances) {
       
   236 	    ObjectName oName = inst.getObjectName();
       
   237 	    InstanceManagedObject imo =
       
   238 		(InstanceManagedObject)oldObjects.get(oName);
       
   239 	    if (imo == null) {
       
   240 		try {
       
   241 		    imo = new InstanceManagedObject(
       
   242 			beanTracker.getClientContext(), inst);
       
   243 		} catch (TrackerException e) {
       
   244 		    imo = null;
       
   245 		}
       
   246 	    } else {
       
   247 		oldObjects.remove(oName);
       
   248 	    }
       
   249 
       
   250 	    if (imo != null) {
       
   251 		objects.put(oName, imo);
       
   252 		addChildren(imo);
       
   253 	    }
       
   254 	}
       
   255 
       
   256 	// Ensure that any SmfManagedObjects for now-non-existent services are
       
   257 	// prepared for garbage collection
       
   258 	for (SmfManagedObject smo : oldObjects.values()) {
       
   259 	    smo.dispose();
       
   260 	}
       
   261     }
       
   262 
       
   263     private void stateChanged(StateChange sc) {
       
   264 	SmfManagedObject smo = objects.get(sc.getSource());
       
   265 	if (smo != null && smo instanceof InstanceManagedObject) {
       
   266 	    InstanceManagedObject imo = (InstanceManagedObject)smo;
       
   267 	    imo.handleStateChange(sc);
       
   268 	}
       
   269     }
       
   270 }