head	1.10;
access;
symbols
	TIGRIS_1_1_0RC2:1.4.2.1
	TIGRIS_1_1_0RC1:1.4.2.1
	TIGRIS_1_1:1.4.2.1.0.2
	TIGRIS_1_0_8:1.4.2.1
	TIGRIS_1_0_8RC3:1.4.2.1
	TIGRIS_1_0_8RC2:1.4.2.1
	TIGRIS_1_0_8RC1:1.4.2.1
	TIGRIS_1_0_7:1.4.2.1
	TIGRIS_1_0_7RC3:1.4.2.1
	TIGRIS_1_0_7RC2:1.4.2.1
	TIGRIS_1_0_7RC1:1.4.2.1
	TIGRIS_1_0_6:1.4.2.1
	TIGRIS_1_0_6RC5:1.4.2.1
	TIGRIS_1_0_6RC4:1.4.2.1
	TIGRIS_1_0_6RC3:1.4.2.1
	TIGRIS_1_0_6RC2:1.4.2.1
	TIGRIS_1_0_6RC1:1.4.2.1
	TIGRIS_1_0_5:1.4.2.1
	TIGRIS_1_0_5RC6:1.4.2.1
	TIGRIS_1_0_5RC5:1.4.2.1
	TIGRIS_1_0_5RC4:1.4.2.1
	TIGRIS_1_0_5RC3:1.4.2.1
	TIGRIS_1_0_5RC2:1.4.2.1
	TIGRIS_1_0_5RC1:1.4.2.1
	TIGRIS_1_0_4:1.4.2.1
	TIGRIS_1_0_3:1.4.2.1
	TIGRIS_1_0_2:1.4.2.1
	TIGRIS_1_0:1.4.0.2;
locks; strict;
comment	@# @;


1.10
date	2001.08.15.23.47.39;	author dlr;	state Exp;
branches;
next	1.9;

1.9
date	2001.08.11.09.08.08;	author dlr;	state Exp;
branches;
next	1.8;

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

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

1.6
date	2001.05.18.02.09.04;	author dlr;	state Exp;
branches;
next	1.5;

1.5
date	2001.05.18.01.42.11;	author dlr;	state Exp;
branches;
next	1.4;

1.4
date	2001.04.24.02.00.24;	author dlr;	state Exp;
branches
	1.4.2.1;
next	1.3;

1.3
date	2001.04.24.01.47.05;	author dlr;	state Exp;
branches;
next	1.2;

1.2
date	2001.04.24.01.39.08;	author dlr;	state Exp;
branches;
next	1.1;

1.1
date	2001.04.24.01.36.05;	author dlr;	state Exp;
branches;
next	;

1.4.2.1
date	2001.04.24.20.30.32;	author dlr;	state Exp;
branches;
next	;


desc
@@


1.10
log
@Changed ProjectFileArtifact interface from xxxSize to xxxFileSize to play nicely with Torque OM-generation for Oracle (in which SIZE is a reserved word).
@
text
@/* ================================================================
 * Copyright (c) 2000-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.tool;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.security.MessageDigest;

import org.apache.turbine.util.Log;
import org.apache.commons.util.exception.NestableException;

import org.tigris.helm.om.Project;
import org.tigris.helm.om.ProjectFileArtifact;
import org.tigris.helm.om.ProposableFileArtifact;
import org.tigris.helm.om.User;

/**
 * A helper class for dealing with file artifacts.
 *
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 */
public class FileArtifactTool
{
    /**
     * The buffer size used when computing the MD5 checksum of a file.
     */
    private static final int MD5_BUF_SIZE = 4096;

    /**
     * Returns the corresponding file artifact, or <code>null</code> iff
     * one couldn't be found.
     *
     * @@param caller    A description of the calling function to use in any log
     *                  messages.
     * @@param artifacts The list of file artifacts.
     */
    public static final ProjectFileArtifact getFileArtifact(String caller,
                                                            String fileName,
                                                            List artifacts)
        throws Exception
    {
        if (artifacts.size() == 1)
        {
            return (ProjectFileArtifact) artifacts.get(0);
        }
        else
        {
            if (artifacts.size() > 0)
            {
                Log.info(caller + ": Multiple matches found for file " +
                         fileName);
            }
            return null;
        }
    }

