001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.repository.file;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import org.apache.ivy.plugins.repository.AbstractRepository;
026: import org.apache.ivy.plugins.repository.RepositoryCopyProgressListener;
027: import org.apache.ivy.plugins.repository.Resource;
028: import org.apache.ivy.plugins.repository.TransferEvent;
029: import org.apache.ivy.util.FileUtil;
030:
031: public class FileRepository extends AbstractRepository {
032: private RepositoryCopyProgressListener progress = new RepositoryCopyProgressListener(
033: this );
034:
035: private File baseDir;
036:
037: private boolean local = true;
038:
039: public FileRepository() {
040: baseDir = null;
041: }
042:
043: public FileRepository(File basedir) {
044: this .baseDir = basedir;
045: }
046:
047: public Resource getResource(String source) throws IOException {
048: return new FileResource(this , getFile(source));
049: }
050:
051: public void get(String source, File destination) throws IOException {
052: fireTransferInitiated(getResource(source),
053: TransferEvent.REQUEST_GET);
054: copy(getFile(source), destination, true);
055: }
056:
057: public void put(File source, String destination, boolean overwrite)
058: throws IOException {
059: fireTransferInitiated(getResource(destination),
060: TransferEvent.REQUEST_PUT);
061: copy(source, getFile(destination), overwrite);
062: }
063:
064: public void move(File src, File dest) {
065: src.renameTo(dest);
066: }
067:
068: public void delete(File f) {
069: FileUtil.forceDelete(f);
070: }
071:
072: private void copy(File src, File destination, boolean overwrite)
073: throws IOException {
074: try {
075: getProgressListener()
076: .setTotalLength(new Long(src.length()));
077: if (!FileUtil.copy(src, destination, getProgressListener(),
078: overwrite)) {
079: if (!overwrite) {
080: throw new IOException(
081: "file copy not done from "
082: + src
083: + " to "
084: + destination
085: + ": destination probably already exists and overwrite is false");
086: } else {
087: throw new IOException("file copy not done from "
088: + src + " to " + destination);
089: }
090: }
091: } catch (IOException ex) {
092: fireTransferError(ex);
093: throw ex;
094: } catch (RuntimeException ex) {
095: fireTransferError(ex);
096: throw ex;
097: } finally {
098: getProgressListener().setTotalLength(null);
099: }
100: }
101:
102: protected RepositoryCopyProgressListener getProgressListener() {
103: return progress;
104: }
105:
106: public List list(String parent) throws IOException {
107: File dir = getFile(parent);
108: if (dir.exists() && dir.isDirectory()) {
109: String[] names = dir.list();
110: if (names != null) {
111: List ret = new ArrayList(names.length);
112: for (int i = 0; i < names.length; i++) {
113: ret.add(parent + getFileSeparator() + names[i]);
114: }
115: return ret;
116: }
117: }
118: return null;
119: }
120:
121: private File getFile(String source) {
122: if (baseDir != null) {
123: return new File(baseDir, source);
124: } else {
125: return new File(source);
126: }
127: }
128:
129: public boolean isLocal() {
130: return local;
131: }
132:
133: public void setLocal(boolean local) {
134: this.local = local;
135: }
136:
137: }
|