Coverage Report - org.webslinger.modules.StandardModuleState
 
Classes in this File Line Coverage Branch Coverage Complexity
StandardModuleState
71%
48/68
100%
5/5
0
 
 1  
 package org.webslinger.modules;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.IOException;
 5  
 import java.io.PrintStream;
 6  
 import java.util.Collection;
 7  
 import java.util.Enumeration;
 8  
 import java.util.HashMap;
 9  
 import java.util.Iterator;
 10  
 import java.util.logging.Logger;
 11  
 
 12  
 import org.apache.commons.vfs.FileObject;
 13  
 import org.apache.commons.vfs.FileSystemManager;
 14  
 
 15  
 import org.webslinger.commons.vfs.FileReplicator;
 16  
 import org.webslinger.commons.vfs.VFSUtil;
 17  
 import org.webslinger.commons.vfs.operations.COWStateOperation;
 18  
 import org.webslinger.commons.vfs.virtual.VirtualFileSystem;
 19  
 
 20  
 public class StandardModuleState implements ModuleState, WorkAreaFactory<WorkArea> {
 21  1
     private static final Logger logger = Logger.getLogger(StandardModuleState.class.getName());
 22  
     protected final GenericWorkAreaFactory workAreaFactory;
 23  
     protected final FileSystemManager fsm;
 24  
     protected final VirtualFileSystem vfs;
 25  
     protected final FileObject cow;
 26  
     protected final FileReplicator replicator;
 27  
 
 28  
     public StandardModuleState(FileObject root) throws IOException {
 29  1
         this(root, (FileObject[]) null);
 30  1
     }
 31  
 
 32  
     public StandardModuleState(FileObject root, FileObject base) throws IOException {
 33  0
         this(root, new FileObject[] {base});
 34  0
     }
 35  
 
 36  22
     public StandardModuleState(FileObject root, FileObject[] bases) throws IOException {
 37  22
         replicator = new FileReplicator(root.resolveFile("Var/ModuleCache/"));
 38  22
         replicator.getCacheLocation().createFolder();
 39  22
         final FileObject workAreaRoot = root.resolveFile("Var/WorkArea");
 40  22
         fsm = root.getFileSystem().getFileSystemManager();
 41  22
         workAreaFactory = new GenericWorkAreaFactory(root, workAreaRoot);
 42  22
         vfs = workAreaFactory.getVirtual();
 43  22
         cow = workAreaFactory.getCOW();
 44  22
         COWStateOperation siteCOW = VFSUtil.getOperation(cow.resolveFile("/site"), COWStateOperation.class);
 45  22
         siteCOW.clear();
 46  22
         VFSUtil.getOperation(cow.resolveFile("/modules/enabled"), COWStateOperation.class).clear();
 47  22
         if (bases != null) {
 48  5
             for (int i = 0; i < bases.length; i++) {
 49  3
                 vfs.resolveFile("/base/" + i).createFolder();
 50  3
                 vfs.addJunction("/base/" + i, bases[i]);
 51  3
                 siteCOW.addBase("base/" + i);
 52  
                 try {
 53  3
                     Thread.sleep(5);
 54  0
                 } catch (InterruptedException e) {
 55  
                     // ignore, bug work-around, race condition?
 56  3
                 }
 57  
             }
 58  
         }
 59  22
         vfs.resolveFile("/libraries/Lib").createFolder();
 60  22
         siteCOW.addBase("libraries");
 61  22
         vfs.resolveFile("/modules/available").createFolder();
 62  22
         vfs.resolveFile("/modules/layout").createFolder();
 63  22
         vfs.resolveFile("/modules/enabled").createFolder();
 64  22
         siteCOW.addBase("modules/enabled");
 65  22
     }
 66  
 
 67  
     public FileObject getRoot() {
 68  22
         return workAreaFactory.getRoot();
 69  
     }
 70  
 
 71  
     protected FileObject makeLocalFile(FileObject file) throws IOException {
 72  58
         return replicator.getLocalFile(file);
 73  
     }
 74  
 
 75  
     protected void deleteLocalFile(String url) throws IOException {
 76  0
         replicator.deleteLocalFile(url);
 77  0
     }
 78  
 
 79  
     public boolean addLibrary(String name, String url) throws IOException {
 80  0
         logger.info("addLibrary(" + name + ", " + url + ")");
 81  0
         FileObject file = makeLocalFile(fsm.resolveFile(url));
 82  0
         logger.fine("file=" + file);
 83  
         //addModulePoint(name, file, "config");
 84  
         //addModulePoint(name, file, "www");
 85  
         //addModulePoint(name, file, "var");
 86  0
         vfs.addJunction("/libraries/Lib/" + name, file);
 87  0
         return true;
 88  
     }
 89  
 
 90  
     public boolean addModule(String name, String url) throws IOException {
 91  58
         logger.info("addModule(" + name + ", " + url + ")");
 92  58
         FileObject file = makeLocalFile(fsm.resolveFile(url));
 93  58
         if (fsm.canCreateFileSystem(file)) file = fsm.createFileSystem(file);
 94  58
         logger.info("file=" + file);
 95  
         //addModulePoint(name, file, "config");
 96  
         //addModulePoint(name, file, "www");
 97  
         //addModulePoint(name, file, "var");
 98  58
         FileObject target = vfs.resolveFile("/modules/available/" + name);
 99  58
         if (!file.getType().hasChildren()) target = target.getParent();
 100  58
         target.createFolder();
 101  58
         vfs.addJunction("/modules/available/" + name, file);
 102  58
         return true;
 103  
     }
 104  
 
 105  
     public boolean removeLibrary(String name) throws IOException {
 106  0
         workAreaFactory.getVirtual().removeJunction("/libraries/Lib/" + name);
 107  0
         return true;
 108  
     }
 109  
 
 110  
     public boolean removeModule(String name) throws IOException {
 111  0
         workAreaFactory.getVirtual().removeJunction("/modules/available/" + name);
 112  0
         return true;
 113  
     }
 114  
 
 115  
     public boolean enableModule(String name) throws IOException {
 116  58
         logger.info("enableModule(" + name + ")");
 117  58
         return VFSUtil.getOperation(vfs, "/modules/enabled", COWStateOperation.class).addBase("/modules/layout/" + name);
 118  
     }
 119  
 
 120  
     public boolean disableModule(String name) throws IOException {
 121  0
         return VFSUtil.getOperation(vfs, "/modules/enabled", COWStateOperation.class).removeBase("/modules/layout/" + name);
 122  
     }
 123  
 
 124  
     public boolean addModuleMount(String name, String src, String dest) throws IOException {
 125  58
         logger.info("addModuleMount(" + name + ", " + src + ", " + dest + ")");
 126  58
         FileObject toMount = vfs.resolveFile("/modules/available/" + name + "/" + src);
 127  58
         FileObject target = vfs.resolveFile("/modules/layout/" + name + "/" + dest);
 128  58
         if (!toMount.getType().hasChildren()) target = target.getParent();
 129  58
         target.createFolder();
 130  58
         vfs.addJunction("/modules/layout/" + name + "/" + dest, toMount);
 131  58
         return true;
 132  
     }
 133  
 
 134  
     public boolean removeModuleMount(String name, String dest) throws IOException {
 135  0
         vfs.removeJunction("/modules/layout/" + name + dest);
 136  0
         return true;
 137  
     }
 138  
 
 139  
     public WorkArea create() throws IOException {
 140  0
         return workAreaFactory.create();
 141  
     }
 142  
 
 143  
     public WorkArea join(String name) throws IOException {
 144  0
         return workAreaFactory.join(name);
 145  
     }
 146  
 
 147  
     public Collection<? extends WorkArea> getWorkAreas() throws IOException {
 148  0
         return workAreaFactory.getWorkAreas();
 149  
     }
 150  
 }