/
Command.java
134 lines (111 loc) · 4.13 KB
/
Command.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Copyright 2009-2016 Igor Polevoy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.javalite.async;
import com.thoughtworks.xstream.XStream;
import org.javalite.common.JsonHelper;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import static org.javalite.common.Collections.map;
/**
* Super class of all commands. Only the method {@link #execute()} is to be provided by subclasses to
* do real application work.
*
* @author Igor Polevoy on 4/4/15.
*/
public abstract class Command {
private String jmsMessageId;
private Map<String, String> params = map("class", getClass().getSimpleName());
private static final XStream X_STREAM = new XStream(new CDATAXppDriver());
/**
* Method used by framework to de-serialize a command from XML.
*
* @param commandXml XML representation of a command.
*
* @return new instance of a command initialized with data from <code>commandXml</code> argument.
*/
public static <T extends Command> T fromXml(String commandXml) {
return (T) X_STREAM.fromXML(commandXml);
}
/**
* Subclasses are to provide implementation of this method to get application-specific work done.
*/
public abstract void execute();
/**
* Serializes this object into XML. The output is used by {@link #fromXml(String)} to de-serialize a command
* after it passes through messaging broker.
*
* @return XML representation of this instance.
*/
public final String toXml() {
return X_STREAM.toXML(this);
}
/**
* Flattens(serializes, dehydrates, etc.) this instance to a binary representation.
*
* @return a binary representation
* @throws IOException
*/
public byte[] toBytes() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ZipOutputStream stream = new ZipOutputStream(bout);
ZipEntry ze = new ZipEntry("async_message");
stream.putNextEntry(ze);
stream.write(toXml().getBytes());
stream.flush();
stream.close();
return bout.toByteArray();
}
@SuppressWarnings("unchecked")
public static <T extends Command> T fromBytes(byte[] bytes) throws IOException {
ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(bytes));
ZipEntry ze = zin.getNextEntry();
if(ze == null){
throw new AsyncException("something is seriously wrong with serialization");
}
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int len;
while((len = zin.read(buffer)) != -1) {
bout.write(buffer, 0, len);
}
return (T) Command.fromXml(bout.toString());
}
public String getJMSMessageID() {
return jmsMessageId;
}
public void setJMSMessageID(String jmsMessageId) {
this.jmsMessageId = jmsMessageId;
}
/**
* Adds parameters and their values to reflect by the {@link #getParams()} method.
*
* @param namesAndValues important parameters of this command - tings you want to see in a log file.
*/
@SuppressWarnings("unchecked")
public void addParams(String ... namesAndValues){
this.params.putAll(map(namesAndValues));
}
/**
* Used to understand important values related to this command (in log files).
*
* @return important values related to processing of this command.
*/
public Map<String, String> getParams(){
return params;
}
}