Felix Meschberger | 9e60059 | 2010-10-10 21:14:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.coordination.impl; |
| 20 | |
| 21 | import java.util.Hashtable; |
| 22 | |
Felix Meschberger | 0055b31 | 2010-10-13 07:39:32 +0000 | [diff] [blame^] | 23 | import javax.management.MBeanServer; |
| 24 | import javax.management.MalformedObjectNameException; |
| 25 | import javax.management.ObjectName; |
| 26 | |
| 27 | import org.apache.felix.jmx.service.coordination.CoordinatorMBean; |
Felix Meschberger | 9e60059 | 2010-10-10 21:14:10 +0000 | [diff] [blame] | 28 | import org.apache.felix.service.coordination.Coordinator; |
| 29 | import org.osgi.framework.Bundle; |
| 30 | import org.osgi.framework.BundleActivator; |
| 31 | import org.osgi.framework.BundleContext; |
| 32 | import org.osgi.framework.Constants; |
| 33 | import org.osgi.framework.ServiceFactory; |
Felix Meschberger | 0055b31 | 2010-10-13 07:39:32 +0000 | [diff] [blame^] | 34 | import org.osgi.framework.ServiceReference; |
Felix Meschberger | 9e60059 | 2010-10-10 21:14:10 +0000 | [diff] [blame] | 35 | import org.osgi.framework.ServiceRegistration; |
Felix Meschberger | 0055b31 | 2010-10-13 07:39:32 +0000 | [diff] [blame^] | 36 | import org.osgi.util.tracker.ServiceTracker; |
Felix Meschberger | 9e60059 | 2010-10-10 21:14:10 +0000 | [diff] [blame] | 37 | |
| 38 | @SuppressWarnings("deprecation") |
| 39 | public class Activator implements BundleActivator { |
| 40 | |
| 41 | private CoordinationMgr mgr; |
| 42 | |
Felix Meschberger | 0055b31 | 2010-10-13 07:39:32 +0000 | [diff] [blame^] | 43 | private ServiceTracker mbeanServerTracker; |
| 44 | |
Felix Meschberger | 9e60059 | 2010-10-10 21:14:10 +0000 | [diff] [blame] | 45 | private ServiceRegistration coordinatorService; |
| 46 | |
Felix Meschberger | 0055b31 | 2010-10-13 07:39:32 +0000 | [diff] [blame^] | 47 | public void start(BundleContext context) { |
Felix Meschberger | 9e60059 | 2010-10-10 21:14:10 +0000 | [diff] [blame] | 48 | mgr = new CoordinationMgr(); |
| 49 | |
Felix Meschberger | 0055b31 | 2010-10-13 07:39:32 +0000 | [diff] [blame^] | 50 | try { |
| 51 | mbeanServerTracker = new MBeanServerTracker(context, mgr); |
| 52 | mbeanServerTracker.open(); |
| 53 | } catch (MalformedObjectNameException e) { |
| 54 | // TODO log |
| 55 | } |
| 56 | |
Felix Meschberger | 9e60059 | 2010-10-10 21:14:10 +0000 | [diff] [blame] | 57 | ServiceFactory factory = new CoordinatorFactory(mgr); |
| 58 | Hashtable<String, String> props = new Hashtable<String, String>(); |
| 59 | props.put(Constants.SERVICE_DESCRIPTION, |
| 60 | "Coordinator Service Implementation"); |
| 61 | props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation"); |
| 62 | coordinatorService = context.registerService( |
| 63 | Coordinator.class.getName(), factory, props); |
| 64 | } |
| 65 | |
Felix Meschberger | 0055b31 | 2010-10-13 07:39:32 +0000 | [diff] [blame^] | 66 | public void stop(BundleContext context) { |
Felix Meschberger | 9e60059 | 2010-10-10 21:14:10 +0000 | [diff] [blame] | 67 | if (coordinatorService != null) { |
| 68 | coordinatorService.unregister(); |
| 69 | coordinatorService = null; |
| 70 | } |
| 71 | |
Felix Meschberger | 0055b31 | 2010-10-13 07:39:32 +0000 | [diff] [blame^] | 72 | if (mbeanServerTracker != null) { |
| 73 | mbeanServerTracker.close(); |
| 74 | mbeanServerTracker = null; |
| 75 | } |
| 76 | |
Felix Meschberger | 9e60059 | 2010-10-10 21:14:10 +0000 | [diff] [blame] | 77 | mgr.cleanUp(); |
| 78 | } |
| 79 | |
| 80 | static final class CoordinatorFactory implements ServiceFactory { |
| 81 | |
| 82 | private final CoordinationMgr mgr; |
| 83 | |
| 84 | CoordinatorFactory(final CoordinationMgr mgr) { |
| 85 | this.mgr = mgr; |
| 86 | } |
| 87 | |
| 88 | public Object getService(Bundle bundle, ServiceRegistration registration) { |
| 89 | return new CoordinatorImpl(bundle, mgr); |
| 90 | } |
| 91 | |
| 92 | public void ungetService(Bundle bundle, |
| 93 | ServiceRegistration registration, Object service) { |
| 94 | // nothing to do |
| 95 | } |
| 96 | |
| 97 | } |
Felix Meschberger | 0055b31 | 2010-10-13 07:39:32 +0000 | [diff] [blame^] | 98 | |
| 99 | static final class MBeanServerTracker extends ServiceTracker { |
| 100 | |
| 101 | private final CoordinationMgr mgr; |
| 102 | |
| 103 | private final ObjectName objectName; |
| 104 | |
| 105 | MBeanServerTracker(final BundleContext context, |
| 106 | final CoordinationMgr mgr) throws MalformedObjectNameException { |
| 107 | super(context, MBeanServer.class.getName(), null); |
| 108 | this.mgr = mgr; |
| 109 | this.objectName = new ObjectName(CoordinatorMBean.OBJECTNAME); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public Object addingService(ServiceReference reference) { |
| 114 | MBeanServer server = (MBeanServer) super.addingService(reference); |
| 115 | |
| 116 | try { |
| 117 | server.registerMBean(mgr, objectName); |
| 118 | } catch (Exception e) { |
| 119 | // TODO: log |
| 120 | } |
| 121 | |
| 122 | return server; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void removedService(ServiceReference reference, Object service) { |
| 127 | try { |
| 128 | ((MBeanServer) service).unregisterMBean(objectName); |
| 129 | } catch (Exception e) { |
| 130 | // TODO: log |
| 131 | } |
| 132 | |
| 133 | super.removedService(reference, service); |
| 134 | } |
| 135 | } |
Felix Meschberger | 9e60059 | 2010-10-10 21:14:10 +0000 | [diff] [blame] | 136 | } |