tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ple...@apache.org
Subject [1/3] incubator-tamaya git commit: Moved API module to ./code/
Date Sun, 20 Dec 2015 12:47:51 GMT
Repository: incubator-tamaya
Updated Branches:
  refs/heads/master 9915f3da3 -> 84d52f16b


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/code/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java
----------------------------------------------------------------------
diff --git a/code/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java
b/code/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java
new file mode 100644
index 0000000..dbda457
--- /dev/null
+++ b/code/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java
@@ -0,0 +1,63 @@
+/*
+ * 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.spi;
+
+/**
+ * Policy that determines how the final value of a configuration entry is evaluated. An instances
of this
+ * interface can be registered to get control how multiple PropertySources are combined.
This is useful in cases
+ * where the default overriding policy as implemented in {@link #DEFAULT_OVERRIDING_COLLECTOR}
is not matching
+ * the need of the current application, e.g. then entries containing multiple values should
be combined to new
+ * values instead of overridden.
+ */
+public interface PropertyValueCombinationPolicy {
+
+    /**
+     * Default overriding collector, where each existing entry ({@code current} is overridden
by a subsequent non-null
+     * entry evaluated by {@code propertySource.get(key)}.
+     */
+    public static final PropertyValueCombinationPolicy DEFAULT_OVERRIDING_COLLECTOR = new
PropertyValueCombinationPolicy(){
+
+        @Override
+        public String collect(String currentValue, String key, PropertySource propertySource)
{
+            String value = propertySource.get(key);
+            return value!=null?value:currentValue;
+        }
+
+    };
+
+    /**
+     * Method that is called for each value evaluated by a PropertySource for the given key.
This method is called
+     * either when a single key is accessed, e.g. by calling {@code org.apache.tamaya.Configuration.getXXX},
but also
+     * when the full configuration property map is accessed by calling
+     * {@link org.apache.tamaya.Configuration#getProperties()}.
+     *
+     * @param currentValue the current value, including null.
+     *                     The collector should either combine the existing value with value
from {@code currentValue}
+     *                     or replace the value in {@code currentValue} with {@code valueRead},
hereby returning the
+     *                     result to be used as new {@code currentValue}.
+     * @param key The current key to be evaluated.
+     * @param propertySource The PropertySource that may return an value for the given key.
The PropertySource given
+     *                       may be evaluated for additional meta-data, how the given values
are to be combined.
+     *                       Note that the value returned by a PropertySource can be null.
In that case
+     *                       {@code currentValue} should be returned in almost all cases.
+     * @return the value to be used for future evaluation.
+     */
+    String collect(String currentValue, String key, PropertySource propertySource);
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/code/api/src/main/java/org/apache/tamaya/spi/ServiceContext.java
----------------------------------------------------------------------
diff --git a/code/api/src/main/java/org/apache/tamaya/spi/ServiceContext.java b/code/api/src/main/java/org/apache/tamaya/spi/ServiceContext.java
new file mode 100644
index 0000000..cdd481e
--- /dev/null
+++ b/code/api/src/main/java/org/apache/tamaya/spi/ServiceContext.java
@@ -0,0 +1,57 @@
+/*
+ * 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.spi;
+
+import java.util.List;
+
+
+/**
+ * This class models the component that is managing the lifecycle current the
+ * services used by the Configuration API.
+ */
+public interface ServiceContext {
+
+    /**
+     * @return ordinal of the ServiceContext. The one with the highest ordinal will be taken.
+     */
+    int ordinal();
+
+    /**
+     * Access a service singleton via its type.
+     * If multiple implementations for the very serviceType exist then
+     * the one with the highest {@link javax.annotation.Priority} will be used.
+     *
+     * @param serviceType the service type.
+     * @return The instance to be used, or {@code null}
+     * @throws org.apache.tamaya.ConfigException if there are multiple service implementations
with the maximum priority.
+     */
+    <T> T getService(Class<T> serviceType);
+
+    /**
+     * Access a list current services, given its type. The bootstrap mechanism should
+     * order the instance for precedence, hereby the most significant should be
+     * first in order.
+     *
+     * @param serviceType
+     *            the service type.
+     * @return The instance to be used, never {@code null}
+     */
+     <T> List<T> getServices(Class<T> serviceType);
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/code/api/src/main/java/org/apache/tamaya/spi/ServiceContextManager.java
----------------------------------------------------------------------
diff --git a/code/api/src/main/java/org/apache/tamaya/spi/ServiceContextManager.java b/code/api/src/main/java/org/apache/tamaya/spi/ServiceContextManager.java
new file mode 100644
index 0000000..259493c
--- /dev/null
+++ b/code/api/src/main/java/org/apache/tamaya/spi/ServiceContextManager.java
@@ -0,0 +1,125 @@
+/*
+ * 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.spi;
+
+import java.util.Objects;
+import java.util.ServiceLoader;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.tamaya.ConfigException;
+
+
+/**
+ * This singleton provides access to the services available in the current {@link ServiceContext}.
The
+ * behaviour can be adapted, by calling {@link ServiceContextManager#set(ServiceContext)}
before accessing any
+ * services.
+ */
+public final class ServiceContextManager {
+
+    /** The logger used. */
+    private static final Logger LOG = Logger.getLogger(ServiceContextManager.class.getName());
+
+    /**
+     * The ServiceProvider used.
+     */
+    private static volatile ServiceContext serviceContextProviderDelegate;
+
+    /**
+     * Private singletons constructor.
+     */
+    private ServiceContextManager() {
+    }
+
+    /**
+     * Load the {@link ServiceContext} to be used.
+     *
+     * @return {@link ServiceContext} to be used for loading the services.
+     */
+    private static ServiceContext loadDefaultServiceProvider() {
+        ServiceContext highestServiceContext = null;
+        try {
+            int highestOrdinal = 0;
+            for (ServiceContext serviceContext : ServiceLoader.load(ServiceContext.class))
{
+                if (serviceContext.ordinal() > highestOrdinal) {
+                    highestServiceContext = serviceContext;
+                    highestOrdinal = serviceContext.ordinal();
+                }
+            }
+        } catch (Exception e) {
+            throw new ConfigException("ServiceContext not loadable", e);
+        }
+//        if (highestServiceContext==null){
+//            String serviceContext = System.getProperty(ServiceContext.class.getName());
+//            if(serviceContext != null){
+//                try{
+//                    highestServiceContext = (ServiceContext)Class.forName(serviceContext).newInstance();
+//                } catch (Exception e) {
+//                    throw new ConfigException("Configured ServiceContext not loadable:
" + serviceContext, e);
+//                }
+//            }
+//        }
+        if (highestServiceContext==null){
+            throw new ConfigException("No ServiceContext found");
+        }
+        LOG.info("Using Service Context of type: " + highestServiceContext.getClass().getName());
+        return highestServiceContext;
+    }
+
+    /**
+     * Replace the current {@link ServiceContext} in use.
+     *
+     * @param serviceContextProvider the new {@link ServiceContext}, not null.
+     */
+    public static ServiceContext set(ServiceContext serviceContextProvider) {
+        ServiceContext currentContext = ServiceContextManager.serviceContextProviderDelegate;
+        Objects.requireNonNull(serviceContextProvider);
+
+        synchronized (ServiceContextManager.class) {
+            if (ServiceContextManager.serviceContextProviderDelegate == null) {
+                ServiceContextManager.serviceContextProviderDelegate = serviceContextProvider;
+                LOG.log(Level.INFO, "Using ServiceProvider: " + serviceContextProvider.getClass().getName());
+            } else {
+                LOG.log(Level.WARNING, "Replacing ServiceProvider " +
+                                ServiceContextManager.serviceContextProviderDelegate.getClass().getName()
+
+                                " with: " + serviceContextProvider.getClass().getName());
+                ServiceContextManager.serviceContextProviderDelegate = serviceContextProvider;
+            }
+        }
+
+        return currentContext;
+    }
+
+    /**
+     * Ge {@link ServiceContext}. If necessary the {@link ServiceContext} will be laziliy
loaded.
+     *
+     * @return the {@link ServiceContext} used.
+     */
+    public static ServiceContext getServiceContext() {
+        if (serviceContextProviderDelegate == null) {
+            synchronized (ServiceContextManager.class) {
+                if (serviceContextProviderDelegate == null) {
+                    serviceContextProviderDelegate = loadDefaultServiceProvider();
+                }
+            }
+        }
+        return serviceContextProviderDelegate;
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/code/api/src/test/java/org/apache/tamaya/ConfigExceptionTest.java
----------------------------------------------------------------------
diff --git a/code/api/src/test/java/org/apache/tamaya/ConfigExceptionTest.java b/code/api/src/test/java/org/apache/tamaya/ConfigExceptionTest.java
new file mode 100644
index 0000000..fa7da0a
--- /dev/null
+++ b/code/api/src/test/java/org/apache/tamaya/ConfigExceptionTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests instantiating {@link ConfigException}.
+ */
+public class ConfigExceptionTest {
+
+    @Test
+    public void testCreationMessage(){
+        ConfigException ex = new ConfigException("test");
+        assertNull(ex.getCause());
+        assertEquals(ex.getMessage(), "test");
+    }
+
+    @Test
+    public void testCreationMessageThrowable(){
+        Exception e = new IllegalStateException("blabla");
+        ConfigException ex = new ConfigException("test", e);
+        assertTrue(ex.getCause() == e);
+        assertEquals("test", ex.getMessage());
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/code/api/src/test/java/org/apache/tamaya/ConfigurationTest.java
----------------------------------------------------------------------
diff --git a/code/api/src/test/java/org/apache/tamaya/ConfigurationTest.java b/code/api/src/test/java/org/apache/tamaya/ConfigurationTest.java
new file mode 100644
index 0000000..11a16b9
--- /dev/null
+++ b/code/api/src/test/java/org/apache/tamaya/ConfigurationTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ * Test class that tests the default methods implemented on {@link org.apache.tamaya.Configuration}.
The provided
+ * {@link org.apache.tamaya.TestConfiguration} is implemeted with maximal use of the default
methods.
+ */
+public class ConfigurationTest {
+
+    @Test
+    public void testget() throws Exception {
+        assertEquals(Boolean.TRUE, ConfigurationProvider.getConfiguration().get("booleanTrue",
Boolean.class));
+        assertEquals(Boolean.FALSE, ConfigurationProvider.getConfiguration().get("booleanFalse",
Boolean.class));
+        assertEquals((int)Byte.MAX_VALUE, (int)ConfigurationProvider.getConfiguration().get("byte",
Byte.class));
+        assertEquals((int)Integer.MAX_VALUE, (int)ConfigurationProvider.getConfiguration().get("int",
Integer.class));
+        assertEquals((long)Long.MAX_VALUE, (long)ConfigurationProvider.getConfiguration().get("long",
Long.class));
+        assertEquals((double)Float.MAX_VALUE, (double)ConfigurationProvider.getConfiguration().get("float",
Float.class), 0.0d);
+        assertEquals((double)Double.MAX_VALUE, (double)ConfigurationProvider.getConfiguration().get("double",
Double.class), 0.0d);
+    }
+
+    @Test
+    public void testGetBoolean() throws Exception {
+        assertTrue(ConfigurationProvider.getConfiguration().get("booleanTrue", Boolean.class));
+        assertFalse(ConfigurationProvider.getConfiguration().get("booleanFalse", Boolean.class));
+        assertFalse(ConfigurationProvider.getConfiguration().get("foorBar", Boolean.class));
+    }
+
+    @Test
+    public void testGetInteger() throws Exception {
+        assertEquals(Integer.MAX_VALUE,(int) ConfigurationProvider.getConfiguration().get("int",
Integer.class));
+    }
+
+    @Test
+    public void testGetLong() throws Exception {
+        assertEquals(Long.MAX_VALUE,(long) ConfigurationProvider.getConfiguration().get("long",
Long.class));
+    }
+
+    @Test
+    public void testGetDouble() throws Exception {
+        assertEquals(Double.MAX_VALUE,(double) ConfigurationProvider.getConfiguration().get("double",
Double.class), 0.0d);
+    }
+
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/code/api/src/test/java/org/apache/tamaya/TestConfiguration.java
----------------------------------------------------------------------
diff --git a/code/api/src/test/java/org/apache/tamaya/TestConfiguration.java b/code/api/src/test/java/org/apache/tamaya/TestConfiguration.java
new file mode 100644
index 0000000..3078714
--- /dev/null
+++ b/code/api/src/test/java/org/apache/tamaya/TestConfiguration.java
@@ -0,0 +1,129 @@
+/*
+ * 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;
+
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Test Configuration class, that is used to testdata the default methods provided by the
API.
+ */
+public class TestConfiguration implements Configuration{
+
+    private static final Map<String, String> VALUES;
+    static {
+        VALUES = new HashMap<>();
+        VALUES.put("long", String.valueOf(Long.MAX_VALUE));
+        VALUES.put("int", String.valueOf(Integer.MAX_VALUE));
+        VALUES.put("double", String.valueOf(Double.MAX_VALUE));
+        VALUES.put("float", String.valueOf(Float.MAX_VALUE));
+        VALUES.put("short", String.valueOf(Short.MAX_VALUE));
+        VALUES.put("byte", String.valueOf(Byte.MAX_VALUE));
+        VALUES.put("booleanTrue", "true");
+        VALUES.put("booleanFalse", "false");
+        VALUES.put("String", "aStringValue");
+    }
+
+    @Override
+    public String get(String key) {
+        return VALUES.get(key);
+    }
+
+    @Override
+    public String getOrDefault(String key, String defaultValue) {
+        String val = get(key);
+        if(val==null){
+            return defaultValue;
+        }
+        return val;
+    }
+
+    @Override
+    public <T> T getOrDefault(String key, Class<T> type, T defaultValue) {
+        T val = get(key, type);
+        if(val==null){
+            return defaultValue;
+        }
+        return val;
+    }
+
+    @Override
+    public <T> T get(String key, Class<T> type) {
+        if(type.equals(Long.class)){
+            return (T)(Object)Long.MAX_VALUE;
+        }
+        else if(type.equals(Integer.class)){
+            return (T)(Object) Integer.MAX_VALUE;
+        }
+        else if(type.equals(Double.class)){
+            return (T)(Object) Double.MAX_VALUE;
+        }
+        else if(type.equals(Float.class)){
+            return (T)(Object) Float.MAX_VALUE;
+        }
+        else if(type.equals(Short.class)){
+            return (T)(Object) Short.MAX_VALUE;
+        }
+        else if(type.equals(Byte.class)){
+            return (T)(Object) Byte.MAX_VALUE;
+        }
+        else if(type.equals(Boolean.class)){
+            if("booleanTrue".equals(key)) {
+                return (T)Boolean.TRUE;
+            }
+            else{
+                return (T)Boolean.FALSE;
+            }
+        }
+        else if(type.equals(String.class)){
+            return (T)"aStringValue";
+        }
+        throw new ConfigException("No such property: " + key);
+    }
+
+    @Override
+    public <T> T get(String key, TypeLiteral<T> type) {
+        throw new RuntimeException("Method not implemented yet.");
+    }
+
+    @Override
+    public <T> T getOrDefault(String key, TypeLiteral<T> type, T defaultValue)
{
+        T val = get(key, type);
+        if(val==null){
+            return defaultValue;
+        }
+        return val;
+    }
+
+    @Override
+    public Configuration with(ConfigOperator operator) {
+        return null;
+    }
+
+    @Override
+    public <T> T query(ConfigQuery<T> query) {
+        throw new RuntimeException("Method not implemented yet.");
+    }
+
+    @Override
+    public Map<String, String> getProperties() {
+        throw new RuntimeException("Method not implemented yet.");
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java
----------------------------------------------------------------------
diff --git a/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java b/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java
new file mode 100644
index 0000000..1a02903
--- /dev/null
+++ b/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java
@@ -0,0 +1,56 @@
+/*
+ * 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;
+
+import org.apache.tamaya.spi.ConfigurationContext;
+import org.apache.tamaya.spi.ConfigurationContextBuilder;
+import org.apache.tamaya.spi.ConfigurationProviderSpi;
+
+/**
+ * Test Configuration class, that is used to testdata the default methods provided by the
API.
+ */
+public class TestConfigurationProvider implements ConfigurationProviderSpi {
+
+    private static final Configuration config = new TestConfiguration();
+
+    @Override
+    public Configuration getConfiguration() {
+        return config;
+    }
+
+    @Override
+    public ConfigurationContext getConfigurationContext() {
+        return null;
+    }
+
+    @Override
+    public void setConfigurationContext(ConfigurationContext context) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public boolean isConfigurationContextSettable() {
+        return false;
+    }
+
+    @Override
+    public ConfigurationContextBuilder getConfigurationContextBuilder() {
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/code/api/src/test/java/org/apache/tamaya/TypeLiteralTest.java
----------------------------------------------------------------------
diff --git a/code/api/src/test/java/org/apache/tamaya/TypeLiteralTest.java b/code/api/src/test/java/org/apache/tamaya/TypeLiteralTest.java
new file mode 100644
index 0000000..51edae9
--- /dev/null
+++ b/code/api/src/test/java/org/apache/tamaya/TypeLiteralTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Tests for the {@link TypeLiteral} class.
+ */
+public class TypeLiteralTest {
+
+    @Test
+    public void test_constrcutor(){
+        TypeLiteral<List<String>> listTypeLiteral = new TypeLiteral<List<String>>(){};
+        assertEquals(List.class, listTypeLiteral.getRawType());
+        assertEquals(String.class, TypeLiteral.getTypeParameters(listTypeLiteral.getType())[0]);
+    }
+
+    @Test
+    public void test_of(){
+        class MyListClass extends ArrayList<String>{};
+        TypeLiteral<MyListClass> listTypeLiteral = TypeLiteral.of(MyListClass.class);
+        assertEquals(MyListClass.class, listTypeLiteral.getRawType());
+        assertEquals(MyListClass.class, listTypeLiteral.getType());
+    }
+
+    @Test
+    public void test_getTypeParameter(){
+        TypeLiteral<List<String>> listTypeLiteral = new TypeLiteral<List<String>>(){};
+        assertEquals(List.class, listTypeLiteral.getRawType());
+        assertEquals(String.class, TypeLiteral.getTypeParameters(listTypeLiteral.getType())[0]);
+    }
+
+    @Test
+    public void test_getGenericInterfaceTypeParameter(){
+        class MyListClass extends ArrayList<String> implements List<String>{};
+        assertEquals(String.class, TypeLiteral.getGenericInterfaceTypeParameters(MyListClass.class,
List.class)[0]);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/code/api/src/test/java/org/apache/tamaya/spi/ServiceContextTest.java
----------------------------------------------------------------------
diff --git a/code/api/src/test/java/org/apache/tamaya/spi/ServiceContextTest.java b/code/api/src/test/java/org/apache/tamaya/spi/ServiceContextTest.java
new file mode 100644
index 0000000..e8c7f90
--- /dev/null
+++ b/code/api/src/test/java/org/apache/tamaya/spi/ServiceContextTest.java
@@ -0,0 +1,98 @@
+/*
+ * 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.spi;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.Test;
+
+public class ServiceContextTest {
+
+    private ServiceContext serviceContext = new ServiceContext(){
+
+        @Override
+        public int ordinal() {
+            return 1;
+        }
+
+        @Override
+        public <T> T getService(Class<T> serviceType) {
+            if(String.class.equals(serviceType)){
+                return serviceType.cast("ServiceContextTest");
+            }
+            return null;
+        }
+
+        @Override
+        public <T> List<T> getServices(Class<T> serviceType) {
+            if(String.class.equals(serviceType)){
+                List<String> list = new ArrayList<>();
+                list.add("ServiceContextTest");
+                return List.class.cast(list);
+            }
+            return Collections.emptyList();
+        }
+    };
+
+    @Test
+    public void testOrdinal() throws Exception {
+        assertEquals(1, serviceContext.ordinal());
+    }
+
+    @Test
+    public void testgetService() throws Exception {
+        assertEquals("ServiceContextTest", serviceContext.getService(String.class));
+        assertNull(serviceContext.getService(Integer.class));
+    }
+
+    @Test
+    public void testGetService() throws Exception {
+        String service = serviceContext.getService(String.class);
+        assertNotNull(service);
+        assertEquals("ServiceContextTest", service);
+        Integer intService = serviceContext.getService(Integer.class);
+        assertNull(intService);
+    }
+
+    @Test
+    public void testGetServices() throws Exception {
+        Collection<String> services = serviceContext.getServices(String.class);
+        assertNotNull(services);
+        assertFalse(services.isEmpty());
+        assertEquals("ServiceContextTest", services.iterator().next());
+        List<Integer> intServices = serviceContext.getServices(Integer.class);
+        assertNotNull(intServices);
+        assertTrue(intServices.isEmpty());
+    }
+
+    @Test
+    public void testGetInstance() throws Exception {
+        assertNotNull(ServiceContextManager.getServiceContext());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/code/api/src/test/java/org/apache/tamaya/spi/TestServiceContext.java
----------------------------------------------------------------------
diff --git a/code/api/src/test/java/org/apache/tamaya/spi/TestServiceContext.java b/code/api/src/test/java/org/apache/tamaya/spi/TestServiceContext.java
new file mode 100644
index 0000000..4df7340
--- /dev/null
+++ b/code/api/src/test/java/org/apache/tamaya/spi/TestServiceContext.java
@@ -0,0 +1,90 @@
+/*
+ * 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.spi;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * This class implements the (default) {@link org.apache.tamaya.spi.ServiceContext} interface
and hereby uses the JDK
+ * {@link java.util.ServiceLoader} to load the services required.
+ */
+public final class TestServiceContext implements ServiceContext {
+    /** List current services loaded, per class. */
+    private final ConcurrentHashMap<Class<?>, List<Object>> servicesLoaded
= new ConcurrentHashMap<>();
+
+    private final Map<Class<?>, Object> singletons = new ConcurrentHashMap<>();
+
+    @Override
+    public int ordinal() {
+        return 1;
+    }
+
+    @Override
+    public <T> T getService(Class<T> serviceType) {
+        T cached = serviceType.cast(singletons.get(serviceType));
+        if(cached==null) {
+            Collection<T> services = getServices(serviceType);
+            if (services.isEmpty()) {
+                cached = (T) Object.class; // as marker for 'nothing here'
+            }
+            else{
+                cached = services.iterator().next();
+            }
+            singletons.put((Class)serviceType, cached);
+        }
+        if (cached == Object.class) {
+            cached = null;
+        }
+        return cached;
+    }
+
+    /**
+     * 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(Class<T> serviceType) {
+        try {
+            List<T> services = new ArrayList<>();
+            for (T t : ServiceLoader.load(serviceType)) {
+                services.add(t);
+            }
+            services = Collections.unmodifiableList(services);
+            final List<T> previousServices = List.class.cast(servicesLoaded.putIfAbsent(serviceType,
(List<Object>)services));
+            return previousServices != null ? previousServices : services;
+        } catch (Exception e) {
+            Logger.getLogger(TestServiceContext.class.getName()).log(Level.WARNING,
+                                      "Error loading services current type " + serviceType,
e);
+            return Collections.emptyList();
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/code/api/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi
----------------------------------------------------------------------
diff --git a/code/api/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi
b/code/api/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi
new file mode 100644
index 0000000..b9c5ba5
--- /dev/null
+++ b/code/api/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi
@@ -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.TestConfigurationProvider

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/code/api/src/test/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext
----------------------------------------------------------------------
diff --git a/code/api/src/test/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext
b/code/api/src/test/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext
new file mode 100644
index 0000000..199956f
--- /dev/null
+++ b/code/api/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.spi.TestServiceContext
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/code/pom.xml
----------------------------------------------------------------------
diff --git a/code/pom.xml b/code/pom.xml
index 4702441..fe2a58e 100644
--- a/code/pom.xml
+++ b/code/pom.xml
@@ -33,5 +33,17 @@ under the License.
     <packaging>pom</packaging>
     <name>Apache Tamaya Codebase</name>
 
+    <modules>
+        <module>api</module>
+    </modules>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>findbugs-maven-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
     
 </project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/84d52f16/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 5083078..4da09d7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -204,7 +204,6 @@ under the License.
     <modules>
         <module>buildconfigurations</module>
         <module>code</module>
-        <module>api</module>
         <module>core</module>
         <module>modules</module>
         <module>docs</module>
@@ -594,10 +593,6 @@ under the License.
                     </execution>
                 </executions>
             </plugin>
-            <plugin>
-                <groupId>org.codehaus.mojo</groupId>
-                <artifactId>findbugs-maven-plugin</artifactId>
-            </plugin>
 
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>



Mime
View raw message