View Javadoc

1   /*
2    Copyright (C) 2007 Grid Systems, S.A.
3   
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8   
9    This library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   Lesser General Public License for more details.
13  
14   You should have received a copy of the GNU Lesser General Public
15   License along with this library; if not, write to the Free Software
16   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
17  */
18  package com.gridsystems.nextgrid.api.pom;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  import nextgrid.api.pom.Process;
24  import nextgrid.api.pom.ProcessException;
25  
26  /**
27   * Runnable for process control.
28   */
29  class EnactionWorker extends Thread {
30  
31    /**
32     * Enaction logger.
33     */
34    protected static final Log ENACTOR_LOG = LogFactory.getLog("ENACTOR");
35  
36    /**
37     * <code>processContext</code> attribute.
38     */
39    private final ProcessContext ctx;
40  
41    /** The instance to execute. */
42    private final Enactable enactable;
43  
44    /** Exception instance, if the execution failed. */
45    private Throwable error;
46  
47    /**
48     * Creates a new instance.
49     *
50     * @param ctx Context for execution
51     * @param e   The instance to execute
52     */
53    public EnactionWorker(ProcessContext ctx, Enactable e) {
54      super("Process-Id:" + (e.getId() == null ? "null" : e.getId().toString()));
55      this.ctx = ctx;
56      this.enactable = e;
57    }
58  
59    /**
60     * {@inheritDoc}
61     */
62    @Override public void run() {
63      String name;
64      if (enactable instanceof Process) {
65        name = ((Process)enactable).getName();
66      } else {
67        name = this.getName();
68      }
69  
70      ENACTOR_LOG.debug("Process '" + name + "': started");
71      try {
72        enactable.run(this.ctx);
73  
74        if (ctx.isRunning()) {
75          ENACTOR_LOG.debug("Process '" + name + "': finished");
76        }
77      } catch (ProcessException e) {
78        ENACTOR_LOG.warn("Process '" + name + "': error", e);
79        error = e;
80      } catch (Throwable t) {
81        ENACTOR_LOG.error("Process '" + name + "': unexpected error", t);
82        error =  t;
83      }
84    }
85  
86    /**
87     * If an error occurred during the enaction, it gets the exception instance.
88     *
89     * @throws ProcessException If an error occurred during the enaction
90     */
91    public void checkError() throws ProcessException {
92      if (error instanceof ProcessException) {
93        throw (ProcessException)error;
94      } else if (error instanceof RuntimeException) {
95        throw (RuntimeException)error;
96      } else if (error instanceof Error) {
97        throw (Error)error;
98      } else if (error != null) {
99        throw new ProcessException(error);
100     }
101   }
102 }