blob: 7ba42b317baa458d9904bc0232f581708544f3cf [file] [log] [blame]
Carsten Ziegeler68b49a82015-02-16 12:33:06 +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.scr.impl.compat;
20
21
Carsten Ziegeler26b82ee2015-02-17 07:34:08 +000022import java.util.Dictionary;
Carsten Ziegeler68b49a82015-02-16 12:33:06 +000023import java.util.Hashtable;
24import java.util.Map;
25import java.util.concurrent.ConcurrentHashMap;
26
27import org.apache.felix.scr.ScrInfo;
28import org.apache.felix.scr.ScrService;
Carsten Ziegelerec816362015-07-29 09:50:17 +000029import org.osgi.framework.BundleActivator;
Carsten Ziegeler68b49a82015-02-16 12:33:06 +000030import org.osgi.framework.BundleContext;
31import org.osgi.framework.Constants;
32import org.osgi.framework.ServiceReference;
33import org.osgi.framework.ServiceRegistration;
34import org.osgi.service.component.runtime.ServiceComponentRuntime;
35import org.osgi.util.tracker.ServiceTracker;
36import org.osgi.util.tracker.ServiceTrackerCustomizer;
37
38
Carsten Ziegelerec816362015-07-29 09:50:17 +000039public class Activator implements BundleActivator
Carsten Ziegeler68b49a82015-02-16 12:33:06 +000040{
41 // tracker of the runtime service
42 private volatile ServiceTracker<ServiceComponentRuntime, ServiceComponentRuntime> runtimeTracker;
43
Carsten Ziegeler26b82ee2015-02-17 07:34:08 +000044 // tracker of the new ScrInfoservice
45 private volatile ServiceTracker<org.apache.felix.scr.info.ScrInfo, org.apache.felix.scr.info.ScrInfo> infoTracker;
46
Carsten Ziegeler68b49a82015-02-16 12:33:06 +000047 // the service registrations
48 private final Map<Long, ServiceRegistration<ScrService>> scrServiceRegMap = new ConcurrentHashMap<Long, ServiceRegistration<ScrService>>();
49
50 // the service registrations
51 private final Map<Long, ServiceRegistration<ScrInfo>> scrCommandRegMap = new ConcurrentHashMap<Long, ServiceRegistration<ScrInfo>>();
52
53 public void start( final BundleContext context ) throws Exception
54 {
55 this.runtimeTracker = new ServiceTracker<ServiceComponentRuntime, ServiceComponentRuntime>(context, ServiceComponentRuntime.class,
56 new ServiceTrackerCustomizer<ServiceComponentRuntime, ServiceComponentRuntime>()
57 {
58
59 public ServiceComponentRuntime addingService(
60 final ServiceReference<ServiceComponentRuntime> reference)
61 {
62 final ServiceComponentRuntime runtime = context.getService(reference);
63 if ( runtime != null )
64 {
Carsten Ziegeler26b82ee2015-02-17 07:34:08 +000065 final Dictionary<String, Object> props = new Hashtable<String, Object>();
Carsten Ziegeler68b49a82015-02-16 12:33:06 +000066 props.put(Constants.SERVICE_DESCRIPTION, "Apache Felix Compat ScrService");
67 props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
68
69 final ScrService service = new ScrServiceImpl(context, runtime);
70 scrServiceRegMap.put((Long)reference.getProperty(Constants.SERVICE_ID),
71 context.registerService(ScrService.class, service, props));
Carsten Ziegeler68b49a82015-02-16 12:33:06 +000072 }
73 return runtime;
74 }
75
76 public void modifiedService(
77 final ServiceReference<ServiceComponentRuntime> reference,
78 final ServiceComponentRuntime service) {
79 // nothing to do
80 }
81
82 public void removedService(
83 final ServiceReference<ServiceComponentRuntime> reference,
84 final ServiceComponentRuntime service) {
85 final ServiceRegistration<ScrService> reg = scrServiceRegMap.remove(reference.getProperty(Constants.SERVICE_ID));
86 if ( reg != null )
87 {
88 reg.unregister();
89 }
Carsten Ziegeler68b49a82015-02-16 12:33:06 +000090 context.ungetService(reference);
91 }
92 });
93 this.runtimeTracker.open();
Carsten Ziegeler26b82ee2015-02-17 07:34:08 +000094 this.infoTracker = new ServiceTracker<org.apache.felix.scr.info.ScrInfo, org.apache.felix.scr.info.ScrInfo>(context, org.apache.felix.scr.info.ScrInfo.class,
95 new ServiceTrackerCustomizer<org.apache.felix.scr.info.ScrInfo, org.apache.felix.scr.info.ScrInfo>()
96 {
97
98 public org.apache.felix.scr.info.ScrInfo addingService(
99 final ServiceReference<org.apache.felix.scr.info.ScrInfo> reference)
100 {
101 final org.apache.felix.scr.info.ScrInfo runtime = context.getService(reference);
102 if ( runtime != null )
103 {
104 final Dictionary<String, Object> props = new Hashtable<String, Object>();
105 props.put(Constants.SERVICE_DESCRIPTION, "Apache Felix Compat SCR Info service");
106 props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
107
108 final ScrInfo info = new ScrCommand(runtime);
109 scrCommandRegMap.put((Long)reference.getProperty(Constants.SERVICE_ID),
110 context.registerService(ScrInfo.class, info, props));
111 }
112 return runtime;
113 }
114
115 public void modifiedService(
116 final ServiceReference<org.apache.felix.scr.info.ScrInfo> reference,
117 final org.apache.felix.scr.info.ScrInfo service) {
118 // nothing to do
119 }
120
121 public void removedService(
122 final ServiceReference<org.apache.felix.scr.info.ScrInfo> reference,
123 final org.apache.felix.scr.info.ScrInfo service) {
124 final ServiceRegistration<ScrInfo> reg = scrCommandRegMap.remove(reference.getProperty(Constants.SERVICE_ID));
125 if ( reg != null )
126 {
127 reg.unregister();
128 }
129 context.ungetService(reference);
130 }
131 });
132 this.infoTracker.open();
Carsten Ziegeler68b49a82015-02-16 12:33:06 +0000133 }
134
135 public void stop(final BundleContext context) throws Exception
136 {
Carsten Ziegeler26b82ee2015-02-17 07:34:08 +0000137 if ( this.infoTracker != null )
138 {
139 this.infoTracker.close();
140 this.infoTracker = null;
141 }
142 if ( this.runtimeTracker != null )
143 {
144 this.runtimeTracker.close();
145 this.runtimeTracker = null;
146 }
Carsten Ziegeler68b49a82015-02-16 12:33:06 +0000147 }
148}