KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > servlets > ApplicationInitialization


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 cowsultants.itracker.web.servlets;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.util.*;
24 import javax.ejb.*;
25 import javax.rmi.*;
26 import javax.naming.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import org.jfree.report.modules.output.support.itext.BaseFontFactory;
31
32 import cowsultants.itracker.ejb.client.exceptions.*;
33 import cowsultants.itracker.ejb.client.interfaces.*;
34 import cowsultants.itracker.ejb.client.models.*;
35 import cowsultants.itracker.ejb.client.resources.*;
36 import cowsultants.itracker.ejb.client.util.*;
37
38 import cowsultants.itracker.web.util.*;
39
40 public class ApplicationInitialization extends GenericController {
41     public ApplicationInitialization() {
42     }
43
44     public void init(ServletConfig config) {
45         try {
46             InitialContext ic = new InitialContext();
47
48             Object JavaDoc scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME);
49             SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class);
50             SystemConfiguration sc = scHome.create();
51
52             ITrackerResources.setDefaultLocale(sc.getProperty("default_locale", ITrackerResources.DEFAULT_LOCALE));
53             Logger.logInfo("Set system default locale to '" + ITrackerResources.getDefaultLocale() + "'");
54             ITrackerResources.setConfigHome(scHome);
55
56             Logger.logInfo("Checking and initializing languages in the database.");
57             SystemConfigurationUtilities.initializeAllLanguages(sc, false);
58
59             Logger.logInfo("Checking and initializing default system configuration in the database.");
60             sc.initializeConfiguration();
61
62             Logger.logInfo("Checking for issue attachment files.");
63             processAttachmentFiles(sc.getProperty("attachment_dir", IssueAttachmentUtilities.DEFAULT_ATTACHMENT_DIR));
64
65             Logger.logInfo("Checking for predefined reports.");
66             processReports();
67
68             Logger.logInfo("Setting up cached configuration entries");
69             sc.resetConfigurationCache();
70
71             // Pre-initialize all of the PDF fonts available. Do it in a seperate thread to speed up the
72
// rest of the startup.
73
Thread JavaDoc initFontThread = new Thread JavaDoc() {
74                 public void run() {
75                     BaseFontFactory fontFactory = BaseFontFactory.getFontFactory();
76                     fontFactory.registerDefaultFontPath();
77                 }
78             };
79             initFontThread.start();
80
81             boolean createAdmin = sc.getBooleanProperty("create_super_user", false);
82             if(createAdmin) {
83                 Logger.logInfo("Create default admin user option set to true. Checking for existing users.");
84                 Object JavaDoc uhRef = ic.lookup("java:comp/env/" + UserHandler.JNDI_NAME);
85                 UserHandlerHome uhHome = (UserHandlerHome) PortableRemoteObject.narrow(uhRef, UserHandlerHome.class);
86                 UserHandler uh = uhHome.create();
87
88                 UserModel[] users = uh.getAllUsers();
89                 if(users.length == 0) {
90                     Logger.logDebug("Attempting to create admin user.");
91                     try {
92                         UserModel adminUser = new UserModel("admin", UserUtilities.encryptPassword("admin"), "Super", "User", "", true);
93                         uh.createUser(adminUser);
94                     } catch(PasswordException pe) {
95                         Logger.logInfo("Unable to create admin user. Error: " + pe.getMessage());
96                     }
97                 }
98             }
99         } catch(NamingException ne) {
100             Logger.logError("Could not locate session EJB.", ne);
101         } catch(CreateException ce) {
102             Logger.logError("Could not create session EJB.", ce);
103         } catch(UserException ue) {
104             Logger.logWarn("Exception while creating admin user.", ue);
105         }
106     }
107
108     public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
109     }
110
111     private void processAttachmentFiles(String JavaDoc attachmentDirectory) {
112         if(attachmentDirectory == null || attachmentDirectory.equals("")) {
113             return;
114         }
115
116         try {
117             File directory = new File(attachmentDirectory.replace('/', File.separatorChar));
118             if(directory == null || ! directory.isDirectory()) {
119                 throw new Exception JavaDoc("Invalid attachment directory.");
120             }
121             File[] attachments = directory.listFiles();
122
123             InitialContext ic = new InitialContext();
124             Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
125             IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
126             IssueHandler ih = ihHome.create();
127
128             for(int i = 0; i < attachments.length; i++) {
129                 try {
130                     if(attachments[i].length() > Integer.MAX_VALUE) {
131                         throw new IOException("File too large to load.");
132                     }
133                     byte[] data = new byte[(int) attachments[i].length()];
134                     FileInputStream fis = new FileInputStream(attachments[i]);
135                     int bytesRead = fis.read(data);
136                     fis.close();
137                     if(bytesRead != data.length) {
138                         throw new IOException("The number of bytes read from the file, did not match the size of the file.");
139                     }
140                     if(ih.setIssueAttachmentData(attachments[i].getName(), data)) {
141                         //attachments[i].delete();
142
Logger.logDebug("Successfully moved attachment " + attachments[i].getName() + " to the database.");
143                     }
144                 } catch(IOException ioe) {
145                     Logger.logError("Unable to save attachment: " + ioe.getMessage());
146                 }
147             }
148         } catch(Exception JavaDoc e) {
149             Logger.logError("Unable to check for existing file attachments: " + e.getMessage());
150         }
151     }
152
153     private void processReports() {
154         try {
155             InitialContext ic = new InitialContext();
156             Object JavaDoc rhRef = ic.lookup("java:comp/env/" + ReportHandler.JNDI_NAME);
157             ReportHandlerHome rhHome = (ReportHandlerHome) PortableRemoteObject.narrow(rhRef, ReportHandlerHome.class);
158             ReportHandler rh = rhHome.create();
159             ReportModel[] reports = rh.getAllReports();
160
161             String JavaDoc line;
162             InputStream is = getClass().getResourceAsStream("/cowsultants/itracker/web/reports/predefined/predefinedReports.properties");
163             BufferedReader br = new BufferedReader(new InputStreamReader(is));
164             LINE: while((line = br.readLine()) != null) {
165                 if(line.equals("") || line.startsWith("#")) {
166                     continue;
167                 }
168                 int splitChar = line.indexOf('=');
169                 if(splitChar < 0) {
170                     continue;
171                 }
172
173                 String JavaDoc key = line.substring(0, splitChar);
174                 String JavaDoc value = line.substring(splitChar + 1);
175
176                 if("report".equalsIgnoreCase(key)) {
177                     try {
178                         String JavaDoc repLine;
179                         ReportModel report = new ReportModel();
180
181                         InputStream reportStream = getClass().getResourceAsStream("/cowsultants/itracker/web/reports/predefined/" + value);
182                         if(reportStream == null) {
183                             throw new IOException("Could not access predefined report '" + value + "'");
184                         }
185                         BufferedReader rbr = new BufferedReader(new InputStreamReader(reportStream));
186                         while((repLine = rbr.readLine()) != null) {
187                             if(repLine.equals("") || line.startsWith("#")) {
188                                 continue;
189                             }
190                             int repSplitChar = repLine.indexOf('=');
191                             if(repSplitChar < 0) {
192                                 continue;
193                             }
194                             String JavaDoc repKey = repLine.substring(0, repSplitChar);
195                             String JavaDoc repValue = (repLine.length() > repSplitChar ? repLine.substring(repSplitChar + 1) : "");
196                             if(repValue == null || "".equals(repValue)) {
197                                 continue;
198                             } else if("id".equals(repKey)) {
199                                 report.setId(new Integer JavaDoc(repValue));
200                             } else if("name".equals(repKey)) {
201                                 report.setName(repValue);
202                             } else if("namekey".equals(repKey)) {
203                                 report.setNameKey(repValue);
204                             } else if("dataType".equals(repKey)) {
205                                 report.setDataType(Integer.parseInt(repValue));
206                             } else if("reportType".equals(repKey)) {
207                                 report.setReportType(Integer.parseInt(repValue));
208                             } else if("className".equals(repKey)) {
209                                 report.setClassName(repValue);
210                             } else if("description".equals(repKey)) {
211                                 report.setDescription((String JavaDoc) Base64.decodeToObject(repValue));
212                             } else if("definition".equals(repKey)) {
213                                 report.setFileData(Base64.decode(repValue));
214                             }
215                         }
216                         if("".equals(report.getName()) || report.getDataType() == 0 || (report.getClassName() == null && report.getFileData().length == 0)) {
217                             throw new Exception JavaDoc("Invalid report definition found.");
218                         }
219                         Logger.logDebug("Loading " + report.toString());
220                         for(int i = 0; i < reports.length; i++) {
221                             if(reports[i].new CompareByDefinition().equals(report)) {
222                                 Logger.logDebug("Found existing report, updating.");
223                                 report.setId(reports[i].getId());
224                                 rh.updateReport(report);
225                                 continue LINE;
226                             }
227                         }
228                         Logger.logDebug("No existing report found, creating new report.");
229                         rh.createReport(report);
230                     } catch(Exception JavaDoc e) {
231                         Logger.logError("Unable to process report '" + value + "': " + e.getMessage());
232                         Logger.logDebug("Stacktrace:", e);
233                     }
234                 }
235             }
236         } catch(Exception JavaDoc e) {
237             Logger.logError("Unable to process predefined reports.", e);
238         }
239     }
240 }
241   
Popular Tags