Coverage Report - argos.metadata.ComponentMetaInfo
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentMetaInfo
0%
0/98
0%
0/54
0
 
 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  0
                 super();
 64  0
                 this.service = service;
 65  0
                 dependencies = new ArrayList<String>();
 66  0
                 attributeValues = new ArrayList<AttributeValue>();
 67  0
         }
 68  
         
 69  
         public ComponentMetaInfo(ServiceMetaInfo service, Element component)
 70  
                         throws InformationMissingException {
 71  0
                 this(service);
 72  
                 
 73  0
                 listenTo = new ArrayList<String>();
 74  0
                 listenToRemote = new ArrayList<String>();
 75  
                 
 76  
                 // Component name
 77  0
                 name = component.getAttribute("name");
 78  0
                 if(name == null) {
 79  0
                         throw new InformationMissingException("The component name is missing");
 80  
                 }
 81  
                 
 82  
                 // Component class
 83  0
                 className = component.getAttribute("class");
 84  0
                 if(className == null) {
 85  0
                         throw new InformationMissingException("The component class is missing");
 86  
                 }
 87  
                 
 88  
                 // Attribute values
 89  0
                 NodeList elements = component.getElementsByTagName("attribute");
 90  0
                 for(int i = 0; i < elements.getLength(); i++) {
 91  0
                         Element element = (Element) elements.item(i);
 92  0
                         String nodeName = element.getAttribute("name");
 93  0
                         String value = element.getAttribute("value");
 94  0
                         if(nodeName == null) {
 95  0
                                 throw new InformationMissingException("Attribute tag is missing name"
 96  
                                                 + " attribute");
 97  
                         }
 98  0
                         if(value == null) {
 99  0
                                 throw new InformationMissingException("Attribute tag is missing "
 100  
                                                 + "value attribute");
 101  
                         }
 102  0
                         AttributeValue att = new AttributeValue(nodeName, value);
 103  0
                         attributeValues.add(att);
 104  
                         //override from config
 105  
                         String key;
 106  0
                         if(this.name.startsWith(Config.BANG_BANG)) {
 107  0
                                 key = "BangBang." + this.name.substring(2) + "." + nodeName;
 108  
                         }
 109  
                         else {
 110  0
                                  key = this.name + "." + nodeName;
 111  
                         }
 112  0
                         String value2 = Config.getInstance().lookup(key, false);
 113  0
                         if(value2 == null) {
 114  
                                 //Config.getInstance().addProperty(key, value);
 115  
                         }
 116  0
                         else if(!"".equals(value2)) {
 117  0
                                 att.setValue(value2);
 118  
                         }
 119  
                 }
 120  
                 
 121  
                 // Listeners
 122  0
                 elements = component.getElementsByTagName("listen");
 123  0
                 for(int i = 0; i < elements.getLength(); i++) {
 124  0
                         Element element = (Element) elements.item(i);
 125  0
                         String to = element.getAttribute("to");
 126  0
                         String url = element.getAttribute("url");
 127  0
                         if(to == null) {
 128  0
                                 throw new InformationMissingException("Listen tag is missing to or url and component"
 129  
                                                 + "attribute");
 130  
                         }
 131  0
                         if(url == null || url.equals("")) {
 132  0
                                 listenTo.add(to);
 133  
                                 
 134  0
                                 if(!to.startsWith(Config.BANG_BANG) && !dependencies.contains(to)) {
 135  0
                                         dependencies.add(to);
 136  
                                 }
 137  
                         }
 138  
                         else {
 139  0
                                 listenToRemote.add(url);
 140  0
                                 listenToRemote.add("Components:name=" + to);
 141  
                         }
 142  
                 }
 143  
                 
 144  
                 // Dependencies
 145  0
                 elements = component.getElementsByTagName("depend");
 146  0
                 for(int i = 0; i < elements.getLength(); i++) {
 147  0
                         Element element = (Element) elements.item(i);
 148  0
                         String on = element.getAttribute("on");
 149  0
                         if(on == null) {
 150  0
                                 throw new InformationMissingException("Dependency tag is missing on attribute");
 151  
                         }
 152  0
                         dependencies.add(on);
 153  
                 }
 154  0
         }
 155  
         
 156  
         public void setupDependencies(List<ServiceMetaInfo> services) throws DependencyMissingException {
 157  0
                 for(String tempName : dependencies) {
 158  0
                         boolean found = false;
 159  0
                         for(ServiceMetaInfo temp : services) {
 160  0
                                 for(ComponentMetaInfo meta : temp.getComponentMetaInfo()) {
 161  0
                                         if(meta.getName().equals(tempName)) {
 162  0
                                                 thisDependsOn.add(meta);
 163  0
                                                 meta.dependsOnThis.add(this);
 164  0
                                                 found = true;
 165  0
                                                 break;
 166  
                                         }
 167  
                                 }
 168  
                         }
 169  0
                         if(!found) {
 170  0
                                 ComponentMetaInfo meta = NamingService.getInstance().getComponentMetaByName(name, false);
 171  0
                                 if(meta == null) {
 172  0
                                         throw new DependencyMissingException("missing dependency " + name + ".");
 173  
                                 }
 174  
                         }
 175  0
                 }
 176  0
         }
 177  
         
 178  
         public String getClassName() {
 179  0
                 return className;
 180  
         }
 181  
         
 182  
         public List<AttributeValue> getAttributeValues() {
 183  0
                 List<AttributeValue> list = new ArrayList<AttributeValue>();
 184  0
                 for(AttributeValue temp : attributeValues) {
 185  0
                         list.add(temp);
 186  
                 }
 187  0
                 return list;
 188  
         }
 189  
         
 190  
         public void addAttributeValue(AttributeValue value) {
 191  0
                 attributeValues.add(value);
 192  0
         }
 193  
         
 194  
         public void addListenTo(String component) {
 195  0
                 listenTo.add(component);
 196  0
         }
 197  
         
 198  
         public List<String> getListenTo() {
 199  0
                 List<String> list = new ArrayList<String>();
 200  0
                 for(String temp : listenTo) {
 201  0
                         list.add(temp);
 202  
                 }
 203  0
                 return list;
 204  
         }
 205  
         
 206  
         public List<String> getListenToRemote() {
 207  0
                 List<String> list = new ArrayList<String>();
 208  0
                 for(String temp : listenToRemote) {
 209  0
                         list.add(temp);
 210  
                 }
 211  0
                 return list;
 212  
         }
 213  
         
 214  
         public ServiceMetaInfo getService() {
 215  0
                 return service;
 216  
         }
 217  
         
 218  
         public NotificationListener getListenerProxy() {
 219  0
                 return listenerProxy;
 220  
         }
 221  
 
 222  
         public void setListenerProxy(NotificationListener listenerProxy) {
 223  0
                 this.listenerProxy = listenerProxy;
 224  0
         }
 225  
 
 226  
         public String getMbeanName() {
 227  0
                 return mbeanName;
 228  
         }
 229  
 
 230  
         public void setMbeanName(String mbeanName) {
 231  0
                 this.mbeanName = mbeanName;
 232  0
         }
 233  
 
 234  
         @Override
 235  
         public String toString() {
 236  0
                 return getName();
 237  
         }
 238  
         
 239  
         @Override
 240  
         public boolean equals(Object o) {
 241  0
                 if(o instanceof ComponentMetaInfo) {
 242  0
                         ComponentMetaInfo comp = (ComponentMetaInfo) o;
 243  0
                         return comp.hashCode() == hashCode();
 244  
                 }
 245  0
                 return false;
 246  
         }
 247  
         
 248  
         @Override
 249  
         public int hashCode() {
 250  0
                 return (service.getName() + name).hashCode();
 251  
         }
 252  
 }