head	1.13;
access;
symbols
	TIGRIS_0_9_2_4:1.9
	TIGRIS_0_9_2_3:1.9
	HELM_PEER_PORT_BRANCH:1.12.0.2
	TURBINE_PEER:1.12
	TIGRIS_0_9_2_1:1.12
	TIGRIS_0_9_2:1.9.0.2
	TIGRIS_0_9_0:1.7.0.2
	TIGRIS_0_8_4:1.5.0.2;
locks; strict;
comment	@# @;


1.13
date	2001.02.21.03.23.57;	author edk;	state dead;
branches;
next	1.12;

1.12
date	2001.01.26.18.29.09;	author edk;	state Exp;
branches
	1.12.2.1;
next	1.11;

1.11
date	2001.01.26.05.24.08;	author edk;	state dead;
branches;
next	1.10;

1.10
date	2001.01.25.02.42.47;	author edk;	state Exp;
branches;
next	1.9;

1.9
date	2001.01.12.19.13.49;	author leonardr;	state Exp;
branches;
next	1.8;

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

1.7
date	2000.12.11.18.07.00;	author leonardr;	state Exp;
branches;
next	1.6;

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

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

1.4
date	2000.12.01.18.14.51;	author edk;	state Exp;
branches;
next	1.3;

1.3
date	2000.12.01.18.01.49;	author edk;	state Exp;
branches;
next	1.2;

1.2
date	2000.12.01.16.25.01;	author edk;	state Exp;
branches;
next	1.1;

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

1.12.2.1
date	2001.01.28.01.02.19;	author dlr;	state dead;
branches;
next	;


desc
@@


