Carsten Ziegeler | 68b49a8 | 2015-02-16 12:33:06 +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.scr.impl.compat; |
| 20 | |
| 21 | |
Carsten Ziegeler | 26b82ee | 2015-02-17 07:34:08 +0000 | [diff] [blame] | 22 | import java.util.Dictionary; |
Carsten Ziegeler | 68b49a8 | 2015-02-16 12:33:06 +0000 | [diff] [blame] | 23 | import java.util.Hashtable; |
| 24 | import java.util.Map; |
| 25 | import java.util.concurrent.ConcurrentHashMap; |
| 26 | |
| 27 | import org.apache.felix.scr.ScrInfo; |
| 28 | import org.apache.felix.scr.ScrService; |
Carsten Ziegeler | ec81636 | 2015-07-29 09:50:17 +0000 | [diff] [blame] | 29 | import org.osgi.framework.BundleActivator; |
Carsten Ziegeler | 68b49a8 | 2015-02-16 12:33:06 +0000 | [diff] [blame] | 30 | import org.osgi.framework.BundleContext; |
| 31 | import org.osgi.framework.Constants; |
| 32 | import org.osgi.framework.ServiceReference; |
| 33 | import org.osgi.framework.ServiceRegistration; |
| 34 | import org.osgi.service.component.runtime.ServiceComponentRuntime; |
| 35 | import org.osgi.util.tracker.ServiceTracker; |
| 36 | import org.osgi.util.tracker.ServiceTrackerCustomizer; |
| 37 | |
| 38 | |
Carsten Ziegeler | ec81636 | 2015-07-29 09:50:17 +0000 | [diff] [blame] | 39 | public class Activator implements BundleActivator |
Carsten Ziegeler | 68b49a8 | 2015-02-16 12:33:06 +0000 | [diff] [blame] | 40 | { |
| 41 | // tracker of the runtime service |
| 42 | private volatile ServiceTracker<ServiceComponentRuntime, ServiceComponentRuntime> runtimeTracker; |
| 43 | |
Carsten Ziegeler | 26b82ee | 2015-02-17 07:34:08 +0000 | [diff] [blame] | 44 | // tracker of the new ScrInfoservice |
| 45 | private volatile ServiceTracker<org.apache.felix.scr.info.ScrInfo, org.apache.felix.scr.info.ScrInfo> infoTracker; |
| 46 | |
Carsten Ziegeler | 68b49a8 | 2015-02-16 12:33:06 +0000 | [diff] [blame] | 47 | // 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 Ziegeler | 26b82ee | 2015-02-17 07:34:08 +0000 | [diff] [blame] | 65 | final Dictionary<String, Object> props = new Hashtable<String, Object>(); |
Carsten Ziegeler | 68b49a8 | 2015-02-16 12:33:06 +0000 | [diff] [blame] | 66 | 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 Ziegeler | 68b49a8 | 2015-02-16 12:33:06 +0000 | [diff] [blame] | 72 | } |
| 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 Ziegeler | 68b49a8 | 2015-02-16 12:33:06 +0000 | [diff] [blame] | 90 | context.ungetService(reference); |
| 91 | } |
| 92 | }); |
| 93 | this.runtimeTracker.open(); |
Carsten Ziegeler | 26b82ee | 2015-02-17 07:34:08 +0000 | [diff] [blame] | 94 | 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 Ziegeler | 68b49a8 | 2015-02-16 12:33:06 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | public void stop(final BundleContext context) throws Exception |
| 136 | { |
Carsten Ziegeler | 26b82ee | 2015-02-17 07:34:08 +0000 | [diff] [blame] | 137 | 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 Ziegeler | 68b49a8 | 2015-02-16 12:33:06 +0000 | [diff] [blame] | 147 | } |
| 148 | } |