From f3e05888d09a2d39375639b056d8e9093e00668b Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Sun, 15 Sep 2013 22:34:10 +0200 Subject: [PATCH] after a lot of work initialization at startup is now working. --- .../photos/model/plumbing/Initializer.java | 45 ++++++++++++++----- .../photos/model/plumbing/PhotoCache.java | 31 +++++++++++++ .../photos/model/plumbing/UserCache.java | 31 +++++++++++++ 3 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 src/main/java/org/wamblee/photos/model/plumbing/PhotoCache.java create mode 100644 src/main/java/org/wamblee/photos/model/plumbing/UserCache.java diff --git a/src/main/java/org/wamblee/photos/model/plumbing/Initializer.java b/src/main/java/org/wamblee/photos/model/plumbing/Initializer.java index 2509641..b5ace14 100644 --- a/src/main/java/org/wamblee/photos/model/plumbing/Initializer.java +++ b/src/main/java/org/wamblee/photos/model/plumbing/Initializer.java @@ -15,11 +15,14 @@ */ package org.wamblee.photos.model.plumbing; +import java.util.logging.Logger; + import javax.annotation.PostConstruct; import javax.ejb.Singleton; import javax.ejb.Startup; import javax.inject.Inject; +import org.wamblee.inject.InjectorBuilder; import org.wamblee.photos.model.Album; import org.wamblee.security.authentication.UserAdministration; @@ -31,18 +34,40 @@ import org.wamblee.security.authentication.UserAdministration; @Startup public class Initializer { - @Inject - private UserAdministration userAdmin; + private static final Logger LOGGER = Logger.getLogger(Initializer.class + .getName()); - @Inject - @AllPhotos - private Album album; + /** + * We need this helper to do initialization of the beans from within the + * post construct method. Injecting these objects into the initializer class + * will lead to initialization outside of a transactional context and this + * is does not work because and entity manager is required. + * + * @author Erik Brakkee + * + */ + public static class Helper { + @Inject + private UserAdministration userAdmin; - @PostConstruct - public void init() { - System.out.println("Photo application initializing"); + @Inject + @AllPhotos + private Album album; - userAdmin.getUserCount(); - album.size(); + public void init() { + userAdmin.getUserCount(); + album.size(); + } + } + + @PostConstruct + public void scheduleInit() { + LOGGER.info("Photo application initializing"); + // timerService.createTimer(1000, 1000, null); + LOGGER.info("Initializing photo application"); + Helper helper = new Helper(); + InjectorBuilder.getInjector().inject(helper); + helper.init(); + LOGGER.info("Initialized photo application"); } } diff --git a/src/main/java/org/wamblee/photos/model/plumbing/PhotoCache.java b/src/main/java/org/wamblee/photos/model/plumbing/PhotoCache.java new file mode 100644 index 0000000..7840562 --- /dev/null +++ b/src/main/java/org/wamblee/photos/model/plumbing/PhotoCache.java @@ -0,0 +1,31 @@ +/* + * Copyright 2005-2013 the original author or authors. + * + * Licensed 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. + */ +package org.wamblee.photos.model.plumbing; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.*; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +@Qualifier +@Retention(RUNTIME) +@Target({ METHOD, PARAMETER, FIELD }) +public @interface PhotoCache { + // Empty. +} diff --git a/src/main/java/org/wamblee/photos/model/plumbing/UserCache.java b/src/main/java/org/wamblee/photos/model/plumbing/UserCache.java new file mode 100644 index 0000000..c4ea7ea --- /dev/null +++ b/src/main/java/org/wamblee/photos/model/plumbing/UserCache.java @@ -0,0 +1,31 @@ +/* + * Copyright 2005-2013 the original author or authors. + * + * Licensed 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. + */ +package org.wamblee.photos.model.plumbing; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.*; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +@Qualifier +@Retention(RUNTIME) +@Target({ METHOD, PARAMETER, FIELD }) +public @interface UserCache { + // Empty. +} -- 2.31.1