head	1.8;
access;
symbols;
locks; strict;
comment	@# @;


1.8
date	2001.08.30.16.57.41;	author dlr;	state Exp;
branches;
next	1.7;

1.7
date	2001.08.27.08.41.27;	author dlr;	state Exp;
branches;
next	1.6;

1.6
date	2001.08.15.22.25.07;	author leonardr;	state Exp;
branches;
next	1.5;

1.5
date	2001.08.15.22.08.50;	author leonardr;	state Exp;
branches;
next	1.4;

1.4
date	2001.08.15.19.02.08;	author dlr;	state Exp;
branches;
next	1.3;

1.3
date	2001.08.15.18.59.02;	author leonardr;	state Exp;
branches;
next	1.2;

1.2
date	2001.08.15.17.34.20;	author leonardr;	state Exp;
branches;
next	1.1;

1.1
date	2001.08.14.22.49.14;	author leonardr;	state Exp;
branches;
next	;


desc
@@


1.8
log
@Updated for latest Turbine API changes.
@
text
@/* ================================================================
 * Copyright (c) 2001 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet (http://www.collab.net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */

package org.tigris.helm.om;

import java.util.*;
import java.io.*;

import org.apache.torque.util.BasePeer;
import org.apache.torque.util.Criteria;
import org.apache.commons.util.ObjectUtils;
import org.apache.commons.util.StringUtils;
import org.apache.commons.util.StreamUtils;
import org.apache.turbine.ParameterParser;
import org.apache.turbine.util.Log;
import org.apache.commons.util.exception.NestableException;
import org.apache.torque.pool.DBConnection;

import org.tigris.helm.om.*;
import org.tigris.helm.tool.HelmProperties;

/** 
 * Business object representing a log file.
 *
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 */
public class LogFile extends BaseLogFile
{
    /**
     * FIXME: should be in a conf file somewhere.
     */
    protected static final String LOG_DIR = "/log/";
    
    /**
     * The maximum number of lines from the log file to play with.
     */
    protected static final int LINE_LIMIT = 1000;

    /**
     * Returns the full filesystem path to the log file. 
     */
    public String getFullPath()
    {
        return (String)HelmProperties.getProperty("SANDBOX") 
            + LOG_DIR + getPath();
    }

    /**
     * Returns a File representation of this log file.
     */
    public File asFile()
        throws Exception
    {
        return new File(getFullPath());
    }

    /**
     * Returns the length of the log file, in bytes.
     */
    public long getLength()
        throws Exception
    {
        return asFile().length();
    }

    /**
     * Returns the last <code>bytes</code> bytes of the log. If
     * <code>bytes</code> is 0, returns the entire log.
     */
    public String contents(long lineOffset)
        throws Exception
    {
        String path = getFullPath();

        String tailArg = null;
        if (lineOffset < 0)
        {
            tailArg = String.valueOf(-LINE_LIMIT);
        }
        else
        {
            tailArg = '+' + String.valueOf(lineOffset);
        }
        String[] tailCommand = {"tail", tailArg, path};
        String[] headCommand = {"head", "-" + LINE_LIMIT};

        Runtime rt = Runtime.getRuntime();
        Process tailPr = rt.exec(tailCommand);
        Process headPr = rt.exec(headCommand);
        InputStream tailOut = tailPr.getInputStream();
        OutputStream headIn = headPr.getOutputStream();
        InputStream headOut = headPr.getInputStream();

        StreamUtils.pipe(tailOut, headIn);
        headIn.close();

        return StreamUtils.streamAsString(headOut);
    }

    /**
     * Throws an exception: LogFile objects are immutable.
     */
    public void save()
        throws NestableException
    {
        throw new NestableException("LogFile objects are immutable.");
    }
}
@


1.7
log
@Updated for Turbine API change.
@
text
@d57 1
a57 1
import org.apache.fulcrum.util.parser.ParameterParser;
@


1.6
log
@StreamUtils is in Apache common now.
@
text
@d57 1
a57 1
import org.apache.turbine.util.ParameterParser;
@


1.5
log
@Cleanup and rename of StreamUtils, and rewrite of streamToString to handle multi-byte characters properly.
@
text
@d56 1
a62 1
import org.tigris.helm.util.StreamUtils;
@


1.4
log
@Added JavaDoc.
@
text
@d62 1
a62 1
import org.tigris.helm.util.StreamTools;
d137 1
a137 1
        StreamTools.pipe(tailOut, headIn);
d140 1
a140 1
        return StreamTools.streamAsString(headOut);
@


1.3
log
@Cleanup and JavaDoc.
@
text
@d72 3
a74 2
    
    //FIXME: should be in a conf file somewhere.
d77 4
a80 1
    protected static final long LINE_LIMIT = 1000;
@


1.2
log
@Factored out stream manipulation methods into a utility class.
@
text
@a77 2
    protected static final int BUFFER_SIZE = 1024;

d79 1
a79 1
     *
a125 3
        byte[] buffer = new byte[BUFFER_SIZE];
        StringBuffer contents = new StringBuffer();
        
@


1.1
log
@Added log view capability.
@
text
@d62 1
d101 1
a101 1
    public long length()
a126 2
        Log.debug("Tail: " + tailCommand[0] + ' ' + tailCommand[1] + ' ' + tailCommand[2]);
        Log.debug("Head: " + headCommand[0] + ' ' + headCommand[1]);
d138 1
a138 11
        int bytesRead;
        while ( (bytesRead = tailOut.read(buffer)) != -1 )
        {
            byte[] newBuffer = buffer;
            if (bytesRead < BUFFER_SIZE)
            {
                newBuffer = new byte[bytesRead];
                System.arraycopy(buffer, 0, newBuffer, 0, bytesRead);
            }
            headIn.write(newBuffer);
        }
d141 1
a141 12
        while ( (bytesRead = headOut.read(buffer)) != -1 )
        {
            byte[] newBuffer = buffer;
            if (bytesRead < BUFFER_SIZE)
            {
                newBuffer = new byte[bytesRead];
                System.arraycopy(buffer, 0, newBuffer, 0, bytesRead);
            }
            contents.append(new String(newBuffer));
        }
        
        return contents.toString();
@

