diff --git a/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/index/ServiceRegistryCache.java b/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/index/ServiceRegistryCache.java index 2fdc1f89b0..27eb8308a2 100644 --- a/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/index/ServiceRegistryCache.java +++ b/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/index/ServiceRegistryCache.java @@ -54,8 +54,9 @@ public void close() { } public void addFilterIndex(FilterIndex index) { - m_filterIndexList.add(index); + // only add the index once opened: an index which fails to open must not be used index.open(m_filterIndexBundleContext); + m_filterIndexList.add(index); } public void removeFilterIndex(FilterIndex index) { diff --git a/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/index/ServiceRegistryCacheManager.java b/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/index/ServiceRegistryCacheManager.java index 37b889883e..0435385c6e 100644 --- a/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/index/ServiceRegistryCacheManager.java +++ b/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/index/ServiceRegistryCacheManager.java @@ -261,49 +261,60 @@ public static void stopped() { m_stopped = true; } + /** + * Creates the indices specified in the org.apache.felix.dependencymanager.filterindex property. + * The cache is only an optimization: an index which can't be created is logged and ignored, + * and the other indices are still created. + */ private static void resetIndices(String index) { + if (index == null) { + return; + } + ServiceRegistryCache cache; try { - if (index != null) { - ServiceRegistryCache cache = createCache(m_context); // may already exist, in case the Activator has called back our registerFilterIndex method - - String[] props = index.split(";"); - for (int i = 0; i < props.length; i++) { - // Check if a classname is specified for the current index. - // (syntax is "full_class_name:index") - int colon = props[i].indexOf(":"); - if (colon != -1) { - String className = props[i].substring(0, colon).trim(); - String indexDefinition = props[i].substring(colon + 1); - cache.addFilterIndex(createCustomIndex(className, indexDefinition)); - } else if (props[i].equals("*aspect*")) { - cache.addFilterIndex(new AspectFilterIndex()); - } else if (props[i].equals("*adapter*")) { - cache.addFilterIndex(new AdapterFilterIndex()); - } else { - cache.addFilterIndex(new MultiPropertyFilterIndex(props[i])); - } - } - } + cache = createCache(m_context); // may already exist, in case the Activator has called back our registerFilterIndex method + } catch (Exception e) { + log("Could not create the service registry cache, it won't be used", e); + return; } + String[] props = index.split(";"); + for (int i = 0; i < props.length; i++) { + try { + cache.addFilterIndex(createIndex(props[i])); + } catch (Exception | LinkageError e) { + log("Could not create the filter index \"" + props[i] + "\", it won't be used", e); + } + } + } - catch (Exception e) { - e.printStackTrace(); - } + private static FilterIndex createIndex(String prop) throws Exception { + // Check if a classname is specified for the current index. + // (syntax is "full_class_name:index") + int colon = prop.indexOf(":"); + if (colon != -1) { + String className = prop.substring(0, colon).trim(); + String indexDefinition = prop.substring(colon + 1); + return createCustomIndex(className, indexDefinition); + } else if (prop.equals("*aspect*")) { + return new AspectFilterIndex(); + } else if (prop.equals("*adapter*")) { + return new AdapterFilterIndex(); + } else { + return new MultiPropertyFilterIndex(prop); + } } - + /** * Creates a custom index using its classname, that has been specified in the org.apache.felix.dependencymanager.filterindex system property. */ - private static FilterIndex createCustomIndex(String className, String indexDefinition) { - try { - Class customIndexClass = Class.forName(className); - Constructor ctor = customIndexClass.getConstructor(String.class); - FilterIndex index = (FilterIndex) ctor.newInstance(indexDefinition); - return index; - } catch (Exception e) { - e.printStackTrace(); - return null; - } + private static FilterIndex createCustomIndex(String className, String indexDefinition) throws Exception { + Class customIndexClass = Class.forName(className); + Constructor ctor = customIndexClass.getConstructor(String.class); + return (FilterIndex) ctor.newInstance(indexDefinition); + } + + private static void log(String msg, Throwable error) { + new Logger(m_context).log(Logger.LOG_ERROR, msg, error); } /** diff --git a/dependencymanager/org.apache.felix.dependencymanager/test/org/apache/felix/dm/impl/index/FELIX6889_InvalidFilterIndexTest.java b/dependencymanager/org.apache.felix.dependencymanager/test/org/apache/felix/dm/impl/index/FELIX6889_InvalidFilterIndexTest.java new file mode 100644 index 0000000000..0b5ef96c55 --- /dev/null +++ b/dependencymanager/org.apache.felix.dependencymanager/test/org/apache/felix/dm/impl/index/FELIX6889_InvalidFilterIndexTest.java @@ -0,0 +1,154 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.dm.impl.index; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.lang.reflect.Field; +import java.util.Collections; +import java.util.List; + +import org.apache.felix.dm.DependencyManager; +import org.apache.felix.dm.FilterIndex; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceEvent; +import org.osgi.framework.ServiceListener; +import org.osgi.framework.ServiceReference; + +/** + * Tests that a filter index configured in the org.apache.felix.dependencymanager.filterindex + * property, but whose class can't be loaded, does not break the service registry cache: the + * cache is an optional optimization, so the invalid index must be ignored. + */ +public class FELIX6889_InvalidFilterIndexTest { + + /** + * A valid custom index, created by the cache manager using its (String) constructor. + */ + public static class TestIndex implements FilterIndex { + public TestIndex(String definition) { + } + + @Override + public void open(BundleContext context) { + } + + @Override + public void close() { + } + + @Override + public boolean isApplicable(String clazz, String filter) { + return "test.Applicable".equals(clazz); + } + + @Override + public List getAllServiceReferences(String clazz, String filter) { + return Collections.emptyList(); + } + + @Override + public void serviceChanged(ServiceEvent event) { + } + + @Override + public void addServiceListener(ServiceListener listener, String filter) { + } + + @Override + public void removeServiceListener(ServiceListener listener) { + } + } + + @Before + public void setUp() throws Exception { + resetManager(); + } + + @After + public void tearDown() throws Exception { + resetManager(); + } + + /** + * The manager is static and only initialized once: reset it between the tests. + */ + private static void resetManager() throws Exception { + set("m_cache", null); + set("m_context", null); + set("m_activatingThread", null); + } + + private static void set(String name, Object value) throws Exception { + Field f = ServiceRegistryCacheManager.class.getDeclaredField(name); + f.setAccessible(true); + f.set(null, value); + } + + private static BundleContext context(String indices) { + BundleContext context = mock(BundleContext.class); + when(context.getProperty(DependencyManager.SERVICEREGISTRY_CACHE_INDICES)).thenReturn(indices); + return context; + } + + @Test + public void testUnknownIndexClassIsIgnored() throws Exception { + ServiceRegistryCacheManager.init(context("no.such.IndexClass:some.definition")); + ServiceRegistryCacheManager.activated(); // like the Activator, once started + { + ServiceRegistryCache cache = ServiceRegistryCacheManager.getCache(null); + assertNotNull(cache); + + // no null index must be in the cache + for (FilterIndex index : cache.getFilterIndices()) { + assertNotNull("a null index has been added to the cache", index); + } + + // the lookups done on each dependency resolution must not fail + assertNull(cache.hasFilterIndexFor("test.Applicable", null)); + cache.serviceChangedForFilterIndices(new ServiceEvent(ServiceEvent.REGISTERED, mock(ServiceReference.class))); + } + } + + @Test + public void testIndexesAfterUnknownIndexClassAreStillCreated() throws Exception { + String indices = "no.such.IndexClass:some.definition;" + TestIndex.class.getName() + ":def"; + ServiceRegistryCacheManager.init(context(indices)); + ServiceRegistryCacheManager.activated(); // like the Activator, once started + { + ServiceRegistryCache cache = ServiceRegistryCacheManager.getCache(null); + assertNotNull(cache); + + assertEquals("only the valid index must be registered", 1, cache.getSize()); + FilterIndex index = cache.hasFilterIndexFor("test.Applicable", null); + assertNotNull("the index following the invalid one has not been created", index); + assertTrue(index instanceof TestIndex); + assertFalse(cache.getFilterIndices().contains(null)); + } + } +}