Authorized album is now working fully.
[photos] / src / main / java / org / wamblee / photos / model / plumbing / Producer.java
index 4e5b1da7b6a2a1540c3fb9c11bab938993f0dd09..a3d9c2a74f75525050cfc36887f674e36e4287b1 100644 (file)
@@ -38,7 +38,6 @@ import org.wamblee.io.InputResource;
 import org.wamblee.photos.concurrent.ConcurrentAlbum;
 import org.wamblee.photos.model.Album;
 import org.wamblee.photos.model.PhotoEntry;
-import org.wamblee.photos.model.authorization.AuthorizedAlbum;
 import org.wamblee.photos.model.filesystem.FileSystemAlbum;
 import org.wamblee.photos.security.PageAuthorizationRule;
 import org.wamblee.photos.security.PhotoAuthorizationRule;
@@ -104,6 +103,14 @@ public class Producer {
     @AllPhotos
     private Album allPhotos;
 
+    @Inject
+    @UserCache
+    private Cache<String, User> userCache;
+
+    @Inject
+    @PhotoCache
+    private Cache<String, ArrayList<PhotoEntry>> photoCache;
+
     private Configuration getCOnfiguration() {
         LOGGER.info("Initializing configuration");
         Configuration config;
@@ -122,32 +129,51 @@ public class Producer {
     @ApplicationScoped
     public UserAdministration getUserAdmin() {
         LOGGER.info("Initializing user administration");
+        NameValidator passwordvalidator = new RegexpNameValidator(".{5,}",
+            "INVALID_PASSWORD", "Password must have at least 5 characters");
+        MessageDigester passwordEncoder = new Md5HexMessageDigester();
+        UserSet userset = new JpaUserSet(userCache, passwordvalidator,
+            passwordEncoder, entityManager);
+        GroupSet groupset = new JpaGroupSet(entityManager);
+        NameValidator uservalidator = new RegexpNameValidator(
+            "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_USERNAME",
+            "User name must consist of alphanumeric characters only");
+        NameValidator groupvalidator = new RegexpNameValidator(
+            "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_GROUPNAME",
+            "Group name must consist of alphanumeric characters only");
+
+        UserAdministration admin = new UserAdministrationImpl(userset,
+            groupset, uservalidator, groupvalidator);
+        UserAdminInitializer initializer = new UserAdminInitializer(admin,
+            new String[] { "erik", "admin" }, new String[] { "users",
+                "administrators" }, new String[] { "abc123", "abc123" });
+        return admin;
+    }
+
+    @Produces
+    @ApplicationScoped
+    @UserCache
+    public Cache<String, User> getUserCache() {
         try {
-            NameValidator passwordvalidator = new RegexpNameValidator(".{5,}",
-                "INVALID_PASSWORD", "Password must have at least 5 characters");
             InputResource cacheConfig = new ClassPathResource(
                 "META-INF/ehcache.xml");
-            Cache<String, User> userCache = new EhCache(cacheConfig, "users");
-            MessageDigester passwordEncoder = new Md5HexMessageDigester();
-            UserSet userset = new JpaUserSet(userCache, passwordvalidator,
-                passwordEncoder, entityManager);
-            GroupSet groupset = new JpaGroupSet(entityManager);
-            NameValidator uservalidator = new RegexpNameValidator(
-                "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_USERNAME",
-                "User name must consist of alphanumeric characters only");
-            NameValidator groupvalidator = new RegexpNameValidator(
-                "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_GROUPNAME",
-                "Group name must consist of alphanumeric characters only");
-
-            UserAdministration admin = new UserAdministrationImpl(userset,
-                groupset, uservalidator, groupvalidator);
-            UserAdminInitializer initializer = new UserAdminInitializer(admin,
-                new String[] { "erik", "admin" }, new String[] { "users",
-                    "administrators" }, new String[] { "abc123", "abc123" });
-            return admin;
+            return new EhCache(cacheConfig, "users");
         } catch (IOException e) {
-            throw new RuntimeException(
-                "Could not initialize user administration", e);
+            throw new RuntimeException("Could not create user cache", e);
+        }
+    }
+
+    @Produces
+    @ApplicationScoped
+    @PhotoCache
+    public Cache<String, ArrayList<PhotoEntry>> getPhotoCache() {
+        try {
+            InputResource cacheConfig = new ClassPathResource(
+                "META-INF/ehcache.xml");
+            return new EhCache<String, ArrayList<PhotoEntry>>(cacheConfig,
+                "photos");
+        } catch (IOException e) {
+            throw new RuntimeException("Could not create photo cache", e);
         }
     }
 
@@ -200,10 +226,6 @@ public class Producer {
 
         try {
             File dir = new File(getCOnfiguration().getPath());
-            InputResource cacheConfig = new ClassPathResource(
-                "META-INF/ehcache.xml");
-            Cache<String, ArrayList<PhotoEntry>> photoCache = new EhCache<String, ArrayList<PhotoEntry>>(
-                cacheConfig, "photos");
             Album fileSystemAlbum = new FileSystemAlbum(dir, "/", photoCache);
             Album concurrentAlbum = new ConcurrentAlbum(fileSystemAlbum);
 
@@ -213,26 +235,6 @@ public class Producer {
         }
     }
 
-    @Produces
-    @SessionScoped
-    @AuthorizedPhotos
-    public Album getAuthorizedAlbum() {
-        LOGGER.info("Initializing authorized photos for current session");
-        try {
-            InputResource cacheConfig = new ClassPathResource(
-                "META-INF/ehcache.xml");
-            Cache<String, User> userCache = new EhCache(cacheConfig, "users");
-            Cache authorizedPhotoCache = new EhCache(cacheConfig, "photos");
-
-            AuthorizedAlbum album = new AuthorizedAlbum(allPhotos,
-                authorizationService, authorizedPhotoCache, session.getId());
-            return album;
-        } catch (IOException e) {
-            throw new RuntimeException("Problem initializing authorized album",
-                e);
-        }
-    }
-
     @Produces
     @SessionScoped
     public User getUser() {