Coverage Report - org.webslinger.modules.GenericWorkAreaFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericWorkAreaFactory
76%
13/17
N/A
0
GenericWorkAreaFactory$GenericWorkArea
0%
0/85
0%
0/28
0
 
 1  
 package org.webslinger.modules;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.util.ArrayList;
 5  
 import java.util.Collection;
 6  
 import java.util.Iterator;
 7  
 
 8  
 import org.apache.commons.vfs.FileName;
 9  
 import org.apache.commons.vfs.FileObject;
 10  
 import org.apache.commons.vfs.FileSystemManager;
 11  
 import org.apache.commons.vfs.FileType;
 12  
 import org.apache.commons.vfs.Selectors;
 13  
 
 14  
 import org.webslinger.commons.vfs.VFSUtil;
 15  
 import org.webslinger.commons.vfs.operations.COWStateOperation;
 16  
 import org.webslinger.commons.vfs.virtual.VirtualFileSystem;
 17  
 
 18  0
 public class GenericWorkAreaFactory extends AbstractWorkAreaFactory<GenericWorkAreaFactory.GenericWorkArea> {
 19  
     protected final FileObject site;
 20  
     protected final VirtualFileSystem vfs;
 21  
     protected final FileObject root;
 22  
     protected final FileObject cow;
 23  
 
 24  
     public GenericWorkAreaFactory(FileObject site, FileObject work) throws IOException {
 25  22
         super(work);
 26  22
         this.site = site;
 27  22
         FileSystemManager fsm = work.getFileSystem().getFileSystemManager();
 28  22
         FileObject virtual = fsm.createFileSystem("wsvfs", work);
 29  22
         vfs = (VirtualFileSystem) virtual.getFileSystem();
 30  22
         virtual.resolveFile("site").createFolder();
 31  22
         vfs.addJunction("/site", site);
 32  22
         cow = virtual; //fsm.createFileSystem("cow", virtual);
 33  22
         root = fsm.createFileSystem("flat", cow.resolveFile("site"));
 34  22
     }
 35  
 
 36  
     public FileObject getRoot() {
 37  22
         return root;
 38  
     }
 39  
 
 40  
     public VirtualFileSystem getVirtual() {
 41  22
         return vfs;
 42  
     }
 43  
 
 44  
     public FileObject getCOW() {
 45  22
         return cow;
 46  
     }
 47  
 
 48  
     protected GenericWorkArea createWorkArea(String name, String location) throws IOException {
 49  0
         FileObject areaWork = work.resolveFile(location);
 50  0
         FileObject areaBase = vfs.getRoot().resolveFile("site");
 51  0
         return new GenericWorkArea(null, name, areaBase, areaWork);
 52  
     }
 53  
 
 54  0
     protected final class GenericWorkArea extends AbstractWorkAreaFactory<GenericWorkArea> implements Comparable<GenericWorkArea>, WorkArea<GenericWorkArea> {
 55  
         private final GenericWorkArea parentArea;
 56  
         private final String name;
 57  
         private final FileObject base;
 58  
         private final FileObject changes;
 59  
         private final FileObject root;
 60  
 
 61  0
         protected GenericWorkArea(GenericWorkArea parentArea, String name, FileObject areaBase, FileObject areaWork) throws IOException {
 62  0
             super(areaWork);
 63  0
             this.parentArea = parentArea;
 64  0
             this.name = name;
 65  0
             this.base = areaBase;
 66  0
             changes = areaBase.resolveFile("changes");
 67  0
             changes.createFolder();
 68  0
             String relativeName = changes.getParent().getName().getRelativeName(areaBase.getName());
 69  0
             VFSUtil.getOperation(changes, COWStateOperation.class).addBase("..");
 70  0
             root = areaBase.getFileSystem().getFileSystemManager().createFileSystem("flat", changes);
 71  0
         }
 72  
 
 73  
         protected GenericWorkArea createWorkArea(String name, String location) throws IOException {
 74  0
             FileObject areaWork = work.resolveFile(location);
 75  0
             return new GenericWorkArea(this, name, changes, areaWork);
 76  
         }
 77  
 
 78  
         public void abort() throws IOException {
 79  0
             close();
 80  0
         }
 81  
 
 82  
         public void commit() throws IOException {
 83  0
             commit("/", work.resolveFile("changes"), base);
 84  0
             work.resolveFile("changes").delete(Selectors.SELECT_SELF_AND_CHILDREN);
 85  0
             close();
 86  0
         }
 87  
 
 88  
         protected void commit(String cowPath, FileObject work, FileObject parent) throws IOException {
 89  0
             FileType workType = work.getType();
 90  0
             FileType parentType = parent.getType();
 91  0
             if (workType.hasChildren()) {
 92  0
                 if (workType.hasContent()) {
 93  
                     // both folder and file, can't handle
 94  0
                     throw new UnsupportedOperationException();
 95  
                 }
 96  0
                 if (!parent.exists()) {
 97  
                     // does not exist
 98  0
                     parent.copyFrom(work, Selectors.SELECT_CHILDREN);
 99  0
                 } else if (!parentType.hasChildren()) {
 100  
                     // exists, but is a file
 101  0
                     parent.delete();
 102  0
                     parent.copyFrom(work, Selectors.SELECT_CHILDREN);
 103  
                 } else {
 104  0
                     for (FileObject child: work.getChildren()) {
 105  0
                         String baseName = child.getName().getBaseName();
 106  0
                         commit(cowPath + FileName.SEPARATOR + baseName, child, parent.resolveFile(baseName));
 107  
                     }
 108  
                 }
 109  0
             } else if (workType.hasContent()) {
 110  0
                 if (!parent.exists()) {
 111  
                     // does not exist
 112  0
                     parent.copyFrom(work, Selectors.SELECT_SELF);
 113  0
                 } else if (!parentType.hasContent()) {
 114  
                     // exists, but is a folder
 115  0
                     parent.delete();
 116  0
                     parent.copyFrom(work, Selectors.SELECT_SELF);
 117  0
                 } else if (VFSUtil.cmp(work, parent)) {
 118  0
                     parent.copyFrom(work, Selectors.SELECT_SELF);
 119  
                 }
 120  
             } else {
 121  
                 // neither file or folder, nothing to do
 122  0
                 System.err.println("unknown types");
 123  0
                 return;
 124  
             }
 125  
             // FIXME: check attributes
 126  0
         }
 127  
 
 128  
         protected void close() throws IOException {
 129  0
             root.close();
 130  0
             FileObject parent = root.getFileSystem().getParentLayer();
 131  0
             parent.close();
 132  0
             parent = parent.getFileSystem().getParentLayer();
 133  0
             parent.close();
 134  0
         }
 135  
 
 136  
         public String getName() {
 137  0
             return appendName(new StringBuilder()).toString();
 138  
         }
 139  
 
 140  
         public GenericWorkArea getParentArea() {
 141  0
             return parentArea;
 142  
         }
 143  
 
 144  
         public FileObject getRoot() {
 145  0
             return root;
 146  
         }
 147  
 
 148  
         public int hashCode() {
 149  0
             int hashCode = GenericWorkArea.class.hashCode();
 150  0
             if (getParentArea() != null) hashCode ^= getParentArea().hashCode();
 151  0
             if (getName() != null) hashCode ^= getName().hashCode();
 152  0
             return hashCode;
 153  
         }
 154  
 
 155  
         public boolean equals(Object o) {
 156  0
             if (!(o instanceof GenericWorkArea)) return false;
 157  0
             GenericWorkArea other = (GenericWorkArea) o;
 158  0
             if (getParentArea() == null) {
 159  0
                 if (other.getParentArea() != null) return false;
 160  0
             } else if (!getParentArea().equals(other.getParentArea())) {
 161  0
                 return false;
 162  
             }
 163  0
             if (getName() == null) {
 164  0
                 if (other.getName() != null) return false;
 165  0
             } else if (!getName().equals(other.getName())) {
 166  0
                 return false;
 167  
             }
 168  0
             return true;
 169  
         }
 170  
 
 171  
         public int compareTo(GenericWorkArea other) {
 172  0
             if (getParentArea() == null) {
 173  0
                 if (other.getParentArea() != null) return -1;
 174  0
             } else if (other.getParentArea() == null) {
 175  0
                 return 1;
 176  
             } else {
 177  0
                 int r = getParentArea().compareTo(other.getParentArea());
 178  0
                 if (r != 0) return r;
 179  
             }
 180  0
             if (getName() == null) {
 181  0
                 if (other.getName() != null) return -1;
 182  0
             } else if (other.getName() == null) {
 183  0
                 return 1;
 184  
             } else {
 185  0
                 int r = getName().compareTo(other.getName());
 186  0
                 if (r != 0) return r;
 187  
             }
 188  0
             return 0;
 189  
         }
 190  
 
 191  
         protected StringBuilder appendName(StringBuilder sb) {
 192  0
             if (name == null) return sb;
 193  0
             if (getParentArea() != null) getParentArea().appendName(sb).append('/');
 194  0
             return sb.append(name);
 195  
         }
 196  
 
 197  
         public String toString() {
 198  0
             return getName();
 199  
         }
 200  
     }
 201  
 }