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, Ali Erdem Ozcan
22   */
23  
24  package org.objectweb.fractal.cecilia.adl.components;
25  
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.objectweb.fractal.adl.ADLException;
30  import org.objectweb.fractal.adl.ComponentVisitor;
31  import org.objectweb.fractal.adl.Definition;
32  import org.objectweb.fractal.adl.Node;
33  import org.objectweb.fractal.adl.components.ComponentContainer;
34  import org.objectweb.fractal.api.Component;
35  import org.objectweb.fractal.cecilia.adl.InstanceNameProvider;
36  import org.objectweb.fractal.task.core.AbstractTaskFactoryUser;
37  import org.objectweb.fractal.task.core.TaskException;
38  import org.objectweb.fractal.task.core.primitive.annotations.ServerInterface;
39  import org.objectweb.fractal.task.core.primitive.annotations.ServerInterfaces;
40  import org.objectweb.fractal.task.core.primitive.annotations.TaskParameters;
41  
42  /**
43   * Creates a {@link InstanceNameProvider} task for the visited component.
44   */
45  public class InstanceVisitor extends AbstractTaskFactoryUser
46      implements
47        ComponentVisitor {
48  
49    public Component visit(final List<Node> path,
50        final ComponentContainer container, final Map<Object, Object> context)
51        throws ADLException, TaskException {
52      return taskFactoryItf.newPrimitiveTask(new InstanceNameProviderImpl(
53          getAbsoluteName(path, container)), container);
54    }
55  
56    // ---------------------------------------------------------------------------
57    // Utility methods
58    // ---------------------------------------------------------------------------
59  
60    protected String getAbsoluteName(final List<Node> path,
61        final ComponentContainer container) {
62      String realname = getComponentName(container);
63      for (int i = path.size() - 1; i >= 0; i--)
64        realname = getComponentName(path.get(i)) + "/" + realname;
65      return realname;
66    }
67  
68    protected String getComponentName(final Node container) {
69      if (container instanceof Definition)
70        return ((Definition) container).getName();
71      else if (container instanceof org.objectweb.fractal.adl.components.Component)
72        return ((org.objectweb.fractal.adl.components.Component) container)
73            .getName();
74      else
75        throw new IllegalArgumentException(
76            "The given node is neither a Definition nor a Component");
77    }
78  
79    // ---------------------------------------------------------------------------
80    // Task classes
81    // ---------------------------------------------------------------------------
82  
83    /**
84     * Provides the name of the given component.
85     */
86    @TaskParameters("componentNode")
87    @ServerInterfaces(@ServerInterface(name = "instance-name-provider", signature = InstanceNameProvider.class, record = "role:instanceNameProvider, id:%", parameters = "componentNode"))
88    public static class InstanceNameProviderImpl implements InstanceNameProvider {
89  
90      protected final String instanceName;
91      protected final String cInstanceName;
92  
93      // -------------------------------------------------------------------------
94      // Constructor
95      // -------------------------------------------------------------------------
96  
97      /**
98       * @param instanceName the name provided by this task.
99       */
100     public InstanceNameProviderImpl(final String instanceName) {
101       this.instanceName = instanceName;
102       cInstanceName = instanceName.replace('.', '_').replace('-', '_').replace(
103           '/', '_');
104     }
105 
106     // -------------------------------------------------------------------------
107     // Implementation of the InstanceNameProvider interface
108     // -------------------------------------------------------------------------
109 
110     public String getInstanceName() {
111       return instanceName;
112     }
113 
114     public String getCInstanceName() {
115       return cInstanceName;
116     }
117   }
118 }