http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/test/java/org/apache/tamaya/spisupport/services/DefaultServiceContext.java ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/test/java/org/apache/tamaya/spisupport/services/DefaultServiceContext.java b/code/old/spi-support/src/test/java/org/apache/tamaya/spisupport/services/DefaultServiceContext.java new file mode 100644 index 0000000..46d69da --- /dev/null +++ b/code/old/spi-support/src/test/java/org/apache/tamaya/spisupport/services/DefaultServiceContext.java @@ -0,0 +1,205 @@ +/* + * 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.tamaya.spisupport.services; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.spi.ServiceContext; +import org.apache.tamaya.spisupport.PriorityServiceComparator; + +import javax.annotation.Priority; +import java.io.IOException; +import java.net.URL; +import java.text.MessageFormat; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * This class implements the (default) {@link ServiceContext} interface and hereby uses the JDK + * {@link ServiceLoader} to load the services required. + */ +public final class DefaultServiceContext implements ServiceContext { + private static final Logger LOG = Logger.getLogger(DefaultServiceContext.class.getName()); + /** + * List current services loaded, per class. + */ + private final ConcurrentHashMap<Class<?>, List<Object>> servicesLoaded = new ConcurrentHashMap<>(); + /** + * Singletons. + */ + private final Map<Class<?>, Object> singletons = new ConcurrentHashMap<>(); + @SuppressWarnings("rawtypes") + private Map<Class, Class> factoryTypes = new ConcurrentHashMap<>(); + + @Override + public <T> T getService(Class<T> serviceType) { + Object cached = singletons.get(serviceType); + if (cached == null) { + cached = create(serviceType); + if(cached!=null) { + singletons.put(serviceType, cached); + } + } + return serviceType.cast(cached); + } + + @Override + public <T> T create(Class<T> serviceType) { + @SuppressWarnings("unchecked") + Class<? extends T> implType = factoryTypes.get(serviceType); + if(implType==null) { + Collection<T> services = getServices(serviceType); + if (services.isEmpty()) { + return null; + } else { + return getServiceWithHighestPriority(services, serviceType); + } + } + try { + return implType.newInstance(); + } catch (Exception e) { + LOG.log(Level.SEVERE, "Failed to create instance of " + implType.getName(), e); + return null; + } + } + + /** + * Loads and registers services. + * + * @param <T> the concrete type. + * @param serviceType The service type. + * @return the items found, never {@code null}. + */ + @Override + public <T> List<T> getServices(final Class<T> serviceType) { + @SuppressWarnings("unchecked") + List<T> found = (List<T>) servicesLoaded.get(serviceType); + if (found != null) { + return found; + } + List<T> services = new ArrayList<>(); + try { + for (T t : ServiceLoader.load(serviceType)) { + services.add(t); + } + if(services.isEmpty()) { + for (T t : ServiceLoader.load(serviceType, serviceType.getClassLoader())) { + services.add(t); + } + } + Collections.sort(services, PriorityServiceComparator.getInstance()); + services = Collections.unmodifiableList(services); + } catch (ServiceConfigurationError e) { + LOG.log(Level.WARNING, + "Error loading services current type " + serviceType, e); + if(services==null){ + services = Collections.emptyList(); + } + } + @SuppressWarnings("unchecked") + final List<T> previousServices = List.class.cast(servicesLoaded.putIfAbsent(serviceType, (List<Object>) services)); + return previousServices != null ? previousServices : services; + } + + /** + * Checks the given instance for a @Priority annotation. If present the annotation's value is evaluated. If no such + * annotation is present, a default priority of {@code 1} is returned. + * @param o the instance, not {@code null}. + * @return a priority, by default 1. + */ + public static int getPriority(Object o){ + int prio = 1; //X TODO discuss default priority + Priority priority = o.getClass().getAnnotation(Priority.class); + if (priority != null) { + prio = priority.value(); + } + return prio; + } + + /** + * @param services to scan + * @param <T> type of the service + * + * @return the service with the highest {@link Priority#value()} + * + * @throws ConfigException if there are multiple service implementations with the maximum priority + */ + private <T> T getServiceWithHighestPriority(Collection<T> services, Class<T> serviceType) { + T highestService = null; + // we do not need the priority stuff if the list contains only one element + if (services.size() == 1) { + highestService = services.iterator().next(); + this.factoryTypes.put(serviceType, highestService.getClass()); + return highestService; + } + + Integer highestPriority = null; + int highestPriorityServiceCount = 0; + + for (T service : services) { + int prio = getPriority(service); + if (highestPriority == null || highestPriority < prio) { + highestService = service; + highestPriorityServiceCount = 1; + highestPriority = prio; + } else if (highestPriority == prio) { + highestPriorityServiceCount++; + } + } + + if (highestPriorityServiceCount > 1) { + throw new ConfigException(MessageFormat.format("Found {0} implementations for Service {1} with Priority {2}: {3}", + highestPriorityServiceCount, + serviceType.getName(), + highestPriority, + services)); + } + this.factoryTypes.put(serviceType, highestService.getClass()); + return highestService; + } + + @Override + public int ordinal() { + return 1; + } + + @Override + public Enumeration<URL> getResources(String resource, ClassLoader cl) throws IOException { + if(cl==null){ + cl = Thread.currentThread().getContextClassLoader(); + } + if(cl==null){ + cl = getClass().getClassLoader(); + } + return cl.getResources(resource); + } + + @Override + public URL getResource(String resource, ClassLoader cl) { + if(cl==null){ + cl = Thread.currentThread().getContextClassLoader(); + } + if(cl==null){ + cl = getClass().getClassLoader(); + } + return cl.getResource(resource); + } + +}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/test/resources/META-INF/javaconfiguration.properties ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/test/resources/META-INF/javaconfiguration.properties b/code/old/spi-support/src/test/resources/META-INF/javaconfiguration.properties new file mode 100644 index 0000000..33beabb --- /dev/null +++ b/code/old/spi-support/src/test/resources/META-INF/javaconfiguration.properties @@ -0,0 +1,22 @@ +# 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. + +confkey1=javaconf-value1 +confkey2=javaconf-value2 +confkey3=javaconf-value3 +confkey4=javaconf-value4 +confkey5=javaconf-value5 http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/test/resources/META-INF/javaconfiguration.xml ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/test/resources/META-INF/javaconfiguration.xml b/code/old/spi-support/src/test/resources/META-INF/javaconfiguration.xml new file mode 100644 index 0000000..f6cdc97 --- /dev/null +++ b/code/old/spi-support/src/test/resources/META-INF/javaconfiguration.xml @@ -0,0 +1,25 @@ +<?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. +--> + +<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> +<properties> + <entry key="aaeehh">ä</entry> + <entry key="ö">o</entry> +</properties> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi b/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi new file mode 100644 index 0000000..aae53e4 --- /dev/null +++ b/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi @@ -0,0 +1,18 @@ +# 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. + +org.apache.tamaya.spisupport.TestConfigurationProvider \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter b/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter new file mode 100644 index 0000000..7c62bb2 --- /dev/null +++ b/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter @@ -0,0 +1,19 @@ +# +# 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 current 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. +# +org.apache.tamaya.spisupport.CTestConverter \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext b/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext new file mode 100644 index 0000000..135caa1 --- /dev/null +++ b/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext @@ -0,0 +1,19 @@ +# +# 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 current 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. +# +org.apache.tamaya.spisupport.DefaultServiceContext http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityInterface ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityInterface b/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityInterface new file mode 100644 index 0000000..c894501 --- /dev/null +++ b/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityInterface @@ -0,0 +1,18 @@ +# 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. +org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityImpl1 +org.apache.tamaya.spisupport.DefaultServiceContextTest$InvalidPriorityImpl2 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImplsInterface ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImplsInterface b/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImplsInterface new file mode 100644 index 0000000..71f7c46 --- /dev/null +++ b/code/old/spi-support/src/test/resources/META-INF/services/org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImplsInterface @@ -0,0 +1,20 @@ +# 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. + +org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImpl1 +org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImpl2 +org.apache.tamaya.spisupport.DefaultServiceContextTest$MultiImpl3 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/test/resources/invalid-properties.xml ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/test/resources/invalid-properties.xml b/code/old/spi-support/src/test/resources/invalid-properties.xml new file mode 100644 index 0000000..d8b10b7 --- /dev/null +++ b/code/old/spi-support/src/test/resources/invalid-properties.xml @@ -0,0 +1,25 @@ +<?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. +--> + +<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> +<properties> + <entry key="a"> + <entry key="b">1</entry> +</properties> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/test/resources/non-xml-properties.xml ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/test/resources/non-xml-properties.xml b/code/old/spi-support/src/test/resources/non-xml-properties.xml new file mode 100644 index 0000000..8de819a --- /dev/null +++ b/code/old/spi-support/src/test/resources/non-xml-properties.xml @@ -0,0 +1,18 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/test/resources/overrideOrdinal.properties ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/test/resources/overrideOrdinal.properties b/code/old/spi-support/src/test/resources/overrideOrdinal.properties new file mode 100644 index 0000000..c68208a --- /dev/null +++ b/code/old/spi-support/src/test/resources/overrideOrdinal.properties @@ -0,0 +1,25 @@ +# 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. + +#overrideValue ordinal +tamaya.ordinal=16784 + +mykey1=myval1 +mykey2=myval2 +mykey3=myval3 +mykey4=myval4 +mykey5=myval5 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/test/resources/testfile.properties ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/test/resources/testfile.properties b/code/old/spi-support/src/test/resources/testfile.properties new file mode 100644 index 0000000..abd7ee8 --- /dev/null +++ b/code/old/spi-support/src/test/resources/testfile.properties @@ -0,0 +1,22 @@ +# 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. + +key1=val1 +key2=val2 +key3=val3 +key4=val4 +key5=val5 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/test/resources/valid-properties.xml ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/test/resources/valid-properties.xml b/code/old/spi-support/src/test/resources/valid-properties.xml new file mode 100644 index 0000000..7eb51d9 --- /dev/null +++ b/code/old/spi-support/src/test/resources/valid-properties.xml @@ -0,0 +1,25 @@ +<?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. +--> + +<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> +<properties> + <entry key="a">b</entry> + <entry key="b">1</entry> +</properties> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/pom.xml ---------------------------------------------------------------------- diff --git a/code/pom.xml b/code/pom.xml index 719d498..ad9c610 100644 --- a/code/pom.xml +++ b/code/pom.xml @@ -33,9 +33,9 @@ under the License. <name>Apache Tamaya Code - all</name> <modules> - <module>api</module> - <module>spi-support</module> + <module>base</module> <module>core</module> + <module>compat</module> </modules> </project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/spi-support/bnd.bnd ---------------------------------------------------------------------- diff --git a/code/spi-support/bnd.bnd b/code/spi-support/bnd.bnd deleted file mode 100644 index 876ca2e..0000000 --- a/code/spi-support/bnd.bnd +++ /dev/null @@ -1,26 +0,0 @@ --buildpath: \ - osgi.annotation; version=6.0.0,\ - osgi.core; version=6.0,\ - osgi.cmpn; version=6.0 - --testpath: \ - ${junit} - -javac.source: 1.8 -javac.target: 1.8 - -Bundle-Version: ${version}.${tstamp} -Bundle-Name: Apache Tamaya - SPI Support -Bundle-SymbolicName: org.apache.tamaya.spisupport -Bundle-Description: Apacha Tamaya Config - SPI Support -Bundle-Category: Implementation -Bundle-Copyright: (C) Apache Foundation -Bundle-License: Apache Licence version 2 -Bundle-Vendor: Apache Software Foundation -Bundle-ContactAddress: [email protected] -Bundle-DocURL: http://tamaya.apache.org -Export-Package: \ - org.apache.tamaya.spisupport,org.apache.tamaya.spisupport.propertysource -Import-Package: \ - org.apache.tamaya,\ - org.apache.tamaya.spi http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/spi-support/pom.xml ---------------------------------------------------------------------- diff --git a/code/spi-support/pom.xml b/code/spi-support/pom.xml deleted file mode 100644 index 4135834..0000000 --- a/code/spi-support/pom.xml +++ /dev/null @@ -1,81 +0,0 @@ -<!-- -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 current 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. ---> -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - - <parent> - <groupId>org.apache.tamaya</groupId> - <artifactId>tamaya-code</artifactId> - <version>0.4-incubating-SNAPSHOT</version> - </parent> - - <artifactId>tamaya-spisupport</artifactId> - <name>Apache Tamaya Core SPI Support</name> - <description>Apache Tamaya Support Classes useful when implementing the Tamaya SPI or code independent of the core RI - implementation.</description> - <packaging>jar</packaging> - - <dependencies> - <dependency> - <groupId>org.apache.tamaya</groupId> - <artifactId>tamaya-api</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.osgi</groupId> - <artifactId>org.osgi.compendium</artifactId> - <scope>provided</scope> - <optional>true</optional> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - </dependency> - <dependency> - <groupId>org.hamcrest</groupId> - <artifactId>java-hamcrest</artifactId> - </dependency> - <dependency> - <groupId>org.assertj</groupId> - <artifactId>assertj-core</artifactId> - </dependency> - <dependency> - <groupId>org.mockito</groupId> - <artifactId>mockito-core</artifactId> - </dependency> - </dependencies> - - <build> - <plugins> - <plugin> - <!-- - ! See https://issues.apache.org/jira/browse/TAMAYA-318 - !--> - <groupId>org.pitest</groupId> - <artifactId>pitest-maven</artifactId> - <configuration> - <skip>true</skip> - </configuration> - </plugin> - </plugins> - </build> - -</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigValueEvaluator.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigValueEvaluator.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigValueEvaluator.java deleted file mode 100644 index 92fd614..0000000 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigValueEvaluator.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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.tamaya.spisupport; - -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.PropertyValue; - -import java.util.Map; - - -/** - * Component SPI which encapsulates the evaluation of a single or full <b>raw</b>value - * for a {@link ConfigurationContext}. - */ -public interface ConfigValueEvaluator { - - /** - * Evaluates single value using a {@link ConfigurationContext}. - * @param key the config key, not null. - * @param context the context, not null. - * @return the value, or null. - */ - PropertyValue evaluteRawValue(String key, ConfigurationContext context); - - /** - * Evaluates all property values from a {@link ConfigurationContext}. - * @param context the context, not null. - * @return the value, or null. - */ - Map<String, PropertyValue> evaluateRawValues(ConfigurationContext context); - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigurationBuilder.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigurationBuilder.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigurationBuilder.java deleted file mode 100644 index b764ed6..0000000 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigurationBuilder.java +++ /dev/null @@ -1,334 +0,0 @@ -/* - * 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.tamaya.spisupport; - - - -import org.apache.tamaya.Configuration; -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.*; - -import java.util.Collection; -import java.util.Comparator; -import java.util.List; -import java.util.Map; - -/** - * A builder for creating new or adapting instances of {@link Configuration}. - * Builders can be obtained in exactly two ways: - * <ol> - * <li>By accessing a preinitialized builder from an existing {@link Configuration}, - * by calling {@link org.apache.tamaya.Configuration#toBuilder()}.</li> - * <li>By accessing an empty builder instance from - * {@link org.apache.tamaya.ConfigurationProvider#getConfigurationBuilder()}.</li> - * </ol> - */ -public interface ConfigurationBuilder { - - /** - * Init this builder instance with the given {@link ConfigurationContext} instance. This - * method will use any existing property sources, filters, converters and the combination - * policy of the given {@link ConfigurationContext} and initialize the current builder - * with them. - * - * @param context the {@link ConfigurationContext} instance to be used, not {@code null}. - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder setContext(ConfigurationContext context); - - /** - * This method can be used for adding {@link PropertySource}s. - * Hereby the property source is added to the tail of property sources with - * lowest priority regardless of its current ordinal value. To sort the property - * sources based on their ordinals call {@link #sortPropertySources}. - * - * @param propertySources the PropertySources to add - * @return this builder, for chaining, never null. - * @throws IllegalArgumentException If a property source with a given name already - * exists. - */ - ConfigurationBuilder addPropertySources(PropertySource... propertySources); - - /** - * This method can be used for programmatically adding {@link PropertySource}s. - * Hereby the property source is added to the tail of property sources with - * lowest priority regardless of its current ordinal value. To sort the property - * sources based on their ordinals call {@link #sortPropertySources}. - * - * @param propertySources the PropertySources to add - * @return this builder, for chaining, never null. - * @throws IllegalArgumentException If a property source with a given name already - * exists. - */ - ConfigurationBuilder addPropertySources(Collection<PropertySource> propertySources); - - /** - * Add all registered (default) property sources to the context built. The sources are ordered - * based on their ordinal values and added to the chain of property sources with - * higher priority. - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder addDefaultPropertySources(); - - /** - * Removes the given property sources, if existing. The existing order of property - * sources is preserved. - * - * @param propertySources the property sources to remove, not {@code null}. - * @return the builder for chaining. - */ - ConfigurationBuilder removePropertySources(PropertySource... propertySources); - - /** - * Removes the given property sources, if existing. The existing order of property - * sources is preserved. - * - * @param propertySources the property sources to remove, not {@code null}. - * @return the builder for chaining. - */ - ConfigurationBuilder removePropertySources(Collection<PropertySource> propertySources); - - /** - * Access the current chain of property sources. Items at the end of the list have - * precedence/more significance. - * - * @return the property source chain, never {@code null}. - */ - List<PropertySource> getPropertySources(); - - /** - * Access the current chain of property filters. Items at the end of the list have - * precedence/more significance. - * - * @return the property source chain, never {@code null}. - */ - List<PropertyFilter> getPropertyFilters(); - - /** - * Access the current registered property converters. - * - * @return the current registered property converters. - */ - Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> getPropertyConverter(); - - /** - * Increases the priority of the given property source, by moving it towards the end - * of the chain of property sources. If the property source given is already at the end - * this method has no effect. This operation does not change any ordinal values. - * - * @param propertySource the property source to be incresed regarding its significance. - * @return the builder for chaining. - * @throws IllegalArgumentException If no such property source exists in the current - * chain. - */ - ConfigurationBuilder increasePriority(PropertySource propertySource); - - /** - * Decreases the priority of the given property source, by moving it towards the start - * of the chain of property sources. If the property source given is already the first - * this method has no effect. This operation does not change any ordinal values. - * - * @param propertySource the property source to be decresed regarding its significance. - * @return the builder for chaining. - * @throws IllegalArgumentException If no such property source exists in the current - * chain. - */ - ConfigurationBuilder decreasePriority(PropertySource propertySource); - - /** - * Increases the priority of the given property source to be maximal, by moving it to - * the tail of the of property source chain. If the property source given is - * already the last item this method has no effect. This operation does not change - * any ordinal values. - * - * @param propertySource the property source to be maximized regarding its significance. - * @return the builder for chaining. - * @throws IllegalArgumentException If no such property source exists in the current - * chain. - */ - ConfigurationBuilder highestPriority(PropertySource propertySource); - - /** - * Decreases the priority of the given property source to be minimal, by moving it to - * the start of the chain of property source chain. If the property source given is - * already the first item this method has no effect. This operation does not change - * any ordinal values. - * - * @param propertySource the property source to be minimized regarding its significance. - * @return the builder for chaining. - * @throws IllegalArgumentException If no such property source exists in the current - * chain. - */ - ConfigurationBuilder lowestPriority(PropertySource propertySource); - - /** - * Adds the given PropertyFilter instances, hereby the instances are added - * to the end of the list with highest priority. The ordering of existing - * property filters remains unchanged. To sort the property - * filters call {@link #sortPropertyFilter}. - * - * @param filters the filters to add - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder addPropertyFilters(PropertyFilter... filters); - - /** - * Adds the given PropertyFilter instances, hereby the instances are added - * to the end of the list with highest priority. The ordering of existing - * property filters remains unchanged. To sort the property - * filters call {@link #sortPropertyFilter}. - * - * @param filters the filters to add - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder addPropertyFilters(Collection<PropertyFilter> filters); - - /** - * Add all registered (default) property filters to the context built. - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder addDefaultPropertyFilters(); - - - /** - * Removes the given PropertyFilter instances, if existing. The order of the remaining - * filters is preserved. - * - * @param filters the filter to remove - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder removePropertyFilters(PropertyFilter... filters); - - /** - * Removes the given PropertyFilter instances, if existing. The order of the remaining - * filters is preserved. - * - * @param filters the filter to remove - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder removePropertyFilters(Collection<PropertyFilter> filters); - - /** - * This method can be used for adding {@link PropertyConverter}s. - * Converters are added at the end after any existing converters. - * For converters already registered for the current target type the - * method has no effect. - * - * @param typeToConvert the type for which the converters is for - * @param propertyConverters the PropertyConverters to add for this type - * @param <T> the target type. - * @return this builder, for chaining, never null. - */ - <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> typeToConvert, - PropertyConverter<T>... propertyConverters); - - /** - * This method can be used for adding {@link PropertyConverter}s. - * Converters are added at the end after any existing converters. - * For converters already registered for the current target type the - * method has no effect. - * - * @param typeToConvert the type for which the converters is for - * @param propertyConverters the PropertyConverters to add for this type - * @param <T> the target type. - * @return this builder, for chaining, never null. - */ - <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> typeToConvert, - Collection<PropertyConverter<T>> propertyConverters); - - /** - * Add all registered (default) property converters to the context built. - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder addDefaultPropertyConverters(); - - /** - * Removes the given PropertyConverter instances for the given type, - * if existing. - * - * @param typeToConvert the type which the converters is for - * @param propertyConverters the converters to remove - * @param <T> the target type. - * @return this builder, for chaining, never null. - */ - <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, - PropertyConverter<T>... propertyConverters); - - /** - * Removes the given PropertyConverter instances for the given type, - * if existing. - * - * @param typeToConvert the type which the converters is for - * @param propertyConverters the converters to remove - * @param <T> the target type. - * @return this builder, for chaining, never null. - */ - <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, - Collection<PropertyConverter<T>> propertyConverters); - - /** - * Removes all converters for the given type, which actually renders a given type - * unsupported for type conversion. - * - * @param typeToConvert the type which the converters is for - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder removePropertyConverters(TypeLiteral<?> typeToConvert); - - /** - * Sorts the current registered property sources using the given comparator. - * - * NOTE: property sources at the beginning have minimal significance. - * - * @param comparator the comparator to be used, not {@code null}. - * @return this instance for chaining. - */ - ConfigurationBuilder sortPropertySources(Comparator<PropertySource> comparator); - - /** - * Sorts the current registered property filters using the given comparator. - * - * NOTE: property filters at the beginning have minimal significance. - * - * @param comparator the comparator to be used, not {@code null}. - * @return this instance for chaining. - */ - ConfigurationBuilder sortPropertyFilter(Comparator<PropertyFilter> comparator); - - /** - * Sets the {@link PropertyValueCombinationPolicy} used to evaluate the final - * property values. - * - * @param policy the {@link PropertyValueCombinationPolicy} used, not {@code null}. - * @return this builder, for chaining, never null. - */ - ConfigurationBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy); - - /** - * Builds a new {@link Configuration} based on the data in this builder. The ordering of property - * sources and property filters is not changed, regardless of their ordinals. For ensure a certain - * ordering/significance call {@link #sortPropertyFilter(Comparator)} and/or {@link #sortPropertySources(Comparator)} - * before building the context. - * - * @return the final configuration. - */ - Configuration build(); - -} - http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluator.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluator.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluator.java deleted file mode 100644 index d50ed7d..0000000 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluator.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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.tamaya.spisupport; - -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.PropertyFilter; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; - -import java.util.HashMap; -import java.util.Map; - - -/** - * Implementation of the Configuration API. This class uses the current {@link ConfigurationContext} to evaluate the - * chain of {@link PropertySource} and {@link PropertyFilter} - * instance to evaluate the current Configuration. - */ -public class DefaultConfigValueEvaluator implements ConfigValueEvaluator{ - - @Override - public PropertyValue evaluteRawValue(String key, ConfigurationContext context) { - PropertyValue unfilteredValue = null; - for (PropertySource propertySource : context.getPropertySources()) { - unfilteredValue = context.getPropertyValueCombinationPolicy(). - collect(unfilteredValue, key, propertySource); - } - if(unfilteredValue==null || unfilteredValue.getValue()==null){ - return null; - } - return unfilteredValue; - } - - @Override - public Map<String, PropertyValue> evaluateRawValues(ConfigurationContext context) { - Map<String, PropertyValue> result = new HashMap<>(); - for (PropertySource propertySource : context.getPropertySources()) { - for (Map.Entry<String,PropertyValue> propEntry: propertySource.getProperties().entrySet()) { - PropertyValue unfilteredValue = result.get(propEntry.getKey()); - unfilteredValue = context.getPropertyValueCombinationPolicy(). - collect(unfilteredValue, propEntry.getKey(), propertySource); - if(unfilteredValue!=null){ - result.put(unfilteredValue.getKey(), unfilteredValue); - } - } - } - return result; - } - - @Override - public String toString() { - return "DefaultConfigEvaluator{}"; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java deleted file mode 100644 index fbcf35f..0000000 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java +++ /dev/null @@ -1,283 +0,0 @@ -/* - * 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.tamaya.spisupport; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.ConfigOperator; -import org.apache.tamaya.ConfigQuery; -import org.apache.tamaya.Configuration; -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConversionContext; -import org.apache.tamaya.spi.PropertyConverter; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; -import org.apache.tamaya.spi.PropertyValueCombinationPolicy; -import org.apache.tamaya.spi.ServiceContextManager; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Implementation of the Configuration API. This class uses the current {@link ConfigurationContext} to evaluate the - * chain of {@link PropertySource} and {@link org.apache.tamaya.spi.PropertyFilter} - * instance to evaluate the current Configuration. - */ -public class DefaultConfiguration implements Configuration { - /** - * The logger. - */ - private static final Logger LOG = Logger.getLogger(DefaultConfiguration.class.getName()); - - /** - * The current {@link ConfigurationContext} of the current instance. - */ - private final ConfigurationContext configurationContext; - - /** - * EvaluationStrategy - */ - private ConfigValueEvaluator configEvaluator = loadConfigValueEvaluator(); - - private ConfigValueEvaluator loadConfigValueEvaluator() { - ConfigValueEvaluator eval = null; - try{ - eval = ServiceContextManager.getServiceContext() - .getService(ConfigValueEvaluator.class); - }catch(Exception e){ - LOG.log(Level.WARNING, "Failed to load ConfigValueEvaluator from ServiceContext, using default.", e); - } - if(eval==null){ - eval = new DefaultConfigValueEvaluator(); - } - return eval; - } - - - /** - * Constructor. - * @param configurationContext The configuration Context to be used. - */ - public DefaultConfiguration(ConfigurationContext configurationContext){ - this.configurationContext = Objects.requireNonNull(configurationContext); - } - - /** - * Get a given value, filtered with the context's filters as needed. - * @param key the property's key, not null. - * @return the filtered value, or null. - */ - @Override - public String get(String key) { - Objects.requireNonNull(key, "Key must not be null."); - - PropertyValue value = configEvaluator.evaluteRawValue(key, configurationContext); - if(value==null || value.getValue()==null){ - return null; - } - value = PropertyFiltering.applyFilter(value, configurationContext); - if(value!=null){ - return value.getValue(); - } - return null; - } - - /** - * Evaluates the raw value using the context's PropertyValueCombinationPolicy. - * @param key the key, not null. - * @return the value, before filtering is applied. - */ - protected PropertyValue evaluteRawValue(String key) { - List<PropertySource> propertySources = configurationContext.getPropertySources(); - PropertyValue filteredValue = null; - PropertyValueCombinationPolicy combinationPolicy = this.configurationContext - .getPropertyValueCombinationPolicy(); - for (PropertySource propertySource : propertySources) { - filteredValue = combinationPolicy.collect(filteredValue, key, propertySource); - } - return filteredValue; - } - - - @Override - public String getOrDefault(String key, String defaultValue) { - Objects.requireNonNull(key, "Key must not be null."); - - String val = get(key); - if(val==null){ - return defaultValue; - } - return val; - } - - @Override - public <T> T getOrDefault(String key, Class<T> type, T defaultValue) { - Objects.requireNonNull(key, "Key must not be null."); - Objects.requireNonNull(type, "Target type must not be null"); - - T val = get(key, type); - if(val==null){ - return defaultValue; - } - return val; - } - - /** - * Get the current properties, composed by the loaded {@link PropertySource} and filtered - * by registered {@link org.apache.tamaya.spi.PropertyFilter}. - * - * @return the final properties. - */ - @Override - public Map<String, String> getProperties() { - Map<String, PropertyValue> filtered = PropertyFiltering.applyFilters( - configEvaluator.evaluateRawValues(configurationContext), - configurationContext); - Map<String,String> result = new HashMap<>(); - for(PropertyValue val:filtered.values()){ - if(val.getValue()!=null) { - result.put(val.getKey(), val.getValue()); - // TODO: Discuss metadata handling... - result.putAll(val.getMetaEntries()); - } - } - return result; - } - - - /** - * Accesses the current String value for the given key and tries to convert it - * using the {@link PropertyConverter} instances provided by the current - * {@link ConfigurationContext}. - * - * @param key the property's absolute, or relative path, e.g. @code - * a/b/c/d.myProperty}, never {@code null}. - * @param type The target type required, not {@code null}. - * @param <T> the value type - * @return the converted value, never {@code null}. - */ - @Override - public <T> T get(String key, Class<T> type) { - return get(key, (TypeLiteral<T>)TypeLiteral.of(type)); - } - - /** - * Accesses the current String value for the given key and tries to convert it - * using the {@link PropertyConverter} instances provided by the current - * {@link ConfigurationContext}. - * - * @param key the property's absolute, or relative path, e.g. @code - * a/b/c/d.myProperty}. - * @param type The target type required, not null. - * @param <T> the value type - * @return the converted value, never null. - */ - @Override - public <T> T get(String key, TypeLiteral<T> type) { - Objects.requireNonNull(key, "Key must not be null."); - Objects.requireNonNull(type, "Target type must not be null"); - - return convertValue(key, get(key), type); - } - - @SuppressWarnings("unchecked") - protected <T> T convertValue(String key, String value, TypeLiteral<T> type) { - if (value != null) { - List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type); - ConversionContext context = new ConversionContext.Builder(this, this.configurationContext, key, type) - .build(); - for (PropertyConverter<T> converter : converters) { - try { - T t = converter.convert(value, context); - if (t != null) { - return t; - } - } catch (Exception e) { - LOG.log(Level.FINEST, "PropertyConverter: " + converter + " failed to convert value: " + value, e); - } - } - // if the target type is a String, we can return the value, no conversion required. - if(type.equals(TypeLiteral.of(String.class))){ - return (T)value; - } - // unsupported type, throw an exception - throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName() + ": " + key + - ", supported formats: " + context.getSupportedFormats()); - } - return null; - } - - @Override - public <T> T getOrDefault(String key, TypeLiteral<T> type, T defaultValue) { - Objects.requireNonNull(key); - Objects.requireNonNull(type); - - T val = get(key, type); - if(val==null){ - return defaultValue; - } - return val; - } - - @Override - public Configuration with(ConfigOperator operator) { - return operator.operate(this); - } - - @Override - public <T> T query(ConfigQuery<T> query) { - return query.query(this); - } - - @Override - public ConfigurationContext getContext() { - return this.configurationContext; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - DefaultConfiguration that = (DefaultConfiguration) o; - - if (!configurationContext.equals(that.configurationContext)) return false; - return configEvaluator.getClass().equals(that.configEvaluator.getClass()); - } - - @Override - public int hashCode() { - int result = configurationContext.hashCode(); - result = 31 * result + configEvaluator.getClass().hashCode(); - return result; - } - - @Override - public String toString() { - return "Configuration{\n " + - configurationContext + - '}'; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java deleted file mode 100644 index ec26ba6..0000000 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java +++ /dev/null @@ -1,236 +0,0 @@ -/* - * 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.tamaya.spisupport; - -import org.apache.tamaya.Configuration; -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.*; -import org.apache.tamaya.spi.ConfigurationBuilder; - -import java.util.*; - -/** - * Default implementation of {@link ConfigurationBuilder}. - */ -public class DefaultConfigurationBuilder implements ConfigurationBuilder { - - protected final DefaultConfigurationContextBuilder contextBuilder; - - /** - * Creates a new builder instance. - */ - public DefaultConfigurationBuilder() { - this.contextBuilder = new DefaultConfigurationContextBuilder(); - } - - /** - * Creates a new builder instance. - */ - public DefaultConfigurationBuilder(ConfigurationContext context) { - this.contextBuilder = new DefaultConfigurationContextBuilder(context); - } - - /** - * Creates a new builder instance initializing it with the given context. - * @param configuration the configuration to be used, not null. - */ - public DefaultConfigurationBuilder(Configuration configuration) { - this.contextBuilder = new DefaultConfigurationContextBuilder(configuration.getContext()); - } - - /** - * Allows to set configuration context during unit tests. - */ - public ConfigurationBuilder setConfiguration(Configuration configuration) { - this.contextBuilder.setContext(configuration.getContext()); - return this; - } - - - @Override - public ConfigurationBuilder setContext(ConfigurationContext context) { - this.contextBuilder.setContext(context); - return this; - } - - @Override - public ConfigurationBuilder addPropertySources(PropertySource... sources){ - this.contextBuilder.addPropertySources(sources); - return this; - } - - @Override - public ConfigurationBuilder addPropertySources(Collection<PropertySource> sources){ - this.contextBuilder.addPropertySources(sources); - return this; - } - - public ConfigurationBuilder addDefaultPropertyFilters() { - this.contextBuilder.addDefaultPropertyFilters(); - return this; - } - - public ConfigurationBuilder addDefaultPropertySources() { - this.contextBuilder.addDefaultPropertySources(); - return this; - } - - public ConfigurationBuilder addDefaultPropertyConverters() { - this.contextBuilder.addDefaultPropertyConverters(); - return this; - } - - @Override - public ConfigurationBuilder removePropertySources(PropertySource... propertySources) { - this.contextBuilder.removePropertySources(propertySources); - return this; - } - - @Override - public ConfigurationBuilder removePropertySources(Collection<PropertySource> propertySources) { - this.contextBuilder.removePropertySources(propertySources); - return this; - } - - @Override - public List<PropertySource> getPropertySources() { - return this.contextBuilder.getPropertySources(); - } - - @Override - public ConfigurationBuilder increasePriority(PropertySource propertySource) { - this.contextBuilder.increasePriority(propertySource); - return this; - } - - @Override - public ConfigurationBuilder decreasePriority(PropertySource propertySource) { - this.contextBuilder.decreasePriority(propertySource); - return this; - } - - @Override - public ConfigurationBuilder highestPriority(PropertySource propertySource) { - this.contextBuilder.highestPriority(propertySource); - return this; - } - - @Override - public ConfigurationBuilder lowestPriority(PropertySource propertySource) { - this.contextBuilder.lowestPriority(propertySource); - return this; - } - - @Override - public ConfigurationBuilder addPropertyFilters(PropertyFilter... filters){ - this.contextBuilder.addPropertyFilters(filters); - return this; - } - - @Override - public ConfigurationBuilder addPropertyFilters(Collection<PropertyFilter> filters){ - this.contextBuilder.addPropertyFilters(filters); - return this; - } - - @Override - public ConfigurationBuilder removePropertyFilters(PropertyFilter... filters) { - this.contextBuilder.removePropertyFilters(filters); - return this; - } - - @Override - public ConfigurationBuilder removePropertyFilters(Collection<PropertyFilter> filters) { - this.contextBuilder.removePropertyFilters(filters); - return this; - } - - - @Override - public <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, - PropertyConverter<T>... converters) { - this.contextBuilder.removePropertyConverters(typeToConvert, converters); - return this; - } - - @Override - public <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, - Collection<PropertyConverter<T>> converters) { - this.contextBuilder.removePropertyConverters(typeToConvert, converters); - return this; - } - - @Override - public ConfigurationBuilder removePropertyConverters(TypeLiteral<?> typeToConvert) { - this.contextBuilder.removePropertyConverters(typeToConvert); - return this; - } - - - @Override - public ConfigurationBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy combinationPolicy){ - this.contextBuilder.setPropertyValueCombinationPolicy(combinationPolicy); - return this; - } - - @Override - public <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> type, PropertyConverter<T>... propertyConverters){ - this.contextBuilder.addPropertyConverters(type, propertyConverters); - return this; - } - - @Override - public <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> type, Collection<PropertyConverter<T>> propertyConverters){ - this.contextBuilder.addPropertyConverters(type, propertyConverters); - return this; - } - - /** - * Builds a new configuration based on the configuration of this builder instance. - * - * @return a new {@link org.apache.tamaya.Configuration configuration instance}, - * never {@code null}. - */ - @Override - public Configuration build() { - return new DefaultConfiguration(this.contextBuilder.build()); - } - - @Override - public ConfigurationBuilder sortPropertyFilter(Comparator<PropertyFilter> comparator) { - this.contextBuilder.sortPropertyFilter(comparator); - return this; - } - - @Override - public ConfigurationBuilder sortPropertySources(Comparator<PropertySource> comparator) { - this.contextBuilder.sortPropertySources(comparator); - return this; - } - - @Override - public List<PropertyFilter> getPropertyFilters() { - return this.contextBuilder.getPropertyFilters(); - } - - @Override - public Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> getPropertyConverter() { - return this.contextBuilder.getPropertyConverter(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java ---------------------------------------------------------------------- diff --git a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java b/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java deleted file mode 100644 index d880e8d..0000000 --- a/code/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java +++ /dev/null @@ -1,277 +0,0 @@ -/* - * 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.tamaya.spisupport; - -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.*; - -import java.util.*; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.logging.Logger; - -/** - * Default implementation of a simple ConfigurationContext. - */ -public class DefaultConfigurationContext implements ConfigurationContext { - - /** The logger used. */ - private final static Logger LOG = Logger.getLogger(DefaultConfigurationContext.class.getName()); - - /** - * Subcomponent handling {@link PropertyConverter} instances. - */ - private final PropertyConverterManager propertyConverterManager = new PropertyConverterManager(); - - /** - * The current unmodifiable list of loaded {@link PropertySource} instances. - */ - private List<PropertySource> immutablePropertySources; - - /** - * The current unmodifiable list of loaded {@link PropertyFilter} instances. - */ - private List<PropertyFilter> immutablePropertyFilters; - - /** - * The overriding policy used when combining PropertySources registered to evalute the final configuration - * values. - */ - private PropertyValueCombinationPolicy propertyValueCombinationPolicy; - - /** - * Lock for internal synchronization. - */ - private final ReentrantReadWriteLock propertySourceLock = new ReentrantReadWriteLock(); - - @SuppressWarnings("unchecked") - protected DefaultConfigurationContext(DefaultConfigurationContextBuilder builder) { - List<PropertySource> propertySources = new ArrayList<>(); - // first we load all PropertySources which got registered via java.util.ServiceLoader - propertySources.addAll(builder.propertySources); - // now sort them according to their ordinal values - immutablePropertySources = Collections.unmodifiableList(propertySources); - - // as next step we pick up the PropertyFilters pretty much the same way - List<PropertyFilter> propertyFilters = new ArrayList<>(builder.getPropertyFilters()); - immutablePropertyFilters = Collections.unmodifiableList(propertyFilters); - - // Finally add the converters - for(Map.Entry<TypeLiteral<?>, Collection<PropertyConverter<?>>> en:builder.getPropertyConverter().entrySet()) { - for (@SuppressWarnings("rawtypes") PropertyConverter converter : en.getValue()) { - this.propertyConverterManager.register(en.getKey(), converter); - } - } - LOG.info("Registered " + propertyConverterManager.getPropertyConverters().size() + " property converters: " + - propertyConverterManager.getPropertyConverters()); - - propertyValueCombinationPolicy = builder.combinationPolicy; - if(propertyValueCombinationPolicy==null){ - propertyValueCombinationPolicy = ServiceContextManager.getServiceContext().getService(PropertyValueCombinationPolicy.class); - } - if(propertyValueCombinationPolicy==null){ - propertyValueCombinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR; - } - LOG.info("Using PropertyValueCombinationPolicy: " + propertyValueCombinationPolicy); - } - - - @Deprecated - @Override - public void addPropertySources(PropertySource... propertySourcesToAdd) { - Lock writeLock = propertySourceLock.writeLock(); - try { - writeLock.lock(); - List<PropertySource> newPropertySources = new ArrayList<>(this.immutablePropertySources); - newPropertySources.addAll(Arrays.asList(propertySourcesToAdd)); - Collections.sort(newPropertySources, PropertySourceComparator.getInstance()); - - this.immutablePropertySources = Collections.unmodifiableList(newPropertySources); - } finally { - writeLock.unlock(); - } - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof DefaultConfigurationContext)){ - return false; - } - - DefaultConfigurationContext that = (DefaultConfigurationContext) o; - - if (!propertyConverterManager.equals(that.propertyConverterManager)) { - return false; - } - if (!immutablePropertySources.equals(that.immutablePropertySources)) { - return false; - } - if (!immutablePropertyFilters.equals(that.immutablePropertyFilters)) { - return false; - } - return getPropertyValueCombinationPolicy().equals(that.getPropertyValueCombinationPolicy()); - - } - - @Override - public int hashCode() { - int result = propertyConverterManager.hashCode(); - result = 31 * result + immutablePropertySources.hashCode(); - result = 31 * result + immutablePropertyFilters.hashCode(); - result = 31 * result + getPropertyValueCombinationPolicy().hashCode(); - return result; - } - - @Override - public String toString() { - StringBuilder b = new StringBuilder("ConfigurationContext{\n"); - b.append(" Property Sources\n"); - b.append(" ----------------\n"); - if(immutablePropertySources.isEmpty()){ - b.append(" No property sources loaded.\n\n"); - }else { - b.append(" CLASS NAME ORDINAL SCANNABLE SIZE STATE ERROR\n\n"); - for (PropertySource ps : immutablePropertySources) { - b.append(" "); - appendFormatted(b, ps.getClass().getSimpleName(), 30); - appendFormatted(b, ps.getName(), 70); - appendFormatted(b, String.valueOf(PropertySourceComparator.getOrdinal(ps)), 8); - appendFormatted(b, String.valueOf(ps.isScannable()), 10); - if (ps.isScannable()) { - appendFormatted(b, String.valueOf(ps.getProperties().size()), 8); - } else { - appendFormatted(b, "-", 8); - } - PropertyValue state = ps.get("_state"); - if(state==null){ - appendFormatted(b, "OK", 10); - }else { - appendFormatted(b, state.getValue(), 10); - if("ERROR".equals(state.getValue())){ - PropertyValue val = ps.get("_exception"); - if(val!=null) { - appendFormatted(b, val.getValue(), 30); - } - } - } - b.append('\n'); - } - b.append("\n"); - } - b.append(" Property Filters\n"); - b.append(" ----------------\n"); - if(immutablePropertyFilters.isEmpty()){ - b.append(" No property filters loaded.\n\n"); - }else { - b.append(" CLASS INFO\n\n"); - for (PropertyFilter filter : getPropertyFilters()) { - b.append(" "); - appendFormatted(b, filter.getClass().getSimpleName(), 30); - b.append(removeNewLines(filter.toString())); - b.append('\n'); - } - b.append("\n\n"); - } - b.append(" Property Converters\n"); - b.append(" -------------------\n"); - b.append(" CLASS TYPE INFO\n\n"); - for(Map.Entry<TypeLiteral<?>, List<PropertyConverter<?>>> converterEntry:getPropertyConverters().entrySet()){ - for(PropertyConverter converter: converterEntry.getValue()){ - b.append(" "); - appendFormatted(b, converter.getClass().getSimpleName(), 30); - appendFormatted(b, converterEntry.getKey().getRawType().getSimpleName(), 30); - b.append(removeNewLines(converter.toString())); - b.append('\n'); - } - } - b.append("\n\n"); - b.append(" PropertyValueCombinationPolicy: " + getPropertyValueCombinationPolicy().getClass().getName()).append('\n'); - b.append('}'); - return b.toString(); - } - - private void appendFormatted(StringBuilder b, String text, int length) { - int padding; - if(text.length() <= (length)){ - b.append(text); - padding = length - text.length(); - }else{ - b.append(text.substring(0, length-1)); - padding = 1; - } - for(int i=0;i<padding;i++){ - b.append(' '); - } - } - - private String removeNewLines(String s) { - return s.replace('\n', ' ').replace('\r', ' '); - } - - - @Override - public List<PropertySource> getPropertySources() { - return immutablePropertySources; - } - - @Override - public PropertySource getPropertySource(String name) { - for(PropertySource ps:getPropertySources()){ - if(name.equals(ps.getName())){ - return ps; - } - } - return null; - } - - @Override - public <T> void addPropertyConverter(TypeLiteral<T> typeToConvert, PropertyConverter<T> propertyConverter) { - propertyConverterManager.register(typeToConvert, propertyConverter); - LOG.info("Added PropertyConverter: " + propertyConverter.getClass().getName()); - } - - @Override - public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() { - return propertyConverterManager.getPropertyConverters(); - } - - @Override - public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> targetType) { - return propertyConverterManager.getPropertyConverters(targetType); - } - - @Override - public List<PropertyFilter> getPropertyFilters() { - return immutablePropertyFilters; - } - - @Override - public PropertyValueCombinationPolicy getPropertyValueCombinationPolicy(){ - return propertyValueCombinationPolicy; - } - - @Override - public ConfigurationContextBuilder toBuilder() { - return new DefaultConfigurationContextBuilder(this); - } - -}
