Add session to cache with both hostname and address.
[cyberduck.git] / source / ch / cyberduck / core / threading / AbstractBackgroundAction.java
blob8f91ed74a5c0529cb1397f6be169d9e7feea6af0
1 package ch.cyberduck.core.threading;
3 /*
4 * Copyright (c) 2002-2013 David Kocher. All rights reserved.
5 * http://cyberduck.ch/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * feedback@cyberduck.ch
21 import ch.cyberduck.core.LocaleFactory;
22 import ch.cyberduck.core.Path;
23 import ch.cyberduck.core.exception.BackgroundException;
24 import ch.cyberduck.core.exception.ConnectionCanceledException;
26 import org.apache.log4j.Logger;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
32 /**
33 * @version $Id$
35 public abstract class AbstractBackgroundAction<T> implements BackgroundAction<T> {
36 private static final Logger log = Logger.getLogger(AbstractBackgroundAction.class);
38 private State state;
40 protected final Set<BackgroundActionListener> listeners
41 = new HashSet<BackgroundActionListener>();
43 @Override
44 public void init() {
48 private enum State {
49 running,
50 canceled,
51 stopped
54 @Override
55 public void cancel() {
56 if(log.isDebugEnabled()) {
57 log.debug(String.format("Cancel background task %s", this));
59 final BackgroundActionListener[] l = listeners.toArray(
60 new BackgroundActionListener[listeners.size()]);
61 for(BackgroundActionListener listener : l) {
62 listener.cancel(this);
64 state = State.canceled;
67 /**
68 * To be overridden by a concrete subclass. Returns false by default for actions
69 * not connected to a graphical user interface
71 * @return True if the user canceled this action
73 @Override
74 public boolean isCanceled() {
75 return state == State.canceled;
78 @Override
79 public boolean isRunning() {
80 return state == State.running;
83 @Override
84 public void prepare() throws ConnectionCanceledException {
85 if(log.isDebugEnabled()) {
86 log.debug(String.format("Prepare background task %s", this));
88 final BackgroundActionListener[] l = listeners.toArray(
89 new BackgroundActionListener[listeners.size()]);
90 for(BackgroundActionListener listener : l) {
91 listener.start(this);
93 state = State.running;
96 @Override
97 public void finish() {
98 if(log.isDebugEnabled()) {
99 log.debug(String.format("Finish background task %s", this));
101 final BackgroundActionListener[] l = listeners.toArray(
102 new BackgroundActionListener[listeners.size()]);
103 for(BackgroundActionListener listener : l) {
104 listener.stop(this);
106 state = State.stopped;
109 @Override
110 public boolean alert() {
111 return false;
114 @Override
115 public T call() throws BackgroundException {
116 if(log.isDebugEnabled()) {
117 log.debug(String.format("Run background task %s", this));
119 return this.run();
122 @Override
123 public void cleanup() {
127 protected String toString(final List<Path> files) {
128 final StringBuilder name = new StringBuilder();
129 name.append(files.get(0).getName());
130 if(files.size() > 1) {
131 name.append("…");
133 return name.toString();
136 @Override
137 public void addListener(final BackgroundActionListener listener) {
138 listeners.add(listener);
141 @Override
142 public void removeListener(final BackgroundActionListener listener) {
143 listeners.remove(listener);
146 @Override
147 public String getActivity() {
148 return LocaleFactory.localizedString("Unknown");
151 @Override
152 public String getName() {
153 return LocaleFactory.localizedString("Unknown");
156 @Override
157 public Object lock() {
158 // No synchronization with other tasks by default
159 return null;