Skip to content

Commit

Permalink
Migrate to type safe manager instances in mutable bean factories
Browse files Browse the repository at this point in the history
  • Loading branch information
mgoellnitz committed Jul 30, 2016
1 parent 9809bb2 commit 310b3e6
Show file tree
Hide file tree
Showing 21 changed files with 82 additions and 46 deletions.
8 changes: 8 additions & 0 deletions ebean/src/guicy/ebean-configurer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import org.tangram.content.BeanFactory
import org.tangram.ebean.EBeanFactoryImpl
import org.tangram.mutable.MutableBeanFactory
import org.tangram.view.GenericPropertyConverter
import com.google.inject.TypeLiteral
import java.lang.reflect.Type

log.info "starting"

Expand Down Expand Up @@ -54,6 +56,12 @@ beanFactory.setBasePackages(basePackages)
beanFactory.setServerConfig(serverConfig)
module.getServletContext().setAttribute(Constants.ATTRIBUTE_BEAN_FACTORY, beanFactory)
module.bind(BeanFactory.class).toInstance(beanFactory)
Object vehicle = new Object() {
MutableBeanFactory<?> v
};
Type interimType = vehicle.getClass().getDeclaredField("v").getGenericType()
TypeLiteral mbf = TypeLiteral.get(interimType)
module.bind(mbf).toInstance(beanFactory)
module.bind(MutableBeanFactory.class).toInstance(beanFactory)

log.info "done."
Expand Down
6 changes: 3 additions & 3 deletions ebean/src/org/tangram/ebean/EBeanFactoryImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2013-2015 Martin Goellnitz
* Copyright 2013-2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -44,7 +44,7 @@

