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-05-12 12:38:56 +02:00
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Properties;
|
2013-03-30 09:22:49 +01:00
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
import javax.annotation.PostConstruct;
|
2013-04-15 11:47:17 +02:00
|
|
|
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.inject.Inject;
|
|
|
|
|
|
2013-05-12 12:38:56 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2013-03-21 16:22:58 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2013-07-24 20:05:58 +02:00
|
|
|
import com.commafeed.backend.dao.ApplicationSettingsDAO;
|
2013-06-30 12:18:24 +02:00
|
|
|
import com.commafeed.backend.feeds.FeedRefreshTaskGiver;
|
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
|
|
|
|
2013-07-26 16:00:02 +02:00
|
|
|
/**
|
|
|
|
|
* Starting point of the application
|
|
|
|
|
*
|
|
|
|
|
*/
|
2013-03-20 20:33:42 +01:00
|
|
|
@Startup
|
|
|
|
|
@Singleton
|
2013-04-15 11:47:17 +02:00
|
|
|
@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);
|
2013-04-21 13:50:10 +02:00
|
|
|
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-04-11 20:49:08 +02:00
|
|
|
@Inject
|
2013-07-24 20:05:58 +02:00
|
|
|
ApplicationSettingsDAO applicationSettingsDAO;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
UserService userService;
|
|
|
|
|
|
2013-04-05 16:31:42 +02:00
|
|
|
@Inject
|
2013-06-30 12:18:24 +02:00
|
|
|
FeedRefreshTaskGiver taskGiver;
|
2013-04-05 16:31:42 +02:00
|
|
|
|
2013-04-11 19:11:27 +02:00
|
|
|
@Inject
|
2013-06-30 12:18:24 +02:00
|
|
|
ApplicationSettingsService applicationSettingsService;
|
2013-04-13 11:41:33 +02:00
|
|
|
|
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
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
@PostConstruct
|
|
|
|
|
private void init() {
|
2013-06-09 16:19:49 +02:00
|
|
|
|
2013-06-25 16:57:48 +02:00
|
|
|
startupTime = System.currentTimeMillis();
|
2013-07-26 16:00:02 +02:00
|
|
|
|
|
|
|
|
// update database schema
|
2013-06-11 17:01:21 +02:00
|
|
|
databaseUpdater.update();
|
|
|
|
|
|
2013-07-24 20:05:58 +02:00
|
|
|
if (applicationSettingsDAO.getCount() == 0) {
|
2013-07-26 16:00:02 +02:00
|
|
|
// import initial data
|
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-07-26 16:00:02 +02:00
|
|
|
|
|
|
|
|
// start fetching feeds
|
2013-06-30 12:18:24 +02:00
|
|
|
taskGiver.start();
|
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()) {
|
2013-07-25 09:17:33 +02:00
|
|
|
supportedLanguages.put(key.toString(), props.getProperty(key.toString()));
|
2013-05-12 12:38:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 16:00:02 +02:00
|
|
|
/**
|
|
|
|
|
* create default users
|
|
|
|
|
*/
|
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
|
|
|
|
2013-05-01 18:09:34 +02:00
|
|
|
ApplicationSettings settings = new ApplicationSettings();
|
2013-07-03 02:17:57 +04:30
|
|
|
settings.setAnnouncement("Set the Public URL in the admin section!");
|
2013-05-01 18:09:34 +02:00
|
|
|
applicationSettingsService.save(settings);
|
2013-05-12 12:38:56 +02:00
|
|
|
|
2013-06-18 12:31:09 +02:00
|
|
|
try {
|
2013-07-25 09:17:33 +02:00
|
|
|
userService.register(USERNAME_ADMIN, "admin", "admin@commafeed.com", Arrays.asList(Role.ADMIN, Role.USER), true);
|
|
|
|
|
userService.register(USERNAME_DEMO, "demo", "demo@commafeed.com", Arrays.asList(Role.USER), true);
|
2013-06-18 12:31:09 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|