blob: 8e2e8e8d151485b747e07e8b7ab65feff53c9cbc [file] [log] [blame]
Marcel Offermans2f6e82b2011-04-19 07:19:58 +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 */
Marcel Offermans90ba4fa2011-04-27 07:57:44 +000019package org.apache.felix.dm.impl.index;
Marcel Offermans227dd712011-04-19 07:14:22 +000020
Marcel Offermans5c4343a2011-04-19 09:50:24 +000021import java.util.Arrays;
22import java.util.Iterator;
23import java.util.List;
24
Marcel Offermans227dd712011-04-19 07:14:22 +000025import org.osgi.framework.BundleContext;
Marcel Offermans5c4343a2011-04-19 09:50:24 +000026import org.osgi.framework.Constants;
Marcel Offermans227dd712011-04-19 07:14:22 +000027import org.osgi.framework.InvalidSyntaxException;
28import org.osgi.framework.ServiceEvent;
29import org.osgi.framework.ServiceListener;
30import org.osgi.framework.ServiceReference;
31
Marcel Offermans5be5f142011-04-26 10:47:12 +000032/**
33 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
34 */
Marcel Offermans227dd712011-04-19 07:14:22 +000035public 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 Offermans5be5f142011-04-26 10:47:12 +000049// System.out.println("BCI:Listener " + listener.getClass().getName() + " filter " + filter);
Marcel Offermans227dd712011-04-19 07:14:22 +000050 m_context.addServiceListener(listener, filter);
Marcel Offermans227dd712011-04-19 07:14:22 +000051 }
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 Offermans5be5f142011-04-26 10:47:12 +000060// System.out.println("BCI:Listener " + listener.getClass().getName() + " without filter");
Marcel Offermans227dd712011-04-19 07:14:22 +000061 m_context.addServiceListener(listener);
Marcel Offermans227dd712011-04-19 07:14:22 +000062 }
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 Offermans227dd712011-04-19 07:14:22 +000072 }
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 Offermans5c4343a2011-04-19 09:50:24 +000079 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 Offermans227dd712011-04-19 07:14:22 +000092 }
93 else {
94 // if they don't know, we ask the real bundle context instead
95 return m_context.getServiceReferences(clazz, filter);
96 }
Marcel Offermans227dd712011-04-19 07:14:22 +000097 }
98
99 public ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException {
Marcel Offermans5c4343a2011-04-19 09:50:24 +0000100 // 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 Offermans227dd712011-04-19 07:14:22 +0000110 }
111
112 public ServiceReference getServiceReference(String clazz) {
Marcel Offermans5c4343a2011-04-19 09:50:24 +0000113 ServiceReference[] references;
114 try {
115 references = getServiceReferences(clazz, null);
Marcel Offermansfd7deb02011-04-20 11:26:09 +0000116 if (references == null || references.length == 0) {
117 return null;
118 }
Marcel Offermans5c4343a2011-04-19 09:50:24 +0000119 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 Offermans227dd712011-04-19 07:14:22 +0000125 }
126
127 public void serviceChanged(ServiceEvent event) {
128 m_cache.serviceChangedForFilterIndices(event);
Marcel Offermans227dd712011-04-19 07:14:22 +0000129 }
130}