1 package ch
.cyberduck
.core
.threading
;
4 * Copyright (c) 2002-2013 David Kocher. All rights reserved.
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
;
38 public class WorkerBackgroundAction
<T
> extends BrowserBackgroundAction
<T
> {
39 private static final Logger log
= Logger
.getLogger(WorkerBackgroundAction
.class);
41 protected Worker
<T
> worker
;
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
);
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
);
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
);
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
);
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
);
99 protected void reset() throws BackgroundException
{
105 public T
run() throws BackgroundException
{
106 if(log
.isDebugEnabled()) {
107 log
.debug(String
.format("Run worker %s", worker
));
110 result
= worker
.run(session
);
112 catch(ConnectionCanceledException e
) {
121 final T result
= super.call();
123 return worker
.initialize();
129 public void cleanup() {
132 log
.warn(String
.format("Missing result for worker %s. Use default value.", worker
));
133 worker
.cleanup(worker
.initialize());
136 if(log
.isDebugEnabled()) {
137 log
.debug(String
.format("Cleanup worker %s", worker
));
139 worker
.cleanup(result
);
144 public void cancel() {
145 if(log
.isDebugEnabled()) {
146 log
.debug(String
.format("Cancel worker %s", worker
));
153 public boolean isCanceled() {
154 return worker
.isCanceled();
158 public String
getActivity() {
159 return worker
.getActivity();
163 public boolean equals(final Object o
) {
167 if(o
== null || getClass() != o
.getClass()) {
170 final WorkerBackgroundAction that
= (WorkerBackgroundAction
) o
;
171 if(worker
!= null ?
!worker
.equals(that
.worker
) : that
.worker
!= null) {
178 public int hashCode() {
179 return worker
!= null ? worker
.hashCode() : 0;
183 public String
toString() {
184 final StringBuilder sb
= new StringBuilder("WorkerBackgroundAction{");
185 sb
.append("worker=").append(worker
);
186 sb
.append(", result=").append(result
);
188 return sb
.toString();