1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
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
81
82
83
84
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
95
96
97
98
99
100 public InstanceNameProviderImpl(final String instanceName) {
101 this.instanceName = instanceName;
102 cInstanceName = instanceName.replace('.', '_').replace('-', '_').replace(
103 '/', '_');
104 }
105
106
107
108
109
110 public String getInstanceName() {
111 return instanceName;
112 }
113
114 public String getCInstanceName() {
115 return cInstanceName;
116 }
117 }
118 }