Files
Athou_commafeed/src/main/java/com/commafeed/backend/StartupBean.java

157 lines
4.2 KiB
Java
Raw Normal View History

2013-03-20 20:33:42 +01:00
package com.commafeed.backend;
2013-05-12 12:38:56 +02:00
import java.io.InputStream;
import java.io.InputStreamReader;
2013-03-30 09:22:49 +01:00
import java.util.Arrays;
2013-04-03 16:24:11 +02:00
import java.util.Calendar;
2013-05-12 12:38:56 +02:00
import java.util.Map;
import java.util.Properties;
2013-04-23 21:29:16 +02:00
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
2013-03-30 09:22:49 +01:00
2013-03-20 20:33:42 +01:00
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
2013-03-20 20:33:42 +01:00
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.enterprise.inject.Instance;
2013-03-20 20:33:42 +01:00
import javax.inject.Inject;
2013-05-12 12:38:56 +02:00
import org.apache.commons.io.IOUtils;
2013-04-16 09:29:33 +02:00
import org.apache.commons.lang.mutable.MutableBoolean;
2013-03-21 16:22:58 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2013-04-11 20:49:08 +02:00
import com.commafeed.backend.dao.FeedCategoryDAO;
import com.commafeed.backend.dao.FeedDAO;
import com.commafeed.backend.dao.FeedSubscriptionDAO;
import com.commafeed.backend.dao.UserDAO;
2013-04-11 19:11:27 +02:00
import com.commafeed.backend.feeds.FeedRefreshWorker;
2013-04-05 16:31:42 +02:00
import com.commafeed.backend.model.ApplicationSettings;
2013-03-30 19:06:32 +01:00
import com.commafeed.backend.model.UserRole.Role;
2013-04-12 13:27:30 +02:00
import com.commafeed.backend.services.ApplicationSettingsService;
2013-04-11 20:49:08 +02:00
import com.commafeed.backend.services.UserService;
2013-05-12 12:38:56 +02:00
import com.google.api.client.util.Maps;
2013-03-20 20:33:42 +01:00
@Startup
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
2013-03-20 20:33:42 +01:00
public class StartupBean {
2013-03-21 16:22:58 +01:00
private static Logger log = LoggerFactory.getLogger(StartupBean.class);
public static final String USERNAME_ADMIN = "admin";
public static final String USERNAME_DEMO = "demo";
2013-03-21 16:22:58 +01:00
2013-06-11 17:01:21 +02:00
@Inject
DatabaseUpdater databaseUpdater;
2013-03-20 20:33:42 +01:00
@Inject
2013-04-11 20:49:08 +02:00
FeedDAO feedDAO;
2013-03-20 20:33:42 +01:00
@Inject
2013-04-11 20:49:08 +02:00
FeedCategoryDAO feedCategoryDAO;
2013-03-20 20:33:42 +01:00
@Inject
2013-04-11 20:49:08 +02:00
FeedSubscriptionDAO feedSubscriptionDAO;
@Inject
UserDAO userDAO;
2013-03-20 20:33:42 +01:00
@Inject
UserService userService;
2013-04-05 16:31:42 +02:00
@Inject
2013-04-12 13:27:30 +02:00
ApplicationSettingsService applicationSettingsService;
2013-04-05 16:31:42 +02:00
2013-04-11 19:11:27 +02:00
@Inject
Instance<FeedRefreshWorker> workers;
2013-04-03 16:24:11 +02:00
private long startupTime;
2013-05-12 12:38:56 +02:00
private Map<String, String> supportedLanguages = Maps.newHashMap();
2013-04-03 16:24:11 +02:00
private ExecutorService executor;
2013-04-16 09:29:33 +02:00
private MutableBoolean running = new MutableBoolean(true);
2013-03-20 20:33:42 +01:00
@PostConstruct
private void init() {
2013-06-09 16:19:49 +02:00
2013-04-03 16:24:11 +02:00
startupTime = Calendar.getInstance().getTimeInMillis();
2013-06-11 17:01:21 +02:00
databaseUpdater.update();
2013-04-11 20:49:08 +02:00
if (userDAO.getCount() == 0) {
2013-04-11 19:11:27 +02:00
initialData();
}
2013-06-09 16:19:49 +02:00
applicationSettingsService.applyLogLevel();
2013-03-22 11:42:25 +01:00
2013-05-12 12:38:56 +02:00
initSupportedLanguages();
2013-04-12 13:27:30 +02:00
ApplicationSettings settings = applicationSettingsService.get();
int threads = settings.getBackgroundThreads();
log.info("Starting {} background threads", threads);
2013-05-15 15:56:06 +02:00
executor = Executors.newFixedThreadPool(Math.max(threads, 1));
for (int i = 0; i < threads; i++) {
final int threadId = i;
executor.execute(new Runnable() {
@Override
public void run() {
FeedRefreshWorker worker = workers.get();
worker.start(running, "Thread " + threadId);
}
});
2013-03-20 20:33:42 +01:00
}
}
2013-05-12 12:38:56 +02:00
private void initSupportedLanguages() {
Properties props = new Properties();
InputStream is = null;
try {
is = getClass().getResourceAsStream("/i18n/languages.properties");
props.load(new InputStreamReader(is, "UTF-8"));
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(is);
}
for (Object key : props.keySet()) {
supportedLanguages.put(key.toString(),
props.getProperty(key.toString()));
}
}
2013-04-11 19:11:27 +02:00
private void initialData() {
log.info("Populating database with default values");
2013-05-12 12:38:56 +02:00
ApplicationSettings settings = new ApplicationSettings();
settings.setAnnouncement("Set the Public URL in the admin section !");
applicationSettingsService.save(settings);
2013-05-12 12:38:56 +02:00
userService.register(USERNAME_ADMIN, "admin",
2013-04-11 19:11:27 +02:00
Arrays.asList(Role.ADMIN, Role.USER));
userService.register(USERNAME_DEMO, "demo", Arrays.asList(Role.USER));
2013-04-11 19:11:27 +02:00
}
2013-04-03 16:24:11 +02:00
public long getStartupTime() {
return startupTime;
}
2013-05-12 12:38:56 +02:00
public Map<String, String> getSupportedLanguages() {
return supportedLanguages;
}
@PreDestroy
2013-04-16 09:29:33 +02:00
public void shutdown() {
running.setValue(false);
executor.shutdownNow();
while (!executor.isTerminated()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
log.error("interrupted while waiting for threads to finish.");
}
}
}
2013-03-20 20:33:42 +01:00
}