KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > util > AttachmentUtilities


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.util;
20
21 import java.io.*;
22 import java.net.*;
23 import java.rmi.*;
24 import java.util.*;
25 import javax.naming.*;
26 import javax.rmi.*;
27 import javax.ejb.*;
28
29 import org.apache.struts.upload.*;
30
31 import cowsultants.itracker.ejb.client.interfaces.*;
32 import cowsultants.itracker.ejb.client.util.*;
33
34 public class AttachmentUtilities {
35     private static boolean initialized = false;
36     private static final String JavaDoc CONTENT_TYPE = "multipart/form-data";
37     private static final String JavaDoc CHAR_ENCODING = "ISO-8859-1";
38     private static final long MAX_FILE_SIZE_KB = 256L;
39     private static final long MAX_TOTAL_FILE_SIZE_KB = 1000000L;
40
41     private static long maxFileSize = MAX_FILE_SIZE_KB * 1024L;
42     private static long maxTotalFileSize = MAX_TOTAL_FILE_SIZE_KB * 1024L;
43     private static long spaceLeft = 0;
44
45     public static boolean checkFile(FormFile file) {
46         if(! initialized) {
47            if(! init()) {
48               return false;
49            }
50         }
51
52         if(file == null) {
53             return false;
54         }
55
56         long origFileSize = file.getFileSize();
57         if(origFileSize > maxFileSize) {
58             Logger.logInfo("Cannot save attachment. File is " + (origFileSize / 1024L) + " kB and max file size is set to " + (maxFileSize / 1024L) + "kB.");
59             return false;
60         }
61
62         if((spaceLeft - origFileSize) < 0) {
63             Logger.logInfo("Cannot save attachment. Total allocated disk space already used.");
64             return false;
65         }
66         spaceLeft = spaceLeft - origFileSize;
67
68 // filename = attachmentDirName + File.separator + filename;
69

70 // if(Logger.isLoggingDebug()) {
71
// Logger.logDebug("Attempting to save attachment: " + filename);
72
// }
73

74 // FileOutputStream out = new FileOutputStream(filename);
75
// out.write(file.getFileData());
76
// out.close();
77
return true;
78     }
79
80     private static boolean init() {
81         if(! initialized) {
82             try {
83                 InitialContext ic = new InitialContext();
84                 Object JavaDoc scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME);
85                 SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class);
86                 SystemConfiguration sc = scHome.create();
87
88                 Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
89                 IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
90                 IssueHandler ih = ihHome.create();
91
92                 maxFileSize = sc.getLongProperty("max_attachment_size", MAX_FILE_SIZE_KB) * 1024L;
93                 maxTotalFileSize = sc.getLongProperty("max_total_attachment_size", MAX_TOTAL_FILE_SIZE_KB) * 1024L;
94                 spaceLeft = maxTotalFileSize - ih.getAllIssueAttachmentsSizeAndCount()[0];
95
96                 if(Logger.isLoggingDebug()) {
97                     Logger.logDebug("Attachment Properties: MaxAttachmentSize set to " + (maxFileSize / 1024L) + " kB");
98                     Logger.logDebug("Attachment Properties: MaxTotalAttachmentsSize set to " + (maxTotalFileSize / 1024L) + " kB");
99                     Logger.logDebug("Attachment Properties: Current space left is " + (spaceLeft / 1024L) + " kB");
100                 }
101                 initialized = true;
102             } catch(Exception JavaDoc e) {
103                 Logger.logError("Exception initializing AttachmentUtilities.", e);
104             }
105         }
106         return initialized;
107     }
108
109 }
110
Popular Tags