Skip to content
This repository was archived by the owner on Mar 5, 2020. It is now read-only.

changed StaticProvider interface #374

Merged
merged 3 commits into from
Aug 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ public void load(Configuration di) {
di.register(new LoggerProvider());
di.register(new PropertiesProvider());

di.registerContextProvider(Context.CLASSLOADER, Class::getClassLoader);
di.registerContextProvider(Context.CLASSLOADER, consumer -> consumer.getConsumerClass().getClassLoader());
}

}
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ public class PropertiesProvider implements ServiceProvider<Properties> {
public ServiceImplementationDescription<Properties> getServiceImplementationDescription(
@Nonnull ContextLocator locator, @Nonnull ServiceConsumer<Properties> serviceConsumer) {
IPlugin plugin = (IPlugin) locator.getContextProvider(Context.PLUGIN)
.getContext(serviceConsumer.getConsumerClass());
.getContext(serviceConsumer);
return new ServiceImplementationDescriptionImpl<>(serviceConsumer.getServiceDescription(),
Collections.singletonMap(Context.PLUGIN, plugin), Properties.class);
}
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.unistuttgart.iaas.amyassist.amy.core.di.consumer.ServiceConsumer;
import de.unistuttgart.iaas.amyassist.amy.core.di.context.provider.StaticProvider;

/**
@@ -46,13 +47,13 @@ public PluginProvider(Collection<IPlugin> plugins) {
}

@Override
public IPlugin getContext(Class<?> consumer) {
public IPlugin getContext(ServiceConsumer<?> consumer) {
for (IPlugin p : this.plugins) {
if (p.getClasses().contains(consumer)) {
if (p.getClasses().contains(consumer.getConsumerClass())) {
return p;
}
}
this.logger.error("The class {} does not seem to belong to any plugin.", consumer.getName());
this.logger.error("The class {} does not seem to belong to any plugin.", consumer.getConsumerClass().getName());
return null;
}

Original file line number Diff line number Diff line change
@@ -23,6 +23,8 @@

package de.unistuttgart.iaas.amyassist.amy.core.di.context.provider;

import de.unistuttgart.iaas.amyassist.amy.core.di.consumer.ServiceConsumer;

/**
* A Static Context Provider which provides context information from then consumer class.
*
@@ -31,9 +33,9 @@
public interface StaticProvider<T> {
/**
*
* @param consumer
* the class of the consumer
* @param serviceConsumer
* the consumer of the service, this contains the consumer class and annotations
* @return the context information
*/
T getContext(Class<?> consumer);
T getContext(ServiceConsumer<?> serviceConsumer);
}
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@ class ServiceKey<T> {
public ServiceKey(ServiceDescription<T> description) {
this.serviceType = description.getServiceType();
this.annotations = new HashSet<>(description.getAnnotations());
this.annotations.removeIf(annotation -> true);
}

@Override
Original file line number Diff line number Diff line change
@@ -23,6 +23,8 @@

package de.unistuttgart.iaas.amyassist.amy.core.di.context.provider;

import de.unistuttgart.iaas.amyassist.amy.core.di.consumer.ServiceConsumer;

/**
* A ContextProvider which provides the class of the consumer
*
@@ -31,8 +33,8 @@
public class ClassProvider implements StaticProvider<Class<?>> {

@Override
public Class<?> getContext(Class<?> consumer) {
return consumer;
public Class<?> getContext(ServiceConsumer<?> consumer) {
return consumer.getConsumerClass();
}

}
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@

import java.util.Map;

import de.unistuttgart.iaas.amyassist.amy.core.di.consumer.ServiceConsumer;
import de.unistuttgart.iaas.amyassist.amy.core.di.context.provider.StaticProvider;

/**
@@ -41,8 +42,8 @@ public CustomProvider(Map<Class<?>, T> mapping) {
}

@Override
public T getContext(Class<?> consumer) {
return this.mapping.get(consumer);
public T getContext(ServiceConsumer<?> consumer) {
return this.mapping.get(consumer.getConsumerClass());
}

}
Original file line number Diff line number Diff line change
@@ -104,7 +104,7 @@ public ServiceImplementationDescription<T> getServiceImplementationDescription(@
HashMap<String, Object> map = new HashMap<>();
for (ContextInjectionPoint c : this.contextInjectionPoints) {
String key = c.getContextIdentifier();
map.put(key, locator.getContextProvider(key).getContext(serviceConsumer.getConsumerClass()));
map.put(key, locator.getContextProvider(key).getContext(serviceConsumer));
}

return new ServiceImplementationDescriptionImpl<>(serviceConsumer.getServiceDescription(), map, this.cls);
Original file line number Diff line number Diff line change
@@ -23,15 +23,9 @@

package de.unistuttgart.iaas.amyassist.amy.core.di;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.theInstance;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;

import java.time.Duration;

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This source file is part of the Amy open source project.
* For more information see github.com/AmyAssist
*
* Copyright (c) 2018 the Amy project authors.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information see notice.md
*/

package de.unistuttgart.iaas.amyassist.amy.core.di.context.provider;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation to the context
*
* @author Leon Kiefer
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(java.lang.annotation.ElementType.FIELD)
public @interface AnnotatoinWithValue {
String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This source file is part of the Amy open source project.
* For more information see github.com/AmyAssist
*
* Copyright (c) 2018 the Amy project authors.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information see notice.md
*/

package de.unistuttgart.iaas.amyassist.amy.core.di.context.provider;

import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.Context;
import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.Service;

/**
* Service that uses the value of annotation on consumer declaration as context
*
* @author Leon Kiefer
*/
@Service
public class ServiceWithAnnotationContext {
@Context("annotation")
private String annotationValue;

public String getValue() {
return this.annotationValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This source file is part of the Amy open source project.
* For more information see github.com/AmyAssist
*
* Copyright (c) 2018 the Amy project authors.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information see notice.md
*/

package de.unistuttgart.iaas.amyassist.amy.core.di.context.provider;

import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.Reference;
import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.Service;

/**
* Service with two references to the same Service type with different annotations
*
* @author Leon Kiefer
*/
@Service
public class ServiceWithDependencies {
@AnnotatoinWithValue("1")
@Reference
private ServiceWithAnnotationContext service1;
@AnnotatoinWithValue("2")
@Reference
private ServiceWithAnnotationContext service2;

public String getValueOfService1() {
return this.service1.getValue();
}

public String getValueOfService2() {
return this.service2.getValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* This source file is part of the Amy open source project.
* For more information see github.com/AmyAssist
*
* Copyright (c) 2018 the Amy project authors.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information see notice.md
*/

package de.unistuttgart.iaas.amyassist.amy.core.di.context.provider;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import de.unistuttgart.iaas.amyassist.amy.core.di.DependencyInjection;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

/**
* Test for the StaticContext provider in combination with annotations of the consumer
*
* @author Leon Kiefer
*/
public class TestStaticProvider {
private DependencyInjection dependencyInjection;

@BeforeEach
public void setup() {
this.dependencyInjection = new DependencyInjection();
}

@Test
void testUseAnnoations() {
this.dependencyInjection.register(ServiceWithAnnotationContext.class);
this.dependencyInjection.register(ServiceWithDependencies.class);

this.dependencyInjection.registerContextProvider("annotation",
consumer -> consumer.getServiceDescription().getAnnotations().stream()
.filter(annotation -> annotation instanceof AnnotatoinWithValue).findFirst()
.map(annotation -> ((AnnotatoinWithValue) annotation).value()).orElse(null));

ServiceWithDependencies service = this.dependencyInjection.getService(ServiceWithDependencies.class);
assertThat(service.getValueOfService1(), is("1"));
assertThat(service.getValueOfService2(), is("2"));
}
}