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.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   * This gets started when the Spring application-context starts up...
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              // check for and create admin user, if so configured
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       * Check if we should create the admin user, if so, do it.
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 }