KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > VersionsCache


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
20 package org.netbeans.modules.subversion;
21
22 import java.io.*;
23 import org.netbeans.modules.subversion.client.*;
24 import org.netbeans.modules.subversion.ui.diff.Setup;
25 import org.netbeans.modules.subversion.util.*;
26 import org.netbeans.modules.subversion.util.FileUtils;
27 import org.openide.filesystems.FileUtil;
28 import org.tigris.subversion.svnclientadapter.*;
29
30 /**
31  * File revisions cache. It can access pristine files.
32  *
33  *
34  * @author Petr Kuzel
35  */

36 public class VersionsCache {
37
38     private static VersionsCache instance;
39
40     /** Creates a new instance of VersionsCache */
41     private VersionsCache() {
42     }
43
44     public static synchronized VersionsCache getInstance() {
45         if (instance == null) {
46             instance = new VersionsCache();
47         }
48         return instance;
49     }
50
51     /**
52      * Loads the file in specified revision.
53      *
54      * <p>It's may connect over network I/O do not
55      * call from the GUI thread.
56      *
57      * @return null if the file does not exit in given revision
58      */

59     public File getFileRevision(File base, String JavaDoc revision) throws IOException {
60         if (Setup.REVISION_BASE.equals(revision)) {
61             try {
62                 File svnDir = getMetadataDir(base.getParentFile());
63                 if (svnDir == null) return null;
64                 File svnBase = new File(svnDir, "text-base/" + base.getName() + ".svn-base");
65                 if (!svnBase.exists()) return null;
66                 File expanded = new File(svnDir, "text-base/" + base.getName() + ".netbeans-base");
67                 if (expanded.canRead() && svnBase.isFile() && expanded.lastModified() > svnBase.lastModified()) {
68                     return expanded;
69                 }
70                 SvnClient client = Subversion.getInstance().getClient(base);
71                 InputStream in = client.getContent(base, SVNRevision.BASE);
72                 expanded = FileUtil.normalizeFile(expanded);
73                 expanded.deleteOnExit();
74                 FileUtils.copyStreamToFile(new BufferedInputStream(in), expanded);
75                 return expanded;
76             } catch (SVNClientException e) {
77                 return null;
78             }
79         } else if (Setup.REVISION_PRISTINE.equals(revision)) {
80             String JavaDoc name = base.getName();
81             File svnDir = getMetadataDir(base.getParentFile());
82             if (svnDir != null) {
83                 File text_base = new File(svnDir, "text-base"); // NOI18N
84
File pristine = new File(text_base, name + ".svn-base"); // NOI18N
85
if (pristine.isFile()) {
86                     return pristine;
87                 } else {
88                     return null;
89                 }
90             } else {
91                 return null;
92             }
93         } else if (Setup.REVISION_CURRENT.equals(revision)) {
94             return base;
95         } else {
96             SVNRevision svnrevision;
97             if (Setup.REVISION_HEAD.equals(revision)) {
98                 svnrevision = SVNRevision.HEAD;
99             } else {
100                 svnrevision = new SVNRevision.Number(Long.parseLong(revision));
101             }
102             try {
103                 SvnClient client = Subversion.getInstance().getClient(base);
104                 FileStatusCache cache = Subversion.getInstance().getStatusCache();
105                 InputStream in;
106                 if ((cache.getStatus(base).getStatus() & FileInformation.STATUS_VERSIONED) != 0) {
107                     in = client.getContent(base, svnrevision);
108                 } else {
109                     SVNUrl url = SvnUtils.getRepositoryUrl(base);
110                     if (url != null) {
111                         url = url.appendPath("@" + revision);
112                         in = client.getContent(url, svnrevision);
113                     } else {
114                         in = new ByteArrayInputStream(org.openide.util.NbBundle.getMessage(VersionsCache.class, "MSG_UnknownURL").getBytes()); // NOI18N
115
}
116                 }
117                 // keep original extension so MIME can be guessed by the extension
118
File tmp = File.createTempFile("nb-svn", base.getName()); // NOI18N
119
tmp = FileUtil.normalizeFile(tmp);
120                 tmp.deleteOnExit(); // hard to track actual lifetime
121
FileUtils.copyStreamToFile(new BufferedInputStream(in), tmp);
122                 return tmp;
123             } catch (SVNClientException ex) {
124                 IOException ioex = new IOException("Can not load: " + base.getAbsolutePath() + " in revision: " + revision); // NOI18N
125
ioex.initCause(ex);
126                 throw ioex;
127             }
128         }
129
130         // TODO how to cache locally? In SVN there are no per file revisions
131
// like in CVS, revision that comes here is repositoty revision
132
//
133
// Example:
134
// unmodified file has many repository revisions
135
// and effective caching should store just one version
136
// (mapping all repository revisions to it)
137
//
138
// File caching is leveraged in Search History
139
}
140
141     private File getMetadataDir(File dir) {
142         File svnDir = new File(dir, ".svn"); // NOI18N
143
if (!svnDir.isDirectory()) {
144             svnDir = new File(dir, "_svn"); // NOI18N
145
if (!svnDir.isDirectory()) {
146                 return null;
147             }
148         }
149         return svnDir;
150     }
151 }
152
Popular Tags