1.13
log
@delete stuff from the head which is obsolete in the branch (more changes
are still needed for a clean compile)
@
text
@/* ================================================================
 * Copyright (c) 2000 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.sql.*;
import java.util.*;
import java.io.*;

import org.webmacro.util.Log;

import com.workingdogs.village.*;
import org.joist.database.*;
import org.joist.util.JoistProperties;
import org.tigris.helm.DoesNotExist;
import org.tigris.helm.database.TaskManager;
import org.tigris.helm.domain.BusinessObject;
import org.tigris.helm.domain.User;

public abstract class TigrisTask extends BusinessObject
{
    private TaskManager tMgr;

    protected final String getTableName() { return TaskManager.TABLE_NAME; }

    public static Log log = new Log("Task", "Tigris Task");

    public static final boolean debug = true;

    public TigrisTask (Connection conn, DataManager dMgr, String onBehalfOf, 
                 String component, String affectedObjectID, String command,
                 String arguments)
        throws SQLException, DataSetException
    {
        super(conn, dMgr);
        try {
            setRecord(tMgr.addTask(conn, onBehalfOf, component, 
                                   affectedObjectID, command, arguments));
        } catch (DoesNotExist ignore) {}
    }

    public TigrisTask (Connection conn, DataManager dMgr, String onBehalfOf, 
                 String component, String affectedObjectID, String command,
                 ArrayList arguments)
        throws SQLException, DataSetException, Exception
    {
        this(conn, dMgr, onBehalfOf, component, affectedObjectID, command,
             writeArgsArray(arguments));
    }

    public TigrisTask (Connection conn, DataManager dMgr, String taskID)
        throws SQLException, DataSetException, DoesNotExist
    {
        super(conn, dMgr);
        setRecord(tMgr.getTaskByID(conn, taskID));
    }

    public TigrisTask (Connection conn, DataManager dMgr, Record r)
        throws SQLException, DataSetException
    {
        super(conn, dMgr);
        try {
            setRecord(r);
        } catch (DoesNotExist ignore) {}
    }

    protected void createManagers()
    {
        tMgr = (TaskManager)dMgr.getManager 
            ("org.tigris.helm.database.TaskManager");
    }

    public static List getTasks(Connection conn, DataManager dMgr)
        throws SQLException, DataSetException
    {
        ArrayList tasks = new ArrayList();
        TaskManager tMgr = (TaskManager)dMgr.getManager
            ("org.tigris.helm.database.TaskManager");
        DataSet taskSet = tMgr.getTasks(conn);
        for (int i = 0 ; i < taskSet.size() ; i++)
        {
            Record record = taskSet.getRecord(i);
            String component=record.getValue(TaskManager.COMPONENT).asString();
            TigrisTask t = null;
            if (component.equals(AnzuTigrisTask.COMPONENT_NAME)) {
                t = new AnzuTigrisTask(conn, dMgr, record);
            } else if (component.equals(HelmTigrisTask.COMPONENT_NAME)) {
                t = new HelmTigrisTask(conn, dMgr, record);
            }
            tasks.add(t);
        }
        return tasks;
    }

    abstract public void runTask() throws UpdateException;

    public void setAsDone()
    {
        try {
            log.info("Marking task " + getID() + " (" + getCommand()
                     + ") as done.");
            tMgr.removeTask(conn, getID());
            //If we run into a problem, that means the task has
            //already been marked as done.
        } catch (Exception ignore) {}
    }
        
    public String getID()
        throws DataSetException
    {
        return rec.getValue(TaskManager.TASK_ID).asString();
    }

    public String getTimestamp()
        throws DataSetException
    {
        return rec.getValue(TaskManager.TIMESTAMP).asString();
    }

    public String getOnBehalfOf()
        throws DataSetException
    {
        return rec.getValue(TaskManager.ON_BEHALF_OF).asString();
    }

    public User getOnBehalfOfUser()
        throws DataSetException, SQLException, DoesNotExist, 
               JoistDatabaseException
    {
        String userID = getOnBehalfOf();
        return User.getInstance(conn, dMgr, userID);            
    }

    public String getComponent()
        throws DataSetException
    {
        return rec.getValue(TaskManager.COMPONENT).asString();
    }

    public String getAffectedObjectID()
        throws DataSetException
    {
        return rec.getValue(TaskManager.AFFECTED_OBJECT_ID).asString();
    }

    public String getCommand()
        throws DataSetException
    {
        return rec.getValue(TaskManager.COMMAND).asString();
    }

    public String getArguments()
        throws DataSetException
    {
        return rec.getValue(TaskManager.ARGUMENTS).asString();
    }

    public String getArgumentsHumanReadable()
        throws DataSetException
    {
        ArrayList argList = null;
        try 
        { 
            argList = readArgsArray();
        } catch (Exception e) {
            return getArguments();
        }
        StringBuffer readable = new StringBuffer();
        readable.append((String)argList.get(0));
        for (int i = 0; i < argList.size(); i++)
        {
            String arg = (String)argList.get(i);
            readable.append("/").append(arg);
        }
        return readable.toString();
    }
    
    protected String[] readArgs()
        throws Exception
    {
        ByteArrayInputStream bais = new ByteArrayInputStream
            (getArguments().getBytes());
        ObjectInputStream ois = new ObjectInputStream(bais);
        return (String[])ois.readObject();
    }

    protected static String writeArgs(String args[])
        throws Exception
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(args);
        return baos.toString();
    }

    protected ArrayList readArgsArray()
        throws Exception
    {
        String array[] = readArgs();
        ArrayList args = new ArrayList();
        for (int i = 0; i < array.length; i++)
        {
            args.add(array[i]);
        }
        return args;
    }

    protected static String writeArgsArray(ArrayList args)
        throws Exception
    {
        String array[] = new String[args.size()];
        Iterator i = args.iterator();
        int offset = 0;
        while (i.hasNext()) {
            array[offset++] = (String)i.next();
        }
        return writeArgs(array);
    }
}

@


1.12
log
@readding all the files as part of the process of returning to the way
things were as of tuesday
@
text
@@


1.12.2.1
log
@Porting to the Turbine Peer system.  -dlr and leonardr
@
text
@@


1.11
log
@revert this stuff to its state as of 2001/01/24 12:42:00 GMT -- with
the following exceptions, which have some different modifications so
they'll work with changes which have happened in joist:

ProjectNewsEdit.java
SkinManager.java
SkinStyleElementValueManager.java
StyleElementManager.java
StyleManager.java

it compiles.  i dunno if it works, though.
@
text
@a59 1
import org.tigris.helm.database.DBException;
d73 1
a73 1
    public TigrisTask (DataManager dMgr, String onBehalfOf, 
d76 1
a76 1
        throws DBException
d78 1
a78 1
        super(dMgr);
d80 1
a80 1
            setRecord(tMgr.addTask(onBehalfOf, component, 
d85 1
a85 1
    public TigrisTask (DataManager dMgr, String onBehalfOf, 
d88 1
a88 1
        throws DBException, Exception
d90 1
a90 1
        this(dMgr, onBehalfOf, component, affectedObjectID, command,
d94 2
a95 2
    public TigrisTask (DataManager dMgr, String taskID)
        throws DBException, DoesNotExist
d97 2
a98 2
        super(dMgr);
        setRecord(tMgr.getTaskByID(taskID));
d101 2
a102 2
    public TigrisTask (DataManager dMgr, Record r)
        throws DBException
d104 1
a104 1
        super(dMgr);
d116 2
a117 2
    public static List getTasks(DataManager dMgr)
        throws DBException
d122 1
a122 1
        DataSet taskSet = tMgr.getTasks();
d125 2
a126 8
            Record record = null;
            String component = null;
            try {
                record = taskSet.getRecord(i);
                component = record.getValue(TaskManager.COMPONENT).asString();
            } catch (DataSetException e) {
                throw new DBException(e);
            }
d129 1
a129 1
                t = new AnzuTigrisTask(dMgr, record);
d131 1
a131 1
                t = new HelmTigrisTask(dMgr, record);
d145 1
a145 1
            tMgr.removeTask(getID());
d152 1
a152 1
        throws DBException
d154 1
a154 1
        return getField(TaskManager.TASK_ID);
d158 1
a158 1
        throws DBException
d160 1
a160 1
        return getField(TaskManager.TIMESTAMP);
d164 1
a164 1
        throws DBException
d166 1
a166 1
        return getField(TaskManager.ON_BEHALF_OF);
d170 1
a170 1
        throws DBException, SQLException, DoesNotExist, 
d174 1
a174 1
        return User.getInstance(dMgr, userID);            
d178 1
a178 1
        throws DBException
d180 1
a180 1
        return getField(TaskManager.COMPONENT);
d184 1
a184 1
        throws DBException
d186 1
a186 1
        return getField(TaskManager.AFFECTED_OBJECT_ID);
d190 1
a190 1
        throws DBException
d192 1
a192 1
        return getField(TaskManager.COMMAND);
d196 1
a196 1
        throws DBException
d198 1
a198 1
        return getField(TaskManager.ARGUMENTS);
d202 1
a202 1
        throws DBException
@


1.10
log
@more changes to match the new pattern
@
text
@@


1.9
log
@Don't throw exceptions when tasks that were going to be deleted are already gone.
@
text
@d60 1
d74 1
a74 1
    public TigrisTask (Connection conn, DataManager dMgr, String onBehalfOf, 
d77 1
a77 1
        throws SQLException, DataSetException
d79 1
a79 1
        super(conn, dMgr);
d81 1
a81 1
            setRecord(tMgr.addTask(conn, onBehalfOf, component, 
d86 1
a86 1
    public TigrisTask (Connection conn, DataManager dMgr, String onBehalfOf, 
d89 1
a89 1
        throws SQLException, DataSetException, Exception
d91 1
a91 1
        this(conn, dMgr, onBehalfOf, component, affectedObjectID, command,
d95 2
a96 2
    public TigrisTask (Connection conn, DataManager dMgr, String taskID)
        throws SQLException, DataSetException, DoesNotExist
d98 2
a99 2
        super(conn, dMgr);
        setRecord(tMgr.getTaskByID(conn, taskID));
d102 2
a103 2
    public TigrisTask (Connection conn, DataManager dMgr, Record r)
        throws SQLException, DataSetException
d105 1
a105 1
        super(conn, dMgr);
d117 2
a118 2
    public static List getTasks(Connection conn, DataManager dMgr)
        throws SQLException, DataSetException
d123 1
a123 1
        DataSet taskSet = tMgr.getTasks(conn);
d126 8
a133 2
            Record record = taskSet.getRecord(i);
            String component=record.getValue(TaskManager.COMPONENT).asString();
d136 1
a136 1
                t = new AnzuTigrisTask(conn, dMgr, record);
d138 1
a138 1
                t = new HelmTigrisTask(conn, dMgr, record);
d152 1
a152 1
            tMgr.removeTask(conn, getID());
d159 1
a159 1
        throws DataSetException
d161 1
a161 1
        return rec.getValue(TaskManager.TASK_ID).asString();
d165 1
a165 1
        throws DataSetException
d167 1
a167 1
        return rec.getValue(TaskManager.TIMESTAMP).asString();
d171 1
a171 1
        throws DataSetException
d173 1
a173 1
        return rec.getValue(TaskManager.ON_BEHALF_OF).asString();
d177 1
a177 1
        throws DataSetException, SQLException, DoesNotExist, 
d181 1
a181 1
        return User.getInstance(conn, dMgr, userID);            
d185 1
a185 1
        throws DataSetException
d187 1
a187 1
        return rec.getValue(TaskManager.COMPONENT).asString();
d191 1
a191 1
        throws DataSetException
d193 1
a193 1
        return rec.getValue(TaskManager.AFFECTED_OBJECT_ID).asString();
d197 1
a197 1
        throws DataSetException
d199 1
a199 1
        return rec.getValue(TaskManager.COMMAND).asString();
d203 1
a203 1
        throws DataSetException
d205 1
a205 1
        return rec.getValue(TaskManager.ARGUMENTS).asString();
d209 1
a209 1
        throws DataSetException
@


1.8
log
@Removed unnecessary getCache() implementations in BusinessObject subclasses and migrated to a factory method for User's primary database key ctor.
@
text
@a140 1
        throws SQLException, DataSetException
d142 7
a148 3
        log.info("Marking task " + getID() + " (" + getCommand()
                 + ") as done.");
        tMgr.removeTask(conn, getID());
@


1.7
log
@Additions for human-readability.
@
text
@d171 1
a171 1
        return new User(conn, dMgr, userID);            
@


1.6
log
@Enabled security check of records passed into business objects.
@
text
@d61 1
d166 8
d196 20
@


1.5
log
@My side of the changes edk made.
@
text
@d66 2
@


1.4
log
@make these methods static
@
text
@d62 1
a62 1
public class TigrisTask extends BusinessObject
a63 3
    public static final String ANZU_COMPONENT = "Anzu";
    public static final String HELM_COMPONENT = "Helm";

a67 3
    //FIXME: Please!
    static final String script_dir = "/helm/perl/scripts/";

d82 9
d125 1
a125 1
            if (component.equals(ANZU_COMPONENT)) {
d127 2
a128 2
            } else {
                t = new TigrisTask(conn, dMgr, record);
d135 1
a135 15
    public static List getTasksForComponent(Connection conn, DataManager dMgr,
                                            String component)
        throws SQLException, DataSetException
    {
        ArrayList tasks = new ArrayList();
        TaskManager tMgr = (TaskManager)dMgr.getManager 
            ("org.tigris.helm.database.TaskManager");
        DataSet taskSet = tMgr.getTasksForComponent(conn, component);
        for (int i = 0; i < taskSet.size(); i++)
        {
            TigrisTask t = new TigrisTask(conn, dMgr, taskSet.getRecord(i));
            tasks.add(t);
        }
        return tasks;
    }
d140 2
a141 1
        log.info("Marking task " + getCommand() + "/" + getID() + " as done.");
a185 42

    public void runTask() throws UpdateException
    {
        UpdateException ue = null;
        String command = null;
        String argblob = null;
        try {
            command = getCommand();
            argblob = getArguments();
        } catch (Exception e) {
            log.exception(e);
        }

        if (command != null && argblob != null)
        {
            ArrayList args = new ArrayList();
            args.add(command);
            
            StringTokenizer tok = new StringTokenizer(argblob, "\n");
            while (tok.hasMoreTokens())
            {
                args.add(tok.nextToken());
            }            
            try 
            {
                runScript(args);
            } catch (UpdateException e) {
                //TODO: If this happens, reset the timestamp of the script
                //rather than deleting it.
                ue = e;
            }
            
            try 
            { 
                setAsDone();
            } catch (Exception e) {
                log.exception(e);
            }

            if (ue != null) { throw ue; }
        }
    }
d187 1
a187 67
    ///////////////////////////////////////////////////////////////////
    // Script runner.
    ///////////////////////////////////////////////////////////////////
    /**
     * Run a command with a populated environment.
     * @@param argList command to run (inc. arguments)
     */
    private static synchronized void runScript(ArrayList argList)
        throws UpdateException 
    {
        String args[] = new String[argList.size()];
        for (int i = 0; i < argList.size(); i++)
        {
            args[i] = (String)argList.get(i);
        }

        //Get the sandbox directory and htdoc_root to put in the
        //script's environment.
        String htdoc_root =
            (String) JoistProperties.getProperty("documentRoot");
        String sandbox = (String) JoistProperties.getProperty("SANDBOX");
        String dbName = (String) JoistProperties.getProperty("databaseName");
        String dbUser = (String) JoistProperties.getProperty("databaseUser");
        String dbPassword =
            (String) JoistProperties.getProperty("databasePassword");

        String[] env = {"HTDOC_ROOT=" + htdoc_root, 
                        "SANDBOX="+sandbox, 
                        "DATABASE_NAME=" + dbName,
                        "DATABASE_USER=" + dbUser,
                        "DATABASE_PASSWORD=" + dbPassword
        };

        Runtime rt = Runtime.getRuntime();
        Process pr;
        int exitCode;

        StringBuffer scriptName = new StringBuffer();
        for (int i = 0; i < args.length; i++)
        {
            if (args[i] == null)
            {
                args[i] = "NULL";
            }
            scriptName.append(args[i]).append(" ");
        }            
        if (debug) log.info("Running " + scriptName.toString());

        try {
            args[0] = sandbox + script_dir + args[0];
            pr = rt.exec(args, env);
        } catch (IOException e) {
            throw new UpdateException("IO error while running script.");
        }
        
        try {
            exitCode = pr.waitFor();
        } catch (InterruptedException e) {
            throw new UpdateException("Script was interrupted.");
        }
        if (pr.exitValue() != 0) {
            throw new UpdateException(scriptName.toString() + "\n\nExit code: "
                                      + new Integer(exitCode).toString());
        }
    }

    protected static String[] readArgs (String args)
