Coverage Report - argos.metadata.ServiceMetaInfo
 
Classes in this File Line Coverage Branch Coverage Complexity
ServiceMetaInfo
0%
0/134
0%
0/60
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.ByteArrayInputStream;
 34  
 import java.io.File;
 35  
 import java.io.IOException;
 36  
 import java.io.Serializable;
 37  
 import java.util.ArrayList;
 38  
 import java.util.List;
 39  
 import java.util.logging.Level;
 40  
 import java.util.logging.Logger;
 41  
 
 42  
 import javax.xml.parsers.DocumentBuilder;
 43  
 import javax.xml.parsers.DocumentBuilderFactory;
 44  
 import javax.xml.parsers.ParserConfigurationException;
 45  
 
 46  
 import org.w3c.dom.Document;
 47  
 import org.w3c.dom.Element;
 48  
 import org.w3c.dom.NodeList;
 49  
 import org.xml.sax.SAXException;
 50  
 
 51  
 import argos.Argos;
 52  
 import argos.deploy.JarClassloader;
 53  
 import argos.deploy.JarResource;
 54  
 import argos.naming.NamingService;
 55  
 import argos.util.Util;
 56  
 import argos.util.graph.Node;
 57  
 import argos.config.Config;
 58  
 
 59  
 /**
 60  
  * Created on 05.jul.2006
 61  
  * 
 62  
  * @author Dan Peder Eriksen
 63  
  */
 64  0
 public class ServiceMetaInfo extends Running implements Serializable {
 65  
         public static final long serialVersionUID = 23947239478293479L;
 66  
         
 67  0
         private static final Logger logger = Logger.getLogger(ServiceMetaInfo.class.getName());
 68  
         
 69  
         private File orginalFile;
 70  
         private File tempFile;
 71  
         private File tempDir;
 72  
         private String remoteFile;
 73  
         private double version;
 74  
         private String soapEndPoint;
 75  
         private List<File> libraries;
 76  
         private List<String> dependencies;
 77  
         private List<ComponentMetaInfo> componentMetaInfo;
 78  
         private transient JarClassloader loader;
 79  
         
 80  
         public ServiceMetaInfo(File file) throws ParserConfigurationException, IOException, SAXException, 
 81  
                         InformationMissingException {
 82  0
                 super();
 83  0
                 this.orginalFile = file;
 84  
                 
 85  0
                 componentMetaInfo = new ArrayList<ComponentMetaInfo>();
 86  0
                 dependencies = new ArrayList<String>();
 87  0
                 libraries = new ArrayList<File>();
 88  
                 
 89  
                 // Load
 90  0
                 JarResource jar = new JarResource(file);
 91  0
                 byte[] bytes = jar.getResource(Config.DEPLOY_DESCRIPTOR);
 92  0
                 if(bytes == null) {
 93  0
                         throw new InformationMissingException("Deploy descriptor (deploy.xml) is missing in " + 
 94  
                                         file.getName() + ".");
 95  
                 }
 96  0
                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 97  0
                 DocumentBuilder parser = factory.newDocumentBuilder();
 98  0
                 Document document = parser.parse(new ByteArrayInputStream(bytes));
 99  0
                 Element service = document.getDocumentElement();
 100  
                 
 101  
                 
 102  
                 // Service name
 103  0
                 name = service.getAttribute("name");
 104  0
                 if(name == null) {
 105  0
                         throw new InformationMissingException("The service name is missing");
 106  
                 }
 107  
                 
 108  
                 //Soap end point
 109  0
                 soapEndPoint = service.getAttribute("SoapEndPoint");
 110  
                 
 111  
                 //Dependencies
 112  0
                 NodeList elements = service.getElementsByTagName("depend");
 113  0
                 for(int i = 0; i < elements.getLength(); i++) {
 114  0
                         Element element = (Element) elements.item(i);
 115  0
                         Element parent = (Element) element.getParentNode();
 116  0
                         if(!service.equals(parent)) {
 117  0
                                 continue;
 118  
                         }
 119  0
                         String on = element.getAttribute("on");
 120  0
                         if(on == null) {
 121  0
                                 throw new InformationMissingException("Dependency tag is missing on attribute");
 122  
                         }
 123  0
                         dependencies.add(on);
 124  
                 }
 125  
                 
 126  
                 
 127  
                 // Service version
 128  0
                 String versionTemp = service.getAttribute("version");
 129  0
                 if(versionTemp == null) {
 130  0
                         throw new InformationMissingException("The service version is missing");
 131  
                 }
 132  
                 else {
 133  0
                         version = Double.parseDouble(versionTemp);
 134  
                 }
 135  
                 
 136  
                 // Deploy
 137  0
                 elements = service.getElementsByTagName("deploy");
 138  0
                 if(elements.getLength() != 1) {
 139  0
                         throw new InformationMissingException("There can only be ONE deploy tag");
 140  
                 }
 141  0
                 Element deploy = (Element) elements.item(0);
 142  0
                 remoteFile = deploy.getAttribute("remote");
 143  
                 
 144  0
                 NodeList components = service.getElementsByTagName("component");
 145  0
                 for(int i = 0; i < components.getLength(); i++) {
 146  0
                         componentMetaInfo.add(new ComponentMetaInfo(this, (Element) components.item(i)));
 147  
                 }
 148  0
         }
 149  
         
 150  
         public void setupDependencies(List<ServiceMetaInfo> services) throws DependencyMissingException {
 151  0
                 for(String depends : dependencies) {
 152  0
                         boolean found = false;
 153  0
                         for(ServiceMetaInfo service : services) {
 154  0
                                 if(depends.equals(service.getName())) {
 155  0
                                         thisDependsOn.add(service);
 156  0
                                         service.dependsOnThis.add(this);
 157  0
                                         found = true;
 158  0
                                         break;
 159  
                                 }
 160  
                         }
 161  0
                         if(!found) {
 162  0
                                 ServiceMetaInfo service = NamingService.getInstance().getServiceByName(depends, false);
 163  0
                                 if(service == null) {
 164  0
                                         throw new DependencyMissingException("missing dependency " + depends + ".");
 165  
                                 }
 166  
                         }
 167  0
                 }
 168  
                 
 169  
                 
 170  
                 try {
 171  0
                         for(ComponentMetaInfo meta : componentMetaInfo) {
 172  0
                                 meta.setupDependencies(services);
 173  
                         }
 174  
                 }
 175  0
                 catch(DependencyMissingException e) {
 176  0
                         for(ComponentMetaInfo meta : componentMetaInfo) {
 177  
                                 
 178  0
                                 meta.resetDependencies();
 179  
                         }
 180  0
                         throw e;
 181  0
                 }
 182  0
         }
 183  
         
 184  
         public void setClassLoader(JarClassloader loader) {
 185  0
                 this.loader = loader;
 186  0
         }
 187  
         
 188  
         public JarClassloader getClassLoader() {
 189  0
                 return loader;
 190  
         }
 191  
         
 192  
         public void addComponentMetaInfo(ComponentMetaInfo component) {
 193  0
                 componentMetaInfo.add(component);
 194  0
         }
 195  
         
 196  
         public List<ComponentMetaInfo> getComponentMetaInfo() {
 197  0
                 List<ComponentMetaInfo> list = new ArrayList<ComponentMetaInfo>();
 198  0
                 for(ComponentMetaInfo temp : componentMetaInfo) {
 199  0
                         list.add(temp);
 200  
                 }
 201  0
                 return list;
 202  
         }
 203  
         
 204  
         public List<File> getLibraries() {
 205  0
                 List<File> list = new ArrayList<File>();
 206  0
                 for(File temp : libraries) {
 207  0
                         list.add(temp);
 208  
                 }
 209  0
                 return list;
 210  
         }
 211  
         
 212  
         public boolean createTempFile() {
 213  0
                 String dir = Argos.TEMP_DIR + "/" + getName() + System.currentTimeMillis();
 214  0
                 tempDir = new File(dir);
 215  0
                 tempDir.mkdir();
 216  0
                 tempFile = new File(dir + "/" + orginalFile.getName());
 217  
                 //Copy file
 218  
                 try {
 219  0
                         Util.copyFile(orginalFile, tempFile);
 220  
                 }
 221  0
                 catch (IOException e) {
 222  0
                         logger.log(Level.SEVERE, "Unable to copy " + orginalFile.getName() + " to temp dir: " + 
 223  
                                         e.getMessage(), e);
 224  0
                         Util.cleanDir(tempDir);
 225  0
                         return false;
 226  0
                 }
 227  
                 //Libraries
 228  0
                 File libDir = new File(dir + "/lib");
 229  0
                 libDir.mkdir();
 230  
                 try {
 231  0
                         libraries = Util.extractFromJar(orginalFile, "lib", dir, false);
 232  0
                         if(libraries.isEmpty()) {
 233  0
                                 libDir.delete();
 234  
                         }
 235  
                 }
 236  0
                 catch(IOException e) {
 237  0
                         logger.log(Level.SEVERE, "Unable to extract files from " + orginalFile.getName() + 
 238  
                                         " to temp dir: " + e.getMessage(), e);
 239  0
                         Util.cleanDir(tempDir);
 240  0
                         return false;
 241  0
                 }
 242  0
                 return true;
 243  
         }
 244  
         
 245  
         public File getTempDir() {
 246  0
                 return tempDir;
 247  
         }
 248  
 
 249  
         public File getTempFile() {
 250  0
                 return tempFile;
 251  
         }
 252  
         
 253  
         public ComponentMetaInfo getComponentMetaInfo(String componentName) {
 254  0
                 for(ComponentMetaInfo comp : componentMetaInfo) {
 255  0
                         if(comp.getName().equals(componentName)) {
 256  0
                                 return comp;
 257  
                         }
 258  
                 }
 259  0
                 logger.severe("Unable to find component " + componentName);
 260  0
                 return null;
 261  
         }
 262  
         
 263  
         public List<String> getDependencies() {
 264  0
                 return dependencies;
 265  
         }
 266  
         
 267  
         public String getRemoteFile() {
 268  0
                 return remoteFile;
 269  
         }
 270  
         
 271  
         public double getVersion() {
 272  0
                 return version;
 273  
         }
 274  
         
 275  
         public File getFile() {
 276  0
                 return orginalFile;
 277  
         }
 278  
         
 279  
         public String getSoapEndPoint() {
 280  0
                 return soapEndPoint;
 281  
         }
 282  
         
 283  
         public void cleanUp() {
 284  0
                 Util.cleanDir(tempDir);
 285  0
                 tempDir.delete();
 286  0
         }
 287  
         
 288  
         public static void cleanUpTempFolder() {
 289  0
                 Util.cleanDir(new File(Argos.TEMP_DIR));
 290  0
         }
 291  
 
 292  
         @Override
 293  
         public boolean equals(Object o) {
 294  0
                 if(o instanceof ServiceMetaInfo) {
 295  0
                         ServiceMetaInfo service = (ServiceMetaInfo) o;
 296  0
                         return service.hashCode() == hashCode();
 297  
                 }
 298  0
                 return false;
 299  
         }
 300  
         
 301  
         @Override
 302  
         public int hashCode() {
 303  0
                 return name.hashCode();
 304  
         }
 305  
         
 306  
         @Override
 307  
         public String toString() {
 308  0
                 return getName();
 309  
         }
 310  
         
 311  
         public boolean dependsOn(ServiceMetaInfo service) {
 312  0
                 for(String s : dependencies) {
 313  0
                         ServiceMetaInfo temp = NamingService.getInstance().getServiceByName(s);
 314  0
                         if(temp != null) {
 315  0
                                 if(service == temp) {
 316  0
                                         return true;
 317  
                                 }
 318  
                                 else {
 319  0
                                         if(temp.dependsOn(service)) {
 320  0
                                                 return true;
 321  
                                         }
 322  
                                 }
 323  
                         }
 324  0
                 }
 325  0
                 return false;
 326  
         }
 327  
         
 328  
         @Override
 329  
         public int compareTo(Node node) {
 330  0
                 if(name.startsWith(Config.BANG_BANG) && !node.getName().startsWith(Config.BANG_BANG)) {
 331  0
                         return -1;
 332  
                 }
 333  0
                 else if(!name.startsWith(Config.BANG_BANG) && node.getName().startsWith(Config.BANG_BANG)) {
 334  0
                         return 1;
 335  
                 }
 336  
                 else {
 337  0
                         return super.compareTo(node);
 338  
                 }
 339  
         }
 340  
 }