Updated wiring to allow spring beans to be used in guice
Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/6f87fff5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/6f87fff5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/6f87fff5 Branch: refs/heads/USERGRID-578 Commit: 6f87fff520fa14341fef74c8e85fc8456ddc02a8 Parents: 84d779f Author: Todd Nine <tn...@apigee.com> Authored: Fri Apr 17 11:57:23 2015 -0600 Committer: Todd Nine <tn...@apigee.com> Committed: Fri Apr 17 11:58:40 2015 -0600 ---------------------------------------------------------------------- .../main/resources/usergrid-default.properties | 3 +- .../corepersistence/CpEntityManager.java | 19 +-- .../corepersistence/CpEntityManagerFactory.java | 15 ++- .../corepersistence/CpRelationManager.java | 12 +- .../usergrid/corepersistence/GuiceFactory.java | 66 +++++----- .../usergrid/persistence/PersistenceModule.java | 124 +++---------------- .../main/resources/usergrid-core-context.xml | 8 +- .../resources/usergrid-custom-test.properties | 2 - 8 files changed, 89 insertions(+), 160 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6f87fff5/stack/config/src/main/resources/usergrid-default.properties ---------------------------------------------------------------------- diff --git a/stack/config/src/main/resources/usergrid-default.properties b/stack/config/src/main/resources/usergrid-default.properties index ffc7628..c21a1ff 100644 --- a/stack/config/src/main/resources/usergrid-default.properties +++ b/stack/config/src/main/resources/usergrid-default.properties @@ -82,10 +82,9 @@ cassandra.cluster=Test Cluster cassandra.keyspace.strategy=org.apache.cassandra.locator.SimpleStrategy #cassandra.keyspace.strategy=org.apache.cassandra.locator.NetworkTopologyStrategy -#cassandra.keyspace.strategy.options.replication_factor=1 -#cassandra.keyspace.strategy.options.us-east=1 cassandra.keyspace.replication=replication_factor:1 +#cassandra.keyspace.replication=us-east:3 cassandra.username= cassandra.password= http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6f87fff5/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java ---------------------------------------------------------------------- diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java index a615a43..1506551 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java @@ -37,6 +37,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.Assert; +import org.apache.usergrid.corepersistence.index.IndexService; import org.apache.usergrid.corepersistence.util.CpEntityMapUtils; import org.apache.usergrid.corepersistence.util.CpNamingUtils; import org.apache.usergrid.persistence.AggregateCounter; @@ -165,17 +166,19 @@ public class CpEntityManager implements EntityManager { public static final String APPLICATION_ENTITIES = "application.entities"; public static final long ONE_COUNT = 1L; - private UUID applicationId; + private final UUID applicationId; private Application application; - private ManagerCache managerCache; + private final ManagerCache managerCache; - private ApplicationScope applicationScope; + private final ApplicationScope applicationScope; - private CassandraService cass; + private final CassandraService cass; - private CounterUtils counterUtils; + private final CounterUtils counterUtils; + + private final IndexService indexService; private boolean skipAggregateCounters; private MetricsFactory metricsFactory; @@ -215,16 +218,18 @@ public class CpEntityManager implements EntityManager { * @param metricsFactory * @param applicationId */ - public CpEntityManager(final CassandraService cass, final CounterUtils counterUtils, final ManagerCache managerCache, final MetricsFactory metricsFactory, final UUID applicationId ) { + public CpEntityManager(final CassandraService cass, final CounterUtils counterUtils, final IndexService indexService, final ManagerCache managerCache, final MetricsFactory metricsFactory, final UUID applicationId ) { Preconditions.checkNotNull( cass, "cass must not be null" ); Preconditions.checkNotNull( counterUtils, "counterUtils must not be null" ); Preconditions.checkNotNull( managerCache, "managerCache must not be null" ); Preconditions.checkNotNull( applicationId, "applicationId must not be null" ); + Preconditions.checkNotNull( indexService, "indexService must not be null" ); this.managerCache = managerCache; this.applicationId = applicationId; + this.indexService = indexService; applicationScope = CpNamingUtils.getApplicationScope( applicationId ); @@ -734,7 +739,7 @@ public class CpEntityManager implements EntityManager { Preconditions.checkNotNull( entityRef, "entityRef cannot be null" ); CpRelationManager relationManager = - new CpRelationManager( metricsFactory, managerCache, this, applicationId, entityRef ); + new CpRelationManager( metricsFactory, managerCache, indexService, this, applicationId, entityRef ); return relationManager; } http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6f87fff5/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java ---------------------------------------------------------------------- diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java index e03ed47..6ad8616 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java @@ -39,6 +39,8 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.apache.commons.lang.StringUtils; + +import org.apache.usergrid.corepersistence.index.IndexService; import org.apache.usergrid.corepersistence.util.CpNamingUtils; import org.apache.usergrid.exception.ConflictException; import org.apache.usergrid.persistence.*; @@ -109,9 +111,10 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application private Injector injector; private final EntityIndex entityIndex; private final MetricsFactory metricsFactory; + private final IndexService indexService; - public CpEntityManagerFactory( - final CassandraService cassandraService, final CounterUtils counterUtils, final Injector injector) { + public CpEntityManagerFactory( final CassandraService cassandraService, final CounterUtils counterUtils, + final Injector injector) { this.cassandraService = cassandraService; this.counterUtils = counterUtils; @@ -120,7 +123,10 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application this.entityIndexFactory = injector.getInstance(EntityIndexFactory.class); this.managerCache = injector.getInstance( ManagerCache.class ); this.metricsFactory = injector.getInstance( MetricsFactory.class ); - this.applicationIdCache = injector.getInstance(ApplicationIdCacheFactory.class).getInstance(getManagementEntityManager()); + this.indexService = injector.getInstance( IndexService.class ); + this.applicationIdCache = injector.getInstance(ApplicationIdCacheFactory.class).getInstance( + getManagementEntityManager() ); + } @@ -175,8 +181,7 @@ public class CpEntityManagerFactory implements EntityManagerFactory, Application private EntityManager _getEntityManager( UUID applicationId ) { - - EntityManager em = new CpEntityManager(cassandraService, counterUtils, managerCache, metricsFactory, applicationId ); + EntityManager em = new CpEntityManager(cassandraService, counterUtils, indexService, managerCache, metricsFactory, applicationId ); return em; } http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6f87fff5/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java ---------------------------------------------------------------------- diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java index 3c72b60..a0a44b3 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java @@ -30,6 +30,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.Assert; +import org.apache.usergrid.corepersistence.index.IndexService; import org.apache.usergrid.corepersistence.results.CollectionResultsLoaderFactoryImpl; import org.apache.usergrid.corepersistence.results.ConnectionResultsLoaderFactoryImpl; import org.apache.usergrid.corepersistence.results.ElasticSearchQueryExecutor; @@ -117,18 +118,23 @@ public class CpRelationManager implements RelationManager { private org.apache.usergrid.persistence.model.entity.Entity cpHeadEntity; - private ApplicationScope applicationScope; + private final ApplicationScope applicationScope; + + private final IndexService indexService; private MetricsFactory metricsFactory; private Timer updateCollectionTimer; - public CpRelationManager(final MetricsFactory metricsFactory, final ManagerCache managerCache, final EntityManager em, final UUID applicationId, final EntityRef headEntity ) { + public CpRelationManager( final MetricsFactory metricsFactory, final ManagerCache managerCache, final IndexService indexService, final EntityManager em, final UUID applicationId, final EntityRef headEntity) { + Assert.notNull( em, "Entity manager cannot be null" ); Assert.notNull( applicationId, "Application Id cannot be null" ); Assert.notNull( headEntity, "Head entity cannot be null" ); Assert.notNull( headEntity.getUuid(), "Head entity uuid cannot be null" ); + Assert.notNull( indexService, "indexService cannot be null" ); + // TODO: this assert should not be failing //Assert.notNull( indexBucketLocator, "indexBucketLocator cannot be null" ); this.em = em; @@ -155,6 +161,8 @@ public class CpRelationManager implements RelationManager { Assert.notNull( cpHeadEntity, String .format( "cpHeadEntity cannot be null for entity id %s, app id %s", entityId.getUuid(), applicationId ) ); + this.indexService = indexService; + } http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6f87fff5/stack/core/src/main/java/org/apache/usergrid/corepersistence/GuiceFactory.java ---------------------------------------------------------------------- diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/GuiceFactory.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/GuiceFactory.java index 3a08034..6a87005 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/GuiceFactory.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/GuiceFactory.java @@ -22,20 +22,22 @@ package org.apache.usergrid.corepersistence; import java.util.Properties; +import javax.inject.Named; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.BeansException; import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; import org.apache.commons.lang.StringUtils; import org.apache.usergrid.persistence.PersistenceModule; +import com.google.common.base.Preconditions; import com.google.inject.Guice; import com.google.inject.Injector; -import com.google.inject.Singleton; import com.netflix.config.ConfigurationManager; import me.prettyprint.cassandra.service.CassandraHost; @@ -45,25 +47,28 @@ import me.prettyprint.cassandra.service.CassandraHostConfigurator; /** * Factory for configuring Guice then returning it */ -@Singleton +@Component public class GuiceFactory implements FactoryBean<Injector> { private static final Logger logger = LoggerFactory.getLogger( GuiceFactory.class ); - private final CassandraHostConfigurator chc; + @Autowired + private CassandraHostConfigurator chc; - private final Properties systemProperties; + @Autowired + @Named("properties") + private Properties systemProperties; + @Autowired private ApplicationContext applicationContext; private Injector injector; - - public GuiceFactory( final ApplicationContext applicationContext, final CassandraHostConfigurator chc, final Properties systemProperties ) { - this.chc = chc; - this.systemProperties = systemProperties; - this.applicationContext = applicationContext; + public GuiceFactory() { + // this.chc = chc; + // this.systemProperties = systemProperties; + // this.applicationContext = applicationContext; } @@ -83,15 +88,16 @@ public class GuiceFactory implements FactoryBean<Injector> { if ( hosts.length == 0 ) { throw new RuntimeException( "Fatal error: no Cassandra hosts configured" ); } - String sep = ""; + for ( CassandraHost host : hosts ) { if ( StringUtils.isEmpty( host.getHost() ) ) { throw new RuntimeException( "Fatal error: Cassandra hostname cannot be empty" ); } - hostsString = hostsString + sep + host.getHost(); - sep = ","; + hostsString += host.getHost() + ","; } + hostsString = hostsString.substring( 0, hostsString.length() - 1 ); + logger.info( "hostsString: " + hostsString ); Properties cpProps = new Properties(); @@ -99,21 +105,14 @@ public class GuiceFactory implements FactoryBean<Injector> { // Some Usergrid properties must be mapped to Core Persistence properties cpProps.put( "cassandra.hosts", hostsString ); cpProps.put( "cassandra.port", hosts[0].getPort() ); - cpProps.put( "cassandra.cluster_name", systemProperties.getProperty( "cassandra.cluster" ) ); - String cassRemoteString = ( String ) systemProperties.getProperty( "cassandra.use_remote" ); - if ( cassRemoteString != null && cassRemoteString.equals( "false" ) ) { - cpProps.put( "cassandra.embedded", "true" ); - } - else { - cpProps.put( "cassandra.embedded", "false" ); - } + cpProps.put( "cassandra.cluster_name", getAndValidateProperty( "cassandra.cluster" ) ); - cpProps.put( "collections.keyspace.strategy.class", - systemProperties.getProperty( "cassandra.keyspace.strategy" ) ); + cpProps + .put( "collections.keyspace.strategy.class", getAndValidateProperty( "cassandra.keyspace.strategy" ) ); cpProps.put( "collections.keyspace.strategy.options", - systemProperties.getProperty( "cassandra.keyspace.replication" ) ); + getAndValidateProperty( "cassandra.keyspace.replication" ) ); logger.debug( "Set Cassandra properties for Core Persistence: " + cpProps.toString() ); @@ -128,14 +127,23 @@ public class GuiceFactory implements FactoryBean<Injector> { } - - //this is seriously fugly, and needs removed we shouldn't be mixing spring and guice - injector = Guice.createInjector( new CoreModule( ), new PersistenceModule( applicationContext ) ); + //we have to inject a couple of spring beans into our Guice. Wire it with PersistenceModule + injector = Guice.createInjector( new CoreModule(), new PersistenceModule( applicationContext ) ); return injector; } + private String getAndValidateProperty( final String propName ) { + + final String propValue = systemProperties.getProperty( propName ); + + Preconditions.checkNotNull( propValue, propName + " cannot be unset. Set this in your properties" ); + + return propValue; + } + + @Override public Class<?> getObjectType() { return Injector.class; @@ -146,6 +154,4 @@ public class GuiceFactory implements FactoryBean<Injector> { public boolean isSingleton() { return true; } - - } http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6f87fff5/stack/core/src/main/java/org/apache/usergrid/persistence/PersistenceModule.java ---------------------------------------------------------------------- diff --git a/stack/core/src/main/java/org/apache/usergrid/persistence/PersistenceModule.java b/stack/core/src/main/java/org/apache/usergrid/persistence/PersistenceModule.java index 6e58676..70fff90 100644 --- a/stack/core/src/main/java/org/apache/usergrid/persistence/PersistenceModule.java +++ b/stack/core/src/main/java/org/apache/usergrid/persistence/PersistenceModule.java @@ -20,32 +20,15 @@ package org.apache.usergrid.persistence; -import java.io.IOException; -import java.net.URL; -import java.nio.charset.Charset; -import java.util.Collection; -import java.util.Properties; - +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.context.ApplicationContext; -import org.apache.usergrid.locking.cassandra.HectorLockManagerImpl; -import com.google.common.base.Preconditions; -import com.google.common.io.CharSource; -import com.google.common.io.Resources; import com.google.inject.AbstractModule; -import com.google.inject.Inject; -import com.google.inject.Provides; -import com.google.inject.Singleton; -import com.google.inject.name.Named; -import com.google.inject.name.Names; +import com.google.inject.Provider; import com.google.inject.spring.SpringIntegration; -import me.prettyprint.cassandra.connection.RoundRobinBalancingPolicy; -import me.prettyprint.cassandra.service.CassandraHostConfigurator; -import me.prettyprint.cassandra.service.ThriftCluster; -import me.prettyprint.hector.api.Cluster; - /** * Replacement for configuration of our spring modules with guice @@ -66,97 +49,18 @@ public class PersistenceModule extends AbstractModule { @Override protected void configure() { - SpringIntegration.bindAll( binder(), applicationContext ); - } + //bind the application context to our guice instance + + final BeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory(); + bind( BeanFactory.class).toInstance( beanFactory); + + //create our bridge and put the EMF into guice. Ultimately all spring needs removed and we need to just use guice + + final Provider<EntityManagerFactory> emfProvider = SpringIntegration.fromSpring( EntityManagerFactory.class, "entityManagerFactory" ); + + bind( EntityManagerFactory.class ).toProvider( emfProvider ); + } -// <bean id="cassandraCluster" class="me.prettyprint.cassandra.service.ThriftCluster"> -// <constructor-arg value="${cassandra.cluster}" /> -// <constructor-arg ref="cassandraHostConfigurator" /> -// </bean> -// @Provides -// @Singleton -// @Inject -// public Cluster configureThrift( @Named( "cassandra.cluster" ) final String cassCluster, -// @Named( "cassandra.connections" ) final int cassandraConnections ){ -// -// final int setSize = cassandraConnections == 0 ? 50: cassandraConnections; -// -// CassandraHostConfigurator hostConfigurator = new CassandraHostConfigurator( cassCluster ); -// -// hostConfigurator.setMaxActive( setSize ); -// hostConfigurator.setLoadBalancingPolicy( new RoundRobinBalancingPolicy() ); -// -// -// ThriftCluster thriftCluster = new ThriftCluster(cassCluster, hostConfigurator); -// -// return thriftCluster; -// -// } -// -// -// @Provides -// @Singleton -// @Inject -// public Properties configureProps(final PropertiesProvider propertiesProvider ){ -// -// final Properties props = new Properties( ); -// -// for(final String propFile: propertiesProvider.getPropertiesFiles()){ -// -// final URL url = Resources.getResource( propFile ); -// -// Preconditions.checkNotNull( url, "Could not find properties file '" + propFile + "' on the classpath" ); -// -// -// final CharSource propsInput = Resources.asCharSource( url, Charset.defaultCharset() ); -// try { -// props.load( propsInput.openStream() ); -// } -// catch ( IOException e ) { -// throw new RuntimeException( "Unable to load properties file '" + propFile + "'", e ); -// } -// } -// -// //bind these properties -// Names.bindProperties( binder(), props ); -// -// return props; -// } -// -// @Provides -// @Singleton -// @Inject -// public void configureLocks(final Cluster hectorCluster, @Named("cassandra.lock.keyspace") final String lockKeyspace, @Named("cassandra.lock.keyspace") final String writeCl, final String readCl ){ -// -// -// final HectorLockManagerImpl hectorLockManager = new HectorLockManagerImpl(); -// -// -//// -//// <bean name="consistencyLevelPolicy" class="me.prettyprint.cassandra.model.ConfigurableConsistencyLevel"> -//// <property name="defaultReadConsistencyLevel" value="${cassandra.readcl}"/> -//// <property name="defaultWriteConsistencyLevel" value="${cassandra.writecl}"/> -//// </bean> -// -//// <bean name="lockManager" class="org.apache.usergrid.locking.cassandra.HectorLockManagerImpl" > -//// <property name="cluster" ref="cassandraCluster"/> -//// <property name="keyspaceName" value="${cassandra.lock.keyspace}"/> -//// <property name="consistencyLevelPolicy" ref="consistencyLevelPolicy"/> -//// </bean> -// -// } -// -// -// /** -// * Interface to allow users to provide and inject properties -// */ -// public interface PropertiesProvider{ -// /** -// * Get the properties files to load -// * @return -// */ -// String[] getPropertiesFiles(); -// } } http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6f87fff5/stack/core/src/main/resources/usergrid-core-context.xml ---------------------------------------------------------------------- diff --git a/stack/core/src/main/resources/usergrid-core-context.xml b/stack/core/src/main/resources/usergrid-core-context.xml index 0be6d00..f33586f 100644 --- a/stack/core/src/main/resources/usergrid-core-context.xml +++ b/stack/core/src/main/resources/usergrid-core-context.xml @@ -95,10 +95,14 @@ <bean id="injector" class="org.apache.usergrid.corepersistence.GuiceFactory"> - <constructor-arg ref="cassandraHostConfigurator" /> - <constructor-arg ref="properties" /> </bean> + <!-- <bean id="injector" + class="org.apache.usergrid.corepersistence.GuiceFactory"> + <constructor-arg ref="cassandraHostConfigurator" /> + <constructor-arg ref="properties" /> + </bean>--> + <bean id="cassandraService" class="org.apache.usergrid.persistence.cassandra.CassandraService" init-method="init" destroy-method="destroy"> <constructor-arg ref="properties" /> http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/6f87fff5/stack/core/src/test/resources/usergrid-custom-test.properties ---------------------------------------------------------------------- diff --git a/stack/core/src/test/resources/usergrid-custom-test.properties b/stack/core/src/test/resources/usergrid-custom-test.properties index 93ef28b..703424f 100644 --- a/stack/core/src/test/resources/usergrid-custom-test.properties +++ b/stack/core/src/test/resources/usergrid-custom-test.properties @@ -14,8 +14,6 @@ # these settings allow tests to run and consistently pass on 16GB MacBook Pro # with ug.heapmax=5000m and ug.heapmin=3000m (set in Maven settings.xml) -cassandra.startup=external -elasticsearch.startup=external cassandra.timeout=2000 cassandra.connections=1000