d190 2
a191 1
        ByteArrayInputStream bais = new ByteArrayInputStream(args.getBytes());
d205 13
a217 1
    protected static String writeArgs(ArrayList args)
@


1.3
log
@rename HelmAnzuTask to AnzuTigrisTask; remove an unused function from
TigrisUpdate ; and add functions (thanks to leonardr) for storing
arguments via serialization/deserialization in TigrisTask
@
text
@d305 1
a305 1
    protected String[] readArgs (String args)
d313 1
a313 1
    protected String writeArgs(String args[])
d322 1
a322 1
    protected String writeArgs(ArrayList args)
@


1.2
log
@steps towards providing for different kinds of tasks.  if this pattern
is used, TigrisTask should probably be broken down into an abstract base
class and a HelmCommandTask (or somesuch) -- but this involves lots
of changes elsewhere.  HelmAnzuTask will be used for anzu mailing
list creation during project creation, though it's not being used
that way yet.
@
text
@d123 1
a123 1
                t = new HelmAnzuTask(conn, dMgr, record);
d303 29
@


1.1
log
@Mega patch which makes script data persistent and which provides a general framework for tasks to be run (which will be used by anzu eventually)
@
text
@d51 1
d57 1
d69 6
a74 1
    static public Log log = new Log("Task", "Tigris Task");
d110 22
d195 108
@

