KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > jmiutils > FSSFImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
28  *
29  * @author mmatula
30  * @version
31  */

32 public class FSSFImpl extends JMIStreamFactory {
33     private final org.openide.filesystems.FileSystem fileSystem;
34     private final java.util.Date JavaDoc modelLastModified;
35
36     /** Creates a new instance of FSSImpl. Using this constructor is
37      * equivalent to using <code>FSSImpl(fs, null)</code>.
38      * @see #FSSFImpl(org.openide.filesystems.FileSystem, java.util.Date)
39      * @param fs The target filesystem
40      * @throws IllegalArgumentException If <code>fs == null</code>
41      */

42      public FSSFImpl(org.openide.filesystems.FileSystem fs) {
43         this(fs, null);
44     }
45     
46     /** Creates a new instance of FSSFImpl. FSSFImpl will compare the last
47      * modified date of any existing target files against the value of the
48      * <code>modelLastModified</code> paramter. If the target file's last
49      * modified date is later than <code>modelLastModified/code>
50      * then {@link #createStream(List, String, String)} will return
51      * <code>null</code> to prevent the {@link JMIMapper} from overwriting
52      * the file. If <code>modelLastModified == null</code>, then
53      * <code>createStream</code> will never return <code>null</code>.
54      * @param fs The target filesystem
55      * @throws IllegalArgumentException If <code>fs == null</code>
56      */

57     public FSSFImpl(org.openide.filesystems.FileSystem fs, java.util.Date JavaDoc modelLastModified) {
58         if (fs == null)
59             throw new IllegalArgumentException JavaDoc("ERROR: fs is null");
60         fileSystem = fs;
61         this.modelLastModified = modelLastModified;
62     }
63
64     public OutputStream createStream(List pkg, String JavaDoc className, String JavaDoc extension) throws IOException {
65         // if the interface should be generated into a filesystem, do so...
66
FileObject folder = fileSystem.getRoot();
67
68         // first create folders for the destination package
69
String JavaDoc folderName;
70         FileObject tempFolder;
71         for (Iterator it = pkg.iterator(); it.hasNext();) {
72             folderName = (String JavaDoc) it.next();
73             // create the folder only if it doesn't exist
74
if ((tempFolder = folder.getFileObject(folderName)) == null) {
75                 tempFolder = folder.createFolder(folderName);
76             }
77             folder = tempFolder;
78         }
79
80         // create file for a class
81
FileObject file = folder.getFileObject(className, extension);
82         if (file != null) {
83             // don't touch the file if it is up to date
84
if (modelLastModified != null && modelLastModified.before(file.lastModified()))
85                 return null;
86             
87             // if the file already exists, delete it
88
FileLock lock = file.lock();
89             file.delete(lock);
90             lock.releaseLock();
91         }
92         file = folder.createData(className, extension);
93
94         // lock the new file and get the output stream for it
95
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