Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/Phase_Reference.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/Phase_Reference.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/Phase_Reference.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/Phase_Reference.java Thu May 4 18:41:59 2017 @@ -1,38 +1,32 @@ +/** + * Licensed 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.cdi.container.internal.container; -import java.util.Collection; -import java.util.List; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; -import org.apache.aries.cdi.container.internal.literal.ReferenceLiteral; -import org.apache.aries.cdi.container.internal.model.ReferenceInjectionPoint; -import org.apache.aries.cdi.container.internal.model.ReferenceModel; -import org.jboss.weld.bootstrap.api.Bootstrap; -import org.jboss.weld.manager.BeanManagerImpl; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; import org.osgi.framework.Filter; -import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; import org.osgi.service.cdi.CdiEvent; import org.osgi.util.tracker.ServiceTracker; import org.osgi.util.tracker.ServiceTrackerCustomizer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class Phase_Reference implements Phase { - public Phase_Reference( - List<ReferenceDependency> references, List<ServiceDeclaration> services, CdiContainerState cdiContainerState, - Bootstrap bootstrap) { - - _references = references; - _services = services; - _cdiContainerState = cdiContainerState; - _bootstrap = bootstrap; - _bundle = _cdiContainerState.getBundle(); - _bundleContext = _bundle.getBundleContext(); + public Phase_Reference(BootstrapContainer bc) { + _bc = bc; } @Override @@ -60,14 +54,12 @@ public class Phase_Reference implements @Override public void open() { - processDescriptorReferences((BeanManagerImpl)_cdiContainerState.getBeanManager()); - - if (!_references.isEmpty()) { - Filter filter = FilterBuilder.createReferenceFilter(_references); + if (_bc.hasReferences()) { + Filter filter = FilterBuilder.createReferenceFilter(_bc.getReferences()); - _cdiContainerState.fire(CdiEvent.Type.WAITING_FOR_SERVICES, filter.toString()); + _bc.fire(CdiEvent.Type.WAITING_FOR_SERVICES, filter.toString()); - _serviceTracker = new ServiceTracker<>(_bundleContext, filter, new ReferencePhaseCustomizer(_bootstrap)); + _serviceTracker = new ServiceTracker<>(_bc.getBundleContext(), filter, new ReferencePhaseCustomizer()); _serviceTracker.open(); } @@ -76,7 +68,7 @@ public class Phase_Reference implements try { if ((_nextPhase == null) && dependenciesAreEmptyOrAllOptional()) { - _nextPhase = new Phase_Publish(_references, _services, _cdiContainerState, _bootstrap); + _nextPhase = new Phase_Publish(_bc); _nextPhase.open(); } @@ -87,11 +79,11 @@ public class Phase_Reference implements } private boolean dependenciesAreEmptyOrAllOptional() { - if (_references.isEmpty()) { + if (!_bc.hasReferences()) { return true; } - for (ReferenceDependency referenceDependency : _references) { + for (ReferenceDependency referenceDependency : _bc.getReferences()) { if (referenceDependency.getMinCardinality() > 0) { return false; } @@ -100,53 +92,14 @@ public class Phase_Reference implements return true; } - private void processDescriptorReferences(BeanManagerImpl beanManagerImpl) { - Collection<ReferenceModel> referenceModels = _cdiContainerState.getBeansModel().getReferenceModels(); - - for (ReferenceModel referenceModel : referenceModels) { - processReferenceModel(referenceModel, beanManagerImpl); - } - } - - private void processReferenceModel(ReferenceModel referenceModel, BeanManagerImpl beanManagerImpl) { - try { - Class<?> beanClass = _bundle.loadClass(referenceModel.getBeanClass()); - - ReferenceDependency referenceDependency = new ReferenceDependency( - beanManagerImpl, ReferenceLiteral.fromTarget(referenceModel.getTarget()), - new ReferenceInjectionPoint(beanClass, referenceModel.getTarget())); - - _references.add(referenceDependency); - } - catch (ClassNotFoundException cnfe) { - _log.error( - "CDIe - osgi bean descriptor reference processing cannot load class {}", - referenceModel.getBeanClass(), cnfe); - } - catch (InvalidSyntaxException ise) { - _log.error("CDIe - osgi bean descriptor reference processing error", ise); - } - } - - private static final Logger _log = LoggerFactory.getLogger(Phase_Reference.class); - - private final Bootstrap _bootstrap; - private final Bundle _bundle; - private final BundleContext _bundleContext; - private final CdiContainerState _cdiContainerState; + private final BootstrapContainer _bc; private final Lock _lock = new ReentrantLock(true); private Phase _nextPhase; - private final List<ReferenceDependency> _references; - private final List<ServiceDeclaration> _services; ServiceTracker<?, ?> _serviceTracker; private class ReferencePhaseCustomizer implements ServiceTrackerCustomizer<Object, Object> { - public ReferencePhaseCustomizer(Bootstrap bootstrap) { - _bootstrap = bootstrap; - } - @Override public Object addingService(ServiceReference<Object> reference) { _lock.lock(); @@ -159,7 +112,7 @@ public class Phase_Reference implements boolean matches = false; boolean resolved = true; - for (ReferenceDependency referenceDependency : _references) { + for (ReferenceDependency referenceDependency : _bc.getReferences()) { if (referenceDependency.matches(reference)) { referenceDependency.resolve(reference); matches = true; @@ -174,7 +127,7 @@ public class Phase_Reference implements } if (resolved) { - _nextPhase = new Phase_Publish(_references, _services, _cdiContainerState, _bootstrap); + _nextPhase = new Phase_Publish(_bc); _nextPhase.open(); } @@ -200,10 +153,10 @@ public class Phase_Reference implements _nextPhase = null; - _cdiContainerState.fire(CdiEvent.Type.WAITING_FOR_SERVICES); + _bc.fire(CdiEvent.Type.WAITING_FOR_SERVICES); } - for (ReferenceDependency referenceDependency : _references) { + for (ReferenceDependency referenceDependency : _bc.getReferences()) { if (referenceDependency.matches(reference)) { referenceDependency.unresolve(reference); } @@ -214,8 +167,6 @@ public class Phase_Reference implements } } - private final Bootstrap _bootstrap; - } }
Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/ReferenceDependency.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/ReferenceDependency.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/ReferenceDependency.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/ReferenceDependency.java Thu May 4 18:41:59 2017 @@ -28,6 +28,7 @@ import java.util.concurrent.ConcurrentSk import javax.enterprise.inject.Instance; import javax.enterprise.inject.spi.InjectionPoint; +import org.apache.aries.cdi.container.internal.util.Conversions; import org.apache.aries.cdi.container.internal.util.Maps; import org.jboss.weld.manager.BeanManagerImpl; import org.osgi.framework.Constants; @@ -35,12 +36,8 @@ import org.osgi.framework.Filter; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; -import org.osgi.service.cdi.annotations.MinCardinality; import org.osgi.service.cdi.annotations.Reference; -import org.osgi.service.cdi.annotations.ReferenceFilter; import org.osgi.service.cdi.annotations.ReferenceScope; -import org.osgi.util.converter.Converter; -import org.osgi.util.converter.StandardConverter; import org.osgi.util.converter.TypeReference; public class ReferenceDependency { @@ -132,14 +129,6 @@ public class ReferenceDependency { } private String buildFilter(Class<?> serviceType, Set<Annotation> qualifiers) throws InvalidSyntaxException { - String targetFilter = _reference.target(); - - int targetFilterLength = targetFilter.length(); - - if (targetFilterLength > 0) { - FrameworkUtil.createFilter(targetFilter); - } - StringBuilder sb = new StringBuilder(); sb.append("(&("); @@ -165,16 +154,25 @@ public class ReferenceDependency { sb.append(")"); } - if ((targetFilterLength > 0)) { + String targetFilter = _reference.target(); + + int targetFilterLength = targetFilter.length(); + + if (targetFilterLength > 0) { + FrameworkUtil.createFilter(targetFilter); + sb.append(targetFilter); } for (Annotation qualifier : qualifiers) { - if (qualifier.annotationType().isAnnotationPresent(ReferenceFilter.class)) { - Map<String, String> map = _converter.convert(qualifier).sourceAs(qualifier.annotationType()).to(_mapType); - - Maps.appendFilter(sb, map); + Class<? extends Annotation> annotationType = qualifier.annotationType(); + if (annotationType.equals(Reference.class)) { + continue; } + + Map<String, String> map = Conversions.c().convert(qualifier).sourceAs(qualifier.annotationType()).to(_mapType); + + Maps.appendFilter(sb, map); } sb.append(")"); @@ -230,11 +228,7 @@ public class ReferenceDependency { int value = 1; if (_instance) { - MinCardinality minCardinality = injectionPoint.getAnnotated().getAnnotation(MinCardinality.class); - - if ((minCardinality != null) && (minCardinality.value() >= 0)) { - value = minCardinality.value(); - } + value = 0; } return value; @@ -299,8 +293,6 @@ public class ReferenceDependency { return cast(first); } - private static final Converter _converter = new StandardConverter(); - private static final TypeReference<Map<String, String>> _mapType = new TypeReference<Map<String, String>>(){}; private final BeanManagerImpl _beanManagerImpl; Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/ServiceDeclaration.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/ServiceDeclaration.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/ServiceDeclaration.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/ServiceDeclaration.java Thu May 4 18:41:59 2017 @@ -21,20 +21,20 @@ import java.util.Dictionary; import java.util.Hashtable; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.inject.spi.Bean; import javax.inject.Singleton; +import org.apache.aries.cdi.container.internal.util.Conversions; +import org.apache.aries.cdi.container.internal.util.Maps; import org.osgi.framework.Bundle; import org.osgi.framework.PrototypeServiceFactory; import org.osgi.framework.ServiceFactory; import org.osgi.framework.ServiceRegistration; import org.osgi.service.cdi.annotations.Service; -import org.osgi.service.cdi.annotations.ServiceProperty; -import org.osgi.util.converter.Converter; -import org.osgi.util.converter.StandardConverter; import org.osgi.util.converter.TypeReference; public class ServiceDeclaration { @@ -49,17 +49,16 @@ public class ServiceDeclaration { for (Object object : bean.getQualifiers()) { Annotation annotation = (Annotation)object; - Map<String, Object> map = _converter.convert(annotation).sourceAs(annotation.annotationType()).to(_mapType); + Map<String, Object> map = Conversions.c().convert( + annotation).sourceAs(annotation.annotationType()).to(_mapType); for (Map.Entry<String, Object> entry : map.entrySet()) { properties.put(entry.getKey(), entry.getValue()); } } - for (ServiceProperty serviceProperty : _service.properties()) { - Object object = getValue(serviceProperty); - - properties.put(serviceProperty.key(), object); + for (Entry<String, Object> entry : Maps.map(_service.property()).entrySet()) { + properties.put(entry.getKey(), entry.getValue()); } _properties = properties; @@ -113,14 +112,6 @@ public class ServiceDeclaration { return new PrototypeScopeWrapper(); } - Object getValue(ServiceProperty serviceProperty) { - Type type = serviceProperty.type().getType(); - String[] value = serviceProperty.value(); - return _converter.convert(value).to(type); - } - - private static final Converter _converter = new StandardConverter(); - private static final TypeReference<Map<String, Object>> _mapType = new TypeReference<Map<String, Object>>(){}; @SuppressWarnings("rawtypes") Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/AnyLiteral.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/AnyLiteral.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/AnyLiteral.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/AnyLiteral.java Thu May 4 18:41:59 2017 @@ -1,3 +1,17 @@ +/** + * Licensed 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.cdi.container.internal.literal; import javax.enterprise.inject.Any; Added: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/ConfigurationLiteral.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/ConfigurationLiteral.java?rev=1793847&view=auto ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/ConfigurationLiteral.java (added) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/ConfigurationLiteral.java Thu May 4 18:41:59 2017 @@ -0,0 +1,52 @@ +/** + * Licensed 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.cdi.container.internal.literal; + +import javax.enterprise.util.AnnotationLiteral; + +import org.osgi.service.cdi.annotations.Configuration; + +public class ConfigurationLiteral extends AnnotationLiteral<Configuration> implements Configuration { + + private static final long serialVersionUID = 1L; + + /** + * @param pids an array of configuration pids + * @return a literal instance of {@link Configuration} + */ + public static ConfigurationLiteral from(String[] pids) { + return new ConfigurationLiteral(pids); + } + + /** + * @param pids an array of configuration pids + */ + public ConfigurationLiteral(String[] pids) { + _pids = pids; + } + + @Override + public boolean required() { + return true; + } + + @Override + public String[] value() { + return _pids; + } + + private final String[] _pids; + +} Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/ReferenceLiteral.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/ReferenceLiteral.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/ReferenceLiteral.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/ReferenceLiteral.java Thu May 4 18:41:59 2017 @@ -1,3 +1,17 @@ +/** + * Licensed 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.cdi.container.internal.literal; import javax.enterprise.util.AnnotationLiteral; @@ -8,9 +22,12 @@ import org.osgi.service.cdi.annotations. public class ReferenceLiteral extends AnnotationLiteral<Reference> implements Reference { private static final long serialVersionUID = 1L; - public static final Reference INSTANCE = new ReferenceLiteral(""); - public static ReferenceLiteral fromTarget(String target) { + /** + * @param target a target filter + * @return a literal instance of {@link Reference} + */ + public static ReferenceLiteral from(String target) { return new ReferenceLiteral(target); } Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/ServiceLiteral.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/ServiceLiteral.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/ServiceLiteral.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/literal/ServiceLiteral.java Thu May 4 18:41:59 2017 @@ -1,20 +1,37 @@ +/** + * Licensed 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.cdi.container.internal.literal; import javax.enterprise.util.AnnotationLiteral; import org.osgi.service.cdi.annotations.Service; -import org.osgi.service.cdi.annotations.ServiceProperty; public class ServiceLiteral extends AnnotationLiteral<Service> implements Service { private static final long serialVersionUID = 1L; - public static final Service INSTANCE = new ServiceLiteral(new Class<?>[0], new ServiceProperty[0]); - public static ServiceLiteral from(Class<?>[] classes, ServiceProperty[] properties) { + /** + * @param classes an array of types under which to publish the service. + * @param properties the set of properties for the service. + * @return a literal instance of {@link Service} + */ + public static ServiceLiteral from(Class<?>[] classes, String[] properties) { return new ServiceLiteral(classes, properties); } - public ServiceLiteral(Class<?>[] classes, ServiceProperty[] properties) { + public ServiceLiteral(Class<?>[] classes, String[] properties) { _type = classes; _properties = properties; } @@ -25,11 +42,11 @@ public class ServiceLiteral extends Anno } @Override - public ServiceProperty[] properties() { + public String[] property() { return _properties; } - private final ServiceProperty[] _properties; + private final String[] _properties; private final Class<?>[] _type; } Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/AbstractModel.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/AbstractModel.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/AbstractModel.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/AbstractModel.java Thu May 4 18:41:59 2017 @@ -1,9 +1,33 @@ +/** + * Licensed 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.cdi.container.internal.model; import org.xml.sax.Attributes; public class AbstractModel { + boolean getBoolean(String uri, String localName, Attributes attributes, boolean defaultValue) { + String value = getValue(uri, localName, attributes); + + if (value == null) { + return defaultValue; + } + + return Boolean.parseBoolean(value); + } + String getValue(String uri, String localName, Attributes attributes) { String value = attributes.getValue(uri, localName); Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/AbstractModelBuilder.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/AbstractModelBuilder.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/AbstractModelBuilder.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/AbstractModelBuilder.java Thu May 4 18:41:59 2017 @@ -1,14 +1,27 @@ +/** + * Licensed 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.cdi.container.internal.model; import static org.apache.aries.cdi.container.internal.util.Reflection.cast; -import static org.osgi.service.cdi.CdiExtenderConstants.REQUIREMENT_BEANS_ATTRIBUTE; -import static org.osgi.service.cdi.CdiExtenderConstants.REQUIREMENT_OSGI_BEANS_ATTRIBUTE; +import static org.osgi.service.cdi.CdiConstants.REQUIREMENT_BEANS_ATTRIBUTE; +import static org.osgi.service.cdi.CdiConstants.REQUIREMENT_OSGI_BEANS_ATTRIBUTE; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.util.Map; @@ -26,35 +39,38 @@ public abstract class AbstractModelBuild public BeansModel build() { List<URL> beanDescriptorURLs = new ArrayList<URL>(); + List<URL> osgiBeanDescriptorURLs = new ArrayList<URL>(); Map<String, Object> attributes = getAttributes(); List<String> beanDescriptorPaths = cast(attributes.get(REQUIREMENT_BEANS_ATTRIBUTE)); if (beanDescriptorPaths != null) { for (String descriptorPath : beanDescriptorPaths) { - Collection<String> resources = getResources(descriptorPath); + URL url = getResource(descriptorPath); - if (resources != null) { - for (String resource : resources) { - URL url = getResource(resource); - - if (url != null) { - beanDescriptorURLs.add(url); - } - } + if (url != null) { + beanDescriptorURLs.add(url); } } } - String osgiBeansDescriptorPath = cast(attributes.get(REQUIREMENT_OSGI_BEANS_ATTRIBUTE)); + List<String> osgiBeansDescriptorPaths = cast(attributes.get(REQUIREMENT_OSGI_BEANS_ATTRIBUTE)); - if (osgiBeansDescriptorPath == null) { - osgiBeansDescriptorPath = "OSGI-INF/cdi/osgi-beans.xml"; + if (osgiBeansDescriptorPaths == null) { + osgiBeansDescriptorPaths = getDefaultResources(); } - URL osgiBeansDescriptorURL = getResource(osgiBeansDescriptorPath); + if (osgiBeansDescriptorPaths != null) { + for (String descriptorPath : osgiBeansDescriptorPaths) { + URL url = getResource(descriptorPath); + + if (url != null) { + osgiBeanDescriptorURLs.add(url); + } + } + } - return parse(osgiBeansDescriptorURL, beanDescriptorURLs); + return parse(osgiBeanDescriptorURLs, beanDescriptorURLs); } abstract Map<String, Object> getAttributes(); @@ -63,19 +79,19 @@ public abstract class AbstractModelBuild abstract URL getResource(String resource); - abstract Collection<String> getResources(String descriptorString); + abstract List<String> getDefaultResources(); private OSGiBeansHandler getHandler(List<URL> beanDescriptorURLs) { return new OSGiBeansHandler(beanDescriptorURLs); } - private BeansModel parse(URL osgiBeansDescriptorURL, List<URL> beanDescriptorURLs) { + private BeansModel parse(List<URL> osgiBeansDescriptorURLs, List<URL> beanDescriptorURLs) { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); - if (osgiBeansDescriptorURL == null) { - throw new IllegalArgumentException("Missing osgi-beans descriptor: " + osgiBeansDescriptorURL); + if (osgiBeansDescriptorURLs.isEmpty()) { + throw new IllegalArgumentException("Missing osgi-beans descriptors"); } SAXParser parser; @@ -87,45 +103,34 @@ public abstract class AbstractModelBuild return Throw.exception(e); } - InputStream inputStream = null; - - try { - inputStream = osgiBeansDescriptorURL.openStream(); - InputSource source = new InputSource(inputStream); - - if (source.getByteStream().available() == 0) { - throw new IllegalArgumentException( - "Specified osgi-beans descriptor is empty: " + osgiBeansDescriptorURL); - } - - try { - parser.setProperty( - "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); - parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", loadXsds()); - } - catch (IllegalArgumentException | SAXNotRecognizedException | SAXNotSupportedException e) { - // No op, we just don't validate the XML - } - - OSGiBeansHandler handler = getHandler(beanDescriptorURLs); + OSGiBeansHandler handler = getHandler(beanDescriptorURLs); - parser.parse(source, handler); + for (URL osgiBeansDescriptorURL: osgiBeansDescriptorURLs) { + try (InputStream inputStream = osgiBeansDescriptorURL.openStream()) { + InputSource source = new InputSource(inputStream); + + if (source.getByteStream().available() == 0) { + throw new IllegalArgumentException( + "Specified osgi-beans descriptor is empty: " + osgiBeansDescriptorURL); + } - return handler.createBeansModel(); - } - catch (IOException | SAXException e) { - return Throw.exception(e); - } - finally { - if (inputStream != null) { try { - inputStream.close(); + parser.setProperty( + "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); + parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", loadXsds()); } - catch (IOException e) { - throw new IllegalStateException(e); + catch (IllegalArgumentException | SAXNotRecognizedException | SAXNotSupportedException e) { + // No op, we just don't validate the XML } + + parser.parse(source, handler); + } + catch (IOException | SAXException e) { + return Throw.exception(e); } } + + return handler.createBeansModel(); } private InputSource loadXsd(String name) { Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/BeansModelBuilder.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/BeansModelBuilder.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/BeansModelBuilder.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/BeansModelBuilder.java Thu May 4 18:41:59 2017 @@ -15,10 +15,10 @@ package org.apache.aries.cdi.container.internal.model; import static org.osgi.namespace.extender.ExtenderNamespace.EXTENDER_NAMESPACE; -import static org.osgi.service.cdi.CdiExtenderConstants.CDI_EXTENDER; +import static org.osgi.service.cdi.CdiConstants.CDI_CAPABILITY_NAME; import java.net.URL; -import java.util.Collection; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; @@ -45,7 +45,7 @@ public class BeansModelBuilder extends A Map<String, Object> attributes = capability.getAttributes(); String extender = (String)attributes.get(EXTENDER_NAMESPACE); - if (extender.equals(CDI_EXTENDER)) { + if (extender.equals(CDI_CAPABILITY_NAME)) { BundleRequirement requirement = wire.getRequirement(); cdiAttributes = requirement.getAttributes(); break; @@ -71,12 +71,8 @@ public class BeansModelBuilder extends A } @Override - Collection<String> getResources(String descriptorString) { - int pos = descriptorString.lastIndexOf('/'); - String path = descriptorString.substring(0, pos); - String fileName = descriptorString.substring(pos, descriptorString.length()); - - return _bundleWiring.listResources(path, fileName, BundleWiring.LISTRESOURCES_LOCAL); + List<String> getDefaultResources() { + return new ArrayList<>(_bundleWiring.listResources("OSGI-INF/cdi", "*.xml", BundleWiring.LISTRESOURCES_LOCAL)); } private final Map<String, Object> _attributes; Copied: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ConfigurationInjectionPoint.java (from r1792762, aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ReferenceInjectionPoint.java) URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ConfigurationInjectionPoint.java?p2=aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ConfigurationInjectionPoint.java&p1=aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ReferenceInjectionPoint.java&r1=1792762&r2=1793847&rev=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ReferenceInjectionPoint.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ConfigurationInjectionPoint.java Thu May 4 18:41:59 2017 @@ -1,3 +1,17 @@ +/** + * Licensed 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.cdi.container.internal.model; import java.lang.annotation.Annotation; @@ -10,15 +24,15 @@ import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.InjectionPoint; import org.apache.aries.cdi.container.internal.literal.AnyLiteral; +import org.apache.aries.cdi.container.internal.literal.ConfigurationLiteral; import org.apache.aries.cdi.container.internal.literal.DefaultLiteral; -import org.apache.aries.cdi.container.internal.literal.ReferenceLiteral; import org.apache.aries.cdi.container.internal.util.Sets; -public class ReferenceInjectionPoint implements InjectionPoint { +public class ConfigurationInjectionPoint implements InjectionPoint { - public ReferenceInjectionPoint(Class<?> beanClass, String target) { + public ConfigurationInjectionPoint(Class<?> beanClass, String[] pids) { _beanClass = beanClass; - _qualifiers = Sets.hashSet(DefaultLiteral.INSTANCE, AnyLiteral.INSTANCE, ReferenceLiteral.fromTarget(target)); + _qualifiers = Sets.hashSet(DefaultLiteral.INSTANCE, AnyLiteral.INSTANCE, ConfigurationLiteral.from(pids)); } @Override Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ConfigurationModel.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ConfigurationModel.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ConfigurationModel.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ConfigurationModel.java Thu May 4 18:41:59 2017 @@ -1,20 +1,48 @@ +/** + * Licensed 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.cdi.container.internal.model; +import static org.apache.aries.cdi.container.internal.model.Constants.BEAN_CLASS_ATTRIBUTE; import static org.apache.aries.cdi.container.internal.model.Constants.CDI10_URI; import static org.apache.aries.cdi.container.internal.model.Constants.PID_ATTRIBUTE; +import static org.apache.aries.cdi.container.internal.model.Constants.REQUIRED_ATTRIBUTE; import org.xml.sax.Attributes; public class ConfigurationModel extends AbstractModel { public ConfigurationModel(Attributes attributes) { - _pid = getValue(CDI10_URI, PID_ATTRIBUTE, attributes); + _beanClass = getValue(CDI10_URI, BEAN_CLASS_ATTRIBUTE, attributes); + _pids = getValue(CDI10_URI, PID_ATTRIBUTE, attributes).split("\\s+"); + _required = getBoolean(CDI10_URI, REQUIRED_ATTRIBUTE, attributes, true); + } + + public String beanClass() { + return _beanClass; + } + + public String[] pids() { + return _pids; } - public String getPid() { - return _pid; + public boolean required() { + return _required; } - private final String _pid; + private final String _beanClass; + private final String[] _pids; + private final boolean _required; } Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/Constants.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/Constants.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/Constants.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/Constants.java Thu May 4 18:41:59 2017 @@ -1,3 +1,17 @@ +/** + * Licensed 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.cdi.container.internal.model; import java.util.Set; @@ -13,20 +27,25 @@ public class Constants { public static final String CDI10_URI = "http://www.osgi.org/xmlns/cdi/v1.0.0"; public static final Set<String> CDI_URIS = Sets.immutableHashSet(CDI10_URI); + public static final String ARRAY_ELEMENT = "array"; public static final String BEAN_CLASS_ATTRIBUTE = "beanClass"; public static final String BEAN_ELEMENT = "bean"; public static final String BEANS_ELEMENT = "beans"; public static final String CLASS_ATTRIBUTE = "class"; public static final String CONFIGURATION_ELEMENT = "configuration"; public static final String INTERFACE_ATTRIBUTE = "interface"; + public static final String LIST_ELEMENT = "list"; public static final String NAME_ATTRIBUTE = "name"; public static final String PID_ATTRIBUTE = "pid"; public static final String PROPERTY_ELEMENT = "property"; public static final String PROVIDE_ELEMENT = "provide"; public static final String REFERENCE_ELEMENT = "reference"; + public static final String REQUIRED_ATTRIBUTE = "required"; public static final String SERVICE_ELEMENT = "service"; + public static final String SET_ELEMENT = "set"; public static final String TARGET_ATTRIBUTE = "target"; - public static final String TYPE_ATTRIBUTE = "type"; public static final String VALUE_ATTRIBUTE = "value"; + public static final String VALUE_ELEMENT = "value"; + public static final String VALUE_TYPE_ATTRIBUTE = "value-type"; } Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/OSGiBeansHandler.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/OSGiBeansHandler.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/OSGiBeansHandler.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/OSGiBeansHandler.java Thu May 4 18:41:59 2017 @@ -1,22 +1,41 @@ +/** + * Licensed 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.cdi.container.internal.model; +import static org.apache.aries.cdi.container.internal.model.Constants.ARRAY_ELEMENT; import static org.apache.aries.cdi.container.internal.model.Constants.BEAN_ELEMENT; import static org.apache.aries.cdi.container.internal.model.Constants.CDI_URIS; import static org.apache.aries.cdi.container.internal.model.Constants.CLASS_ATTRIBUTE; import static org.apache.aries.cdi.container.internal.model.Constants.CONFIGURATION_ELEMENT; import static org.apache.aries.cdi.container.internal.model.Constants.INTERFACE_ATTRIBUTE; +import static org.apache.aries.cdi.container.internal.model.Constants.LIST_ELEMENT; +import static org.apache.aries.cdi.container.internal.model.Constants.NAME_ATTRIBUTE; import static org.apache.aries.cdi.container.internal.model.Constants.PROPERTY_ELEMENT; import static org.apache.aries.cdi.container.internal.model.Constants.PROVIDE_ELEMENT; import static org.apache.aries.cdi.container.internal.model.Constants.REFERENCE_ELEMENT; import static org.apache.aries.cdi.container.internal.model.Constants.SERVICE_ELEMENT; +import static org.apache.aries.cdi.container.internal.model.Constants.SET_ELEMENT; +import static org.apache.aries.cdi.container.internal.model.Constants.VALUE_ATTRIBUTE; +import static org.apache.aries.cdi.container.internal.model.Constants.VALUE_ELEMENT; +import static org.apache.aries.cdi.container.internal.model.Constants.VALUE_TYPE_ATTRIBUTE; import java.net.URL; import java.util.ArrayList; +import java.util.Formatter; import java.util.List; -import org.apache.aries.cdi.container.internal.literal.ServicePropertyLiteral; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; @@ -34,31 +53,46 @@ public class OSGiBeansHandler extends De @Override public void characters(char[] c, int start, int length) { - if (_propertyModel == null) { + if (_propertySB == null) { return; } - StringBuilder sb = new StringBuilder(); - - sb.append(c, start, length); - - _propertyModel.appendValue(sb.toString()); + _propertySB.append(c, start, length); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + if (matches(ARRAY_ELEMENT, uri, localName) && (_propertyValue == null)) { + _collectionType = CollectionType.ARRAY; + } if (matches(BEAN_ELEMENT, uri, localName)) { - _beanClass = attributes.getValue(uri, CLASS_ATTRIBUTE).trim(); - _beanClasses.add(_beanClass); + _beanClass = attributes.getValue(CLASS_ATTRIBUTE).trim(); + if (!_beanClasses.contains(_beanClass)) { + _beanClasses.add(_beanClass); + } } if (matches(CONFIGURATION_ELEMENT, uri, localName)) { _configurationModel = new ConfigurationModel(attributes); } + if (matches(LIST_ELEMENT, uri, localName) && (_propertyValue == null)) { + _collectionType = CollectionType.LIST; + } if (matches(PROPERTY_ELEMENT, uri, localName)) { - _propertyModel = new PropertyModel(attributes); + _propertyName = attributes.getValue(NAME_ATTRIBUTE).trim(); + _propertyType = attributes.getValue(VALUE_TYPE_ATTRIBUTE); + if (_propertyType == null) { + _propertyType = "String"; + } + String value = attributes.getValue(uri, VALUE_ATTRIBUTE); + if (value != null) { + _propertyValue = value.trim(); + } + else { + _propertySB = new StringBuilder(); + } } if (matches(PROVIDE_ELEMENT, uri, localName)) { - String value = attributes.getValue(uri, INTERFACE_ATTRIBUTE).trim(); + String value = attributes.getValue(INTERFACE_ATTRIBUTE).trim(); _serviceModel.addProvide(value); } if (matches(REFERENCE_ELEMENT, uri, localName)) { _referenceModel = new ReferenceModel(attributes); @@ -66,19 +100,40 @@ public class OSGiBeansHandler extends De if (matches(SERVICE_ELEMENT, uri, localName)) { _serviceModel = new ServiceModel(_beanClass); } + if (matches(SET_ELEMENT, uri, localName) && (_propertyValue == null)) { + _collectionType = CollectionType.SET; + } + if (matches(VALUE_ELEMENT, uri, localName) && (_collectionType != null)) { + _propertySB = new StringBuilder(); + } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { + if (matches(ARRAY_ELEMENT, uri, localName)) { + _collectionType = null; + } if (matches(CONFIGURATION_ELEMENT, uri, localName)) { _configurationModels.add(_configurationModel); _configurationModel = null; } + if (matches(LIST_ELEMENT, uri, localName)) { + _collectionType = null; + } if (matches(PROPERTY_ELEMENT, uri, localName)) { - ServicePropertyLiteral servicePropertyLiteral = ServicePropertyLiteral.from( - _propertyModel.getName(), _propertyModel.getValue(), _propertyModel.getType()); - _serviceModel.addProperty(servicePropertyLiteral); - _propertyModel = null; + if ((_propertyValue == null) && (_propertySB != null)) { + _propertyValue = _propertySB.toString().trim(); + } + if (_propertyValue != null) { + try (Formatter f = new Formatter()) { + f.format("%s:%s=%s", _propertyName, _propertyType, _propertyValue); + _serviceModel.addProperty(f.toString()); + } + } + _propertySB = null; + _propertyName = null; + _propertyType = null; + _propertyValue = null; } if (matches(REFERENCE_ELEMENT, uri, localName)) { _referenceModels.add(_referenceModel); @@ -89,6 +144,32 @@ public class OSGiBeansHandler extends De _serviceModel = null; _beanClass = null; } + if (matches(SET_ELEMENT, uri, localName)) { + _collectionType = null; + } + if (matches(VALUE_ELEMENT, uri, localName) && (_collectionType != null)) { + StringBuilder sb = new StringBuilder(); + sb.append(_propertyName); + sb.append(":"); + if (_collectionType == CollectionType.LIST) { + sb.append("List<"); + sb.append(_propertyType); + sb.append(">"); + } + else if (_collectionType == CollectionType.SET) { + sb.append("Set<"); + sb.append(_propertyType); + sb.append(">"); + } + else { + sb.append(_propertyType); + } + sb.append("="); + sb.append(_propertySB.toString().trim()); + + _serviceModel.addProperty(sb.toString()); + _propertySB = null; + } } private boolean matches(String elementName, String uri, String localName) { @@ -98,14 +179,20 @@ public class OSGiBeansHandler extends De return false; } - private static final Logger _log = LoggerFactory.getLogger(OSGiBeansHandler.class); + enum CollectionType { + ARRAY, LIST, SET + } private String _beanClass; private final List<String> _beanClasses = new ArrayList<String>(); private final List<URL> _beanDescriptorURLs; private ConfigurationModel _configurationModel; private final List<ConfigurationModel> _configurationModels = new ArrayList<>(); - private PropertyModel _propertyModel; + private String _propertyName; + private StringBuilder _propertySB; + private String _propertyType; + private String _propertyValue; + private CollectionType _collectionType; private ReferenceModel _referenceModel; private final List<ReferenceModel> _referenceModels = new ArrayList<>(); private ServiceModel _serviceModel; Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ReferenceInjectionPoint.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ReferenceInjectionPoint.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ReferenceInjectionPoint.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ReferenceInjectionPoint.java Thu May 4 18:41:59 2017 @@ -1,3 +1,17 @@ +/** + * Licensed 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.cdi.container.internal.model; import java.lang.annotation.Annotation; @@ -18,7 +32,7 @@ public class ReferenceInjectionPoint imp public ReferenceInjectionPoint(Class<?> beanClass, String target) { _beanClass = beanClass; - _qualifiers = Sets.hashSet(DefaultLiteral.INSTANCE, AnyLiteral.INSTANCE, ReferenceLiteral.fromTarget(target)); + _qualifiers = Sets.hashSet(DefaultLiteral.INSTANCE, AnyLiteral.INSTANCE, ReferenceLiteral.from(target)); } @Override Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ReferenceModel.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ReferenceModel.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ReferenceModel.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ReferenceModel.java Thu May 4 18:41:59 2017 @@ -1,3 +1,17 @@ +/** + * Licensed 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.cdi.container.internal.model; import static org.apache.aries.cdi.container.internal.model.Constants.BEAN_CLASS_ATTRIBUTE; Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ServiceModel.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ServiceModel.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ServiceModel.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/ServiceModel.java Thu May 4 18:41:59 2017 @@ -1,10 +1,22 @@ +/** + * Licensed 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.cdi.container.internal.model; import java.util.ArrayList; import java.util.List; -import org.osgi.service.cdi.annotations.ServiceProperty; - public class ServiceModel { public ServiceModel(String beanClass) { @@ -15,16 +27,16 @@ public class ServiceModel { _provides.add(className); } - public void addProperty(ServiceProperty serviceProperty) { - _properties.add(serviceProperty); + public void addProperty(String property) { + _properties.add(property); } public String getBeanClass() { return _beanClass; } - public ServiceProperty[] getProperties() { - return _properties.toArray(new ServiceProperty[0]); + public String[] getProperties() { + return _properties.toArray(new String[0]); } public List<String> getProvides() { @@ -32,6 +44,6 @@ public class ServiceModel { } private final String _beanClass; - private List<ServiceProperty> _properties = new ArrayList<>(); + private List<String> _properties = new ArrayList<>(); private List<String> _provides = new ArrayList<>(); } Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/XmlSchema.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/XmlSchema.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/XmlSchema.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/XmlSchema.java Thu May 4 18:41:59 2017 @@ -1,3 +1,17 @@ +/** + * Licensed 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.cdi.container.internal.model; public enum XmlSchema { Added: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Conversions.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Conversions.java?rev=1793847&view=auto ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Conversions.java (added) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Conversions.java Thu May 4 18:41:59 2017 @@ -0,0 +1,50 @@ +/** + * Licensed 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.cdi.container.internal.util; + +import java.util.Arrays; + +import org.osgi.util.converter.Converter; +import org.osgi.util.converter.ConverterBuilder; +import org.osgi.util.converter.StandardConverter; +import org.osgi.util.converter.TypeRule; + +public class Conversions { + + public static String toString(Object object) { + return INSTANCE._converter.convert(object).defaultValue("").to(String.class); + } + + public static Converter c() { + return INSTANCE._converter; + } + + private Conversions() { + ConverterBuilder builder = new StandardConverter().newConverterBuilder(); + + builder + .rule(new TypeRule<>(String[].class, String.class, i -> Arrays.toString((String[])i))) + .rule(new TypeRule<>(double[].class, String.class, i -> Arrays.toString((double[])i))) + .rule(new TypeRule<>(int[].class, String.class, i -> Arrays.toString((int[])i))) + .rule(new TypeRule<>(long[].class, String.class, i -> Arrays.toString((long[])i))); + + _converter = builder.build(); + } + + public static final Conversions INSTANCE = new Conversions(); + + private final Converter _converter; + +} Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Maps.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Maps.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Maps.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Maps.java Thu May 4 18:41:59 2017 @@ -1,6 +1,25 @@ +/** + * Licensed 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.cdi.container.internal.util; +import java.lang.reflect.Array; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Set; public class Maps { @@ -23,4 +42,72 @@ public class Maps { } + public static Map<String, Object> map(String[] properties) { + Map<String,Object> map = new HashMap<>(); + + for (String property : properties) { + map(map, property); + } + + return map; + } + + static void map(Map<String, Object> map, String property) { + int eq = property.indexOf('='); + + String key = property.substring(0, eq); + String type = "String"; + String value = property.substring(eq + 1, property.length()); + + int colon = key.indexOf(':'); + + if (colon != -1) { + property = key; + key = property.substring(0, colon); + type = property.substring(colon + 1, property.length()); + } + + map(map, key, type, value); + } + + static void map(Map<String, Object> map, String key, String type, String value) { + PropertyType propertyType = PropertyType.find(type); + + Object object = map.get(key); + + if (object == null) { + Object valueObject = Conversions.c().convert(value).to(propertyType.getType()); + + map.put(key, valueObject); + + return; + } + + Object valueObject = Conversions.c().convert(value).to(propertyType.componentType()); + + if (propertyType.isRaw()) { + if (!object.getClass().isArray()) { + Object array = Array.newInstance(propertyType.componentType(), 2); + Array.set(array, 0, object); + Array.set(array, 1, valueObject); + map.put(key, array); + } + else { + int length = Array.getLength(object); + Object array = Array.newInstance(propertyType.componentType(), length + 1); + System.arraycopy(object, 0, array, 0, length); + Array.set(array, length, valueObject); + map.put(key, array); + } + } + else if (propertyType.isList()) { + List list = Collections.checkedList((List)object, propertyType.componentType()); + list.add(valueObject); + } + else if (propertyType.isSet()) { + Set set = Collections.checkedSet((Set)object, propertyType.componentType()); + set.add(valueObject); + } + } + } Added: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/PropertyType.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/PropertyType.java?rev=1793847&view=auto ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/PropertyType.java (added) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/PropertyType.java Thu May 4 18:41:59 2017 @@ -0,0 +1,146 @@ +/** + * Licensed 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.cdi.container.internal.util; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.List; +import java.util.Set; + +import javax.enterprise.util.TypeLiteral; + +import org.jboss.weld.exceptions.IllegalArgumentException; + +@SuppressWarnings("serial") +public enum PropertyType { + + Boolean("Boolean", Aggregate.RAW, new TypeLiteral<Boolean>() {}), + Byte("Byte", Aggregate.RAW, new TypeLiteral<Byte>() {}), + Character("Character", Aggregate.RAW, new TypeLiteral<Character>() {}), + Double("Double", Aggregate.RAW, new TypeLiteral<Double>() {}), + Float("Float", Aggregate.RAW, new TypeLiteral<Float>() {}), + Integer("Integer", Aggregate.RAW, new TypeLiteral<Integer>() {}), + Long("Long", Aggregate.RAW, new TypeLiteral<Long>() {}), + Short("Short", Aggregate.RAW, new TypeLiteral<Short>() {}), + String("String", Aggregate.RAW, new TypeLiteral<String>() {}), + + Boolean_Array("Boolean", Aggregate.ARRAY, new TypeLiteral<Boolean[]>() {}), + Byte_Array("Byte", Aggregate.ARRAY, new TypeLiteral<Byte[]>() {}), + Character_Array("Character", Aggregate.ARRAY, new TypeLiteral<Character[]>() {}), + Double_Array("Double", Aggregate.ARRAY, new TypeLiteral<Double[]>() {}), + Float_Array("Float", Aggregate.ARRAY, new TypeLiteral<Float[]>() {}), + Integer_Array("Integer", Aggregate.ARRAY, new TypeLiteral<Integer[]>() {}), + Long_Array("Long", Aggregate.ARRAY, new TypeLiteral<Long[]>() {}), + Short_Array("Short", Aggregate.ARRAY, new TypeLiteral<Short[]>() {}), + String_Array("String", Aggregate.ARRAY, new TypeLiteral<String[]>() {}), + + Boolean_List("Boolean", Aggregate.LIST, new TypeLiteral<List<Boolean>>() {}), + Byte_List("Byte", Aggregate.LIST, new TypeLiteral<List<Byte>>() {}), + Character_List("Character", Aggregate.LIST, new TypeLiteral<List<Character>>() {}), + Double_List("Double", Aggregate.LIST, new TypeLiteral<List<Double>>() {}), + Float_List("Float", Aggregate.LIST, new TypeLiteral<List<Float>>() {}), + Integer_List("Integer", Aggregate.LIST, new TypeLiteral<List<Integer>>() {}), + Long_List("Long", Aggregate.LIST, new TypeLiteral<List<Long>>() {}), + Short_List("Short", Aggregate.LIST, new TypeLiteral<List<Short>>() {}), + String_List("String", Aggregate.LIST, new TypeLiteral<List<String>>() {}), + + Boolean_Set("Boolean", Aggregate.SET, new TypeLiteral<Set<Boolean>>() {}), + Byte_Set("Byte", Aggregate.SET, new TypeLiteral<Set<Byte>>() {}), + Character_Set("Character", Aggregate.SET, new TypeLiteral<Set<Character>>() {}), + Double_Set("Double", Aggregate.SET, new TypeLiteral<Set<Double>>() {}), + Float_Set("Float", Aggregate.SET, new TypeLiteral<Set<Float>>() {}), + Integer_Set("Integer", Aggregate.SET, new TypeLiteral<Set<Integer>>() {}), + Long_Set("Long", Aggregate.SET, new TypeLiteral<Set<Long>>() {}), + Short_Set("Short", Aggregate.SET, new TypeLiteral<Set<Short>>() {}), + String_Set("String", Aggregate.SET, new TypeLiteral<Set<String>>() {}); + + public static PropertyType arrayOf(String value) { + PropertyType propertyType = valueOf(value); + + return valueOf(propertyType.raw + "_Array"); + } + + public static PropertyType find(String value) { + for (PropertyType propertyType : values()) { + if (propertyType.toString().equals(value)) + return propertyType; + } + + throw new IllegalArgumentException("No such PropertyType: " + value); + } + + PropertyType(String raw, Aggregate a, TypeLiteral<?> typeLiteral) { + this.raw = raw; + this.a = a; + this.typeLiteral = typeLiteral; + } + + public Class<?> componentType() { + switch (a) { + case RAW: + return (Class<?>)getType(); + case ARRAY: + return getType().getClass().getComponentType(); + default: + ParameterizedType pt = (ParameterizedType)getType(); + return (Class<?>)pt.getActualTypeArguments()[0]; + } + } + + public Type getType() { + return typeLiteral.getType(); + } + + public boolean isArray() { + return a == Aggregate.ARRAY; + } + + public boolean isList() { + return a == Aggregate.LIST; + } + + public boolean isRaw() { + return a == Aggregate.RAW; + } + + public boolean isSet() { + return a == Aggregate.SET; + } + + @Override + public java.lang.String toString() { + switch (a) { + case ARRAY: + return raw + "[]"; + case LIST: + return "List<" + raw + ">"; + case RAW: + return raw; + case SET: + return "Set<" + raw + ">"; + } + + return raw; + } + + private final Aggregate a; + private final String raw; + private final TypeLiteral<?> typeLiteral; + + enum Aggregate { + ARRAY, LIST, SET, RAW + } + +} Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Reflection.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Reflection.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Reflection.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Reflection.java Thu May 4 18:41:59 2017 @@ -1,3 +1,17 @@ +/** + * Licensed 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.cdi.container.internal.util; public class Reflection { Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Sets.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Sets.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Sets.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Sets.java Thu May 4 18:41:59 2017 @@ -1,3 +1,17 @@ +/** + * Licensed 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.cdi.container.internal.util; import java.util.Collections; Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Strings.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Strings.java?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Strings.java (original) +++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Strings.java Thu May 4 18:41:59 2017 @@ -1,3 +1,17 @@ +/** + * Licensed 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.cdi.container.internal.util; import java.util.regex.Matcher; Modified: aries/trunk/cdi/cdi-extender/src/main/resources/META-INF/cdi.xsd URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/resources/META-INF/cdi.xsd?rev=1793847&r1=1793846&r2=1793847&view=diff ============================================================================== --- aries/trunk/cdi/cdi-extender/src/main/resources/META-INF/cdi.xsd (original) +++ aries/trunk/cdi/cdi-extender/src/main/resources/META-INF/cdi.xsd Thu May 4 18:41:59 2017 @@ -7,7 +7,7 @@ * 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 + * 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, @@ -17,9 +17,27 @@ */ --> <schema xmlns="http://www.w3.org/2001/XMLSchema" + xmlns:cdi="http://www.osgi.org/xmlns/cdi/v1.0.0" targetNamespace="http://www.osgi.org/xmlns/cdi/v1.0.0" - xmlns:cdi="http://www.osgi.org/xmlns/cdi/v1.0.0"> - + elementFormDefault="unqualified" + attributeFormDefault="unqualified" + version="1.0.0"> + + <annotation> + <documentation xml:lang="en"> + This is the XML Schema for OSGi Bean descriptions used by + the OSGi CDI Extender. OSGi Bean description documents + may be embedded in other XML documents. The OSGi CDI Extender + will process all XML documents listed in the osgi.beans + attribute of the CDI extender requirement. XML documents containing + OSGi Bean descriptions may contain one or more bean, configuration, + and/or reference elements embedded in a larger document. Use of + the namespace for bean descriptions is mandatory. The attributes + and subelements of bean, configuration, and/or reference elements + are always unqualified. + </documentation> + </annotation> + <element name="bean" type="cdi:Tbean" /> <complexType name="Tbean"> <sequence> <element name="service" maxOccurs="1" minOccurs="0" @@ -31,57 +49,68 @@ <anyAttribute processContents="lax" /> </complexType> - <complexType name="Tbeans"> + <element name="configuration" type="cdi:Tconfiguration" /> + <complexType name="Tconfiguration"> <sequence> - <element name="bean" maxOccurs="unbounded" minOccurs="0" - type="cdi:Tbean" /> - <element name="configuration" type="cdi:Tconfiguration" - maxOccurs="unbounded" minOccurs="0" /> - <element name="reference" maxOccurs="unbounded" minOccurs="0" - type="cdi:Treference" /> - <any namespace="##other" maxOccurs="unbounded" minOccurs="0" + <any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax" /> </sequence> + <attribute name="beanClass" type="token" use="required" /> + <attribute name="pid" type="token" use="required" /> + <attribute name="required" type="boolean" default="true" use="optional" /> <anyAttribute processContents="lax" /> </complexType> - <complexType name="Tconfiguration"> + <element name="reference" type="cdi:Treference" /> + <complexType name="Treference"> <sequence> + <any namespace="##any" processContents="lax" + minOccurs="0" maxOccurs="unbounded" /> + </sequence> + <attribute name="beanClass" type="token" use="required" /> + <attribute name="target" type="token" use="required" /> + <anyAttribute processContents="lax" /> + </complexType> + + <complexType name="Tproperty" mixed="true"> + <sequence maxOccurs="1"> + <choice minOccurs="0" maxOccurs="1"> + <element name="array" type="cdi:Tmulti-value"/> + <element name="list" type="cdi:Tmulti-value"/> + <element name="set" type="cdi:Tmulti-value"/> + </choice> <any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax" /> </sequence> - <attribute name="pid" type="token" use="required" /> + <attribute name="name" type="string" use="required" /> + <attribute name="value" type="string" use="optional" /> + <attribute name="value-type" type="cdi:Tvalue-types" default="String" use="optional" /> <anyAttribute processContents="lax" /> </complexType> - <complexType name="Tproperty"> - <simpleContent> - <extension base="string"> - <attribute name="name" type="token" use="required" /> - <attribute name="value" type="token" use="optional" /> - <attribute default="String" name="type" - type="cdi:Tproperty_type" use="optional" /> - <anyAttribute processContents="lax" /> - </extension> - </simpleContent> + <complexType name="Tmulti-value"> + <sequence> + <element name="value" minOccurs="0" maxOccurs="unbounded" type="cdi:Tvalue"/> + <any namespace="##other" minOccurs="0" maxOccurs="unbounded" + processContents="lax" /> + </sequence> + <anyAttribute processContents="lax" /> </complexType> - <complexType name="Tprovide"> + <complexType name="Tvalue" mixed="true"> <sequence> - <any namespace="##any" processContents="lax" - minOccurs="0" maxOccurs="unbounded" /> + <any namespace="##other" minOccurs="0" maxOccurs="unbounded" + processContents="lax" /> </sequence> - <attribute name="interface" type="token" use="required" /> <anyAttribute processContents="lax" /> </complexType> - <complexType name="Treference"> + <complexType name="Tprovide"> <sequence> <any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded" /> </sequence> - <attribute name="beanClass" type="token" /> - <attribute name="target" type="token" use="required" /> + <attribute name="interface" type="token" use="required" /> <anyAttribute processContents="lax" /> </complexType> @@ -97,19 +126,31 @@ <anyAttribute processContents="lax" /> </complexType> - <simpleType name="Tproperty_type"> + <!-- Specifies the data type of a property or of the elements in a multi-value + property. Numerical and boolean values are trimmed before they are processed. + Simple types are automatically boxed if needed. Only the array data type + allows for simple type values. When specifying a simple type on any other + type of property it will automatically be boxed. --> + <simpleType name="Tvalue-types"> <restriction base="string"> <enumeration value="String" /> + <enumeration value="long" /> <enumeration value="Long" /> + <enumeration value="double" /> <enumeration value="Double" /> + <enumeration value="float" /> <enumeration value="Float" /> + <enumeration value="int" /> <enumeration value="Integer" /> + <enumeration value="byte" /> <enumeration value="Byte" /> + <enumeration value="char" /> <enumeration value="Character" /> + <enumeration value="boolean" /> <enumeration value="Boolean" /> + <enumeration value="short" /> <enumeration value="Short" /> </restriction> </simpleType> - <element name="beans" type="cdi:Tbeans"></element> </schema> Added: aries/trunk/cdi/cdi-extender/src/test/java/org/apache/aries/cdi/container/internal/model/MapsTest.java URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/test/java/org/apache/aries/cdi/container/internal/model/MapsTest.java?rev=1793847&view=auto ============================================================================== --- aries/trunk/cdi/cdi-extender/src/test/java/org/apache/aries/cdi/container/internal/model/MapsTest.java (added) +++ aries/trunk/cdi/cdi-extender/src/test/java/org/apache/aries/cdi/container/internal/model/MapsTest.java Thu May 4 18:41:59 2017 @@ -0,0 +1,246 @@ +package org.apache.aries.cdi.container.internal.model; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.apache.aries.cdi.container.internal.util.Maps; +import org.junit.Test; + +public class MapsTest { + + @Test + public void testSingleRawStringConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo=bar"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals("bar", entry.getValue()); + } + + @Test + public void testSingleStringConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:String=bar"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals("bar", entry.getValue()); + } + + @Test + public void testSingleListStringConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:List<String>=bar"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(Collections.singletonList("bar"), entry.getValue()); + } + + @Test + public void testSingleSetStringConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:Set<String>=bar"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(Collections.singleton("bar"), entry.getValue()); + } + + @Test + public void testRawStringArrayConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo=bar", "foo=baz"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertArrayEquals(new String[] {"bar", "baz"}, (String[])entry.getValue()); + } + + @Test + public void testRawStringArrayConversion2() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo=bar", "foo=baz", "foo=fee"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertArrayEquals(new String[] {"bar", "baz", "fee"}, (String[])entry.getValue()); + } + + @Test + public void testStringArrayConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:String=bar", "foo:String=baz"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertArrayEquals(new String[] {"bar", "baz"}, (String[])entry.getValue()); + } + + @Test + public void testStringArrayConversion2() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:String=bar", "foo:String=baz", "foo:String=fee"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertArrayEquals(new String[] {"bar", "baz", "fee"}, (String[])entry.getValue()); + } + + @Test + public void testListStringConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:List<String>=bar", "foo:List<String>=baz"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(Arrays.asList("bar", "baz"), entry.getValue()); + } + + @Test + public void testListStringConversion2() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:List<String>=bar", "foo:List<String>=baz", "foo:List<String>=fee"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(Arrays.asList("bar", "baz", "fee"), entry.getValue()); + } + + @Test + public void testSetStringConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:Set<String>=bar", "foo:Set<String>=baz"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(new HashSet<String>(Arrays.asList("bar", "baz")), entry.getValue()); + } + + @Test + public void testSetStringConversion2() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:Set<String>=bar", "foo:Set<String>=baz", "foo:Set<String>=fee"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(new HashSet<String>(Arrays.asList("bar", "baz", "fee")), entry.getValue()); + } + + @Test + public void testSingleBooleanConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:Boolean=bar"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(false, entry.getValue()); + } + + @Test + public void testSingleBooleanConversion2() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:Boolean=true"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(true, entry.getValue()); + } + + @Test + public void testSingleBooleanConversion3() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:Boolean=0"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(false, entry.getValue()); + } + + @Test + public void testArrayBooleanConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:Boolean=bar", "foo:Boolean=false", "foo:Boolean=true"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertArrayEquals(new Boolean[] {false, false, true}, (Boolean[])entry.getValue()); + } + + @Test + public void testListBooleanConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:List<Boolean>=true", "foo:List<Boolean>=bar", "foo:List<Boolean>=false"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(Arrays.asList(true, false, false), entry.getValue()); + } + + @Test + public void testSetBooleanConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:Set<Boolean>=0", "foo:Set<Boolean>=true", "foo:Set<Boolean>=false"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(new HashSet<Boolean>(Arrays.asList(false, true)), entry.getValue()); + } + + @Test + public void testSingleByteConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:Byte=1"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(new Byte("1"), entry.getValue()); + } + + @Test + public void testSingleByteConversion2() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:Byte=126"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(new Byte("126"), entry.getValue()); + } + + @Test + public void testArrayByteConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:Byte=1", "foo:Byte=96"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertArrayEquals(new Byte[] {new Byte("1"), new Byte("96")}, (Byte[])entry.getValue()); + } + + @Test + public void testListByteConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:List<Byte>=126", "foo:List<Byte>=91"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(Arrays.asList(new Byte("126"), new Byte("91")), entry.getValue()); + } + + @Test + public void testSetByteConversion() throws Exception { + Set<Entry<String,Object>> set = Maps.map(new String[] {"foo:Set<Byte>=126", "foo:Set<Byte>=91", "foo:Set<Byte>=91"}).entrySet(); + + Entry<String, Object> entry = set.iterator().next(); + assertEquals("foo", entry.getKey()); + assertEquals(new HashSet<Byte>(Arrays.asList(new Byte("126"), new Byte("91"))), entry.getValue()); + } + + @Test + public void testMixedConversion() throws Exception { + Map<String,Object> map = Maps.map( + new String[] { + "foo:Set<Byte>=126", "foo:Set<Byte>=91", "foo:Set<Byte>=91", + "fum=blaz", "fee:List<Double>=91.8765", "fee:List<Double>=34567.8965"}); + + assertEquals(3, map.size()); + assertTrue(map.containsKey("foo")); + assertTrue(map.containsKey("fum")); + assertTrue(map.containsKey("fee")); + assertTrue(map.get("foo") instanceof Set); + assertTrue(map.get("fum") instanceof String); + assertTrue(map.get("fee") instanceof List); + assertEquals(2, ((Set)map.get("foo")).size()); + assertEquals("blaz", map.get("fum")); + assertEquals(2, ((List)map.get("fee")).size()); + } + +}
