You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by gn...@apache.org on 2017/04/03 16:01:00 UTC
svn commit: r1790017 - in /aries/trunk/blueprint/blueprint-core/src:
main/java/org/apache/aries/blueprint/ext/
test/java/org/apache/aries/blueprint/ test/resources/
Author: gnodet
Date: Mon Apr 3 16:01:00 2017
New Revision: 1790017
URL: http://svn.apache.org/viewvc?rev=1790017&view=rev
Log:
[ARIES-1559] Support injection of static values to bean properties or constructor's args
Added:
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/ExtPlaceholderTest.java
aries/trunk/blueprint/blueprint-core/src/test/resources/test-staticvalues.xml
Modified:
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ext/PropertyPlaceholder.java
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/TestBlueprintContainer.java
Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ext/PropertyPlaceholder.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ext/PropertyPlaceholder.java?rev=1790017&r1=1790016&r2=1790017&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ext/PropertyPlaceholder.java (original)
+++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ext/PropertyPlaceholder.java Mon Apr 3 16:01:00 2017
@@ -20,6 +20,7 @@ package org.apache.aries.blueprint.ext;
import java.io.IOException;
import java.io.InputStream;
+import java.lang.reflect.Field;
import java.net.URL;
import java.util.Dictionary;
import java.util.Enumeration;
@@ -27,7 +28,13 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
+import org.apache.aries.blueprint.ComponentDefinitionRegistry;
+import org.apache.aries.blueprint.PassThroughMetadata;
import org.apache.aries.blueprint.ext.evaluator.PropertyEvaluator;
+import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
+import org.osgi.framework.Bundle;
+import org.osgi.service.blueprint.container.BlueprintContainer;
+import org.osgi.service.blueprint.container.ComponentDefinitionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -52,6 +59,7 @@ public class PropertyPlaceholder extends
private boolean ignoreMissingLocations;
private SystemProperties systemProperties = SystemProperties.fallback;
private PropertyEvaluator evaluator = null;
+ private ExtendedBlueprintContainer container;
public Map getDefaultProperties() {
return defaultProperties;
@@ -118,6 +126,12 @@ public class PropertyPlaceholder extends
}
}
+ @Override
+ public void process(ComponentDefinitionRegistry registry) throws ComponentDefinitionException {
+ container = (ExtendedBlueprintContainer) ((PassThroughMetadata) registry.getComponentDefinition("blueprintContainer")).getObject();
+ super.process(registry);
+ }
+
protected String getProperty(String val) {
LOGGER.debug("Retrieving property {}", val);
Object v = null;
@@ -155,6 +169,37 @@ public class PropertyPlaceholder extends
if (val.startsWith("env:")) {
return System.getenv(val.substring("env:".length()));
}
+ if (val.startsWith("static:")) {
+ val = val.substring("static:".length());
+ int idx = val.indexOf('#');
+ if (idx <= 0 || idx == val.length() - 1) {
+ throw new IllegalArgumentException("Bad syntax: " + val);
+ }
+ String clazz = val.substring(0, idx);
+ String name = val.substring(idx + 1);
+ try {
+ Class cl = container.loadClass(clazz);
+ Field field = null;
+ try {
+ field = cl.getField(name);
+ } catch (NoSuchFieldException e) {
+ while (field == null && cl != null) {
+ try {
+ field = cl.getDeclaredField(name);
+ } catch (NoSuchFieldException t) {
+ cl = cl.getSuperclass();
+ }
+ }
+ }
+ if (field == null) {
+ throw new NoSuchFieldException(name);
+ }
+ Object obj = field.get(null);
+ return obj != null ? obj.toString() : null;
+ } catch (Throwable t) {
+ LOGGER.warn("Unable to retrieve static field: " + val + " (" + t + ")");
+ }
+ }
return System.getProperty(val);
}
Added: aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/ExtPlaceholderTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/ExtPlaceholderTest.java?rev=1790017&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/ExtPlaceholderTest.java (added)
+++ aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/ExtPlaceholderTest.java Mon Apr 3 16:01:00 2017
@@ -0,0 +1,41 @@
+/*
+ * 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;
+
+import org.apache.aries.blueprint.di.Repository;
+import org.apache.aries.blueprint.ext.impl.ExtNamespaceHandler;
+import org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl;
+import org.apache.aries.blueprint.pojos.BeanD;
+import org.junit.Test;
+
+public class ExtPlaceholderTest extends AbstractBlueprintTest {
+
+ @Test
+ public void testStaticValues() throws Exception {
+ ComponentDefinitionRegistryImpl registry = parse("/test-staticvalues.xml");
+ Repository repository = new TestBlueprintContainer(registry).getRepository();
+
+ Object obj1 = repository.create("beanD");
+ assertNotNull(obj1);
+ assertTrue(obj1 instanceof BeanD);
+ BeanD beanD = (BeanD) obj1;
+ assertEquals(ExtNamespaceHandler.BLUEPRINT_EXT_NAMESPACE_V1_5, beanD.getName());
+ }
+
+}
Modified: aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/TestBlueprintContainer.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/TestBlueprintContainer.java?rev=1790017&r1=1790016&r2=1790017&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/TestBlueprintContainer.java (original)
+++ aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/TestBlueprintContainer.java Mon Apr 3 16:01:00 2017
@@ -18,74 +18,50 @@
*/
package org.apache.aries.blueprint;
-import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
-import org.apache.aries.blueprint.container.AggregateConverter;
import org.apache.aries.blueprint.container.BlueprintContainerImpl;
import org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl;
-import org.apache.aries.blueprint.proxy.ProxyUtils;
-import org.apache.aries.blueprint.reflect.PassThroughMetadataImpl;
import org.apache.aries.proxy.ProxyManager;
import org.apache.aries.proxy.impl.JdkProxyManager;
-import org.osgi.service.blueprint.container.ComponentDefinitionException;
-import org.osgi.service.blueprint.container.Converter;
import org.osgi.service.blueprint.reflect.ComponentMetadata;
-import org.osgi.service.blueprint.reflect.RefMetadata;
import org.osgi.service.blueprint.reflect.Target;
public class TestBlueprintContainer extends BlueprintContainerImpl {
- private ComponentDefinitionRegistryImpl registry;
-
public TestBlueprintContainer(ComponentDefinitionRegistryImpl registry) throws Exception {
this(registry, new JdkProxyManager());
}
public TestBlueprintContainer(ComponentDefinitionRegistryImpl registry, ProxyManager proxyManager) throws Exception {
super(null, new TestBundleContext(), null, null, null, null, null, null, proxyManager, null);
- this.registry = registry;
- if (registry != null) {
- registry.registerComponentDefinition(new PassThroughMetadataImpl("blueprintContainer", this));
- registry.registerComponentDefinition(new PassThroughMetadataImpl("blueprintBundle", getBundleContext().getBundle()));
- registry.registerComponentDefinition(new PassThroughMetadataImpl("blueprintBundleContext", getBundleContext()));
- registry.registerComponentDefinition(new PassThroughMetadataImpl("blueprintConverter", getConverter()));
- processTypeConverters();
- }
- }
-
- protected void processTypeConverters() throws Exception {
- List<String> typeConverters = new ArrayList<String>();
- for (Target target : registry.getTypeConverters()) {
- if (target instanceof ComponentMetadata) {
- typeConverters.add(((ComponentMetadata) target).getId());
- } else if (target instanceof RefMetadata) {
- typeConverters.add(((RefMetadata) target).getComponentId());
- } else {
- throw new ComponentDefinitionException("Unexpected metadata for type converter: " + target);
- }
+ resetComponentDefinitionRegistry();
+
+ if (registry == null) {
+ return;
}
- Map<String, Object> objects = getRepository().createAll(typeConverters, ProxyUtils.asList(Converter.class));
- for (String name : typeConverters) {
- Object obj = objects.get(name);
- if (obj instanceof Converter) {
- ((AggregateConverter)getConverter()).registerConverter((Converter) obj);
- } else {
- throw new ComponentDefinitionException("Type converter " + obj + " does not implement the " + Converter.class.getName() + " interface");
+ List<Target> converters = registry.getTypeConverters();
+ for (Target converter : converters) {
+ getComponentDefinitionRegistry().registerTypeConverter(converter);
+ }
+ for (String name : registry.getComponentDefinitionNames()) {
+ ComponentMetadata comp = registry.getComponentDefinition(name);
+ if (!converters.contains(comp)) {
+ getComponentDefinitionRegistry().registerComponentDefinition(comp);
+ for (Interceptor interceptor : registry.getInterceptors(comp)) {
+ getComponentDefinitionRegistry().registerInterceptorWithComponent(comp, interceptor);
+ }
}
}
- }
+ processTypeConverters();
+ processProcessors();
+ }
+
@Override
public Class loadClass(String name) throws ClassNotFoundException {
return Thread.currentThread().getContextClassLoader().loadClass(name);
}
- @Override
- public ComponentDefinitionRegistryImpl getComponentDefinitionRegistry() {
- return registry;
- }
-
}
Added: aries/trunk/blueprint/blueprint-core/src/test/resources/test-staticvalues.xml
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/resources/test-staticvalues.xml?rev=1790017&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/test/resources/test-staticvalues.xml (added)
+++ aries/trunk/blueprint/blueprint-core/src/test/resources/test-staticvalues.xml Mon Apr 3 16:01:00 2017
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+ xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
+ default-availability="mandatory">
+
+ <ext:property-placeholder />
+
+ <bean id="beanD" class="org.apache.aries.blueprint.pojos.BeanD">
+ <property name="name" value="${static:org.apache.aries.blueprint.ext.impl.ExtNamespaceHandler#BLUEPRINT_EXT_NAMESPACE_V1_5}"/>
+ </bean>
+
+</blueprint>
\ No newline at end of file