001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.catalina.ssi;
018:
019: import java.io.IOException;
020: import java.io.PrintWriter;
021: import java.text.DecimalFormat;
022:
023: /**
024: * Implements the Server-side #fsize command
025: *
026: * @author Bip Thelin
027: * @author Paul Speed
028: * @author Dan Sandberg
029: * @author David Becker
030: * @version $Revision: 531303 $, $Date: 2007-04-23 02:24:01 +0200 (lun., 23 avr. 2007) $
031: */
032: public final class SSIFsize implements SSICommand {
033: protected final static int ONE_KILOBYTE = 1024;
034: protected final static int ONE_MEGABYTE = 1024 * 1024;
035:
036: /**
037: * @see SSICommand
038: */
039: public long process(SSIMediator ssiMediator, String commandName,
040: String[] paramNames, String[] paramValues,
041: PrintWriter writer) {
042: long lastModified = 0;
043: String configErrMsg = ssiMediator.getConfigErrMsg();
044: for (int i = 0; i < paramNames.length; i++) {
045: String paramName = paramNames[i];
046: String paramValue = paramValues[i];
047: String substitutedValue = ssiMediator
048: .substituteVariables(paramValue);
049: try {
050: if (paramName.equalsIgnoreCase("file")
051: || paramName.equalsIgnoreCase("virtual")) {
052: boolean virtual = paramName
053: .equalsIgnoreCase("virtual");
054: lastModified = ssiMediator.getFileLastModified(
055: substitutedValue, virtual);
056: long size = ssiMediator.getFileSize(
057: substitutedValue, virtual);
058: String configSizeFmt = ssiMediator
059: .getConfigSizeFmt();
060: writer.write(formatSize(size, configSizeFmt));
061: } else {
062: ssiMediator.log("#fsize--Invalid attribute: "
063: + paramName);
064: writer.write(configErrMsg);
065: }
066: } catch (IOException e) {
067: ssiMediator.log("#fsize--Couldn't get size for file: "
068: + substitutedValue, e);
069: writer.write(configErrMsg);
070: }
071: }
072: return lastModified;
073: }
074:
075: public String repeat(char aChar, int numChars) {
076: if (numChars < 0) {
077: throw new IllegalArgumentException(
078: "Num chars can't be negative");
079: }
080: StringBuffer buf = new StringBuffer();
081: for (int i = 0; i < numChars; i++) {
082: buf.append(aChar);
083: }
084: return buf.toString();
085: }
086:
087: public String padLeft(String str, int maxChars) {
088: String result = str;
089: int charsToAdd = maxChars - str.length();
090: if (charsToAdd > 0) {
091: result = repeat(' ', charsToAdd) + str;
092: }
093: return result;
094: }
095:
096: //We try to mimick Apache here, as we do everywhere
097: //All the 'magic' numbers are from the util_script.c Apache source file.
098: protected String formatSize(long size, String format) {
099: String retString = "";
100: if (format.equalsIgnoreCase("bytes")) {
101: DecimalFormat decimalFormat = new DecimalFormat("#,##0");
102: retString = decimalFormat.format(size);
103: } else {
104: if (size == 0) {
105: retString = "0k";
106: } else if (size < ONE_KILOBYTE) {
107: retString = "1k";
108: } else if (size < ONE_MEGABYTE) {
109: retString = Long.toString((size + 512) / ONE_KILOBYTE);
110: retString += "k";
111: } else if (size < 99 * ONE_MEGABYTE) {
112: DecimalFormat decimalFormat = new DecimalFormat("0.0M");
113: retString = decimalFormat.format(size
114: / (double) ONE_MEGABYTE);
115: } else {
116: retString = Long.toString((size + (529 * ONE_KILOBYTE))
117: / ONE_MEGABYTE);
118: retString += "M";
119: }
120: retString = padLeft(retString, 5);
121: }
122: return retString;
123: }
124: }
|