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    package org.apache.geronimo.system.plugin;
018    
019    import java.io.Serializable;
020    import java.util.List;
021    import java.util.ArrayList;
022    import org.apache.geronimo.kernel.repository.Artifact;
023    
024    /**
025     * Provides the results of a configuration download operation.  This is updated
026     * along the way for callers who want to monitor the ongoing progress.
027     *
028     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
029     */
030    public class DownloadResults implements Serializable, DownloadPoller {
031        private List removedConfigIDs = new ArrayList();
032        private List restartedConfigIDs = new ArrayList();
033        private List installedConfigIDs = new ArrayList();
034        private List dependenciesPresent = new ArrayList();
035        private List dependenciesInstalled = new ArrayList();
036        private String currentFile;
037        private String currentMessage;
038        private int currentFileProgress = -1;
039        private Exception failure;
040        private boolean finished;
041        private long totalDownloadBytes = 0;
042    
043        public synchronized DownloadResults duplicate() {
044            DownloadResults other = new DownloadResults();
045            other.removedConfigIDs.addAll(removedConfigIDs);
046            other.restartedConfigIDs.addAll(restartedConfigIDs);
047            other.installedConfigIDs.addAll(installedConfigIDs);
048            other.dependenciesPresent.addAll(dependenciesPresent);
049            other.dependenciesInstalled.addAll(dependenciesInstalled);
050            other.currentFile = currentFile;
051            other.currentMessage = currentMessage;
052            other.currentFileProgress = currentFileProgress;
053            other.failure = failure;
054            other.finished = finished;
055            other.totalDownloadBytes = totalDownloadBytes;
056            return other;
057        }
058    
059        public synchronized void addInstalledConfigID(Artifact dep) {
060            installedConfigIDs.add(dep);
061        }
062    
063        public synchronized void addRemovedConfigID(Artifact obsolete) {
064            removedConfigIDs.add(obsolete);
065        }
066    
067        public synchronized void addRestartedConfigID(Artifact target) {
068            restartedConfigIDs.add(target);
069        }
070    
071        public synchronized void addDependencyPresent(Artifact dep) {
072            dependenciesPresent.add(dep);
073        }
074    
075        public synchronized void addDependencyInstalled(Artifact dep) {
076            dependenciesInstalled.add(dep);
077        }
078    
079        public synchronized void setCurrentFile(String currentFile) {
080            this.currentFile = currentFile;
081        }
082    
083        public synchronized void setCurrentMessage(String currentMessage) {
084            this.currentMessage = currentMessage;
085        }
086    
087        public synchronized void setCurrentFilePercent(int currentFileProgress) {
088            this.currentFileProgress = currentFileProgress;
089        }
090    
091        public synchronized void setFailure(Exception failure) {
092            this.failure = failure;
093        }
094    
095        public synchronized void setFinished() {
096            finished = true;
097        }
098    
099        public synchronized void addDownloadBytes(long bytes) {
100            totalDownloadBytes += bytes;
101        }
102    
103        public boolean isFinished() {
104            return finished;
105        }
106    
107        public boolean isFailed() {
108            return failure != null;
109        }
110    
111        /**
112         * The total number of bytes in the archives downloaded from remote
113         * repositories.
114         */
115        public long getTotalDownloadBytes() {
116            return totalDownloadBytes;
117        }
118    
119        /**
120         * If the operation failed, the Exception that caused the failure.
121         */
122        public Exception getFailure() {
123            return failure;
124        }
125    
126        /**
127         * Gets the list of the originally requested Config IDs that were
128         * successfully installed.  Ordinarily this is not necessary, but
129         * it may be important in case of failure midway through, or if the
130         * request passed previously downloaded configurations on the command
131         * line and the caller doesn't know what the Config IDs are.
132         */
133        public Artifact[] getInstalledConfigIDs() {
134            return (Artifact[]) installedConfigIDs.toArray(new Artifact[installedConfigIDs.size()]);
135        }
136    
137        public Artifact[] getRemovedConfigIDs() {
138            return (Artifact[]) removedConfigIDs.toArray(new Artifact[installedConfigIDs.size()]);
139        }
140    
141        public Artifact[] getRestartedConfigIDs() {
142            return (Artifact[]) restartedConfigIDs.toArray(new Artifact[installedConfigIDs.size()]);
143        }
144    
145        /**
146         * Gets the dependencies that we've needed but they're already present in
147         * the local server so no installation was necessary.
148         */
149        public Artifact[] getDependenciesPresent() {
150            return (Artifact[]) dependenciesPresent.toArray(new Artifact[dependenciesPresent.size()]);
151        }
152    
153        /**
154         * Gets the dependencies that we've successfully downloaded and installed
155         * into the local server environment.
156         */
157        public Artifact[] getDependenciesInstalled() {
158            return (Artifact[]) dependenciesInstalled.toArray(new Artifact[dependenciesInstalled.size()]);
159        }
160    
161        /**
162         * Gets the name of the file that is currently being operated on.
163         */
164        public String getCurrentFile() {
165            return currentFile;
166        }
167    
168        /**
169         * Gets a description of the work currently being done.
170         */
171        public String getCurrentMessage() {
172            return currentMessage;
173        }
174    
175        /**
176         * Gets the progress on the current file expressed as a percentage.  This
177         * value may be -1 in which case the progress cannot be calculated (e.g. a
178         * download where the server doesn't supply the file size up front).
179         */
180        public int getCurrentFilePercent() {
181            return currentFileProgress;
182        }
183    }