Created
October 24, 2011 10:05
-
-
Save monzou/1308711 to your computer and use it in GitHub Desktop.
MesagePack for Java Test 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package msgpack; | |
import java.io.IOException; | |
import java.util.Collection; | |
import java.util.Set; | |
import javax.tools.DiagnosticCollector; | |
import javax.tools.JavaCompiler; | |
import javax.tools.JavaFileManager; | |
import javax.tools.JavaFileObject; | |
import javax.tools.JavaFileObject.Kind; | |
import javax.tools.StandardLocation; | |
import javax.tools.ToolProvider; | |
import com.google.common.collect.ImmutableSet; | |
import com.google.common.collect.Sets; | |
/** | |
* ClassCollector | |
* | |
* @author monzou | |
*/ | |
final class ClassCollector { | |
private static final Set<Kind> KINDS = ImmutableSet.of(Kind.CLASS); | |
private final JavaFileManager fileManager; | |
ClassCollector() { | |
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); | |
fileManager = compiler.getStandardFileManager(new DiagnosticCollector<JavaFileObject>(), null, null); | |
} | |
Collection<Class<?>> collect(String... pkgs) throws IOException { | |
Set<Class<?>> classes = Sets.newHashSet(); | |
for (String pkg : pkgs) { | |
classes.addAll(collect(pkg)); | |
} | |
return classes; | |
} | |
private Set<Class<?>> collect(String pkg) throws IOException { | |
Set<Class<?>> classes = Sets.newHashSet(); | |
for (JavaFileObject o : fileManager.list(StandardLocation.CLASS_PATH, pkg, KINDS, true)) { | |
String path = o.toUri().getPath(); | |
path = path.replaceAll("\\.class", "").replaceAll("/", "."); | |
int p = path.lastIndexOf(pkg); | |
if (p > 0) { | |
path = path.substring(p, path.length()); | |
} | |
classes.add(getClass(path)); | |
} | |
return classes; | |
} | |
private Class<?> getClass(String fqn) { | |
try { | |
return Class.forName(fqn); | |
} catch (ClassNotFoundException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package msgpack; | |
import java.io.IOException; | |
import org.apache.commons.io.output.ByteArrayOutputStream; | |
import org.apache.commons.lang.builder.ToStringBuilder; | |
import org.apache.commons.lang.builder.ToStringStyle; | |
import org.msgpack.packer.Packer; | |
import org.msgpack.unpacker.Unpacker; | |
import com.google.common.collect.Lists; | |
/** | |
* Example | |
* | |
* @author monzou | |
*/ | |
public class Example { | |
public static void main(String[] args) throws IOException { | |
Node junction = new Node(); | |
junction.setId(1L); | |
junction.setType(NodeType.JUNCTION); | |
junction.setIndex(1); | |
junction.setEncodedValue("AND"); | |
Node node = new Node(); | |
node.setId(2L); | |
node.setType(NodeType.PROPERTY); | |
node.setIndex(2); | |
node.setParentIndex(1); | |
node.setEncodedValue("hoge"); | |
Tree tree = new Tree(); | |
tree.setId(1L); | |
tree.setNodes(Lists.newArrayList(junction, node)); | |
MessagePackAdapter msgpack = new MessagePackAdapter(); | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
Packer packer = msgpack.createPacker(out); | |
packer.write(tree); | |
Unpacker unpacker = msgpack.createUnpacker(out.toByteArray()); | |
Tree deserialized = unpacker.read(Tree.class); | |
System.err.println(ToStringBuilder.reflectionToString(tree, ToStringStyle.MULTI_LINE_STYLE)); | |
System.err.println(ToStringBuilder.reflectionToString(deserialized, ToStringStyle.MULTI_LINE_STYLE)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package msgpack; | |
import java.io.ByteArrayInputStream; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.util.Collection; | |
import org.msgpack.MessagePack; | |
import org.msgpack.packer.Packer; | |
import org.msgpack.unpacker.Unpacker; | |
import com.google.common.base.Predicate; | |
/** | |
* MessagePackAdapter | |
* | |
* @author monzou | |
*/ | |
public class MessagePackAdapter { | |
private static final Predicate<Class<?>> ENUM_PREDICATE = new Predicate<Class<?>>() { | |
/** {@inheritDoc} */ | |
@Override | |
public boolean apply(Class<?> clazz) { | |
return clazz.isEnum(); | |
} | |
}; | |
private final MessagePack msgpack; | |
private volatile boolean initialized; | |
public MessagePackAdapter() { | |
msgpack = new MessagePack(); | |
} | |
public Packer createPacker(OutputStream out) { | |
initializeIfNeeded(); | |
return msgpack.createPacker(out); | |
} | |
public Unpacker createUnpacker(byte[] bytes) { | |
initializeIfNeeded(); | |
return msgpack.createUnpacker(new ByteArrayInputStream(bytes)); | |
} | |
private void initializeIfNeeded() { | |
if (initialized) { | |
return; | |
} | |
try { | |
Collection<Class<?>> classes = new ClassCollector().collect(getClass().getPackage().getName()); | |
for (Class<?> clazz : classes) { | |
if (ENUM_PREDICATE.apply(clazz)) { | |
System.out.println(clazz); | |
msgpack.register(clazz); | |
} | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} finally { | |
initialized = true; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package msgpack; | |
import java.io.Serializable; | |
import org.apache.commons.lang.builder.ToStringBuilder; | |
import org.msgpack.annotation.Message; | |
/** | |
* Node | |
* | |
* @author monzou | |
*/ | |
@Message | |
@SuppressWarnings("serial") | |
public class Node implements Serializable { | |
private Long id; | |
private NodeType type; | |
private Integer index; | |
private Integer parentIndex; | |
private String encodedValue; | |
/** | |
* id を取得します。 | |
* | |
* @return id | |
*/ | |
public Long getId() { | |
return id; | |
} | |
/** | |
* id を設定します。 | |
* | |
* @param id id | |
*/ | |
public void setId(Long id) { | |
this.id = id; | |
} | |
/** | |
* type を取得します。 | |
* | |
* @return type | |
*/ | |
public NodeType getType() { | |
return type; | |
} | |
/** | |
* type を設定します。 | |
* | |
* @param type type | |
*/ | |
public void setType(NodeType type) { | |
this.type = type; | |
} | |
/** | |
* index を取得します。 | |
* | |
* @return index | |
*/ | |
public Integer getIndex() { | |
return index; | |
} | |
/** | |
* index を設定します。 | |
* | |
* @param index index | |
*/ | |
public void setIndex(Integer index) { | |
this.index = index; | |
} | |
/** | |
* parentIndex を取得します。 | |
* | |
* @return parentIndex | |
*/ | |
public Integer getParentIndex() { | |
return parentIndex; | |
} | |
/** | |
* parentIndex を設定します。 | |
* | |
* @param parentIndex parentIndex | |
*/ | |
public void setParentIndex(Integer parentIndex) { | |
this.parentIndex = parentIndex; | |
} | |
/** | |
* encodedValue を取得します。 | |
* | |
* @return encodedValue | |
*/ | |
public String getEncodedValue() { | |
return encodedValue; | |
} | |
/** | |
* encodedValue を設定します。 | |
* | |
* @param encodedValue encodedValue | |
*/ | |
public void setEncodedValue(String encodedValue) { | |
this.encodedValue = encodedValue; | |
} | |
/** {@inheritDoc} */ | |
@Override | |
public String toString() { | |
return ToStringBuilder.reflectionToString(this); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package msgpack; | |
import org.msgpack.annotation.Message; | |
/** | |
* NodeType | |
* | |
* @author monzou | |
*/ | |
@Message | |
public enum NodeType { | |
JUNCTION, | |
PROPERTY, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package msgpack; | |
import java.io.Serializable; | |
import java.util.List; | |
import org.msgpack.annotation.Message; | |
/** | |
* Tree | |
* | |
* @author monzou | |
*/ | |
@Message | |
@SuppressWarnings("serial") | |
public class Tree implements Serializable { | |
private Long id; | |
private String name; | |
private List<Node> nodes; | |
/** | |
* id を取得します。 | |
* | |
* @return id | |
*/ | |
public Long getId() { | |
return id; | |
} | |
/** | |
* id を設定します。 | |
* | |
* @param id id | |
*/ | |
public void setId(Long id) { | |
this.id = id; | |
} | |
/** | |
* name を取得します。 | |
* | |
* @return name | |
*/ | |
public String getName() { | |
return name; | |
} | |
/** | |
* name を設定します。 | |
* | |
* @param name name | |
*/ | |
public void setName(String name) { | |
this.name = name; | |
} | |
/** | |
* nodes を取得します。 | |
* | |
* @return nodes | |
*/ | |
public List<Node> getNodes() { | |
return nodes; | |
} | |
/** | |
* nodes を設定します。 | |
* | |
* @param nodes nodes | |
*/ | |
public void setNodes(List<Node> nodes) { | |
this.nodes = nodes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment