All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.camunda.bpm.container.impl.jmx.MBeanServiceContainer Maven / Gradle / Ivy

There is a newer version: 7.22.0-alpha5
Show newest version
/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. Camunda licenses this file to you under the Apache License,
 * Version 2.0; you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.camunda.bpm.container.impl.jmx;

import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.concurrent.ConcurrentHashMap;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.camunda.bpm.container.impl.ContainerIntegrationLogger;
import org.camunda.bpm.container.impl.spi.DeploymentOperation;
import org.camunda.bpm.container.impl.spi.DeploymentOperation.DeploymentOperationBuilder;
import org.camunda.bpm.container.impl.spi.PlatformService;
import org.camunda.bpm.container.impl.spi.PlatformServiceContainer;
import org.camunda.bpm.engine.ProcessEngineException;
import org.camunda.bpm.engine.impl.ProcessEngineLogger;

import static org.camunda.bpm.engine.impl.util.EnsureUtil.ensureNotNull;

/**
 * 

A simple Service Container that delegates to the JVM's {@link MBeanServer}.

* * @author Daniel Meyer * */ public class MBeanServiceContainer implements PlatformServiceContainer { private final static ContainerIntegrationLogger LOG = ProcessEngineLogger.CONTAINER_INTEGRATION_LOGGER; protected MBeanServer mBeanServer; protected Map> servicesByName = new ConcurrentHashMap>(); /** set if the current thread is performing a composite deployment operation */ protected ThreadLocal> activeDeploymentOperations = new ThreadLocal>(); public final static String SERVICE_NAME_EXECUTOR = "executor-service"; public synchronized void startService(ServiceType serviceType, String localName, PlatformService service) { String serviceName = composeLocalName(serviceType, localName); startService(serviceName, service); } public synchronized void startService(String name, PlatformService service) { ObjectName serviceName = getObjectName(name); if (getService(serviceName) != null) { throw new ProcessEngineException("Cannot register service " + serviceName + " with MBeans Container, service with same name already registered."); } final MBeanServer beanServer = getmBeanServer(); // call the service-provided start behavior service.start(this); try { beanServer.registerMBean(service, serviceName); servicesByName.put(serviceName, service); Stack currentOperationContext = activeDeploymentOperations.get(); if (currentOperationContext != null) { currentOperationContext.peek().serviceAdded(name); } } catch (Exception e) { throw LOG.cannotRegisterService(serviceName, e); } } public static ObjectName getObjectName(String serviceName) { try { return new ObjectName(serviceName); } catch(Exception e) { throw LOG.cannotComposeNameFor(serviceName, e); } } public static String composeLocalName(ServiceType type, String localName) { return type.getTypeName() + ":type=" + localName; } public synchronized void stopService(ServiceType serviceType, String localName) { String globalName = composeLocalName(serviceType, localName); stopService(globalName); } public synchronized void stopService(String name) { final MBeanServer mBeanServer = getmBeanServer(); ObjectName serviceName = getObjectName(name); final PlatformService service = getService(serviceName); ensureNotNull("Cannot stop service " + serviceName + ": no such service registered", "service", service); try { // call the service-provided stop behavior service.stop(this); } finally { // always unregister, even if the stop method throws an exception. try { mBeanServer.unregisterMBean(serviceName); servicesByName.remove(serviceName); } catch (Throwable t) { throw LOG.exceptionWhileUnregisteringService(serviceName.getCanonicalName(), t); } } } public DeploymentOperationBuilder createDeploymentOperation(String name) { return new DeploymentOperation.DeploymentOperationBuilder(this, name); } public DeploymentOperationBuilder createUndeploymentOperation(String name) { DeploymentOperationBuilder builder = new DeploymentOperation.DeploymentOperationBuilder(this, name); builder.setUndeploymentOperation(); return builder; } public void executeDeploymentOperation(DeploymentOperation operation) { Stack currentOperationContext = activeDeploymentOperations.get(); if(currentOperationContext == null) { currentOperationContext = new Stack(); activeDeploymentOperations.set(currentOperationContext); } try { currentOperationContext.push(operation); // execute the operation operation.execute(); } finally { currentOperationContext.pop(); if(currentOperationContext.isEmpty()) { activeDeploymentOperations.remove(); } } } /** * get a specific service by name or null if no such Service exists. * */ public S getService(ServiceType type, String localName) { String globalName = composeLocalName(type, localName); ObjectName serviceName = getObjectName(globalName); return getService(serviceName); } /** * get a specific service by name or null if no such Service exists. * */ @SuppressWarnings("unchecked") public S getService(ObjectName name) { return (S) servicesByName.get(name); } /** * get the service value for a specific service by name or null if no such * Service exists. * */ public S getServiceValue(ObjectName name) { PlatformService service = getService(name); if(service != null) { return service.getValue(); } else { return null; } } /** * get the service value for a specific service by name or null if no such * Service exists. * */ public S getServiceValue(ServiceType type, String localName) { String globalName = composeLocalName(type, localName); ObjectName serviceName = getObjectName(globalName); return getServiceValue(serviceName); } /** * @return all services for a specific {@link ServiceType} */ @SuppressWarnings("unchecked") public List> getServicesByType(ServiceType type) { // query the MBeanServer for all services of the given type Set serviceNames = getServiceNames(type); List> res = new ArrayList>(); for (String serviceName : serviceNames) { res.add((PlatformService) servicesByName.get(getObjectName(serviceName))); } return res; } /** * @return the service names ( {@link ObjectName} ) for all services for a given type */ public Set getServiceNames(ServiceType type) { String typeName = composeLocalName(type, "*"); ObjectName typeObjectName = getObjectName(typeName); Set resultNames = getmBeanServer().queryNames(typeObjectName, null); Set result= new HashSet(); for (ObjectName objectName : resultNames) { result.add(objectName.toString()); } return result; } /** * @return the values of all services for a specific {@link ServiceType} */ @SuppressWarnings("unchecked") public List getServiceValuesByType(ServiceType type) { // query the MBeanServer for all services of the given type Set serviceNames = getServiceNames(type); List res = new ArrayList(); for (String serviceName : serviceNames) { PlatformService BpmPlatformService = (PlatformService) servicesByName.get(getObjectName(serviceName)); if (BpmPlatformService != null) { res.add(BpmPlatformService.getValue()); } } return res; } public MBeanServer getmBeanServer() { if (mBeanServer == null) { synchronized (this) { if (mBeanServer == null) { mBeanServer = createOrLookupMbeanServer(); } } } return mBeanServer; } public void setmBeanServer(MBeanServer mBeanServer) { this.mBeanServer = mBeanServer; } protected MBeanServer createOrLookupMbeanServer() { return ManagementFactory.getPlatformMBeanServer(); } }