1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2011 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.Date; |
26 |
import java.util.logging.Level; |
27 |
|
28 |
import net.sf.juife.AbstractTask; |
29 |
|
30 |
import org.jsampler.CC; |
31 |
import org.jsampler.HF; |
32 |
|
33 |
import org.linuxsampler.lscp.LSException; |
34 |
|
35 |
import static org.jsampler.JSI18n.i18n; |
36 |
|
37 |
|
38 |
/** |
39 |
* This class extends <code>AbstractTask</code> to add new features. |
40 |
* @author Grigor Iliev |
41 |
*/ |
42 |
public abstract class EnhancedTask<R> extends AbstractTask<R> { |
43 |
public static final int SOCKET_ERROR = 1; |
44 |
private boolean stopped = false; |
45 |
private boolean silent = false; |
46 |
private boolean showErrorDetails; |
47 |
private boolean calculateElapsedTime = false; |
48 |
private long elapsedTime = -1; |
49 |
|
50 |
public |
51 |
EnhancedTask() { this(false); } |
52 |
|
53 |
public |
54 |
EnhancedTask(boolean showErrorDetails) { |
55 |
this.showErrorDetails = showErrorDetails; |
56 |
} |
57 |
|
58 |
public void |
59 |
run() { |
60 |
try { |
61 |
if(getCalculateElapsedTime()) { |
62 |
elapsedTime = new Date().getTime(); |
63 |
} |
64 |
|
65 |
exec(); |
66 |
|
67 |
if(getCalculateElapsedTime()) { |
68 |
elapsedTime = new Date().getTime() - elapsedTime; |
69 |
} |
70 |
if(getCalculateElapsedTime()) System.out.println("time: " + getElapsedTime()); |
71 |
} catch(java.net.SocketException x) { |
72 |
setErrorCode(SOCKET_ERROR); |
73 |
setErrorMessage(i18n.getError("SOCKET_ERROR")); |
74 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
75 |
} catch(Exception x) { |
76 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
77 |
if(showErrorDetails) setErrorDetails(x); |
78 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
79 |
onError(x); |
80 |
} |
81 |
} |
82 |
|
83 |
public void |
84 |
exec() throws Exception { } |
85 |
|
86 |
public void |
87 |
onError(Exception e) { e.printStackTrace(); } |
88 |
|
89 |
/** |
90 |
* Marks that the execution of this task was interrupted. |
91 |
*/ |
92 |
public void |
93 |
stop() { stopped = true; } |
94 |
|
95 |
/** |
96 |
* Determines whether the execution of this task was interrupted. |
97 |
* @return <code>true</code> if the execution of this task was interrupted, |
98 |
* <code>false</code> otherwise. |
99 |
*/ |
100 |
public boolean |
101 |
isStopped() { return stopped; } |
102 |
|
103 |
/** |
104 |
* Determines whether an error message should be shown |
105 |
* if the execution of the task fail. |
106 |
*/ |
107 |
public boolean |
108 |
isSilent() { return silent; } |
109 |
|
110 |
/** |
111 |
* Sets whether an error message should be shown |
112 |
* if the execution of the task fail. |
113 |
*/ |
114 |
public void |
115 |
setSilent(boolean b) { silent = b; } |
116 |
|
117 |
/** |
118 |
* Sets the error details provided by the given exception (if the given |
119 |
* exception is <code>LSException</code> instance and contains error details). |
120 |
*/ |
121 |
public void |
122 |
setErrorDetails(Exception e) { |
123 |
if(e == null) return; |
124 |
|
125 |
if(e instanceof LSException) { |
126 |
LSException x = (LSException)e; |
127 |
if(x.getDetails() != null && x.getDetails().length() > 0) { |
128 |
setErrorDetails(x.getDetails()); |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
/** Determines whether to calculate the elapsed time for this task. */ |
134 |
public boolean |
135 |
getCalculateElapsedTime() { return calculateElapsedTime; } |
136 |
|
137 |
/** Sets whether to calculate the elapsed time for this task. */ |
138 |
public void |
139 |
setCalculateElapsedTime(boolean b) { calculateElapsedTime = b; } |
140 |
|
141 |
/** Gets the elapsed time for this task. */ |
142 |
public long |
143 |
getElapsedTime() { return elapsedTime; } |
144 |
} |