1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.pgp; |
8 | |
|
9 | |
import java.io.IOException; |
10 | |
|
11 | |
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; |
12 | |
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong; |
13 | |
|
14 | |
import org.apache.commons.io.IOUtils; |
15 | |
import org.apache.commons.logging.Log; |
16 | |
import org.apache.commons.logging.LogFactory; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
public abstract class AbstractTransformPolicy implements TransformPolicy |
24 | |
{ |
25 | 0 | protected static final Log logger = LogFactory.getLog(AbstractTransformPolicy.class); |
26 | |
|
27 | |
private AtomicBoolean startedCopying; |
28 | |
private Thread copyingThread; |
29 | |
private LazyTransformedInputStream inputStream; |
30 | |
protected volatile boolean isClosed; |
31 | |
private AtomicLong bytesRequested; |
32 | |
|
33 | |
public AbstractTransformPolicy() |
34 | 0 | { |
35 | 0 | this.startedCopying = new AtomicBoolean(false); |
36 | 0 | this.isClosed = false; |
37 | 0 | this.bytesRequested = new AtomicLong(0); |
38 | 0 | } |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public void initialize(LazyTransformedInputStream lazyTransformedInputStream) { |
44 | 0 | this.inputStream = lazyTransformedInputStream; |
45 | 0 | } |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public void readRequest(long length) |
51 | |
{ |
52 | 0 | this.bytesRequested.addAndGet(length); |
53 | 0 | startCopyingThread(); |
54 | 0 | } |
55 | |
|
56 | |
protected void startCopyingThread() |
57 | |
{ |
58 | 0 | if (this.startedCopying.compareAndSet(false, true)) |
59 | |
{ |
60 | 0 | this.copyingThread = this.getCopyingThread(); |
61 | 0 | this.copyingThread.start(); |
62 | |
} |
63 | 0 | } |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public void release() |
69 | |
{ |
70 | 0 | this.isClosed = true; |
71 | 0 | if (this.copyingThread != null) |
72 | |
{ |
73 | 0 | synchronized (this.copyingThread) |
74 | |
{ |
75 | 0 | this.copyingThread.notifyAll(); |
76 | 0 | } |
77 | |
} |
78 | 0 | } |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
protected abstract Thread getCopyingThread(); |
84 | |
|
85 | |
protected StreamTransformer getTransformer() |
86 | |
{ |
87 | 0 | return this.inputStream.getTransformer(); |
88 | |
} |
89 | |
|
90 | |
protected LazyTransformedInputStream getInputStream() |
91 | |
{ |
92 | 0 | return this.inputStream; |
93 | |
} |
94 | |
|
95 | |
protected AtomicLong getBytesRequested() |
96 | |
{ |
97 | 0 | return bytesRequested; |
98 | |
} |
99 | |
|
100 | 0 | protected abstract class TransformerWork extends Thread |
101 | |
{ |
102 | |
public synchronized void run() |
103 | |
{ |
104 | |
try |
105 | |
{ |
106 | 0 | execute(); |
107 | 0 | IOUtils.closeQuietly(getInputStream().getOut()); |
108 | |
|
109 | 0 | while (!isClosed) |
110 | |
{ |
111 | |
try |
112 | |
{ |
113 | 0 | this.wait(); |
114 | |
} |
115 | 0 | catch (InterruptedException e) |
116 | |
{ |
117 | 0 | } |
118 | |
} |
119 | |
} |
120 | 0 | catch (Exception e) |
121 | |
{ |
122 | 0 | logger.error(e.getMessage(), e); |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
try |
128 | |
{ |
129 | 0 | IOUtils.write(e.getMessage().toCharArray(), getInputStream().getOut()); |
130 | |
} |
131 | 0 | catch (IOException exp) |
132 | |
{ |
133 | 0 | } |
134 | 0 | } |
135 | 0 | } |
136 | |
|
137 | |
protected abstract void execute() throws Exception; |
138 | |
} |
139 | |
} |