Package org.syrup.functions

Source Code of org.syrup.functions.Alarm

package org.syrup.functions;

import org.syrup.Context;
import org.syrup.Data;
import org.syrup.Function;
import org.syrup.Result;
import org.syrup.helpers.ResultImpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;

/**
* Waits for specific date/time to pass and then raises an alarm.
*
* @author Robbert van Dalen
*/
public class Alarm implements Function
{
    static final String COPYRIGHT = "Copyright 2005 Robbert van Dalen."
        + "At your option, you may copy, distribute, or make derivative works under "
        + "the terms of The Artistic License. This License may be found at "
        + "http://www.opensource.org/licenses/artistic-license.php. "
        + "THERE IS NO WARRANTY; USE THIS PRODUCT AT YOUR OWN RISK.";

    private final static Logger logger = Logger.getLogger("org.syrup.functions.Alarm");

    private final static SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmmss");

    /**
     */
    public Result execute(Context context)
    {
        try
        {
            if (context.in_1_link() != null)
            {
                Data in1 = context.in_1_link().content();
                String d = new String(in1.bytes());

                Date date = formatter.parse(d);

                long ft = date.getTime();
                long t = new Date().getTime();

                /*
                 * Block Thread for (target-time - current-time) millis [TODO:
                 * Make this more reactive taking small steps instead of one big
                 * one.]
                 */
                if (ft > t)
                {
                    Thread.sleep(ft
                        - t);
                }

                /*
                 * Return the first input to indicate that the alarm is raised.
                 */
                return new ResultImpl(context, true, true, context.in_1_link().content(), null);
            }
            else
            {
                throw new Exception("in-1 is mandatory");
            }
        }
        catch (Throwable e1)
        {
            return new ResultImpl(context, true, true, null, org.syrup.helpers.Utils.manageError(logger, e1, "Alarm error"));
        }
    }
}
TOP

Related Classes of org.syrup.functions.Alarm

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.