Coverage Report - org.webslinger.modules.ModuleStateXML
 
Classes in this File Line Coverage Branch Coverage Complexity
ModuleStateXML
20%
31/157
22%
8/37
0
 
 1  
 package org.webslinger.modules;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.IOException;
 5  
 import java.net.URL;
 6  
 import java.util.HashMap;
 7  
 import java.util.Iterator;
 8  
 import java.util.logging.Logger;
 9  
 import javax.xml.parsers.ParserConfigurationException;
 10  
 import org.w3c.dom.Document;
 11  
 import org.w3c.dom.Element;
 12  
 import org.w3c.dom.Node;
 13  
 import org.xml.sax.SAXException;
 14  
 
 15  
 import org.apache.commons.vfs.FileObject;
 16  
 
 17  
 import org.webslinger.xml.XmlUtil;
 18  
 
 19  
 public final class ModuleStateXML implements ModuleStorage {
 20  1
     private static final Logger logger = Logger.getLogger(ModuleStateXML.class.getName());
 21  
     protected final FileObject file;
 22  
     protected Document moduleStateXml;
 23  
 
 24  21
     public ModuleStateXML(FileObject file) throws IOException {
 25  21
         this.file = file;
 26  21
     }
 27  
 
 28  
     public void init(ModuleState moduleState) throws IOException {
 29  
         try {
 30  21
             if (file.exists()) {
 31  19
                 boolean needsSave = false;
 32  19
                 moduleStateXml = XmlUtil.read(file.getContent().getInputStream());
 33  19
                 Element element = moduleStateXml.getDocumentElement();
 34  19
                 Iterator<Element> it = XmlUtil.getElementsNamed(element, "module").iterator();
 35  74
                 while (it.hasNext()) {
 36  55
                     Element moduleElement = it.next();
 37  55
                     String moduleName = moduleElement.getAttribute("name");
 38  55
                     moduleState.addModule(moduleName, moduleElement.getAttribute("url"));
 39  55
                     Iterator<Element> it2 = XmlUtil.getElementsNamed(moduleElement, "alias").iterator();
 40  55
                     while (it2.hasNext()) {
 41  0
                         Element aliasElement = it2.next();
 42  0
                         moduleElement.removeChild(aliasElement);
 43  0
                         needsSave = true;
 44  0
                     }
 45  55
                     it2 = XmlUtil.getElementsNamed(moduleElement, "mount").iterator();
 46  55
                     if (it2.hasNext()) {
 47  0
                         while (it2.hasNext()) {
 48  0
                             Element mountElement = it2.next();
 49  0
                             moduleState.addModuleMount(moduleName, mountElement.getAttribute("src"), mountElement.getAttribute("dest"));
 50  0
                         }
 51  
                     } else {
 52  55
                         moduleState.addModuleMount(moduleName, "", "");
 53  
                     }
 54  55
                 }
 55  19
                 it = XmlUtil.getElementsNamed(element, "library").iterator();
 56  19
                 while (it.hasNext()) {
 57  0
                     Element libraryElement = it.next();
 58  0
                     String libraryName = libraryElement.getAttribute("name");
 59  0
                     moduleState.addLibrary(libraryName, libraryElement.getAttribute("url"));
 60  0
                 }
 61  19
                 it = XmlUtil.getElementsNamed(element, "module").iterator();
 62  74
                 while (it.hasNext()) {
 63  55
                     Element moduleElement = it.next();
 64  55
                     if ("true".equals(moduleElement.getAttribute("enabled"))) moduleState.enableModule(moduleElement.getAttribute("name"));
 65  55
                 }
 66  19
                 if (needsSave) saveModuleState();
 67  19
             } else {
 68  2
                 moduleStateXml = XmlUtil.newDocument();
 69  
             }
 70  0
         } catch (ParserConfigurationException e) {
 71  0
             throw (IOException) new IOException(e.getMessage()).initCause(e);
 72  0
         } catch (SAXException e) {
 73  0
             throw (IOException) new IOException(e.getMessage()).initCause(e);
 74  21
         }
 75  21
     }
 76  
 
 77  
     protected Element findElement(String elementName, String moduleName) throws IOException {
 78  0
         Node ptr = moduleStateXml.getDocumentElement().getFirstChild();
 79  0
         while (ptr != null) {
 80  0
             if (ptr instanceof Element && ptr.getNodeName().equals(elementName)) {
 81  0
                 if (moduleName.equals(((Element) ptr).getAttribute("name"))) return (Element) ptr;
 82  
             }
 83  0
             ptr = ptr.getNextSibling();
 84  
         }
 85  0
         return null;
 86  
     }
 87  
 
 88  
     protected Element findLibraryElement(String moduleName) throws IOException {
 89  0
         return findElement("module", moduleName);
 90  
     }
 91  
 
 92  
     protected Element findModuleElement(String moduleName) throws IOException {
 93  0
         return findElement("module", moduleName);
 94  
     }
 95  
 
 96  
     protected Element findElementWithAttribute(String elementName, String attrName, String value) throws IOException {
 97  0
         Node modulePtr = moduleStateXml.getDocumentElement().getFirstChild();
 98  0
         while (modulePtr != null) {
 99  0
             if (modulePtr instanceof Element && modulePtr.getNodeName().equals(elementName)) {
 100  0
                 if (value.equals(((Element) modulePtr).getAttribute(attrName))) return (Element) modulePtr;
 101  
             }
 102  0
             modulePtr = modulePtr.getNextSibling();
 103  
         }
 104  0
         return null;
 105  
     }
 106  
 
 107  
     protected Element findLibraryElementWithAttribute(String attrName, String value) throws IOException {
 108  0
         return findElementWithAttribute("library", attrName, value);
 109  
     }
 110  
 
 111  
     protected Element findModuleElementWithAttribute(String attrName, String value) throws IOException {
 112  0
         return findElementWithAttribute("module", attrName, value);
 113  
     }
 114  
 
 115  
     protected void saveModuleState() throws IOException {
 116  
         try {
 117  0
             XmlUtil.write(moduleStateXml, file.getContent().getOutputStream());
 118  0
         } catch (RuntimeException e) {
 119  0
             throw e;
 120  0
         } catch (Exception e) {
 121  0
             throw (IOException) new IOException(e.getMessage()).initCause(e);
 122  0
         }
 123  0
     }
 124  
 
 125  
     public boolean addLibrary(String name, File library) throws IOException {
 126  0
         return addLibrary(name, file.getFileSystem().getFileSystemManager().toFileObject(library));
 127  
     }
 128  
 
 129  
     public boolean addLibrary(String name, String path) throws IOException {
 130  0
         return addLibrary(name, file.getFileSystem().getFileSystemManager().resolveFile(path));
 131  
     }
 132  
 
 133  
     public boolean addModule(String name, File module) throws IOException {
 134  0
         return addModule(name, file.getFileSystem().getFileSystemManager().toFileObject(module));
 135  
     }
 136  
 
 137  
     public boolean addModule(String name, String path) throws IOException {
 138  0
         return addModule(name, file.getFileSystem().getFileSystemManager().resolveFile(path));
 139  
     }
 140  
 
 141  
     protected FileObject makeLocalFile(FileObject file) throws IOException {
 142  0
         return file;
 143  
     }
 144  
 
 145  
     protected void deleteLocalFile(String url) throws IOException {
 146  0
     }
 147  
 
 148  
     public boolean addLibrary(String name, FileObject file) throws IOException {
 149  0
         logger.info("addLibrary(" + name + ", " + file + ")");
 150  0
         Element libraryElement = findLibraryElement(name);
 151  0
         String url = file.getURL().toExternalForm();
 152  0
         if (libraryElement != null) {
 153  0
             if (url.equals(libraryElement.getAttribute("url"))) return false;
 154  0
             throw new IOException("library already exists");
 155  
         }
 156  0
         libraryElement = moduleStateXml.createElement("library");
 157  0
         libraryElement.setAttribute("name", name);
 158  0
         libraryElement.setAttribute("url", url);
 159  0
         moduleStateXml.getDocumentElement().appendChild(libraryElement);
 160  0
         saveModuleState();
 161  0
         return true;
 162  
     }
 163  
 
 164  
     public boolean addModule(String name, FileObject file) throws IOException {
 165  0
         logger.info("addModule(" + name + ", " + file + ")");
 166  0
         Element moduleElement = findModuleElement(name);
 167  0
         String url = file.getURL().toExternalForm();
 168  0
         if (moduleElement != null) {
 169  0
             if (url.equals(moduleElement.getAttribute("url"))) return false;
 170  0
             throw new IOException("module already exists");
 171  
         }
 172  0
         moduleElement = moduleStateXml.createElement("module");
 173  0
         moduleElement.setAttribute("name", name);
 174  0
         moduleElement.setAttribute("url", url);
 175  0
         moduleStateXml.getDocumentElement().appendChild(moduleElement);
 176  0
         saveModuleState();
 177  0
         return true;
 178  
     }
 179  
 
 180  
     public boolean removeLibrary(String name) throws IOException {
 181  0
         Element libraryElement = findLibraryElement(name);
 182  0
         if (libraryElement == null) return false;
 183  0
         moduleStateXml.removeChild(libraryElement);
 184  0
         String url = libraryElement.getAttribute("url");
 185  0
         if (findLibraryElementWithAttribute("url", url) == null) {
 186  
             // no other modules reference this url, remove it from the cache
 187  0
             deleteLocalFile(url);
 188  
         }
 189  0
         saveModuleState();
 190  0
         return true;
 191  
     }
 192  
 
 193  
     public boolean removeModule(String name) throws IOException {
 194  0
         if (!disableModule(name)) return false;
 195  0
         Element moduleElement = findModuleElement(name);
 196  0
         if (moduleElement == null) return false;
 197  0
         moduleStateXml.removeChild(moduleElement);
 198  0
         String url = moduleElement.getAttribute("url");
 199  0
         if (findModuleElementWithAttribute("url", url) == null) {
 200  
             // no other modules reference this url, remove it from the cache
 201  0
             deleteLocalFile(url);
 202  
         }
 203  0
         saveModuleState();
 204  0
         return true;
 205  
     }
 206  
 
 207  
     public boolean enableModule(String name) throws IOException {
 208  0
         Element moduleElement = findModuleElement(name);
 209  0
         if (moduleElement == null) throw new IOException("no module found with name(" + name + ")");
 210  0
         if ("true".equals(moduleElement.getAttribute("enabled"))) return false;
 211  0
         moduleElement.setAttribute("enabled", "true");
 212  0
         saveModuleState();
 213  0
         return true;
 214  
     }
 215  
 
 216  
     public boolean disableModule(String name) throws IOException {
 217  0
         Element moduleElement = findModuleElement(name);
 218  0
         if (moduleElement == null) throw new IOException("no module found with name(" + name + ")");
 219  0
         if ("false".equals(moduleElement.getAttribute("enabled"))) return false;
 220  0
         moduleElement.removeAttribute("enabled");
 221  0
         saveModuleState();
 222  0
         return true;
 223  
     }
 224  
 
 225  
     public boolean addModuleMount(String name, String src, String dest) throws IOException {
 226  0
         Element moduleElement = findModuleElement(name);
 227  0
         if (moduleElement == null) throw new IOException("no module found with name(" + name + ")");
 228  0
         Node modulePtr = moduleElement.getFirstChild();
 229  0
         while (modulePtr != null) {
 230  0
             if (modulePtr instanceof Element && modulePtr.getNodeName().equals("mount")) {
 231  0
                 Element mountElement = (Element) modulePtr;
 232  0
                 if (dest.equals(mountElement.getAttribute("dest"))) {
 233  0
                     if (src.equals(mountElement.getAttribute("src"))) throw new IOException("Something already mounted on (" + dest + ") for module (" + name + ")");
 234  0
                     return false;
 235  
                 }
 236  
             }
 237  0
             modulePtr = modulePtr.getNextSibling();
 238  
         }
 239  0
         Element mountElement = moduleStateXml.createElement("mount");
 240  0
         mountElement.setAttribute("src", src);
 241  0
         mountElement.setAttribute("dest", dest);
 242  0
         moduleElement.appendChild(mountElement);
 243  0
         saveModuleState();
 244  0
         return true;
 245  
     }
 246  
 
 247  
     public boolean removeModuleMount(String name, String dest) throws IOException {
 248  0
         Element moduleElement = findModuleElement(name);
 249  0
         if (moduleElement == null) throw new IOException("no module found with name(" + name + ")");
 250  0
         Node modulePtr = moduleElement.getFirstChild();
 251  0
         while (modulePtr != null) {
 252  0
             if (modulePtr instanceof Element && modulePtr.getNodeName().equals("mount")) {
 253  0
                 Element mountElement = (Element) modulePtr;
 254  0
                 if (dest.equals(mountElement.getAttribute("dest"))) {
 255  0
                     moduleElement.removeChild(mountElement);
 256  0
                     saveModuleState();
 257  0
                     return true;
 258  
                 }
 259  
             }
 260  0
             modulePtr = modulePtr.getNextSibling();
 261  
         }
 262  0
         return false;
 263  
     }
 264  
 }