1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2009 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler 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. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler.task; |
24 |
|
25 |
import java.util.logging.Level; |
26 |
|
27 |
import net.sf.juife.AbstractTask; |
28 |
|
29 |
import org.jsampler.CC; |
30 |
import org.jsampler.HF; |
31 |
|
32 |
import org.linuxsampler.lscp.LSException; |
33 |
|
34 |
import static org.jsampler.JSI18n.i18n; |
35 |
|
36 |
|
37 |
/** |
38 |
* This class extends <code>AbstractTask</code> to add new features. |
39 |
* @author Grigor Iliev |
40 |
*/ |
41 |
public abstract class EnhancedTask<R> extends AbstractTask<R> { |
42 |
public static final int SOCKET_ERROR = 1; |
43 |
private boolean stopped = false; |
44 |
private boolean silent = false; |
45 |
private boolean showErrorDetails; |
46 |
|
47 |
public |
48 |
EnhancedTask() { this(false); } |
49 |
|
50 |
public |
51 |
EnhancedTask(boolean showErrorDetails) { |
52 |
this.showErrorDetails = showErrorDetails; |
53 |
} |
54 |
|
55 |
public void |
56 |
run() { |
57 |
try { exec(); } |
58 |
catch(java.net.SocketException x) { |
59 |
setErrorCode(SOCKET_ERROR); |
60 |
setErrorMessage(i18n.getError("SOCKET_ERROR")); |
61 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
62 |
} catch(Exception x) { |
63 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
64 |
if(showErrorDetails) setErrorDetails(x); |
65 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
66 |
onError(x); |
67 |
} |
68 |
} |
69 |
|
70 |
public void |
71 |
exec() throws Exception { } |
72 |
|
73 |
public void |
74 |
onError(Exception e) { } |
75 |
|
76 |
/** |
77 |
* Marks that the execution of this task was interrupted. |
78 |
*/ |
79 |
public void |
80 |
stop() { stopped = true; } |
81 |
|
82 |
/** |
83 |
* Determines whether the execution of this task was interrupted. |
84 |
* @return <code>true</code> if the execution of this task was interrupted, |
85 |
* <code>false</code> otherwise. |
86 |
*/ |
87 |
public boolean |
88 |
isStopped() { return stopped; } |
89 |
|
90 |
/** |
91 |
* Determines whether an error message should be shown |
92 |
* if the execution of the task fail. |
93 |
*/ |
94 |
public boolean |
95 |
isSilent() { return silent; } |
96 |
|
97 |
/** |
98 |
* DSets whether an error message should be shown |
99 |
* if the execution of the task fail. |
100 |
*/ |
101 |
public void |
102 |
setSilent(boolean b) { silent = b; } |
103 |
|
104 |
/** |
105 |
* Sets the error details provided by the given exception (if the given |
106 |
* exception is <code>LSException</code> instance and contains error details). |
107 |
*/ |
108 |
public void |
109 |
setErrorDetails(Exception e) { |
110 |
if(e == null) return; |
111 |
|
112 |
if(e instanceof LSException) { |
113 |
LSException x = (LSException)e; |
114 |
if(x.getDetails() != null && x.getDetails().length() > 0) { |
115 |
setErrorDetails(x.getDetails()); |
116 |
} |
117 |
} |
118 |
} |
119 |
} |