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.versioning.system.cvss;
043:
044: import org.netbeans.lib.cvsclient.admin.StandardAdminHandler;
045: import org.netbeans.lib.cvsclient.admin.AdminHandler;
046: import org.netbeans.lib.cvsclient.admin.Entry;
047: import org.netbeans.lib.cvsclient.command.GlobalOptions;
048: import org.netbeans.modules.versioning.util.Utils;
049: import org.openide.filesystems.FileUtil;
050:
051: import java.io.*;
052: import java.util.*;
053:
054: /**
055: * Behaves as standard admin handler except for deleted files and directories where it acts as if the files
056: * were removed with 'cvs remove'.
057: *
058: * @author Maros Sandor
059: */
060: class CvsLiteAdminHandler implements AdminHandler {
061:
062: static final String INVALID_METADATA_MARKER = "invalid-metadata"; // NOI18N
063:
064: private static final String INVALID_METADATA_MARKER_PATH = CvsVersioningSystem.FILENAME_CVS
065: + "/" + INVALID_METADATA_MARKER; // NOI18N
066:
067: private StandardAdminHandler stdHandler;
068:
069: public CvsLiteAdminHandler() {
070: this .stdHandler = new StandardAdminHandler();
071: }
072:
073: private void checkForInvalidMetadata(File dir) {
074: File marker = new File(dir, INVALID_METADATA_MARKER_PATH);
075: if (marker.exists()) {
076: Utils.deleteRecursively(marker.getParentFile());
077: }
078: }
079:
080: public void updateAdminData(String localDirectory,
081: String repositoryPath, Entry entry,
082: GlobalOptions globalOptions) throws IOException {
083: checkForInvalidMetadata(new File(localDirectory));
084: stdHandler.updateAdminData(localDirectory, repositoryPath,
085: entry, globalOptions);
086: }
087:
088: public Entry getEntry(File file) throws IOException {
089: checkForInvalidMetadata(file.getParentFile());
090: return stdHandler.getEntry(file);
091: }
092:
093: public boolean exists(File file) {
094: if (file.exists())
095: return true;
096: if (CvsVersioningSystem.FILENAME_CVS.equals(file.getName()))
097: file = file.getParentFile(); // NOI18N
098: return false;
099: }
100:
101: public Iterator<Entry> getEntries(File directory)
102: throws IOException {
103: checkForInvalidMetadata(directory);
104: if (new File(directory, CvsVersioningSystem.FILENAME_CVS)
105: .isDirectory()) {
106: return stdHandler.getEntries(directory);
107: }
108: directory = FileUtil.normalizeFile(directory);
109: return stdHandler.getEntries(directory);
110: }
111:
112: public Entry[] getEntriesAsArray(File directory) throws IOException {
113: checkForInvalidMetadata(directory);
114: List<Entry> entries = new ArrayList<Entry>();
115: for (Iterator<Entry> i = getEntries(directory); i.hasNext();) {
116: entries.add(i.next());
117: }
118: return entries.toArray(new Entry[entries.size()]);
119: }
120:
121: public void setEntry(File file, Entry entry) throws IOException {
122: checkForInvalidMetadata(file.getParentFile());
123: // create missing directories beforehand
124: File adminDir = new File(file.getParentFile(),
125: CvsVersioningSystem.FILENAME_CVS);
126: createAdminDirs(adminDir);
127: stdHandler.setEntry(file, entry);
128: }
129:
130: /**
131: * Restores all administration files in the given directory and all parent directories recursively.
132: *
133: * @param adminDir directory to restore
134: * @throws IOException
135: */
136: private void createAdminDirs(File adminDir) throws IOException {
137: if (!adminDir.exists()) {
138: if (adminDir.getParentFile() != null
139: && adminDir.getParentFile().getParentFile() != null) {
140: createAdminDirs(new File(adminDir.getParentFile()
141: .getParentFile(),
142: CvsVersioningSystem.FILENAME_CVS));
143: }
144: }
145: }
146:
147: public String getRepositoryForDirectory(String directory,
148: String repository) throws IOException {
149:
150: checkForInvalidMetadata(new File(directory));
151: // TODO consult MetadataAttic.getScheduledRepository
152:
153: File dirFile = new File(directory);
154: if (dirFile.exists())
155: return stdHandler.getRepositoryForDirectory(directory,
156: repository);
157:
158: return stdHandler.getRepositoryForDirectory(directory,
159: repository);
160: }
161:
162: public void removeEntry(File file) throws IOException {
163: File parent = file.getParentFile();
164: checkForInvalidMetadata(parent);
165: stdHandler.removeEntry(file);
166: }
167:
168: public Set getAllFiles(File directory) throws IOException {
169: checkForInvalidMetadata(directory);
170: // TODO: override
171: return stdHandler.getAllFiles(directory);
172: }
173:
174: public String getStickyTagForDirectory(File directory) {
175: checkForInvalidMetadata(directory);
176: return stdHandler.getStickyTagForDirectory(directory);
177: }
178: }
|