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 org.apache.log4j.Logger;
22  import org.itracker.core.resources.ITrackerResources;
23  import org.itracker.model.User;
24  import org.itracker.persistence.dao.NoSuchEntityException;
25  import org.itracker.services.ConfigurationService;
26  import org.itracker.services.ReportService;
27  import org.itracker.services.UserService;
28  import org.itracker.services.exceptions.PasswordException;
29  import org.itracker.services.exceptions.UserException;
30  import org.itracker.services.util.SystemConfigurationUtilities;
31  import org.itracker.services.util.UserUtilities;
32  
33  
34  /**
35   * TODO: Add Javadocs here: please comment this for documentation reasons. What is this Class used for?
36   * 
37   * It seems like this gets started when the application starts up...
38   *
39   * What's the general idea?
40   * 
41   * Why is processAttachmentFiles commented and therefore not used currently?
42   * Where does itracker store its attachments?  
43   * What's the idea behind the attachment_dir ? 
44   * 
45   * @author ready
46   * 
47   */
48  
49  public class ApplicationInitialization {
50      
51      private final Logger logger;
52      private UserService userService;    
53      private ConfigurationService configurationService;
54      
55      public ApplicationInitialization(UserService userService, ConfigurationService configurationService, ReportService reportService) {
56          this.userService = userService;
57          this.configurationService = configurationService;
58          this.logger = Logger.getLogger(getClass());
59          init();
60      }
61      
62      public void init() {
63          try {
64              ITrackerResources.setDefaultLocale(configurationService.getProperty("default_locale", ITrackerResources.DEFAULT_LOCALE));
65              logger.info("Set system default locale to '" + ITrackerResources.getDefaultLocale() + "'");
66              
67              logger.info("Checking and initializing languages in the database.");
68              SystemConfigurationUtilities.initializeAllLanguages(configurationService, false);
69              
70              logger.info("Checking and initializing default system configuration in the database.");
71              configurationService.initializeConfiguration();
72              
73  //            logger.info("Checking for issue attachment files.");
74  //            processAttachmentFiles(configurationService.getProperty("attachment_dir", IssueAttachmentUtilities.DEFAULT_ATTACHMENT_DIR));
75                         
76              logger.info("Setting up cached configuration entries");
77              configurationService.resetConfigurationCache();
78              
79              // Preinitialize all of the PDF fonts available. Do it in a
80              // separate thread to speed up the rest of the startup.
81              // TODO: I think this should be removed... why do we need to pre-init ? (rjst)
82              // old code to pre-init fonts for jfree reports. make sure we can delete it
83              // BaseFontFactory fontFactory = BaseFontFactory.getFontFactory();
84              // fontFactory.registerDefaultFontPath();
85              
86              // check for and create admin user, if so configured
87              createAdminUser(configurationService);
88          } catch (PasswordException pe) {
89              logger.info("Unable to create admin user.  Error: " + pe.getMessage());
90          } catch (UserException ue) {
91              logger.warn("Exception while creating admin user.", ue);
92          }
93      }
94      
95      /**
96       * Check if we should create the admin user, if so, do it.
97       *
98       * @param configurationService
99       * @throws PasswordException
100      * @throws UserException
101      */
102     private void createAdminUser(ConfigurationService configurationService) throws PasswordException, UserException {
103         boolean createAdmin = configurationService.getBooleanProperty("create_super_user", false);
104         if (createAdmin) {
105             logger.info("Create default admin user option set to true.  Checking for existing admin user.");
106             try {
107                 userService.getUserByLogin("admin");
108             } catch (NoSuchEntityException e) {
109                 logger.debug("Attempting to create admin user.");
110                 User adminUser = new User("admin", UserUtilities.encryptPassword("admin"), "Super", "User",
111                         "", true);
112                 userService.createUser(adminUser);
113             }
114         }
115     }
116     
117 }