1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.core;
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.services.exceptions.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.HashSet;
37 import java.util.StringTokenizer;
38
39
40
41
42
43
44
45 public class ApplicationInitialization implements ServletConfigAware {
46
47 private final Logger logger;
48 private UserService userService;
49 private ConfigurationService configurationService;
50 private ServletConfig servletConfig;
51
52 public ApplicationInitialization(UserService userService, ConfigurationService configurationService, ReportService reportService) {
53 this.userService = userService;
54 this.configurationService = configurationService;
55 this.logger = Logger.getLogger(getClass());
56 }
57
58 public void init() {
59 try {
60 ITrackerResources.setConfigurationService(configurationService);
61
62 logger.info("Checking and initializing languages in the database.");
63
64 initializeAllLanguages(configurationService, false);
65
66 logger.info("Checking and initializing default system configuration in the database.");
67 configurationService.initializeConfiguration();
68
69 logger.info("Setting up cached configuration entries");
70 configurationService.resetConfigurationCache();
71
72
73 createAdminUser(configurationService);
74
75 setUpPdfFonts();
76 } catch (PasswordException pe) {
77 logger.info("Unable to create admin user. Error: " + pe.getMessage());
78 } catch (UserException ue) {
79 logger.warn("Exception while creating admin user.", ue);
80 }
81 }
82
83 private void setUpPdfFonts() {
84
85 StringTokenizer st = new StringTokenizer(configurationService.getProperty("pdf.export.fonts", "arial"), ",");
86 String font;
87 while (st.hasMoreTokens()) {
88 font = StringUtils.trim(st.nextToken());
89 DefaultJasperReportsContext.getInstance().setProperty("net.sf.jasperreports.export.pdf.font." + font,
90 this.getClass().getResource("/fonts/" + font + ".ttf").toString());
91
92 }
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107 public static void initializeAllLanguages(
108 ConfigurationService configurationService, boolean forceReload) {
109 HashSet<String> definedLocales = new HashSet<String>();
110
111 configurationService.initializeLocale(ITrackerResources.BASE_LOCALE,
112 forceReload);
113
114 String definedLocalesString;
115 try {
116 definedLocalesString = configurationService
117 .getLanguageItemByKey(ITrackerResources.DEFINED_LOCALES_KEY,
118 null).getResourceValue();
119 } catch (RuntimeException e) {
120 definedLocalesString = ITrackerResources.getString(ITrackerResources.DEFINED_LOCALES_KEY);
121 }
122 if (definedLocalesString != null) {
123 StringTokenizer token = new StringTokenizer(definedLocalesString, ",");
124 while (token.hasMoreTokens()) {
125 String locale = token.nextToken();
126 if (locale.length() == 5 && locale.indexOf('_') == 2) {
127 definedLocales.add(locale.substring(0, 2));
128 }
129 definedLocales.add(locale);
130 }
131 }
132
133 for (String locale: definedLocales ) {
134 configurationService.initializeLocale(locale, forceReload);
135 }
136 }
137
138
139
140 private void createAdminUser(ConfigurationService configurationService) throws PasswordException, UserException {
141 boolean createAdmin = configurationService.getBooleanProperty("create_super_user", false);
142 if (createAdmin) {
143 logger.info("Create default admin user option set to true. Checking for existing admin user.");
144 try {
145 userService.getUserByLogin("admin");
146 } catch (NoSuchEntityException e) {
147 logger.debug("Attempting to create admin user.");
148 User adminUser = new User("admin", UserUtilities.encryptPassword("admin"), "Super", "User",
149 "", true);
150 userService.createUser(adminUser);
151 }
152 }
153 }
154
155 public void setServletConfig(ServletConfig servletConfig) {
156 this.servletConfig = servletConfig;
157 }
158 }