Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARIES-2046 Adding support for blacklisting namespace handlers #125

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

import java.net.URL;

import org.apache.aries.blueprint.NamespaceHandler;
import org.apache.felix.utils.extender.AbstractExtender;
import org.apache.felix.utils.extender.Extension;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -33,16 +34,23 @@
* @see SpringExtension
*/
public class Activator extends AbstractExtender {

public static final String SPRING_NAMESPACE_BLACKLIST_PID = "org.apache.karaf.blueprint.spring.namespace.handler.blacklist";
private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);

ServiceRegistration<NamespaceHandler> registration;
private Configuration blacklistedNamespaces;

@Override
public void start(BundleContext context) throws Exception {
ConfigurationAdmin configAdmin = context.getService(context.getServiceReference(ConfigurationAdmin.class));
blacklistedNamespaces = configAdmin.getConfiguration(SPRING_NAMESPACE_BLACKLIST_PID);
super.start(context);
}

@Override
protected Extension doCreateExtension(Bundle bundle) throws Exception {
URL handlers = bundle.getResource(SpringExtension.SPRING_HANDLERS);
if (handlers != null) {
return new SpringExtension(bundle);
return new SpringExtension(bundle, blacklistedNamespaces);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.*;

import org.apache.aries.blueprint.NamespaceHandler;
import org.apache.felix.utils.extender.Extension;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;

Expand All @@ -44,21 +42,30 @@ public class SpringExtension implements Extension {

public static final String SPRING_HANDLERS = "META-INF/spring.handlers";
public static final String SPRING_SCHEMAS = "META-INF/spring.schemas";
private static final Logger LOGGER = LoggerFactory.getLogger(SpringExtension.class);

private final Bundle bundle;
private final List<ServiceRegistration<NamespaceHandler>> registrations;
private final Configuration blacklistedNamespaces;

public SpringExtension(Bundle bundle) {
public SpringExtension(Bundle bundle, Configuration blacklistedNamespaces) {
this.bundle = bundle;
this.blacklistedNamespaces = blacklistedNamespaces;
this.registrations = new ArrayList<ServiceRegistration<NamespaceHandler>>();
}

@Override
public void start() throws Exception {
Dictionary<String, Object> nsHandlerBlacklist = blacklistedNamespaces.getProperties();
Map<String, NamespaceHandler> handlers = new HashMap<String, NamespaceHandler>();
Properties props = loadSpringHandlers();
Properties schemas = loadSpringSchemas();
for (String key : props.stringPropertyNames()) {
if (nsHandlerBlacklist != null && nsHandlerBlacklist.get(key) != null && props.get(key).equals(nsHandlerBlacklist.get(key))) {
LOGGER.info("Ignoring namespace handler for namespace: {}={}, bundle: {}:{}", key, props.get(key), bundle.getSymbolicName(), bundle.getBundleId());
continue;
}

String clazzName = props.getProperty(key);
org.springframework.beans.factory.xml.NamespaceHandler springHandler
= (org.springframework.beans.factory.xml.NamespaceHandler) bundle.loadClass(clazzName).newInstance();
Expand Down
14 changes: 14 additions & 0 deletions blueprint/itests/blueprint-itests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@
<version>${exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-cm</artifactId>
<version>${exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-junit4</artifactId>
Expand Down Expand Up @@ -347,6 +353,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
*/
package org.apache.aries.blueprint.itests;

import java.util.Arrays;
import java.util.List;

import org.apache.aries.blueprint.testbundles.BeanC;
import org.apache.aries.blueprint.spring.Activator;
import org.apache.aries.blueprint.testbundles.BeanCItf;
import org.junit.Test;
import org.ops4j.pax.exam.Option;
Expand All @@ -29,10 +30,9 @@
import org.springframework.beans.factory.NoSuchBeanDefinitionException;

import static org.apache.aries.blueprint.itests.Helper.mvnBundle;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.cm.ConfigurationAdminOptions.newConfiguration;

public class SpringTest extends AbstractBlueprintIntegrationTest {

Expand All @@ -57,11 +57,25 @@ public void testSpringBundle() throws Exception {
}
}

@Test
public void testSpringNsHandlerBlacklist() throws Exception {
Bundle bundles = context().getBundleByName("org.apache.aries.blueprint.testbundles");
assertNotNull(bundles);
bundles.start();

Thread.sleep(500); //allow registration to happen

//Asserting NS blacklisting
assertEquals(1, Arrays.stream(bundles.getRegisteredServices()).filter(sr -> sr.getProperty("osgi.service.blueprint.namespace") != null).count());
assertTrue(Arrays.stream(bundles.getRegisteredServices()).anyMatch(sr -> sr.getProperty("osgi.service.blueprint.namespace").equals("http://www.springframework.org/schema/good")));
}

@org.ops4j.pax.exam.Configuration
public Option[] configuration() {
return new Option[] {
baseOptions(),
Helper.blueprintBundles(),
newConfiguration(Activator.SPRING_NAMESPACE_BLACKLIST_PID).put("http://www.springframework.org/schema/bad", "org.apache.aries.blueprint.testbundles.TestNamespaceHandler").asOption(),
// Blueprint spring
mvnBundle("org.apache.aries.blueprint", "org.apache.aries.blueprint.spring"),
// Spring
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.aries.blueprint.testbundles;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.xml.NamespaceHandler;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class TestNamespaceHandler implements NamespaceHandler {
@Override
public void init() {

}

@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
return null;
}

@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder beanDefinitionHolder, ParserContext parserContext) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
################################################################################
#
# 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.
#
################################################################################

http\://www.springframework.org/schema/bad=org.apache.aries.blueprint.testbundles.TestNamespaceHandler
http\://www.springframework.org/schema/good=org.apache.aries.blueprint.testbundles.TestNamespaceHandler
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
################################################################################
#
# 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.
#
################################################################################

http\://www.springframework.org/schema/bad.xsd=/does/not/exist
http\://www.springframework.org/schema/good.xsd=/does/not/exist