    /**
     * Assuming that the provided file object is not <code>null</code>, fills
     * in the specified artifact object with the file's attributes.
     */
    public static void fillInAttributes(ProjectFileArtifact artifact, File f)
    {
        if (f != null && f.exists())
        {
            // Handle fields inferred from the File.
            artifact.setFileName(f.getName());
            artifact.setLocation(extractLocation(f));

            // Compute the file MD5 checksum and size.
            int size = -1;
            String md5Checksum = null;
            try
            {
                if (f.canRead())
                {
                    FileInputStream in = new FileInputStream(f);
                    MessageDigest md5 = MessageDigest.getInstance
                        (CryptoProviderService.MD5,
                         CryptoProviderService.CRYPTIX_PROVIDER);
                    byte[] buf = new byte[MD5_BUF_SIZE];
                    int read = 0;
                    while ((read = in.read(buf)) != -1)
                    {
                        size += read;
                        md5.update(buf, 0, read);
                    }
                    in.close();
                    md5Checksum = ByteToHex.transform(md5.digest());
                }
            }
            catch (Exception e)
            {
                Log.error("Couldn't determine file MD5 checksum or " + 
                          "size: " + f.getAbsolutePath() + ": " +
                          e.getMessage());
            }

            if (size != -1)
            {
                artifact.setFileSize(size);
            }
            artifact.setMd5(md5Checksum);
        }
    }

    /**
     * Filters {@@link org.tigris.helm.om.ProposableFileArtifact} objects of
     * all but those from <code>proposer</code>.
     */
    public static List artifactsProposedBy(User proposer, List artifacts)
        throws Exception
    {
        List proposedArtifacts = new ArrayList();
        for (Iterator i = artifacts.iterator(); i.hasNext(); )
        {
            ProposableFileArtifact artifact = (ProposableFileArtifact)i.next();
            if (proposer.equals(artifact.getProposedByUser()))
            {
                proposedArtifacts.add(artifact);
            }
        }
        return proposedArtifacts;
    }

    /**
     * Extracts the directory path to the file.
     *
     * @@param f The file to extract path information from.
     * @@return  The extracted path information.
     */
    private static final String extractLocation(File f)
    {
        return f.getParentFile().getAbsolutePath();
    }
}
@


1.9
log
@Upgrading to Turbine 3.0-dev, Fulcrum 1.0, and Torque 1.0.
@
text
@d145 4
a148 1
            if (size != -1) artifact.setSize(size);
@


1.8
log
@Ported Helm from WebMacro to Velocity.
@
text
@d57 1
a57 1
import org.apache.turbine.util.TurbineException;
@


1.7
log
@getProposedByUser() throws Exception...propogated.
@
text
@d57 1
@


1.6
log
@Imported Iterator.
@
text
@d154 1
@


1.5
log
@Added List artifactsProposedBy(User proposer, List artifacts) class method.
@
text
@d52 1
@


1.4
log
@Added fillInAttributes and extractLocation class methods pulled from
the om.Project class.
@
text
@d51 1
d59 2
d146 18
@


1.4.2.1
log
@Used in conjunction with ProjectFileArtifacts.
@
text
@@


1.3
log
@Fixed up build.
@
text
@d49 2
d52 1
d67 5
d97 57
@


1.2
log
@Corrected getFileArtifact signature and impl (FileArtifact ->
ProjectFileArtifact).
@
text
@d49 4
d72 1
d76 1
a76 1
        if (list.length() == 1)
d78 1
a78 1
            return (ProjectFileArtifact) list.get(0);
d82 1
a82 1
            if (list.length() > 0)
d84 2
a85 1
                Log.info(caller + ": Multiple matches found for file " + file);
@


1.1
log
@A helper class for dealing with file artifacts.
@
text
@d67 2
a68 2
    public static final FileArtifact getFileArtifact(String caller,
                                                     List artifacts)
d73 1
a73 1
            return list.get(0);
@

