KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > ssi > SSIFsize


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation. Licensed under the
3  * Apache License, Version 2.0 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of the License
5  * at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
6  * law or agreed to in writing, software distributed under the License is
7  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the specific language
9  * governing permissions and limitations under the License.
10  */

11 package org.apache.catalina.ssi;
12
13
14 import java.io.IOException JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.text.DecimalFormat JavaDoc;
17 /**
18  * Implements the Server-side #fsize command
19  *
20  * @author Bip Thelin
21  * @author Paul Speed
22  * @author Dan Sandberg
23  * @author David Becker
24  * @version $Revision: 467222 $, $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
25  */

26 public final class SSIFsize implements SSICommand {
27     protected final static int ONE_KILOBYTE = 1024;
28     protected final static int ONE_MEGABYTE = 1024 * 1024;
29
30
31     /**
32      * @see SSICommand
33      */

34     public long process(SSIMediator ssiMediator, String JavaDoc commandName,
35             String JavaDoc[] paramNames, String JavaDoc[] paramValues, PrintWriter JavaDoc writer) {
36         long lastModified = 0;
37         String JavaDoc configErrMsg = ssiMediator.getConfigErrMsg();
38         for (int i = 0; i < paramNames.length; i++) {
39             String JavaDoc paramName = paramNames[i];
40             String JavaDoc paramValue = paramValues[i];
41             String JavaDoc substitutedValue = ssiMediator
42                     .substituteVariables(paramValue);
43             try {
44                 if (paramName.equalsIgnoreCase("file")
45                         || paramName.equalsIgnoreCase("virtual")) {
46                     boolean virtual = paramName.equalsIgnoreCase("virtual");
47                     lastModified = ssiMediator.getFileLastModified(
48                             substitutedValue, virtual);
49                     long size = ssiMediator.getFileSize(substitutedValue,
50                             virtual);
51                     String JavaDoc configSizeFmt = ssiMediator.getConfigSizeFmt();
52                     writer.write(formatSize(size, configSizeFmt));
53                 } else {
54                     ssiMediator.log("#fsize--Invalid attribute: " + paramName);
55                     writer.write(configErrMsg);
56                 }
57             } catch (IOException JavaDoc e) {
58                 ssiMediator.log("#fsize--Couldn't get size for file: "
59                         + substitutedValue, e);
60                 writer.write(configErrMsg);
61             }
62         }
63         return lastModified;
64     }
65
66
67     public String JavaDoc repeat(char aChar, int numChars) {
68         if (numChars < 0) {
69             throw new IllegalArgumentException JavaDoc("Num chars can't be negative");
70         }
71         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
72         for (int i = 0; i < numChars; i++) {
73             buf.append(aChar);
74         }
75         return buf.toString();
76     }
77
78
79     public String JavaDoc padLeft(String JavaDoc str, int maxChars) {
80         String JavaDoc result = str;
81         int charsToAdd = maxChars - str.length();
82         if (charsToAdd > 0) {
83             result = repeat(' ', charsToAdd) + str;
84         }
85         return result;
86     }
87
88
89     //We try to mimick Apache here, as we do everywhere
90
//All the 'magic' numbers are from the util_script.c Apache source file.
91
protected String JavaDoc formatSize(long size, String JavaDoc format) {
92         String JavaDoc retString = "";
93         if (format.equalsIgnoreCase("bytes")) {
94             DecimalFormat JavaDoc decimalFormat = new DecimalFormat JavaDoc("#,##0");
95             retString = decimalFormat.format(size);
96         } else {
97             if (size == 0) {
98                 retString = "0k";
99             } else if (size < ONE_KILOBYTE) {
100                 retString = "1k";
101             } else if (size < ONE_MEGABYTE) {
102                 retString = Long.toString((size + 512) / ONE_KILOBYTE);
103                 retString += "k";
104             } else if (size < 99 * ONE_MEGABYTE) {
105                 DecimalFormat JavaDoc decimalFormat = new DecimalFormat JavaDoc("0.0M");
106                 retString = decimalFormat.format(size / (double)ONE_MEGABYTE);
107             } else {
108                 retString = Long.toString((size + (529 * ONE_KILOBYTE))
109                         / ONE_MEGABYTE);
110                 retString += "M";
111             }
112             retString = padLeft(retString, 5);
113         }
114         return retString;
115     }
116 }
Popular Tags