Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / core / threading / WorkerBackgroundAction.java
blob3d673ed7e8bcc1afa68169a3f645912fae955d3c
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 feedback@cyberduck.ch
20 import ch.cyberduck.core.Cache;
21 import ch.cyberduck.core.ConnectionService;
22 import ch.cyberduck.core.Controller;
23 import ch.cyberduck.core.LoginService;
24 import ch.cyberduck.core.Path;
25 import ch.cyberduck.core.PathCache;
26 import ch.cyberduck.core.ProgressListener;
27 import ch.cyberduck.core.Session;
28 import ch.cyberduck.core.TranscriptListener;
29 import ch.cyberduck.core.exception.BackgroundException;
30 import ch.cyberduck.core.exception.ConnectionCanceledException;
31 import ch.cyberduck.core.worker.Worker;
33 import org.apache.log4j.Logger;
35 /**
36 * @version $Id$
38 public class WorkerBackgroundAction<T> extends BrowserBackgroundAction<T> {
39 private static final Logger log = Logger.getLogger(WorkerBackgroundAction.class);
41 protected Worker<T> worker;
43 private T result;
45 public WorkerBackgroundAction(final Controller controller,
46 final Session session,
47 final Worker<T> worker) {
48 this(controller, session, PathCache.empty(), worker);
51 public WorkerBackgroundAction(final LoginService login,
52 final Controller controller,
53 final Session session,
54 final Cache<Path> cache,
55 final Worker<T> worker) {
56 super(login, controller, session, cache);
57 this.worker = worker;
60 public WorkerBackgroundAction(final ConnectionService connection,
61 final Controller controller,
62 final Session session,
63 final Cache<Path> cache,
64 final Worker<T> worker) {
65 super(connection, controller, session, cache);
66 this.worker = worker;
69 public WorkerBackgroundAction(final ConnectionService connection,
70 final Controller controller,
71 final Session<?> session,
72 final Cache<Path> cache,
73 final Worker<T> worker,
74 final ProgressListener progress,
75 final TranscriptListener transcript) {
76 super(connection, controller, session, cache, progress, transcript);
77 this.worker = worker;
80 public WorkerBackgroundAction(final Controller controller,
81 final Session session,
82 final Cache<Path> cache,
83 final Worker<T> worker) {
84 super(controller, session, cache);
85 this.worker = worker;
88 public WorkerBackgroundAction(final Controller controller,
89 final Session<?> session,
90 final Cache<Path> cache,
91 final Worker<T> worker,
92 final ProgressListener progress,
93 final TranscriptListener transcript) {
94 super(controller, session, cache, progress, transcript);
95 this.worker = worker;
98 @Override
99 protected void reset() throws BackgroundException {
100 worker.reset();
101 super.reset();
104 @Override
105 public T run() throws BackgroundException {
106 if(log.isDebugEnabled()) {
107 log.debug(String.format("Run worker %s", worker));
109 try {
110 result = worker.run(session);
112 catch(ConnectionCanceledException e) {
113 worker.cancel();
114 throw e;
116 return result;
119 @Override
120 public T call() {
121 final T result = super.call();
122 if(null == result) {
123 return worker.initialize();
125 return result;
128 @Override
129 public void cleanup() {
130 super.cleanup();
131 if(null == result) {
132 log.warn(String.format("Missing result for worker %s. Use default value.", worker));
133 worker.cleanup(worker.initialize());
135 else {
136 if(log.isDebugEnabled()) {
137 log.debug(String.format("Cleanup worker %s", worker));
139 worker.cleanup(result);
143 @Override
144 public void cancel() {
145 if(log.isDebugEnabled()) {
146 log.debug(String.format("Cancel worker %s", worker));
148 worker.cancel();
149 super.cancel();
152 @Override
153 public boolean isCanceled() {
154 return worker.isCanceled();
157 @Override
158 public String getActivity() {
159 return worker.getActivity();
162 @Override
163 public boolean equals(final Object o) {
164 if(this == o) {
165 return true;
167 if(o == null || getClass() != o.getClass()) {
168 return false;
170 final WorkerBackgroundAction that = (WorkerBackgroundAction) o;
171 if(worker != null ? !worker.equals(that.worker) : that.worker != null) {
172 return false;
174 return true;
177 @Override
178 public int hashCode() {
179 return worker != null ? worker.hashCode() : 0;
182 @Override
183 public String toString() {
184 final StringBuilder sb = new StringBuilder("WorkerBackgroundAction{");
185 sb.append("worker=").append(worker);
186 sb.append(", result=").append(result);
187 sb.append('}');
188 return sb.toString();