1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package argos.metadata;
32
33 import java.io.Serializable;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import javax.management.NotificationListener;
38
39 import org.w3c.dom.Element;
40 import org.w3c.dom.NodeList;
41
42 import argos.config.Config;
43 import argos.naming.NamingService;
44
45
46
47
48
49
50
51 public class ComponentMetaInfo extends Running implements Serializable {
52 public static final long serialVersionUID = 2807813948753460L;
53 private String className;
54 private NotificationListener listenerProxy;
55 private String mbeanName;
56 private ServiceMetaInfo service;
57 private List<AttributeValue> attributeValues;
58 private List<String> dependencies;
59 private List<String> listenTo;
60 private List<String> listenToRemote;
61
62 protected ComponentMetaInfo(ServiceMetaInfo service) {
63 super();
64 this.service = service;
65 dependencies = new ArrayList<String>();
66 attributeValues = new ArrayList<AttributeValue>();
67 }
68
69 public ComponentMetaInfo(ServiceMetaInfo service, Element component)
70 throws InformationMissingException {
71 this(service);
72
73 listenTo = new ArrayList<String>();
74 listenToRemote = new ArrayList<String>();
75
76
77 name = component.getAttribute("name");
78 if(name == null) {
79 throw new InformationMissingException("The component name is missing");
80 }
81
82
83 className = component.getAttribute("class");
84 if(className == null) {
85 throw new InformationMissingException("The component class is missing");
86 }
87
88
89 NodeList elements = component.getElementsByTagName("attribute");
90 for(int i = 0; i < elements.getLength(); i++) {
91 Element element = (Element) elements.item(i);
92 String nodeName = element.getAttribute("name");
93 String value = element.getAttribute("value");
94 if(nodeName == null) {
95 throw new InformationMissingException("Attribute tag is missing name"
96 + " attribute");
97 }
98 if(value == null) {
99 throw new InformationMissingException("Attribute tag is missing "
100 + "value attribute");
101 }
102 AttributeValue att = new AttributeValue(nodeName, value);
103 attributeValues.add(att);
104
105 String key;
106 if(this.name.startsWith(Config.BANG_BANG)) {
107 key = "BangBang." + this.name.substring(2) + "." + nodeName;
108 }
109 else {
110 key = this.name + "." + nodeName;
111 }
112 String value2 = Config.getInstance().lookup(key, false);
113 if(value2 == null) {
114
115 }
116 else if(!"".equals(value2)) {
117 att.setValue(value2);
118 }
119 }
120
121
122 elements = component.getElementsByTagName("listen");
123 for(int i = 0; i < elements.getLength(); i++) {
124 Element element = (Element) elements.item(i);
125 String to = element.getAttribute("to");
126 String url = element.getAttribute("url");
127 if(to == null) {
128 throw new InformationMissingException("Listen tag is missing to or url and component"
129 + "attribute");
130 }
131 if(url == null || url.equals("")) {
132 listenTo.add(to);
133
134 if(!to.startsWith(Config.BANG_BANG) && !dependencies.contains(to)) {
135 dependencies.add(to);
136 }
137 }
138 else {
139 listenToRemote.add(url);
140 listenToRemote.add("Components:name=" + to);
141 }
142 }
143
144
145 elements = component.getElementsByTagName("depend");
146 for(int i = 0; i < elements.getLength(); i++) {
147 Element element = (Element) elements.item(i);
148 String on = element.getAttribute("on");
149 if(on == null) {
150 throw new InformationMissingException("Dependency tag is missing on attribute");
151 }
152 dependencies.add(on);
153 }
154 }
155
156 public void setupDependencies(List<ServiceMetaInfo> services) throws DependencyMissingException {
157 for(String tempName : dependencies) {
158 boolean found = false;
159 for(ServiceMetaInfo temp : services) {
160 for(ComponentMetaInfo meta : temp.getComponentMetaInfo()) {
161 if(meta.getName().equals(tempName)) {
162 thisDependsOn.add(meta);
163 meta.dependsOnThis.add(this);
164 found = true;
165 break;
166 }
167 }
168 }
169 if(!found) {
170 ComponentMetaInfo meta = NamingService.getInstance().getComponentMetaByName(name, false);
171 if(meta == null) {
172 throw new DependencyMissingException("missing dependency " + name + ".");
173 }
174 }
175 }
176 }
177
178 public String getClassName() {
179 return className;
180 }
181
182 public List<AttributeValue> getAttributeValues() {
183 List<AttributeValue> list = new ArrayList<AttributeValue>();
184 for(AttributeValue temp : attributeValues) {
185 list.add(temp);
186 }
187 return list;
188 }
189
190 public void addAttributeValue(AttributeValue value) {
191 attributeValues.add(value);
192 }
193
194 public void addListenTo(String component) {
195 listenTo.add(component);
196 }
197
198 public List<String> getListenTo() {
199 List<String> list = new ArrayList<String>();
200 for(String temp : listenTo) {
201 list.add(temp);
202 }
203 return list;
204 }
205
206 public List<String> getListenToRemote() {
207 List<String> list = new ArrayList<String>();
208 for(String temp : listenToRemote) {
209 list.add(temp);
210 }
211 return list;
212 }
213
214 public ServiceMetaInfo getService() {
215 return service;
216 }
217
218 public NotificationListener getListenerProxy() {
219 return listenerProxy;
220 }
221
222 public void setListenerProxy(NotificationListener listenerProxy) {
223 this.listenerProxy = listenerProxy;
224 }
225
226 public String getMbeanName() {
227 return mbeanName;
228 }
229
230 public void setMbeanName(String mbeanName) {
231 this.mbeanName = mbeanName;
232 }
233
234 @Override
235 public String toString() {
236 return getName();
237 }
238
239 @Override
240 public boolean equals(Object o) {
241 if(o instanceof ComponentMetaInfo) {
242 ComponentMetaInfo comp = (ComponentMetaInfo) o;
243 return comp.hashCode() == hashCode();
244 }
245 return false;
246 }
247
248 @Override
249 public int hashCode() {
250 return (service.getName() + name).hashCode();
251 }
252 }