View Javadoc

1   /***
2    * Cecilia ADL Compiler
3    * Copyright (C) 2006-2007 STMicroelectronics
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   *
19   * Contact: fractal@objectweb.org
20   *
21   * Author: Matthieu Leclercq
22   */
23  
24  package org.objectweb.fractal.cecilia.adl.interfaces;
25  
26  import static org.objectweb.fractal.adl.NodeUtil.castNodeError;
27  import static org.objectweb.fractal.adl.types.TypeInterfaceUtil.isClient;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.Map;
32  
33  import org.objectweb.fractal.adl.ADLException;
34  import org.objectweb.fractal.adl.ComponentVisitor;
35  import org.objectweb.fractal.adl.Node;
36  import org.objectweb.fractal.adl.components.ComponentContainer;
37  import org.objectweb.fractal.adl.interfaces.Interface;
38  import org.objectweb.fractal.adl.interfaces.InterfaceContainer;
39  import org.objectweb.fractal.adl.types.TypeInterface;
40  import org.objectweb.fractal.api.Component;
41  import org.objectweb.fractal.api.factory.InstantiationException;
42  import org.objectweb.fractal.task.core.AbstractTaskFactoryUser;
43  import org.objectweb.fractal.task.core.TaskException;
44  
45  /**
46   * Abstract visitor component that creates a task that depends on the client
47   * interfaces of the component (if any).
48   */
49  public abstract class AbstractClientInterfaceVisitor
50      extends
51        AbstractTaskFactoryUser implements ComponentVisitor {
52  
53    /**
54     * Creates the task that is returned by this visitor.
55     * 
56     * @param container the component that is visited.
57     * @param clientInterfaces the client interfaces of the component (contains at
58     *            least one element).
59     * @return the created task.
60     * @throws InstantiationException if the instantiation of the task fails.
61     * @throws ADLException if the instantiation of the task fails.
62     */
63    protected abstract Component createTask(ComponentContainer container,
64        List<TypeInterface> clientInterfaces) throws ADLException, TaskException;
65  
66    // ---------------------------------------------------------------------------
67    // Implementation of the ComponentVisitor interface
68    // ---------------------------------------------------------------------------
69  
70    /**
71     * Visits {@link ComponentContainer} nodes and call
72     * {@link #createTask(ComponentContainer, Interface[]) createTask} if the
73     * given component contains client {@link Interface} sub-nodes.
74     */
75    public Component visit(final List<Node> path,
76        final ComponentContainer container, final Map<Object, Object> context)
77        throws ADLException, TaskException {
78      final List<TypeInterface> clientInterfaces = new ArrayList<TypeInterface>();
79      if (container instanceof InterfaceContainer) {
80        for (final Interface itf : ((InterfaceContainer) container)
81            .getInterfaces()) {
82          if (isClient(itf))
83            clientInterfaces.add(castNodeError(itf, TypeInterface.class));
84        }
85      }
86      return createTask(container, clientInterfaces);
87    }
88  
89  }