Skip to content

Commit

Permalink
#9 sample invocation strategy is in place
Browse files Browse the repository at this point in the history
  • Loading branch information
marvec committed May 12, 2015
1 parent ee3f75a commit 66877cb
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface InvocationPolicy {

Class<LookupStrategy> lookupStrategy();
}
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/
public class LookupStrategyFactory {

/**
* Logger.
*/
private static Logger log = LogManager.getLogger(LookupStrategyFactory.class);

public static LookupStrategy getStrategy(final Context context, final MicroserviceMetaData metaData, final Set<Annotation> options) {
LookupStrategy strategy = null;

for (Annotation option: options) {
if (option.annotationType().isAssignableFrom(InvocationPolicy.class)) {
InvocationPolicy policy = (InvocationPolicy) option;
Class<LookupStrategy> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/
abstract public class AbstractLookupStrategy implements LookupStrategy {

protected Context context;
protected MicroserviceMetaData metaData;
protected Set<Annotation> options;

@Override
public void initialize(final Context context, final MicroserviceMetaData metaData, final Set<Annotation> options) {
this.context = context;
this.metaData = metaData;
this.options = options;
}
}
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/
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)];
}
}

0 comments on commit 66877cb

Please sign in to comment.