1 19 package org.netbeans.modules.jmiutils; 20 21 import org.openide.filesystems.*; 22 import java.io.*; 23 import java.util.*; 24 import org.netbeans.api.mdr.JMIMapper; 25 import org.netbeans.api.mdr.JMIStreamFactory; 26 27 32 public class FSSFImpl extends JMIStreamFactory { 33 private final org.openide.filesystems.FileSystem fileSystem; 34 private final java.util.Date modelLastModified; 35 36 42 public FSSFImpl(org.openide.filesystems.FileSystem fs) { 43 this(fs, null); 44 } 45 46 57 public FSSFImpl(org.openide.filesystems.FileSystem fs, java.util.Date modelLastModified) { 58 if (fs == null) 59 throw new IllegalArgumentException ("ERROR: fs is null"); 60 fileSystem = fs; 61 this.modelLastModified = modelLastModified; 62 } 63 64 public OutputStream createStream(List pkg, String className, String extension) throws IOException { 65 FileObject folder = fileSystem.getRoot(); 67 68 String folderName; 70 FileObject tempFolder; 71 for (Iterator it = pkg.iterator(); it.hasNext();) { 72 folderName = (String ) it.next(); 73 if ((tempFolder = folder.getFileObject(folderName)) == null) { 75 tempFolder = folder.createFolder(folderName); 76 } 77 folder = tempFolder; 78 } 79 80 FileObject file = folder.getFileObject(className, extension); 82 if (file != null) { 83 if (modelLastModified != null && modelLastModified.before(file.lastModified())) 85 return null; 86 87 FileLock lock = file.lock(); 89 file.delete(lock); 90 lock.releaseLock(); 91 } 92 file = folder.createData(className, extension); 93 94 FileLock fileLock = file.lock(); 96 return new FSStream(file.getOutputStream(fileLock), fileLock); 97 } 98 99 private static class FSStream extends FilterOutputStream { 100 private final FileLock fileLock; 101 102 private FSStream(OutputStream stream, FileLock fileLock) { 103 super(stream); 104 this.fileLock = fileLock; 105 } 106 107 public void close() throws IOException { 108 super.close(); 109 fileLock.releaseLock(); 110 } 111 } 112 } 113 | Popular Tags |