org.coos.messaging.jmx.ManagementServiceImpl Maven / Gradle / Ivy
/**
* COOS - Connected Objects Operating System (www.connectedobjects.org).
*
* Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*
* You may also contact one of the following for additional information:
* Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
* Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
*/
package org.coos.messaging.jmx;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import org.coos.messaging.COOS;
import org.coos.messaging.Plugin;
import org.coos.messaging.routing.Router;
import org.coos.messaging.util.Log;
import org.coos.messaging.util.LogFactory;
/**
* Implementing class of ManagementService for J2SE.
*
* @author Robert Bjarum, Tellu AS
* @since 1.1
*
*/
public class ManagementServiceImpl implements ManagementService {
private Map registry = Collections.synchronizedMap(new HashMap());
private MBeanServer mbServer;
private Log logger = LogFactory.getLog(ManagementServiceImpl.class.getName());
protected ManagementServiceImpl() {
mbServer = java.lang.management.ManagementFactory.getPlatformMBeanServer();
}
/*
* (non-Javadoc)
* @see org.coos.messaging.jmx.ManagementService#registerManagedObject(org.coos.messaging.jmx.ManagedObject, boolean)
*/
public ManagedObject register(ManagedObject managedObject, boolean replace) {
ObjectName objectName;
try {
objectName = new ObjectName(managedObject.getObjectName());
if (replace && mbServer.isRegistered(objectName)) {
mbServer.unregisterMBean(objectName);
}
mbServer.registerMBean(managedObject, objectName);
registry.put(managedObject, objectName);
logger.info("Registered, name: " + managedObject);
} catch (MalformedObjectNameException e) {
logger.error("Cannot accept object name: " + managedObject);
e.printStackTrace();
} catch (NullPointerException e) {
logger.error("Could not create object name from: " + managedObject);
e.printStackTrace();
} catch (InstanceAlreadyExistsException e) {
logger.warn("Could not register object with name: " + managedObject + " Consider setting replace to true");
e.printStackTrace();
} catch (MBeanRegistrationException e) {
logger.error("MBeanRegistrationException thrown for object with name: " + managedObject);
e.printStackTrace();
} catch (NotCompliantMBeanException e) {
logger.error("Could not register object with name: " + managedObject + " Not MBEAN compliant");
e.printStackTrace();
} catch (InstanceNotFoundException e) {
e.printStackTrace();
}
return managedObject;
}
/*
* (non-Javadoc)
* @see org.coos.messaging.jmx.ManagementService#unregisterManagedObject(org.coos.messaging.jmx.ManagedObject)
*/
public void unregister(ManagedObject managedObject) {
ObjectName objectName = registry.remove(managedObject);
if (objectName != null) {
try {
mbServer.unregisterMBean(objectName);
} catch (MBeanRegistrationException e) {
logger.error("Could not unregister object: " + managedObject);
e.printStackTrace();
} catch (InstanceNotFoundException e) {
logger.warn("Object not found, " + managedObject);
e.printStackTrace();
}
}
}
@Override
public ManagedObject registerPlugin(Plugin plugin) {
PluginMBeanWrapper wrapper = new PluginMBeanWrapper(plugin);
register(wrapper, true);
return wrapper;
}
@Override
public ManagedObject registerCoos(Object obj) {
COOS coos = (COOS) obj;
CoosMBeanWrapper wrapper = new CoosMBeanWrapper(coos);
register(wrapper, true);
return wrapper;
}
@Override
public ManagedObject registerRouter(Object obj) {
Router router = (Router) obj;
RouterMBeanWrapper wrapper = new RouterMBeanWrapper(router);
register(wrapper, true);
return wrapper;
}
}