View Javadoc

1   /*
2   Copyright (c) 2006, University of Tromsø
3   All rights reserved.
4   
5   Redistribution and use in source and binary forms, with or without 
6   modification, are permitted provided that the following conditions are met:
7   
8    * Redistributions of source code must retain the above copyright notice, this list 
9      of conditions and the following disclaimer.
10  
11   * Redistributions in binary form must reproduce the above copyright notice, this 
12     list of conditions and the following disclaimer in the documentation and/or other 
13     materials provided with the distribution.
14  
15   * Neither the name of the University of Tromsø nor the names of its contributors may 
16     be used to endorse or promote products derived from this software without specific 
17     prior written permission.
18  
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
20  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
21  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
22  SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
23  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
24  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
25  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
26  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
27  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
28  DAMAGE.
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   * Created on 05.jul.2006
48   * 
49   * @author Dan Peder Eriksen
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  		// Component name
77  		name = component.getAttribute("name");
78  		if(name == null) {
79  			throw new InformationMissingException("The component name is missing");
80  		}
81  		
82  		// Component class
83  		className = component.getAttribute("class");
84  		if(className == null) {
85  			throw new InformationMissingException("The component class is missing");
86  		}
87  		
88  		// Attribute values
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 			//override from config
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 				//Config.getInstance().addProperty(key, value);
115 			}
116 			else if(!"".equals(value2)) {
117 				att.setValue(value2);
118 			}
119 		}
120 		
121 		// Listeners
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 		// Dependencies
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 }