PageRenderTime 24ms CodeModel.GetById 21ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/ingenieux/nullweblog/util/PersistenceModule.java

https://bitbucket.org/aldrinleal/nullweblog
Java | 57 lines | 45 code | 12 blank | 0 comment | 0 complexity | b3cda0d77185f23ac0ee8a3d6235911e MD5 | raw file
 1package com.ingenieux.nullweblog.util;
 2
 3import java.util.HashMap;
 4import java.util.Map;
 5
 6import javax.cache.Cache;
 7import javax.cache.CacheFactory;
 8import javax.cache.CacheManager;
 9
10import com.google.appengine.api.memcache.stdimpl.GCacheFactory;
11import com.google.appengine.api.users.UserService;
12import com.google.appengine.api.users.UserServiceFactory;
13import com.google.inject.Binder;
14import com.google.inject.Module;
15import com.googlecode.objectify.Objectify;
16import com.googlecode.objectify.ObjectifyService;
17import com.ingenieux.nullweblog.model.Post;
18import com.ingenieux.nullweblog.model.Usuario;
19import com.ingenieux.nullweblog.server.PostDao;
20import com.ingenieux.nullweblog.server.UsuarioDao;
21
22public class PersistenceModule implements Module {
23	@Override
24	public void configure(Binder binder) {
25		configureCache(binder);
26		configurePersistence(binder);
27	}
28
29	private void configureCache(Binder binder) {
30		try {
31			Map<Integer, Object> cacheConfig = new HashMap<Integer, Object>();
32
33			cacheConfig.put(GCacheFactory.EXPIRATION_DELTA, 3600);
34
35			CacheFactory cacheFactory = CacheManager.getInstance()
36					.getCacheFactory();
37
38			binder.bind(Cache.class).toInstance(
39					cacheFactory.createCache(cacheConfig));
40		} catch (Exception exc) {
41			throw new RuntimeException(exc);
42		}
43	}
44
45	private void configurePersistence(Binder binder) {
46		ObjectifyService.register(Post.class);
47		ObjectifyService.register(Usuario.class);
48
49		binder.bind(UserService.class).toInstance(
50				UserServiceFactory.getUserService());
51
52		binder.bind(Objectify.class).toInstance(ObjectifyService.begin());
53
54		binder.bind(PostDao.class).toInstance(new PostDao());
55		binder.bind(UsuarioDao.class).toInstance(new UsuarioDao());
56	}
57}