1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.web;
20
21 import net.sf.jasperreports.engine.DefaultJasperReportsContext;
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.log4j.Logger;
24 import org.itracker.core.resources.ITrackerResources;
25 import org.itracker.model.User;
26 import org.itracker.UserException;
27 import org.itracker.persistence.dao.NoSuchEntityException;
28 import org.itracker.services.ConfigurationService;
29 import org.itracker.services.ReportService;
30 import org.itracker.services.UserService;
31 import org.itracker.PasswordException;
32 import org.itracker.model.util.UserUtilities;
33 import org.springframework.web.context.ServletConfigAware;
34
35 import javax.servlet.ServletConfig;
36 import java.util.StringTokenizer;
37
38
39
40
41
42
43
44 public class ApplicationInitialization implements ServletConfigAware {
45
46 private final Logger logger;
47 private UserService userService;
48 private ConfigurationService configurationService;
49 private ServletConfig servletConfig;
50
51 public ApplicationInitialization(UserService userService, ConfigurationService configurationService, ReportService reportService) {
52 this.userService = userService;
53 this.configurationService = configurationService;
54 this.logger = Logger.getLogger(getClass());
55 }
56
57 public void init() {
58 try {
59 ITrackerResources.setConfigurationService(configurationService);
60
61 logger.info("Checking and initializing languages in the database.");
62
63 configurationService.initializeAllLanguages(false);
64
65 logger.info("Checking and initializing default system configuration in the database.");
66 configurationService.initializeConfiguration();
67
68 logger.info("Setting up cached configuration entries");
69 configurationService.resetConfigurationCache();
70
71
72 createAdminUser(configurationService);
73
74 setUpPdfFonts();
75 } catch (PasswordException pe) {
76 logger.info("Unable to create admin user. Error: " + pe.getMessage());
77 } catch (UserException ue) {
78 logger.warn("Exception while creating admin user.", ue);
79 }
80 }
81
82 private void setUpPdfFonts() {
83
84 StringTokenizer st = new StringTokenizer(configurationService.getProperty("pdf.export.fonts", "arial"), ",");
85 String font;
86 while (st.hasMoreTokens()) {
87 font = StringUtils.trim(st.nextToken());
88 DefaultJasperReportsContext.getInstance().setProperty("net.sf.jasperreports.export.pdf.font." + font,
89 this.getClass().getResource("/fonts/" + font + ".ttf").toString());
90
91 }
92 }
93
94
95
96
97
98
99 private void createAdminUser(ConfigurationService configurationService) throws PasswordException, UserException {
100 boolean createAdmin = configurationService.getBooleanProperty("create_super_user", false);
101 if (createAdmin) {
102 logger.info("Create default admin user option set to true. Checking for existing admin user.");
103 try {
104 userService.getUserByLogin("admin");
105 } catch (NoSuchEntityException e) {
106 logger.debug("Attempting to create admin user.");
107 User adminUser = new User("admin", UserUtilities.encryptPassword("admin"), "Super", "User",
108 "", true);
109 userService.createUser(adminUser);
110 }
111 }
112 }
113
114 public void setServletConfig(ServletConfig servletConfig) {
115 this.servletConfig = servletConfig;
116 }
117 }