@Bean public CacheManager cacheManager() { log.debug("Starting Ehcache"); cacheManager = net.sf.ehcache.CacheManager.create(); cacheManager .getConfiguration() .setMaxBytesLocalHeap( env.getProperty("cache.ehcache.maxBytesLocalHeap", String.class, "16M")); log.debug("Registering Ehcache Metrics gauges"); Set<EntityType<?>> entities = entityManager.getMetamodel().getEntities(); for (EntityType<?> entity : entities) { String name = entity.getName(); if (name == null || entity.getJavaType() != null) { name = entity.getJavaType().getName(); } Assert.notNull(name, "entity cannot exist without a identifier"); net.sf.ehcache.Cache cache = cacheManager.getCache(name); if (cache != null) { cache .getCacheConfiguration() .setTimeToLiveSeconds(env.getProperty("cache.timeToLiveSeconds", Long.class, 3600L)); net.sf.ehcache.Ehcache decoratedCache = InstrumentedEhcache.instrument(metricRegistry, cache); cacheManager.replaceCacheWithDecoratedCache(cache, decoratedCache); } } EhCacheCacheManager ehCacheManager = new EhCacheCacheManager(); ehCacheManager.setCacheManager(cacheManager); return ehCacheManager; }
private void checkEntityId(Object entityId) { if (entityId == null) { throw new IllegalArgumentException("Invalid null entity id given"); } EntityType<?> entityType = em.getMetamodel() .entity( joinManager .getRootNodeOrFail( "Paginated queries do not support multiple from clause elements!") .getPropertyClass()); Attribute<?, ?> idAttribute = JpaUtils.getIdAttribute(entityType); Class<?> idType = JpaUtils.resolveFieldClass(entityType.getJavaType(), idAttribute); if (!idType.isInstance(entityId)) { throw new IllegalArgumentException( "The type of the given entity id '" + entityId.getClass().getName() + "' is not an instance of the expected id type '" + idType.getName() + "' of the entity class '" + entityType.getJavaType().getName() + "'"); } }
private synchronized void reloadCache(EntityManager em) { if (em == null || ref.get() != null) return; Metamodel metamodel = em.getMetamodel(); Map<String, Class<?>> map = new HashMap<>(metamodel.getEntities().size() + 2); for (EntityType<?> entity : metamodel.getEntities()) { System.out.printf( "--- Carregando do Metamodel: %s [%s] \n", entity.getName(), entity.getJavaType()); map.put(entity.getName(), entity.getJavaType()); } ref = new SoftReference(map); }
/** * Checks if entity is supclass of a @MappedSuperclass. * * @param subEntity Subentity * @param superEntity Possible super entity * @return true if super entity is subentity's superclass */ private static boolean isSubClassOfMappedSuperClass( EntityType<?> subEntity, EntityType<?> superEntity) { boolean returnValue = false; if (subEntity.getSupertype().getSupertype() == null) { returnValue = false; } else if (subEntity.getSupertype().getSupertype().getJavaType() == superEntity.getJavaType()) { returnValue = true; } return returnValue; }
@Override public Map<String, Class<Entity>> getTableMap() { Map<String, Class<Entity>> map = new HashMap<String, Class<Entity>>(); Metamodel metamodel = entityManager.getEntityManagerFactory().getMetamodel(); Set<EntityType<?>> set = metamodel.getEntities(); for (EntityType<?> entityType : set) { Class<Entity> clazz = (Class<Entity>) entityType.getJavaType(); map.put(entityType.getName(), clazz); } return map; }
@Before public void setUp() throws Exception { initMocks(this); final EntityType<Foo> foo = mock(EntityType.class); when(foo.getName()).thenReturn("foo"); when(foo.getJavaType()).thenReturn(Foo.class); when(metamodel.entity(Foo.class)).thenReturn(foo); when(metamodel.getEntities()).thenReturn(new HashSet<EntityType<?>>(Arrays.asList(foo))); wrapper = new MetamodelWrapper(metamodel); }
@Bean public CacheManager cacheManager(JHipsterProperties jHipsterProperties) { log.debug("Starting Ehcache"); cacheManager = net.sf.ehcache.CacheManager.create(); cacheManager .getConfiguration() .setMaxBytesLocalHeap(jHipsterProperties.getCache().getEhcache().getMaxBytesLocalHeap()); log.debug("Registering Ehcache Metrics gauges"); Set<EntityType<?>> entities = entityManager.getMetamodel().getEntities(); for (EntityType<?> entity : entities) { String name = entity.getName(); if (name == null || entity.getJavaType() != null) { name = entity.getJavaType().getName(); } Assert.notNull(name, "entity cannot exist without an identifier"); reconfigureCache(name, jHipsterProperties); for (PluralAttribute pluralAttribute : entity.getPluralAttributes()) { reconfigureCache(name + "." + pluralAttribute.getName(), jHipsterProperties); } } EhCacheCacheManager ehCacheManager = new EhCacheCacheManager(); ehCacheManager.setCacheManager(cacheManager); return ehCacheManager; }
@SuppressWarnings("unchecked") private Class<E> getResultType(EntityManager em) { // We support this so users don't have to specify the query return type when using @Sync. if (resultTypeFqcn.equals("java.lang.Object")) { return (Class<E>) Object.class; } for (EntityType<?> et : em.getMetamodel().getEntities()) { if (et.getJavaType().getName().equals(resultTypeFqcn)) { return ((EntityType<E>) et).getJavaType(); } } throw new IllegalStateException( "Result type " + resultTypeFqcn + " is not known to the EntityManager."); }
public void run() { logger.debug("Starting protean server"); if (entityManager != null) { for (EntityType<?> entity : entityManager.getMetamodel().getEntities()) { if (entity.getJavaType().getAnnotation(Protean.class) == null) { continue; } getEntities().put(entity.getName(), entity); } } Thread thread = new Thread(server); thread.start(); logger.info("Started protean server"); }