From: <st...@us...> - 2007-02-27 17:43:06
|
Revision: 3938 http://svn.sourceforge.net/smartfrog/?rev=3938&view=rev Author: steve_l Date: 2007-02-27 09:41:22 -0800 (Tue, 27 Feb 2007) Log Message: ----------- updated workflow; new CounterAction and RunningFor components Modified Paths: -------------- trunk/core/smartfrog/docs/sfWorkflow.sxw Added Paths: ----------- trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/combinators/CounterAction.java trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/combinators/counteraction.sf trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/conditional/conditions/RunningFor.java trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/conditional/conditions/RunningForImpl.java trunk/core/testharness/docs/testing_under_smartfrog.sxw trunk/core/testharness/testcases/org/smartfrog/test/unit/sfcore/utils/ trunk/core/testharness/testcases/org/smartfrog/test/unit/sfcore/utils/SmartFrogThreadTest.java Modified: trunk/core/smartfrog/docs/sfWorkflow.sxw =================================================================== (Binary files differ) Added: trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/combinators/CounterAction.java =================================================================== --- trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/combinators/CounterAction.java (rev 0) +++ trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/combinators/CounterAction.java 2007-02-27 17:41:22 UTC (rev 3938) @@ -0,0 +1,184 @@ +/** (C) Copyright 2007 Hewlett-Packard Development Company, LP + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + For more information: www.smartfrog.org + + */ +package org.smartfrog.sfcore.workflow.combinators; + +import org.smartfrog.sfcore.workflow.eventbus.EventCompoundImpl; +import org.smartfrog.sfcore.compound.Compound; +import org.smartfrog.sfcore.prim.Prim; +import org.smartfrog.sfcore.prim.TerminationRecord; +import org.smartfrog.sfcore.common.SmartFrogException; +import org.smartfrog.sfcore.common.SmartFrogLivenessException; +import org.smartfrog.sfcore.componentdescription.ComponentDescription; +import org.smartfrog.sfcore.reference.Reference; + +import java.rmi.RemoteException; + +/** + * This component is designed to wrap two workflow actions/sequences into a pair, with one sequence run + * at startup, and the second sequence executed during termination. The idea is that the second sequence + * is the counteraction to the first; such as the deletion of a database or the dropping of a table. + + * when started, this component + * <ol> + * <li>Deploys and starts the <i>action</i> component + * <li>If it is marked as terminating, it then terminates itself. Otherwise it stays deployed (default) + * <li>Deploys the <i>counterAction</i> component, which will be run at termination. + * <li>Deploys and starts the <i>liveness</i> component, if it exists. + * This component gets liveness tests relayed to it. + * <li>When terminating, starts the <i>counterAction<i> component. + * </ol> + */ +public class CounterAction extends EventCompoundImpl implements Compound { + + + public static final String ATTR_COUNTER_ACTION="counterAction"; + public static final String ATTR_LIVENESS = "liveness"; + + private static final Reference COUNTER_ACTION_REF=new Reference(ATTR_COUNTER_ACTION); + private static final Reference LIVENESS_REF = new Reference(ATTR_LIVENESS); + + private Prim actionPrim; + private Prim livenessPrim; + private Prim counterActionPrim; + + /** + * Constructs Try. + * + * @throws java.rmi.RemoteException In case of RMI or network failure. + */ + public CounterAction() throws java.rmi.RemoteException { + super(); + } + + + /** + * This is an override point. The original set of event components suppored the 'old' notation, in which actions + * were listed in the {@link #ATTR_ACTIONS element} New subclasses do not need to remain backwards compatible and + * should declare this fact by returning false from this method + * + * @return false + */ + protected boolean isOldNotationSupported() { + return false; + } + + /** + * Deploys and reads the basic configuration of the component. + * Overrides EventCOmpoundImpl.sfStart. + * + * @throws RemoteException In case of network/rmi error + * @throws SmartFrogException In case of any error while + * deploying the component + */ + public synchronized void sfDeploy() throws SmartFrogException, RemoteException { + super.sfDeploy(); + checkActionDefined(); + } + + /** + * Deploys and manages the primary subcomponent. + * + * @throws RemoteException In case of network/rmi error + * @throws SmartFrogException In case of any error while starting + * the component + */ + public synchronized void sfStart() throws SmartFrogException, RemoteException { + super.sfStart(); + + actionPrim = deployChildCD(ATTR_ACTION, true); + ComponentDescription counterAct=null; + counterAct = sfResolve(ATTR_COUNTER_ACTION, counterAct, false); + //deploy but dont start the counter action. + if(counterAct!=null) { + counterActionPrim = deployComponentDescription(ATTR_COUNTER_ACTION, counterAct); + } + livenessPrim = deployChildCD(ATTR_LIVENESS,false); + } + + + /** + * Handle child termination. + * + * Abnormal child terminations are relayed up. + * @param status exit record of the component + * @param comp child component that is terminating + * @return true whenever a child component is not started + */ + protected boolean onChildTerminated(TerminationRecord status, Prim comp) { + //get and reset the primary flag in a thread safe manner. + + boolean isAction; + synchronized(this) { + isAction = actionPrim==comp; + } + if (isAction) { + try { + sfRemoveChild(comp); + return false; + } catch (Exception e) { + String message = "error in starting follow-on component for '" + status.errorType + "' try action"; + if (sfLog().isErrorEnabled()) { + sfLog().error(message, e); + } + //trigger our own abnormal termination + sfTerminate(TerminationRecord.abnormal(message, name)); + //and tell the container not to + return false; + } + } else { + //not the action + return true; + } + } + + + /** + * When we terminate, we start the counterAction, that gets to do the cleanup. + * + * @param status termination record. + */ + public synchronized void sfTerminateWith(TerminationRecord status) { + if (counterActionPrim != null) { + if (sfLog().isDebugEnabled()) { + sfLog().debug("Starting the CounterAction"); + } + try { + counterActionPrim.sfStart(); + } catch (SmartFrogException e) { + sfLog().error("When starting the CounterAction", e); + } catch (RemoteException e) { + sfLog().error("When starting the CounterAction", e); + } + } + super.sfTerminateWith(status); + } + + + /** + * Called by {@link #sfPing(Object)} to run through the list of children and ping each in turn. + * In this subclass, only the liveness prim (if deployed) is checked for health. + * + */ + protected void sfPingChildren() { + if(livenessPrim!=null) { + sfPingChildAndTerminateOnFailure(livenessPrim); + } + } +} Added: trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/combinators/counteraction.sf =================================================================== --- trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/combinators/counteraction.sf (rev 0) +++ trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/combinators/counteraction.sf 2007-02-27 17:41:22 UTC (rev 3938) @@ -0,0 +1,49 @@ +/** (C) Copyright 1998-2006 Hewlett-Packard Development Company, LP + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +For more information: www.smartfrog.org + +*/ +#include "org/smartfrog/sfcore/workflow/eventbus/eventbus.sf" + +/** + * This component is designed to wrap two workflow actions/sequences into a pair, with one sequence run + * at startup, and the second sequence executed during termination. The idea is that the second sequence + * is the counteraction to the first; such as the deletion of a database or the dropping of a table. + + * when started, this component + * 1. Deploys and starts the action component + * 2. If it is marked as terminating, it then terminates itself. Otherwise it stays deployed (default) + * 3. Deploys the "counterAction" component, which will be run at termination. + * 3. Deploys and starts the liveness component, if it exists. This component gets liveness tests relayed to it. + * 4. When terminating, starts the counterAction component that was deployed earlier. + */ +CounterAction extends ActionCompound { + sfClass "org.smartfrog.sfcore.workflow.combinators.Try"; + + /** + * the name of the failing component is based on the + * string returned from the fault. normal and abnormal + * are the common ones, but there is nothing to prevent + * different terminations from being used, even though + * it is very unusual + */ + CounterAction extends Schema { + counterAction extends OptionalCD; + liveness extends OptionalCD; + } + +} Added: trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/conditional/conditions/RunningFor.java =================================================================== --- trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/conditional/conditions/RunningFor.java (rev 0) +++ trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/conditional/conditions/RunningFor.java 2007-02-27 17:41:22 UTC (rev 3938) @@ -0,0 +1,54 @@ +/** (C) Copyright 2007 Hewlett-Packard Development Company, LP + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + For more information: www.smartfrog.org + + */ +package org.smartfrog.sfcore.workflow.conditional.conditions; + +import java.rmi.Remote; + +/** created 27-Feb-2007 13:48:23 */ + + +public interface RunningFor extends Remote { + + /** + * Unit of time. + * {@value} + */ + String ATTR_MILLISECONDS="milliseconds"; + /** + * Unit of time. + * {@value} + */ + String ATTR_SECONDS="seconds"; + /** + * Unit of time. + * {@value} + */ + String ATTR_MINUTES="minutes"; + /** + * Unit of time. + * {@value} + */ + String ATTR_HOURS="hours"; + /** + * Unit of time. + * {@value} + */ + String ATTR_DAYS="days"; +} Added: trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/conditional/conditions/RunningForImpl.java =================================================================== --- trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/conditional/conditions/RunningForImpl.java (rev 0) +++ trunk/core/smartfrog/src/org/smartfrog/sfcore/workflow/conditional/conditions/RunningForImpl.java 2007-02-27 17:41:22 UTC (rev 3938) @@ -0,0 +1,83 @@ +/** (C) Copyright 2007 Hewlett-Packard Development Company, LP + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + For more information: www.smartfrog.org + + */ +package org.smartfrog.sfcore.workflow.conditional.conditions; + +import org.smartfrog.sfcore.workflow.conditional.Condition; +import org.smartfrog.sfcore.prim.PrimImpl; +import org.smartfrog.sfcore.common.SmartFrogException; + +import java.rmi.RemoteException; + +/** created 27-Feb-2007 13:50:56 */ + +public class RunningForImpl extends PrimImpl implements Condition, RunningFor { + + private long startTime = -1; + private long endTime = -1; + + public RunningForImpl() throws RemoteException { + + } + + + /** + * record the current time and work out when our runningfor test will begin to pass + * + * @throws SmartFrogException failure while starting + * @throws RemoteException In case of network/rmi error + */ + public synchronized void sfStart() throws SmartFrogException, RemoteException { + super.sfStart(); + startTime = System.currentTimeMillis(); + long days, hours, minutes, seconds, milliseconds; + days = sfResolve(ATTR_DAYS, 0L, true); + hours = sfResolve(ATTR_HOURS, 0L, true); + minutes = sfResolve(ATTR_MINUTES, 0L, true); + seconds = sfResolve(ATTR_SECONDS, 0L, true); + milliseconds = sfResolve(ATTR_MILLISECONDS, 0L, true); + long delay=days; + delay=delay*24+hours; + delay=delay*60+minutes; + delay=delay*60+seconds; + delay=delay*1000+milliseconds; + endTime=startTime+delay; + + if(sfLog().isDebugEnabled()) { + if(delay>0) + sfLog().debug("RunningFor will pass after "+delay + +"milliseconds"); + else + sfLog().debug("RunningFor will always pass; delay="+delay); + } + } + + + /** + * Evaluate the condition. + * + * @return true if it is successful, false if not + * @throws RemoteException for network problems + * @throws SmartFrogException for any other problem + */ + public boolean evaluate() throws RemoteException, SmartFrogException { + long now = System.currentTimeMillis(); + return now >= endTime; + } +} Added: trunk/core/testharness/docs/testing_under_smartfrog.sxw =================================================================== (Binary files differ) Property changes on: trunk/core/testharness/docs/testing_under_smartfrog.sxw ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/core/testharness/testcases/org/smartfrog/test/unit/sfcore/utils/SmartFrogThreadTest.java =================================================================== --- trunk/core/testharness/testcases/org/smartfrog/test/unit/sfcore/utils/SmartFrogThreadTest.java (rev 0) +++ trunk/core/testharness/testcases/org/smartfrog/test/unit/sfcore/utils/SmartFrogThreadTest.java 2007-02-27 17:41:22 UTC (rev 3938) @@ -0,0 +1,92 @@ +/** (C) Copyright 2007 Hewlett-Packard Development Company, LP + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + For more information: www.smartfrog.org + + */ +package org.smartfrog.test.unit.sfcore.utils; + +import junit.framework.TestCase; +import org.smartfrog.sfcore.utils.SmartFrogThread; +import org.smartfrog.sfcore.common.SmartFrogException; + +/** + * Test that our thread code works; that exceptions are caught, etc. + * created 13-Feb-2007 11:02:46 + * */ + +public class SmartFrogThreadTest extends TestCase { + + + + /** + * Constructs a test case with the given name. + * @param name test name + */ + public SmartFrogThreadTest(String name) { + super(name); + } + + /** + * Test that an exception is caught in the run + * @throws Exception trouble + */ + public void testExceptionCaught() throws Exception { + SmartFrogThread thread=new SmartFrogThread( + new ThrowingRunnable( + new RuntimeException("test"))); + thread.run(); + assertTrue(thread.isThrown()); + try { + thread.rethrow(); + fail("expected rethrow"); + } catch (SmartFrogException e) { + Throwable cause = e.getCause(); + assertNotNull(cause); + assertTrue (cause instanceof RuntimeException); + } + } + + + /** + * A little runnable that throws an exception + */ + private static class ThrowingRunnable implements Runnable { + + private RuntimeException rte; + + + /** + * A runnable designed to throw a RuntimeException + * @param rte the exception to throw + */ + public ThrowingRunnable(RuntimeException rte) { + this.rte = rte; + } + + /** + * throw any runtime exception we have been created with + */ + public void run() { + if(rte!=null) { + throw rte; + } + } + } + + + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |