Marcel Offermans | 2f6e82b | 2011-04-19 07:19:58 +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 | */ |
Marcel Offermans | 90ba4fa | 2011-04-27 07:57:44 +0000 | [diff] [blame^] | 19 | package org.apache.felix.dm.impl.index; |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 20 | |
Marcel Offermans | 5c4343a | 2011-04-19 09:50:24 +0000 | [diff] [blame] | 21 | import java.util.Arrays; |
| 22 | import java.util.Iterator; |
| 23 | import java.util.List; |
| 24 | |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 25 | import org.osgi.framework.BundleContext; |
Marcel Offermans | 5c4343a | 2011-04-19 09:50:24 +0000 | [diff] [blame] | 26 | import org.osgi.framework.Constants; |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 27 | import org.osgi.framework.InvalidSyntaxException; |
| 28 | import org.osgi.framework.ServiceEvent; |
| 29 | import org.osgi.framework.ServiceListener; |
| 30 | import org.osgi.framework.ServiceReference; |
| 31 | |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame] | 32 | /** |
| 33 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 34 | */ |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 35 | public class BundleContextInterceptor extends BundleContextInterceptorBase { |
| 36 | private final ServiceRegistryCache m_cache; |
| 37 | |
| 38 | public BundleContextInterceptor(ServiceRegistryCache cache, BundleContext context) { |
| 39 | super(context); |
| 40 | m_cache = cache; |
| 41 | } |
| 42 | |
| 43 | public void addServiceListener(ServiceListener listener, String filter) throws InvalidSyntaxException { |
| 44 | FilterIndex filterIndex = m_cache.hasFilterIndexFor(null, filter); |
| 45 | if (filterIndex != null) { |
| 46 | filterIndex.addServiceListener(listener, filter); |
| 47 | } |
| 48 | else { |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame] | 49 | // System.out.println("BCI:Listener " + listener.getClass().getName() + " filter " + filter); |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 50 | m_context.addServiceListener(listener, filter); |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | public void addServiceListener(ServiceListener listener) { |
| 55 | FilterIndex filterIndex = m_cache.hasFilterIndexFor(null, null); |
| 56 | if (filterIndex != null) { |
| 57 | filterIndex.addServiceListener(listener, null); |
| 58 | } |
| 59 | else { |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame] | 60 | // System.out.println("BCI:Listener " + listener.getClass().getName() + " without filter"); |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 61 | m_context.addServiceListener(listener); |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | public void removeServiceListener(ServiceListener listener) { |
| 66 | FilterIndex filterIndex = m_cache.hasFilterIndexFor(null, null); |
| 67 | if (filterIndex != null) { |
| 68 | filterIndex.removeServiceListener(listener); |
| 69 | } |
| 70 | else { |
| 71 | m_context.removeServiceListener(listener); |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException { |
| 76 | // first we ask the cache if there is an index for our request (class and filter combination) |
| 77 | FilterIndex filterIndex = m_cache.hasFilterIndexFor(clazz, filter); |
| 78 | if (filterIndex != null) { |
Marcel Offermans | 5c4343a | 2011-04-19 09:50:24 +0000 | [diff] [blame] | 79 | List /* <ServiceReference> */ result = filterIndex.getAllServiceReferences(clazz, filter); |
| 80 | Iterator iterator = result.iterator(); |
| 81 | while (iterator.hasNext()) { |
| 82 | ServiceReference reference = (ServiceReference) iterator.next(); |
| 83 | String[] list = (String[]) reference.getProperty(Constants.OBJECTCLASS); |
| 84 | for (int i = 0; i < list.length; i++) { |
| 85 | if (!reference.isAssignableTo(m_context.getBundle(), list[i])) { |
| 86 | iterator.remove(); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | return (ServiceReference[]) result.toArray(new ServiceReference[result.size()]); |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 92 | } |
| 93 | else { |
| 94 | // if they don't know, we ask the real bundle context instead |
| 95 | return m_context.getServiceReferences(clazz, filter); |
| 96 | } |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | public ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException { |
Marcel Offermans | 5c4343a | 2011-04-19 09:50:24 +0000 | [diff] [blame] | 100 | // first we ask the cache if there is an index for our request (class and filter combination) |
| 101 | FilterIndex filterIndex = m_cache.hasFilterIndexFor(clazz, filter); |
| 102 | if (filterIndex != null) { |
| 103 | List /* <ServiceReference> */ result = filterIndex.getAllServiceReferences(clazz, filter); |
| 104 | return (ServiceReference[]) result.toArray(new ServiceReference[result.size()]); |
| 105 | } |
| 106 | else { |
| 107 | // if they don't know, we ask the real bundle context instead |
| 108 | return m_context.getAllServiceReferences(clazz, filter); |
| 109 | } |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | public ServiceReference getServiceReference(String clazz) { |
Marcel Offermans | 5c4343a | 2011-04-19 09:50:24 +0000 | [diff] [blame] | 113 | ServiceReference[] references; |
| 114 | try { |
| 115 | references = getServiceReferences(clazz, null); |
Marcel Offermans | fd7deb0 | 2011-04-20 11:26:09 +0000 | [diff] [blame] | 116 | if (references == null || references.length == 0) { |
| 117 | return null; |
| 118 | } |
Marcel Offermans | 5c4343a | 2011-04-19 09:50:24 +0000 | [diff] [blame] | 119 | Arrays.sort(references); |
| 120 | return references[references.length - 1]; |
| 121 | } |
| 122 | catch (InvalidSyntaxException e) { |
| 123 | throw new Error("Invalid filter syntax thrown for null filter.", e); |
| 124 | } |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | public void serviceChanged(ServiceEvent event) { |
| 128 | m_cache.serviceChangedForFilterIndices(event); |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 129 | } |
| 130 | } |