PK %aHrK[ build.xml
PK &aH % % manifestversion=1.5
frameworkVersions=]1.2,)
PK %aH5"S& & README## Changelog
### 1.5
* support injecting of non-static dependencies into controllers
### 1.4
* upgrade to guice 4.0
### 1.3
* support both javax.inject.Inject and com.google.inject.Inject annotations
* first search for custom injectors, and only then - for Modules. (the previous code depended on order of classes: which of them meets first in classpath)
### 1.2
* copy of play-guice 1.2 plugin
(taken from https://www.playframework.com/modules/guice)
Initial github project seems to be dead: http://github.com/pk11/play-guice-modulePK %aH
W1 build.sh#!/bin/bash
ORGANIZATION="play-guice"
MODULE="guice"
VERSION=`grep self conf/dependencies.yml | sed "s/.*$MODULE //"`
DESTINATION=/var/www/repo/$ORGANIZATION
TARGET=$DESTINATION/$MODULE-$VERSION.zip
rm -fr dist
play dependencies --sync || exit $?
play build-module || exit $?
if [ -d $DESTINATION ]; then
if [ -e $TARGET ]; then
echo "Not publishing, $MODULE-$VERSION already exists"
else
cp dist/*.zip $TARGET
echo "Package is available at https://repo.codeborne.com/$ORGANIZATION/$MODULE-$VERSION.zip"
fi
fi
PK %aH$# # src/play.plugins1000:play.modules.guice.GuicePluginPK %aHrc! ! ) src/play/modules/guice/InjectSupport.javapackage play.modules.guice;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface InjectSupport {
}
PK %aHh ' src/play/modules/guice/GuicePlugin.javapackage play.modules.guice;
import com.google.inject.*;
import com.google.inject.name.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import play.Play;
import play.PlayPlugin;
import play.inject.BeanSource;
import play.mvc.Http;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
/**
* Enable Guice integration in
* Playframework. This plugin first scans for a custom Guice Injector if it's
* not found, then it tries to create an injector from all the guice modules
* available on the classpath. The Plugin is then passed to Play injector for
* Controller IoC.
*
* @author Luciano Fiandesio
* @author Peter Hausel
* @author Lucas Galego
* @author Alexander Vasiljev
*/
public class GuicePlugin extends PlayPlugin implements BeanSource {
private static final Logger logger = LoggerFactory.getLogger(GuicePlugin.class);
Injector injector;
private final List modules = new ArrayList();
@Override
public void onApplicationStart() {
logger.debug("Starting Guice modules scanning");
loadInjector();
play.inject.Injector.inject(this);
injectAnnotated();
}
@Override public void beforeActionInvocation(Method actionMethod) {
if (Modifier.isStatic(actionMethod.getModifiers())) return;
Http.Request request = Http.Request.current();
if (request.controllerInstance == null) request.controllerInstance = getBeanOfType(request.controllerClass);
}
private void loadInjector() {
try {
modules.clear();
logger.debug("Guice modules cleared");
List customInjectors = Play.classloader.getAssignableClasses(GuiceSupport.class);
if (!customInjectors.isEmpty()) {
if (customInjectors.size() > 1) {
logger.warn("Found multiple customer injectors: {}, using first of them", customInjectors);
}
loadCustomInjector(customInjectors.get(0));
return;
}
List modulesClasses = Play.classloader.getAssignableClasses(AbstractModule.class);
for (Class moduleClass : modulesClasses) {
modules.add((Module) moduleClass.newInstance());
}
loadInjectorFromModules();
}
catch (Exception e) {
throw new IllegalStateException("Unable to create Guice injector", e);
}
}
private void loadCustomInjector(Class clazz) throws InstantiationException, IllegalAccessException {
final GuiceSupport gs = (GuiceSupport) clazz.newInstance();
injector = gs.configure();
logger.info("Guice injector created: {}", clazz.getName());
}
private void loadInjectorFromModules() {
if (modules.isEmpty()) {
throw new IllegalStateException("Could not find any custom guice injector or abstract modules. Are you sure you have at least one on the classpath?");
}
injector = Guice.createInjector(modules);
logger.info("Guice injector created with modules: " + moduleList());
}
private String moduleList() {
final StringBuilder moduleList = new StringBuilder("\n");
for (Module module : modules) {
moduleList.append(module.getClass());
moduleList.append("\n");
}
return moduleList.toString();
}
@Override
public T getBeanOfType(Class clazz) {
if (injector == null) {
return null;
}
try {
return injector.getInstance(clazz);
}
catch (ConfigurationException e) {
logger.error("Failed to get bean of type " + clazz, e);
return null;
}
}
public T getBeanWithKey(Key key) {
if (injector == null) {
return null;
}
return injector.getInstance(key);
}
private void injectAnnotated() {
try {
for (Class> clazz : Play.classloader.getAnnotatedClasses(play.modules.guice.InjectSupport.class)) {
for (Field field : clazz.getDeclaredFields()) {
if (isInjectable(field)) {
inject(field);
}
}
}
}
catch (Exception e) {
throw new RuntimeException("Error injecting dependencies", e);
}
}
private boolean isInjectable(Field field) {
return Modifier.isStatic(field.getModifiers()) &&
(field.isAnnotationPresent(javax.inject.Inject.class) || field.isAnnotationPresent(com.google.inject.Inject.class));
}
private void inject(Field field) throws IllegalAccessException {
field.setAccessible(true);
final Annotation fieldBinding = fieldBinding(field);
if (fieldBinding != null) {
field.set(null, getBeanWithKey(Key.get(field.getType(), fieldBinding)));
}
else {
field.set(null, getBeanOfType(field.getType()));
}
}
private Annotation fieldBinding(Field field) {
for (Annotation annotation : field.getAnnotations()) {
if (annotation.annotationType().equals(Named.class)) {
return annotation;
}
for (Annotation internal : annotation.annotationType().getAnnotations()) {
if (internal.annotationType().equals(BindingAnnotation.class)) {
return annotation;
}
}
}
return null;
}
}
PK %aHL ( src/play/modules/guice/GuiceSupport.javapackage play.modules.guice;
import com.google.inject.Injector;
/**
* Implemented if a custom injector is desired
*/
public abstract class GuiceSupport {
protected abstract Injector configure();
}
PK &aH&Q Q lib/play-guice.jarPK
'aH META-INF/ PK
&aHf META-INF/MANIFEST.MFM @wQH[#Ѹ+7@x{yф)'DuB>V8"BQ?j{F0crqv;qُ"g&2H>(Z
zuPK
&aH