blob: 5748a1d99281f292df229e81bb2a03178a0d08ff [file] [log] [blame]
Felix Meschberger9e600592010-10-10 21:14:10 +00001/*
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 */
19package org.apache.felix.coordination.impl;
20
21import java.util.Hashtable;
22
Felix Meschberger0055b312010-10-13 07:39:32 +000023import javax.management.MBeanServer;
24import javax.management.MalformedObjectNameException;
25import javax.management.ObjectName;
26
27import org.apache.felix.jmx.service.coordination.CoordinatorMBean;
Felix Meschberger9e600592010-10-10 21:14:10 +000028import org.apache.felix.service.coordination.Coordinator;
29import org.osgi.framework.Bundle;
30import org.osgi.framework.BundleActivator;
31import org.osgi.framework.BundleContext;
32import org.osgi.framework.Constants;
33import org.osgi.framework.ServiceFactory;
Felix Meschberger0055b312010-10-13 07:39:32 +000034import org.osgi.framework.ServiceReference;
Felix Meschberger9e600592010-10-10 21:14:10 +000035import org.osgi.framework.ServiceRegistration;
Felix Meschberger0055b312010-10-13 07:39:32 +000036import org.osgi.util.tracker.ServiceTracker;
Felix Meschberger9e600592010-10-10 21:14:10 +000037
38@SuppressWarnings("deprecation")
39public class Activator implements BundleActivator {
40
41 private CoordinationMgr mgr;
42
Felix Meschberger0055b312010-10-13 07:39:32 +000043 private ServiceTracker mbeanServerTracker;
44
Felix Meschberger9e600592010-10-10 21:14:10 +000045 private ServiceRegistration coordinatorService;
46
Felix Meschberger0055b312010-10-13 07:39:32 +000047 public void start(BundleContext context) {
Felix Meschberger9e600592010-10-10 21:14:10 +000048 mgr = new CoordinationMgr();
49
Felix Meschberger0055b312010-10-13 07:39:32 +000050 try {
51 mbeanServerTracker = new MBeanServerTracker(context, mgr);
52 mbeanServerTracker.open();
53 } catch (MalformedObjectNameException e) {
54 // TODO log
55 }
56
Felix Meschberger9e600592010-10-10 21:14:10 +000057 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 Meschberger0055b312010-10-13 07:39:32 +000066 public void stop(BundleContext context) {
Felix Meschberger9e600592010-10-10 21:14:10 +000067 if (coordinatorService != null) {
68 coordinatorService.unregister();
69 coordinatorService = null;
70 }
71
Felix Meschberger0055b312010-10-13 07:39:32 +000072 if (mbeanServerTracker != null) {
73 mbeanServerTracker.close();
74 mbeanServerTracker = null;
75 }
76
Felix Meschberger9e600592010-10-10 21:14:10 +000077 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 Meschberger0055b312010-10-13 07:39:32 +000098
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 Meschberger9e600592010-10-10 21:14:10 +0000136}