You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by st...@apache.org on 2015/01/11 21:07:56 UTC
incubator-tamaya git commit: TAMAYA-54 added @Priority support to
DefaultServiceContext and Test
Repository: incubator-tamaya
Updated Branches:
refs/heads/master 8c3883e1f -> b5f409357
TAMAYA-54 added @Priority support to DefaultServiceContext and Test
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/b5f40935
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/b5f40935
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/b5f40935
Branch: refs/heads/master
Commit: b5f409357c9edfdfb209c286edc543b65f793b7d
Parents: 8c3883e
Author: Reinhard Sandtner <rs...@apache.org>
Authored: Thu Jan 8 09:33:00 2015 +0100
Committer: Mark Struberg <st...@apache.org>
Committed: Sun Jan 11 21:06:04 2015 +0100
----------------------------------------------------------------------
.../core/internal/DefaultServiceContext.java | 51 ++++++-
.../internal/DefaultServiceContextTest.java | 133 +++++++++++++++++++
...tServiceContextTest$InvalidPriorityInterface | 19 +++
...efaultServiceContextTest$MultiImplsInterface | 20 +++
4 files changed, 222 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5f40935/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultServiceContext.java
----------------------------------------------------------------------
diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultServiceContext.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultServiceContext.java
index 8e27d4a..d5b132a 100644
--- a/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultServiceContext.java
+++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultServiceContext.java
@@ -18,8 +18,11 @@
*/
package org.apache.tamaya.core.internal;
+import org.apache.tamaya.ConfigException;
import org.apache.tamaya.spi.ServiceContext;
+import javax.annotation.Priority;
+import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -52,7 +55,7 @@ public final class DefaultServiceContext implements ServiceContext {
if (services.isEmpty()) {
cached = Optional.empty();
} else {
- cached = Optional.of(services.get(0));
+ cached = Optional.of(getServiceWithHighestPriority(services, serviceType));
}
singletons.put(serviceType, cached);
}
@@ -86,4 +89,50 @@ public final class DefaultServiceContext implements ServiceContext {
return previousServices != null ? previousServices : services;
}
+
+ /**
+ * @param services to scan
+ * @param <T> type of the service
+ *
+ * @return the service with the highest {@link javax.annotation.Priority#value()}
+ *
+ * @throws ConfigException if there are multiple service implementations with the maximum priority
+ */
+ private <T> T getServiceWithHighestPriority(List<? extends T> services, Class<T> serviceType) {
+
+ // we do not need the priority stuff if the list contains only one element
+ if (services.size() == 1) {
+ return services.get(0);
+ }
+
+ Integer highestPriority = null;
+ int highestPriorityServiceCount = 0;
+ T highestService = null;
+
+ for (T service : services) {
+ int prio = 1; //X TODO discuss default priority
+ Priority priority = service.getClass().getAnnotation(Priority.class);
+ if (priority != null) {
+ prio = priority.value();
+ }
+
+ 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}",
+ highestPriorityServiceCount,
+ serviceType.getName(),
+ highestPriority));
+ }
+
+ return highestService;
+ }
+
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5f40935/java8/core/src/test/java/org/apache/tamaya/core/test/internal/DefaultServiceContextTest.java
----------------------------------------------------------------------
diff --git a/java8/core/src/test/java/org/apache/tamaya/core/test/internal/DefaultServiceContextTest.java b/java8/core/src/test/java/org/apache/tamaya/core/test/internal/DefaultServiceContextTest.java
new file mode 100644
index 0000000..0aaf63f
--- /dev/null
+++ b/java8/core/src/test/java/org/apache/tamaya/core/test/internal/DefaultServiceContextTest.java
@@ -0,0 +1,133 @@
+/*
+ * 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.core.test.internal;
+
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.core.internal.DefaultConfigurationContext;
+import org.apache.tamaya.core.internal.DefaultServiceContext;
+import org.apache.tamaya.spi.ConfigurationContext;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.annotation.Priority;
+import java.util.List;
+import java.util.Optional;
+
+public class DefaultServiceContextTest {
+
+ /**
+ * context to test
+ */
+ private DefaultServiceContext context = new DefaultServiceContext();
+
+
+ @Test
+ public void testGetService() {
+ Optional<ConfigurationContext> configurationContext = context.getService(ConfigurationContext.class);
+
+ Assert.assertNotNull(configurationContext);
+ Assert.assertTrue(configurationContext.isPresent());
+ Assert.assertTrue(configurationContext.get() instanceof DefaultConfigurationContext);
+ }
+
+ @Test(expected = ConfigException.class)
+ public void testGetService_multipleServicesWithoutPriority_shouldThrowConfigException() {
+ context.getService(InvalidPriorityInterface.class);
+ }
+
+ @Test
+ public void testGetService_multipleService_shouldReturnServiceWithHighestPriority() {
+ Optional<MultiImplsInterface> service = context.getService(MultiImplsInterface.class);
+
+ Assert.assertTrue(service.isPresent());
+ Assert.assertTrue(service.get() instanceof MultiImpl2);
+ }
+
+ @Test
+ public void testGetService_noImpl_shouldReturnEmptyOpional() {
+ Optional<NoImplInterface> service = context.getService(NoImplInterface.class);
+ Assert.assertFalse(service.isPresent());
+ }
+
+
+ @Test
+ public void testGetServices_shouldReturnServices() {
+ {
+ List<InvalidPriorityInterface> services = context.getServices(InvalidPriorityInterface.class);
+ Assert.assertNotNull(services);
+ Assert.assertEquals(2, services.size());
+
+ for (InvalidPriorityInterface service : services) {
+ Assert.assertTrue(service instanceof InvalidPriorityImpl1 || service instanceof InvalidPriorityImpl2);
+ }
+ }
+
+ {
+ List<MultiImplsInterface> services = context.getServices(MultiImplsInterface.class);
+ Assert.assertNotNull(services);
+ Assert.assertEquals(3, services.size());
+
+ for (MultiImplsInterface service : services) {
+ Assert.assertTrue(service instanceof MultiImpl1 ||
+ service instanceof MultiImpl2 ||
+ service instanceof MultiImpl3);
+ }
+ }
+ }
+
+ @Test
+ public void testGetServices_noImpl_shouldReturnEmptyList() {
+ List<NoImplInterface> services = context.getServices(NoImplInterface.class);
+ Assert.assertNotNull(services);
+ Assert.assertTrue(services.isEmpty());
+ }
+
+
+ // some test interfaces and classes
+
+ public static interface InvalidPriorityInterface {
+ }
+
+ @Priority(value = 50)
+ public static class InvalidPriorityImpl1 implements InvalidPriorityInterface {
+ }
+
+ @Priority(value = 50)
+ public static class InvalidPriorityImpl2 implements InvalidPriorityInterface {
+ }
+
+
+ public static interface MultiImplsInterface {
+ }
+
+ public static class MultiImpl1 implements MultiImplsInterface {
+ }
+
+ @Priority(value = 500)
+ public static class MultiImpl2 implements MultiImplsInterface {
+ }
+
+ @Priority(value = -10)
+ public static class MultiImpl3 implements MultiImplsInterface {
+ }
+
+
+ private static interface NoImplInterface {
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5f40935/java8/core/src/test/resources/META-INF/services/org.apache.tamaya.core.test.internal.DefaultServiceContextTest$InvalidPriorityInterface
----------------------------------------------------------------------
diff --git a/java8/core/src/test/resources/META-INF/services/org.apache.tamaya.core.test.internal.DefaultServiceContextTest$InvalidPriorityInterface b/java8/core/src/test/resources/META-INF/services/org.apache.tamaya.core.test.internal.DefaultServiceContextTest$InvalidPriorityInterface
new file mode 100644
index 0000000..6f0ccc7
--- /dev/null
+++ b/java8/core/src/test/resources/META-INF/services/org.apache.tamaya.core.test.internal.DefaultServiceContextTest$InvalidPriorityInterface
@@ -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 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.core.test.internal.DefaultServiceContextTest$InvalidPriorityImpl1
+org.apache.tamaya.core.test.internal.DefaultServiceContextTest$InvalidPriorityImpl2
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b5f40935/java8/core/src/test/resources/META-INF/services/org.apache.tamaya.core.test.internal.DefaultServiceContextTest$MultiImplsInterface
----------------------------------------------------------------------
diff --git a/java8/core/src/test/resources/META-INF/services/org.apache.tamaya.core.test.internal.DefaultServiceContextTest$MultiImplsInterface b/java8/core/src/test/resources/META-INF/services/org.apache.tamaya.core.test.internal.DefaultServiceContextTest$MultiImplsInterface
new file mode 100644
index 0000000..1168b01
--- /dev/null
+++ b/java8/core/src/test/resources/META-INF/services/org.apache.tamaya.core.test.internal.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.core.test.internal.DefaultServiceContextTest$MultiImpl1
+org.apache.tamaya.core.test.internal.DefaultServiceContextTest$MultiImpl2
+org.apache.tamaya.core.test.internal.DefaultServiceContextTest$MultiImpl3
\ No newline at end of file