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;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.util.Arrays;
023:
024: import javax.swing.event.EventListenerList;
025:
026: import org.apache.ivy.core.module.descriptor.Artifact;
027:
028: public abstract class AbstractRepository implements Repository {
029: private EventListenerList listeners = new EventListenerList();
030:
031: private String name;
032:
033: private TransferEvent evt;
034:
035: public void addTransferListener(TransferListener listener) {
036: listeners.add(TransferListener.class, listener);
037: }
038:
039: public void removeTransferListener(TransferListener listener) {
040: listeners.remove(TransferListener.class, listener);
041: }
042:
043: public boolean hasTransferListener(TransferListener listener) {
044: return Arrays.asList(
045: listeners.getListeners(TransferListener.class))
046: .contains(listener);
047: }
048:
049: protected void fireTransferInitiated(Resource res, int requestType) {
050: evt = new TransferEvent(this , res,
051: TransferEvent.TRANSFER_INITIATED, requestType);
052: fireTransferEvent(evt);
053: }
054:
055: protected void fireTransferStarted() {
056: evt.setEventType(TransferEvent.TRANSFER_STARTED);
057: fireTransferEvent(evt);
058: }
059:
060: protected void fireTransferStarted(long totalLength) {
061: evt.setEventType(TransferEvent.TRANSFER_STARTED);
062: evt.setTotalLength(totalLength);
063: evt.setTotalLengthSet(true);
064: fireTransferEvent(evt);
065: }
066:
067: protected void fireTransferProgress(long length) {
068: evt.setEventType(TransferEvent.TRANSFER_PROGRESS);
069: evt.setLength(length);
070: if (!evt.isTotalLengthSet()) {
071: evt.setTotalLength(evt.getTotalLength() + length);
072: }
073: fireTransferEvent(evt);
074: }
075:
076: protected void fireTransferCompleted() {
077: evt.setEventType(TransferEvent.TRANSFER_COMPLETED);
078: if (evt.getTotalLength() > 0 && !evt.isTotalLengthSet()) {
079: evt.setTotalLengthSet(true);
080: }
081: fireTransferEvent(evt);
082: }
083:
084: protected void fireTransferCompleted(long totalLength) {
085: evt.setEventType(TransferEvent.TRANSFER_COMPLETED);
086: evt.setTotalLength(totalLength);
087: evt.setTotalLengthSet(true);
088: fireTransferEvent(evt);
089: }
090:
091: protected void fireTransferError() {
092: evt.setEventType(TransferEvent.TRANSFER_ERROR);
093: fireTransferEvent(evt);
094: }
095:
096: protected void fireTransferError(Exception ex) {
097: evt.setEventType(TransferEvent.TRANSFER_ERROR);
098: evt.setException(ex);
099: fireTransferEvent(evt);
100: }
101:
102: protected void fireTransferEvent(TransferEvent evt) {
103: Object[] listeners = this .listeners.getListenerList();
104: for (int i = listeners.length - 2; i >= 0; i -= 2) {
105: if (listeners[i] == TransferListener.class) {
106: ((TransferListener) listeners[i + 1])
107: .transferProgress(evt);
108: }
109: }
110: }
111:
112: public String getFileSeparator() {
113: return "/";
114: }
115:
116: public String standardize(String source) {
117: return source.replace('\\', '/');
118: }
119:
120: public String getName() {
121: return name;
122: }
123:
124: public void setName(String name) {
125: this .name = name;
126: }
127:
128: public String toString() {
129: return getName();
130: }
131:
132: public void put(Artifact artifact, File source, String destination,
133: boolean overwrite) throws IOException {
134: put(source, destination, overwrite);
135: }
136:
137: protected void put(File source, String destination,
138: boolean overwrite) throws IOException {
139: throw new UnsupportedOperationException(
140: "put in not supported by " + getName());
141: }
142: }
|