@Named("beanFactory")
@Singleton
public class EBeanFactoryImpl extends AbstractMutableBeanFactory implements MutableBeanFactory {
public class EBeanFactoryImpl extends AbstractMutableBeanFactory<EbeanServer> implements MutableBeanFactory<EbeanServer> {

private static final Logger LOG = LoggerFactory.getLogger(EBeanFactoryImpl.class);

Expand Down Expand Up @@ -220,7 +220,7 @@ public String getFilterQuery(Class<?> cls, String filterProperty, String filterV


@Override
public Object getManager() {
public EbeanServer getManager() {
return server;
} // getManager()

Expand Down
7 changes: 4 additions & 3 deletions ebean/test/org/tangram/ebean/test/EbeanContentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.tangram.ebean.test;

import com.avaje.ebean.EbeanServer;
import dinistiq.Dinistiq;
import java.lang.reflect.Method;
import java.util.ArrayList;
Expand All @@ -36,7 +37,7 @@
import org.testng.annotations.Test;


public class EbeanContentTest extends BaseContentTest {
public class EbeanContentTest extends BaseContentTest<EbeanServer> {

@Override
protected Map<String, Object> getBeansForContentCreate() {
Expand Down Expand Up @@ -67,13 +68,13 @@ protected <T extends Object> T getInstance(Class<T> type, boolean create) throws


@Override
protected BaseInterface createBaseBean(MutableBeanFactory beanFactory) throws Exception {
protected BaseInterface createBaseBean(MutableBeanFactory<EbeanServer> beanFactory) throws Exception {
return beanFactory.createBean(BaseClass.class);
}


@Override
protected SubInterface createSubBean(MutableBeanFactory beanFactory) throws Exception {
protected SubInterface createSubBean(MutableBeanFactory<EbeanServer> beanFactory) throws Exception {
return beanFactory.createBean(SubClass.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void setDeleteMethodEnabled(boolean deleteMethodEnabled) {
} // setDeleteMethodEnabled()


private MutableBeanFactory getMutableBeanFactory() {
private MutableBeanFactory<?> getMutableBeanFactory() {
return (MutableBeanFactory) getBeanFactory();
} // getMutableBeanFactory()

Expand Down
4 changes: 2 additions & 2 deletions ftp/src/org/tangram/ftp/RntoFtpCommandHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2013-2014 Martin Goellnitz
* Copyright 2013-2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -40,7 +40,7 @@ public class RntoFtpCommandHandler extends RntoCommandHandler {

private static final Logger LOG = LoggerFactory.getLogger(RntoFtpCommandHandler.class);

private final MutableBeanFactory beanFactory;
private final MutableBeanFactory<?> beanFactory;


public RntoFtpCommandHandler(MutableBeanFactory beanFactory) {
Expand Down
6 changes: 3 additions & 3 deletions jdo/src/org/tangram/jdo/AbstractJdoBeanFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2011-2015 Martin Goellnitz
* Copyright 2011-2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -41,7 +41,7 @@
import org.tangram.util.SystemUtils;


public abstract class AbstractJdoBeanFactory extends AbstractMutableBeanFactory implements JdoBeanFactory {
public abstract class AbstractJdoBeanFactory extends AbstractMutableBeanFactory<PersistenceManager> implements JdoBeanFactory {

private static final Logger LOG = LoggerFactory.getLogger(AbstractJdoBeanFactory.class);

Expand Down Expand Up @@ -296,7 +296,7 @@ protected void appendItem(StringBuffer result, String filterProperty, String fil


@Override
public Object getManager() {
public PersistenceManager getManager() {
return manager;
} // getManager()

Expand Down
5 changes: 3 additions & 2 deletions jdo/src/org/tangram/jdo/JdoBeanFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2011-2014 Martin Goellnitz
* Copyright 2011-2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -19,14 +19,15 @@
package org.tangram.jdo;

import java.util.Collection;
import javax.jdo.PersistenceManager;
import org.tangram.content.Content;
import org.tangram.mutable.MutableBeanFactory;


/**
* All BeanFactories dealing with Java Data Objects implement this interface.
*/
public interface JdoBeanFactory extends MutableBeanFactory {
public interface JdoBeanFactory extends MutableBeanFactory<PersistenceManager> {

/**
* set a list of classes to be used as model classes in addition to the statically scanned ones.
Expand Down
10 changes: 9 additions & 1 deletion jpa/src/guicy/jpa-configurer.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2015 Martin Goellnitz
* Copyright 2015-2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -23,6 +23,8 @@ import org.tangram.jpa.JpaBeanFactoryImpl
import org.tangram.content.BeanFactory
import org.tangram.mutable.MutableBeanFactory
import org.tangram.view.GenericPropertyConverter
import com.google.inject.TypeLiteral
import java.lang.reflect.Type

log.info "starting"

Expand All @@ -46,6 +48,12 @@ if (servletContext != null) {
servletContext.setAttribute(Constants.ATTRIBUTE_BEAN_FACTORY, beanFactory)
}
module.bind(BeanFactory.class).toInstance(beanFactory)
Object vehicle = new Object() {
MutableBeanFactory<?> v
};
Type interimType = vehicle.getClass().getDeclaredField("v").getGenericType()
TypeLiteral mbf = TypeLiteral.get(interimType)
module.bind(mbf).toInstance(beanFactory)
module.bind(MutableBeanFactory.class).toInstance(beanFactory)

log.info "done."
Expand Down
4 changes: 2 additions & 2 deletions jpa/src/org/tangram/jpa/JpaBeanFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
*/
@Named("beanFactory")
@Singleton
public class JpaBeanFactoryImpl extends AbstractMutableBeanFactory implements MutableBeanFactory {
public class JpaBeanFactoryImpl extends AbstractMutableBeanFactory<EntityManager> implements MutableBeanFactory<EntityManager> {

private static final Logger LOG = LoggerFactory.getLogger(JpaBeanFactoryImpl.class);

Expand Down Expand Up @@ -252,7 +252,7 @@ public String getFilterQuery(Class<?> cls, String filterProperty, String filterV


@Override
public Object getManager() {
public EntityManager getManager() {
return manager;
} // getManager()

Expand Down
7 changes: 4 additions & 3 deletions jpa/test/org/tangram/jpa/test/JpaContentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.persistence.EntityManager;
import org.tangram.guicy.TangramServletModule;
import org.tangram.jpa.protection.PasswordProtection;
import org.tangram.jpa.test.content.BaseClass;
Expand All @@ -36,7 +37,7 @@
import org.testng.annotations.Test;


public class JpaContentTest extends BaseContentTest {
public class JpaContentTest extends BaseContentTest<EntityManager> {

static {
org.apache.openjpa.enhance.InstrumentationFactory.setDynamicallyInstallAgent(false);
Expand All @@ -51,13 +52,13 @@ protected <T extends Object> T getInstance(Class<T> type, boolean create) throws


@Override
protected BaseInterface createBaseBean(MutableBeanFactory beanFactory) throws Exception {
protected BaseInterface createBaseBean(MutableBeanFactory<EntityManager> beanFactory) throws Exception {
return beanFactory.createBean(BaseClass.class);
}


@Override
protected SubInterface createSubBean(MutableBeanFactory beanFactory) throws Exception {
protected SubInterface createSubBean(MutableBeanFactory<EntityManager> beanFactory) throws Exception {
return beanFactory.createBean(SubClass.class);
}

Expand Down
8 changes: 8 additions & 0 deletions morphia/src/guicy/morphia-configurer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import org.tangram.content.BeanFactory
import org.tangram.morphia.MorphiaBeanFactory
import org.tangram.mutable.MutableBeanFactory
import org.tangram.view.GenericPropertyConverter
import com.google.inject.TypeLiteral
import java.lang.reflect.Type

log.info "starting"

Expand All @@ -37,6 +39,12 @@ beanFactory.setDatabase(config.getProperty("mongo.database", "tangram"))
beanFactory.setBasePackages(basePackages)
module.getServletContext().setAttribute(Constants.ATTRIBUTE_BEAN_FACTORY, beanFactory)
module.bind(BeanFactory.class).toInstance(beanFactory)
Object vehicle = new Object() {
MutableBeanFactory<?> v
};
Type interimType = vehicle.getClass().getDeclaredField("v").getGenericType()
TypeLiteral mbf = TypeLiteral.get(interimType)
module.bind(mbf).toInstance(beanFactory)
module.bind(MutableBeanFactory.class).toInstance(beanFactory)

log.info "done."
Expand Down
4 changes: 2 additions & 2 deletions morphia/src/org/tangram/morphia/MorphiaBeanFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

@Named("beanFactory")
@Singleton
public class MorphiaBeanFactory extends AbstractMutableBeanFactory implements MutableBeanFactory {
public class MorphiaBeanFactory extends AbstractMutableBeanFactory<Datastore> implements MutableBeanFactory<Datastore> {

private static final Logger LOG = LoggerFactory.getLogger(MorphiaBeanFactory.class);

Expand Down Expand Up @@ -205,7 +205,7 @@ public String getFilterQuery(Class<?> cls, String filterProperty, String filterV


@Override
public Object getManager() {
public Datastore getManager() {
return datastore;
} // getManager()

Expand Down
6 changes: 3 additions & 3 deletions morphia/test/org/tangram/morphia/test/MorphiaContentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.testng.annotations.Test;


public class MorphiaContentTest extends BaseContentTest {
public class MorphiaContentTest extends BaseContentTest<Datastore> {

@Override
protected <T extends Object> T getInstance(Class<T> type, boolean create) throws Exception {
Expand All @@ -48,13 +48,13 @@ protected <T extends Object> T getInstance(Class<T> type, boolean create) throws


@Override
protected BaseInterface createBaseBean(MutableBeanFactory beanFactory) throws Exception {
protected BaseInterface createBaseBean(MutableBeanFactory<Datastore> beanFactory) throws Exception {
return beanFactory.createBean(BaseClass.class);
}


@Override
protected SubInterface createSubBean(MutableBeanFactory beanFactory) throws Exception {
protected SubInterface createSubBean(MutableBeanFactory<Datastore> beanFactory) throws Exception {
return beanFactory.createBean(SubClass.class);
}

Expand Down
4 changes: 2 additions & 2 deletions mutable/src/org/tangram/components/mutable/ToolHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class ToolHandler {
private Statistics statistics;

@Inject
private MutableBeanFactory beanFactory;
private MutableBeanFactory<?> beanFactory;

@Inject
private CodeResourceCache codeResourceCache;
Expand Down Expand Up @@ -304,7 +304,7 @@ public TargetDescriptor contentImport(@ActionParameter("xmlfile") byte[] xmlfile
for (Class<? extends Content> c : classes) {
xstream.alias(c.getSimpleName(), c);
} // for

Object contents = xstream.fromXML(input);
LOG.info("doImport() {}", contents);
if (contents instanceof List) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2013-2015 Martin Goellnitz
* Copyright 2013-2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -47,7 +47,7 @@
/**
* Common stuff for all bean factories dealing with mutable content.
*/
public abstract class AbstractMutableBeanFactory extends AbstractBeanFactory implements MutableBeanFactory {
public abstract class AbstractMutableBeanFactory<M extends Object> extends AbstractBeanFactory implements MutableBeanFactory<M> {

private static final Logger LOG = LoggerFactory.getLogger(AbstractMutableBeanFactory.class);

Expand Down
4 changes: 2 additions & 2 deletions mutable/src/org/tangram/mutable/CodeHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright 2013-2015 Martin Goellnitz
* Copyright 2013-2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -212,7 +212,7 @@ public static String getAnnotation(String filename) {
* @param modificationTime time of last modification of the code item in milliseconds since the epoche
* @throws Exception IO and class lookup related exceptions may occur but are unlikely
*/
public static void updateCode(MutableBeanFactory beanFactory, CodeResourceCache codeResourceCache, String mimetype, String filename, byte[] contents, long modificationTime) throws Exception {
public static void updateCode(MutableBeanFactory<?> beanFactory, CodeResourceCache codeResourceCache, String mimetype, String filename, byte[] contents, long modificationTime) throws Exception {
// This hopefully is one really just one class effectively
Class<? extends MutableCode> codeClass = beanFactory.getImplementingClasses(MutableCode.class).get(0);
String annotation = CodeHelper.getAnnotation(filename);
Expand Down
4 changes: 2 additions & 2 deletions mutable/src/org/tangram/mutable/MutableBeanFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.tangram.content.Content;


public interface MutableBeanFactory extends BeanFactory {
public interface MutableBeanFactory<M extends Object> extends BeanFactory {

/**
* Returns the root class of all content classes handled by the implementing instance - may be null;
Expand Down Expand Up @@ -170,6 +170,6 @@ public interface MutableBeanFactory extends BeanFactory {
*
* @return implementation specific manager instance
*/
Object getManager();
M getManager();

} // MutableBeanFactory
6 changes: 3 additions & 3 deletions mutable/test/org/tangram/mock/MockMutableBeanFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
/**
* Mock utility class for mutable bean factory instances needed in tests.
*/
public class MockMutableBeanFactory extends MockBeanFactory implements MutableBeanFactory {
public class MockMutableBeanFactory extends MockBeanFactory implements MutableBeanFactory<MockOrmManager> {

private static final Logger LOG = LoggerFactory.getLogger(MockMutableBeanFactory.class);

private final Object manager = new MockOrmManager();
private final MockOrmManager manager = new MockOrmManager();

private final Collection<Class<? extends Content>> clearedClasses = new HashSet<>();

Expand Down Expand Up @@ -180,7 +180,7 @@ public Collection<Class<? extends Content>> getClearedCacheClasses() {


@Override
public Object getManager() {
public MockOrmManager getManager() {
return manager;
} // getManager()

Expand Down
Loading

0 comments on commit 310b3e6

Please sign in to comment.