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