001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.subversion;
043:
044: import java.io.*;
045: import org.netbeans.modules.subversion.client.*;
046: import org.netbeans.modules.subversion.ui.diff.Setup;
047: import org.netbeans.modules.subversion.util.*;
048: import org.netbeans.modules.subversion.util.FileUtils;
049: import org.openide.filesystems.FileUtil;
050: import org.tigris.subversion.svnclientadapter.*;
051:
052: /**
053: * File revisions cache. It can access pristine files.
054: *
055: * XXX and what exactly is cached here?!
056: *
057: * @author Petr Kuzel
058: */
059: public class VersionsCache {
060:
061: private static VersionsCache instance;
062:
063: /** Creates a new instance of VersionsCache */
064: private VersionsCache() {
065: }
066:
067: public static synchronized VersionsCache getInstance() {
068: if (instance == null) {
069: instance = new VersionsCache();
070: }
071: return instance;
072: }
073:
074: /**
075: * Loads the file in specified revision.
076: *
077: * <p>It's may connect over network I/O do not
078: * call from the GUI thread.
079: *
080: * @return null if the file does not exit in given revision
081: */
082: public File getFileRevision(File base, String revision)
083: throws IOException {
084: if (Setup.REVISION_BASE.equals(revision)) {
085: try {
086: File svnDir = getMetadataDir(base.getParentFile());
087: if (svnDir == null)
088: return null;
089: File svnBase = new File(svnDir, "text-base/"
090: + base.getName() + ".svn-base");
091: if (!svnBase.exists())
092: return null;
093: File expanded = new File(svnDir, "text-base/"
094: + base.getName() + ".netbeans-base");
095: if (expanded.canRead()
096: && svnBase.isFile()
097: && expanded.lastModified() >= svnBase
098: .lastModified()) {
099: return expanded;
100: }
101: SvnClient client = Subversion.getInstance().getClient(
102: base);
103: InputStream in = client.getContent(base,
104: SVNRevision.BASE);
105: expanded = FileUtil.normalizeFile(expanded);
106: expanded.deleteOnExit();
107: FileUtils.copyStreamToFile(new BufferedInputStream(in),
108: expanded);
109: expanded.setLastModified(svnBase.lastModified());
110: return expanded;
111: } catch (SVNClientException e) {
112: IOException ioe = new IOException();
113: ioe.initCause(e);
114: throw ioe;
115: }
116: } else if (Setup.REVISION_PRISTINE.equals(revision)) {
117: String name = base.getName();
118: File svnDir = getMetadataDir(base.getParentFile());
119: if (svnDir != null) {
120: File text_base = new File(svnDir, "text-base"); // NOI18N
121: File pristine = new File(text_base, name + ".svn-base"); // NOI18N
122: if (pristine.isFile()) {
123: return pristine;
124: } else {
125: return null;
126: }
127: } else {
128: return null;
129: }
130: } else if (Setup.REVISION_CURRENT.equals(revision)) {
131: return base;
132: } else {
133: SVNRevision svnrevision;
134: if (Setup.REVISION_HEAD.equals(revision)) {
135: svnrevision = SVNRevision.HEAD;
136: } else {
137: svnrevision = new SVNRevision.Number(Long
138: .parseLong(revision));
139: }
140: try {
141: SvnClient client = Subversion.getInstance().getClient(
142: base);
143: FileStatusCache cache = Subversion.getInstance()
144: .getStatusCache();
145: InputStream in;
146: if ((cache.getStatus(base).getStatus() & FileInformation.STATUS_VERSIONED) != 0) {
147: in = client.getContent(base, svnrevision);
148: } else {
149: SVNUrl url = SvnUtils.getRepositoryUrl(base);
150: if (url != null) {
151: url = url.appendPath("@" + revision);
152: in = client.getContent(url, svnrevision);
153: } else {
154: in = new ByteArrayInputStream(
155: org.openide.util.NbBundle.getMessage(
156: VersionsCache.class,
157: "MSG_UnknownURL").getBytes()); // NOI18N
158: }
159: }
160: // keep original extension so MIME can be guessed by the extension
161: File tmp = File
162: .createTempFile("nb-svn", base.getName()); // NOI18N
163: tmp = FileUtil.normalizeFile(tmp);
164: tmp.deleteOnExit(); // hard to track actual lifetime
165: FileUtils.copyStreamToFile(new BufferedInputStream(in),
166: tmp);
167: return tmp;
168: } catch (SVNClientException ex) {
169: IOException ioex = new IOException("Can not load: "
170: + base.getAbsolutePath() + " in revision: "
171: + revision); // NOI18N
172: ioex.initCause(ex);
173: throw ioex;
174: }
175: }
176:
177: // TODO how to cache locally? In SVN there are no per file revisions
178: // like in CVS, revision that comes here is repositoty revision
179: //
180: // Example:
181: // unmodified file has many repository revisions
182: // and effective caching should store just one version
183: // (mapping all repository revisions to it)
184: //
185: // File caching is leveraged in Search History
186: }
187:
188: private File getMetadataDir(File dir) {
189: File svnDir = new File(dir, ".svn"); // NOI18N
190: if (!svnDir.isDirectory()) {
191: svnDir = new File(dir, "_svn"); // NOI18N
192: if (!svnDir.isDirectory()) {
193: return null;
194: }
195: }
196: return svnDir;
197: }
198: }
|