View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
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   * This gets started when the Spring application-context starts up...
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              // check for and create admin user, if so configured
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       * This method will attempt to load all of the locales defined in the
99       * ITracker.properties file, and add them to the database if they don't
100      * already exist.
101      *
102      * @param configurationService a configurationService object to use when processing the
103      *                             locales
104      * @param forceReload          if true, it will reload the languages from the property files
105      *                             even if they are listed as being up to date
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      * Check if we should create the admin user, if so, do it.
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 }