diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheFactory.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheFactory.java new file mode 100644 index 0000000000..ac8b43bfbe --- /dev/null +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheFactory.java @@ -0,0 +1,43 @@ +/* + * + * * Licensed to the Apache Software Foundation (ASF) under one or more + * * contributor license agreements. The ASF licenses this file to You + * * 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. For additional information regarding + * * copyright in this work, please see the NOTICE file in the top level + * * directory of this distribution. + * + */ +package org.apache.usergrid.corepersistence; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import org.apache.usergrid.persistence.EntityManager; + +/** + * returns instances of cache. + */ +@Singleton +public class ApplicationIdCacheFactory { + private final ApplicationIdCacheFig fig; + private final ManagerCache cache; + + @Inject + public ApplicationIdCacheFactory(ApplicationIdCacheFig fig,ManagerCache cache){ + + this.fig = fig; + this.cache = cache; + } + public ApplicationIdCache getInstance(EntityManager managementEntityManager){ + return new ApplicationIdCacheImpl(managementEntityManager,cache,fig); + } +} diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java index 2063fb108b..ec98e2303d 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java @@ -51,7 +51,6 @@ * Implements the org app cache for faster runtime lookups. These values are immutable, so this LRU cache can stay * full for the duration of the execution */ -@Singleton public class ApplicationIdCacheImpl implements ApplicationIdCache { private static final Logger logger = LoggerFactory.getLogger(ApplicationIdCacheImpl.class); @@ -59,17 +58,15 @@ public class ApplicationIdCacheImpl implements ApplicationIdCache { /** * Cache the pointer to our root entity manager for reference */ - private final EntityManager rootEm; - private final CpEntityManagerFactory emf; private final LoadingCache> appCache; + private final EntityManager managementEnityManager; + private final ManagerCache managerCache; - - @Inject - public ApplicationIdCacheImpl(final EntityManagerFactory emf, ApplicationIdCacheFig fig) { - this.emf = (CpEntityManagerFactory)emf; - this.rootEm = emf.getEntityManager(emf.getManagementAppId()); + public ApplicationIdCacheImpl(final EntityManager managementEnityManager, ManagerCache managerCache, ApplicationIdCacheFig fig) { + this.managementEnityManager = managementEnityManager; + this.managerCache = managerCache; appCache = CacheBuilder.newBuilder() .maximumSize(fig.getCacheSize()) .expireAfterWrite(fig.getCacheTimeout(), TimeUnit.MILLISECONDS) @@ -101,12 +98,12 @@ private Optional fetchApplicationId( final String applicationName ) { UUID value = null; - EntityCollectionManager ecm = emf.getManagerCache().getEntityCollectionManager( + EntityCollectionManager ecm = managerCache.getEntityCollectionManager( new ApplicationScopeImpl( new SimpleId( CpNamingUtils.MANAGEMENT_APPLICATION_ID, Schema.TYPE_APPLICATION ) ) ); try { - if ( rootEm.getApplication() == null ) { + if ( managementEnityManager.getApplication() == null ) { return Optional.empty(); } } catch ( Exception e ) { diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java index 8a8a1e0886..7859ffc3fa 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java @@ -110,6 +110,7 @@ public void configureMigrationProvider() { // install(new QueueModule()); bind(ManagerCache.class).to( CpManagerCache.class ); + bind(ApplicationIdCacheFactory.class); Multibinder entityBinder = Multibinder.newSetBinder(binder(), EntityDeleted.class); @@ -143,7 +144,6 @@ public void configureMigrationProvider() { bind(AllApplicationsObservable.class).to(AllApplicationsObservableImpl.class); install(new GuicyFigModule(ApplicationIdCacheFig.class)); - bind(ApplicationIdCache.class).to(ApplicationIdCacheImpl.class); } 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 f5ae1c6408..642d5557a7 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 @@ -118,7 +118,7 @@ public CpEntityManagerFactory( this.entityIndexFactory = injector.getInstance(EntityIndexFactory.class); this.managerCache = injector.getInstance( ManagerCache.class ); this.metricsFactory = injector.getInstance( MetricsFactory.class ); - this.applicationIdCache = injector.getInstance(ApplicationIdCache.class); + this.applicationIdCache = injector.getInstance(ApplicationIdCacheFactory.class).getInstance(getManagementEntityManager()); }