diff --git a/microservices/src/main/java/org/silverware/microservices/annotations/InvocationPolicy.java b/microservices/src/main/java/org/silverware/microservices/annotations/InvocationPolicy.java new file mode 100644 index 0000000..80d1038 --- /dev/null +++ b/microservices/src/main/java/org/silverware/microservices/annotations/InvocationPolicy.java @@ -0,0 +1,39 @@ +/* + * -----------------------------------------------------------------------\ + * SilverWare + *   + * Copyright (C) 2010 - 2013 the original author or authors. + *   + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -----------------------------------------------------------------------/ + */ +package org.silverware.microservices.annotations; + +import org.silverware.microservices.silver.services.LookupStrategy; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author Martin Večeřa + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface InvocationPolicy { + + Class lookupStrategy(); +} diff --git a/microservices/src/main/java/org/silverware/microservices/silver/services/LookupStrategyFactory.java b/microservices/src/main/java/org/silverware/microservices/silver/services/LookupStrategyFactory.java new file mode 100644 index 0000000..308bf13 --- /dev/null +++ b/microservices/src/main/java/org/silverware/microservices/silver/services/LookupStrategyFactory.java @@ -0,0 +1,63 @@ +/* + * -----------------------------------------------------------------------\ + * SilverWare + *   + * Copyright (C) 2010 - 2013 the original author or authors. + *   + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -----------------------------------------------------------------------/ + */ +package org.silverware.microservices.silver.services; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.silverware.microservices.Context; +import org.silverware.microservices.MicroserviceMetaData; +import org.silverware.microservices.annotations.InvocationPolicy; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.util.Set; + +/** + * @author Martin Večeřa + */ +public class LookupStrategyFactory { + + /** + * Logger. + */ + private static Logger log = LogManager.getLogger(LookupStrategyFactory.class); + + public static LookupStrategy getStrategy(final Context context, final MicroserviceMetaData metaData, final Set options) { + LookupStrategy strategy = null; + + for (Annotation option: options) { + if (option.annotationType().isAssignableFrom(InvocationPolicy.class)) { + InvocationPolicy policy = (InvocationPolicy) option; + Class clazz = policy.lookupStrategy(); + + try { + Constructor c = clazz.getConstructor(); + strategy = (LookupStrategy) c.newInstance(); + strategy.initialize(context, metaData, options); + break; + } catch (Exception e) { + log.warn(String.format("Could not instantiate lookup strategy class %s:", clazz.getName()), e); + } + } + } + + return strategy; + } +} diff --git a/microservices/src/main/java/org/silverware/microservices/silver/services/lookup/AbstractLookupStrategy.java b/microservices/src/main/java/org/silverware/microservices/silver/services/lookup/AbstractLookupStrategy.java new file mode 100644 index 0000000..adf234d --- /dev/null +++ b/microservices/src/main/java/org/silverware/microservices/silver/services/lookup/AbstractLookupStrategy.java @@ -0,0 +1,44 @@ +/* + * -----------------------------------------------------------------------\ + * SilverWare + *   + * Copyright (C) 2010 - 2013 the original author or authors. + *   + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -----------------------------------------------------------------------/ + */ +package org.silverware.microservices.silver.services.lookup; + +import org.silverware.microservices.Context; +import org.silverware.microservices.MicroserviceMetaData; +import org.silverware.microservices.silver.services.LookupStrategy; + +import java.lang.annotation.Annotation; +import java.util.Set; + +/** + * @author Martin Večeřa + */ +abstract public class AbstractLookupStrategy implements LookupStrategy { + + protected Context context; + protected MicroserviceMetaData metaData; + protected Set options; + + @Override + public void initialize(final Context context, final MicroserviceMetaData metaData, final Set options) { + this.context = context; + this.metaData = metaData; + this.options = options; + } +} diff --git a/microservices/src/main/java/org/silverware/microservices/silver/services/lookup/RandomRobinLookupStrategy.java b/microservices/src/main/java/org/silverware/microservices/silver/services/lookup/RandomRobinLookupStrategy.java new file mode 100644 index 0000000..c68bf98 --- /dev/null +++ b/microservices/src/main/java/org/silverware/microservices/silver/services/lookup/RandomRobinLookupStrategy.java @@ -0,0 +1,36 @@ +/* + * -----------------------------------------------------------------------\ + * SilverWare + *   + * Copyright (C) 2010 - 2013 the original author or authors. + *   + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -----------------------------------------------------------------------/ + */ +package org.silverware.microservices.silver.services.lookup; + +import java.util.Random; + +/** + * @author Martin Večeřa + */ +public class RandomRobinLookupStrategy extends AbstractLookupStrategy { + + private Random rnd = new Random(); + + @Override + public Object getService() { + final Object[] services = context.lookupMicroservice(metaData).toArray(); + return services[rnd.nextInt(services.length)]; + } +}