View Javadoc

1   /*
2    Copyright (C) 2006 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 java.net.URI;
21  import java.util.PriorityQueue;
22  
23  import nextgrid.api.env.ProcessEnvironment;
24  import nextgrid.api.pom.Grounding;
25  import nextgrid.api.pom.GroundingFactory;
26  import nextgrid.api.pom.GroundingFactoryFinder;
27  import nextgrid.api.pom.Process;
28  import nextgrid.api.pom.ProcessException;
29  import nextgrid.api.pom.Service;
30  
31  /**
32   * Services are concrete processes, bound to a specific grounding.
33   *
34   * @author Rodrigo Ruiz
35   */
36  public final class ServiceImpl extends ProcessImpl implements Service {
37  
38    /**
39     * <code>serialVersionUID</code> attribute.
40     */
41    private static final long serialVersionUID = 3531778252650157690L;
42  
43    /**
44     * The grounding instance.
45     */
46    private Grounding grounding;
47  
48    /**
49     * Flag that controls how the grounding was set.
50     * <p>
51     * Used in reset to determine if the current grounding must
52     * be removed or not.
53     */
54    private boolean discovered = true;
55  
56    /**
57     * Creates a new instance.
58     */
59    public ServiceImpl() {
60      super();
61    }
62  
63    /**
64     * {@inheritDoc}
65     */
66    public Process findProcessById(URI id) {
67      if (id != null && this.getId().equals(id)) {
68        return this;
69      } else {
70        return null;
71      }
72    }
73  
74    // -------------------------------------------------------------
75    // Enaction Management
76    // -------------------------------------------------------------
77  
78    /**
79     * {@inheritDoc}
80     */
81    public void prioritise(ProcessEnvironment env, PriorityQueue<Process> queue) {
82      // Services do nothing on prioritisation!
83    }
84  
85    /**
86     * {@inheritDoc}
87     */
88    public void discover(ProcessEnvironment env) {
89      // Services do nothing on discovery!
90    }
91  
92    /**
93     * {@inheritDoc}
94     */
95    @Override
96    protected void doValidate(ValidationType type) throws ProcessException {
97      if (type == ValidationType.BEFORE_CHILDREN) {
98        getGrounding().validate();
99      }
100   }
101 
102   /**
103    * {@inheritDoc}
104    */
105   public Grounding getGrounding() {
106     if (this.grounding == null) {
107       // Try to instantiate the grounding from the service attributes
108       String type = this.getAttribute(Grounding.ATTR_GROUNDING_TYPE);
109       GroundingFactory gf = GroundingFactoryFinder.findFactory(type);
110       if (gf != null) {
111         this.grounding = gf.createGrounding();
112       }
113     }
114 
115     return this.grounding;
116   }
117 
118   /**
119    * {@inheritDoc}
120    */
121   public void setGrounding(Grounding g) {
122     if (g == null) {
123       this.grounding = null;
124       this.discovered = true;
125     } else {
126       this.grounding = g;
127       this.discovered = false;
128     }
129   }
130 
131   /**
132    * {@inheritDoc}
133    */
134   @Override
135   public void doEvaluate(ProcessEnvironment env) throws ProcessException {
136     // No actions needed to evaluate a Service
137   }
138 
139   /**
140    * {@inheritDoc}
141    */
142   public void run(ProcessContext ctx) throws ProcessException, InterruptedException {
143     try {
144       Grounding g = this.getGrounding();
145       if (g == null) {
146         throw new ProcessException("Grounding not found for service " + this.getId());
147       } else {
148         waitForInputs();
149         ENACTOR_LOG.debug("Service '" + this.getName() + "': grounding invoked");
150         fireProcessStarted();
151         g.invoke(ctx, this);
152         fireProcessFinished();
153       }
154     } catch (ProcessException e) {
155       fireProcessFailed(e);
156       throw e;
157     } catch (RuntimeException e) {
158       throw e;
159     }
160   }
161 
162   /**
163    * {@inheritDoc}
164    */
165   @Override protected void doReset() {
166     if (discovered) {
167       this.grounding = null;
168     }
169   }
170 
171   /**
172    * {@inheritDoc}
173    */
174   @Override protected void resetChildren() {
175     // Nothing to be done here
176   }
177 
178   /**
179    * {@inheritDoc}
180    */
181   @Override public String toString() {
182     return "Service#" + this.getId();
183   }
184 
185 }