<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss">

<channel>
	<title>Here comes the Sun &#187; java</title>
	<atom:link href="http://sunng.info/blog/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://sunng.info/blog</link>
	<description>Life ramblings</description>
	<lastBuildDate>Thu, 02 Sep 2010 12:29:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Bason: A BSON Serialization Code Generator</title>
		<link>http://sunng.info/blog/2010/08/bason-a-bson-serialization-code-generator/</link>
		<comments>http://sunng.info/blog/2010/08/bason-a-bson-serialization-code-generator/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 15:40:01 +0000</pubDate>
		<dc:creator>Sunng</dc:creator>
				<category><![CDATA[广告]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[opensource]]></category>

		<guid isPermaLink="false">http://sunng.info/blog/2010/08/bason-a-bson-serialization-code-generator/</guid>
		<description><![CDATA[Bason is a code generator for object to bson serialization and deserialization. Different from tranditional reflection way, bason uses an annotation processor to generate serialization manager at compile time. You just add Bason as compilation dependency and drop it in the runtime. To use Bason, you simply add annotation to JavaBeans: /** &#160;* &#160;*/ package [...]]]></description>
			<content:encoded><![CDATA[<p>Bason is a code generator for object to bson serialization and deserialization. Different from tranditional reflection way, bason uses an annotation processor to generate serialization manager at compile time. You just add Bason as compilation dependency and drop it in the runtime.</p>
<p>To use Bason, you simply add annotation to JavaBeans:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* <br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">info.sunng.bason.example</span><span style="color: #339933;">;</span><br />
<br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">info.sunng.bason.annotations.BsonAlias</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">info.sunng.bason.annotations.BsonDocument</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">info.sunng.bason.annotations.BsonIgnore</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* @author SunNing<br />
&nbsp;*<br />
&nbsp;* @since Aug 18, 2010<br />
&nbsp;*/</span><br />
@BsonDocument<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Passenger <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span> packageWeight<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> ticketId<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Date</span></a> createdDate<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Flight flight<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @return the packageWeight<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @BsonIgnore<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> getPackageWeight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> packageWeight<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param packageWeight the packageWeight to set<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setPackageWeight<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> packageWeight<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">packageWeight</span> <span style="color: #339933;">=</span> packageWeight<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @return the ticketId<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @BsonAlias<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ticket&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getTicketId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ticketId<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param ticketId the ticketId to set<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTicketId<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> ticketId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">ticketId</span> <span style="color: #339933;">=</span> ticketId<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @return the name<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param name the name to set<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param createdDate the createdDate to set<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setCreatedDate<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Date</span></a> createdDate<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">createdDate</span> <span style="color: #339933;">=</span> createdDate<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @return the createdDate<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Date</span></a> getCreatedDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> createdDate<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param flight the flight to set<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setFlight<span style="color: #009900;">&#40;</span>Flight flight<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">flight</span> <span style="color: #339933;">=</span> flight<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @return the flight<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Flight getFlight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> flight<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<ul>
<li>@BsonDocument marks this bean to be processed by bason processor. Serialization and deserialization support for this bean will be added to the manager. The bean must follow the Java Bean specification that has a getter and a setter for each property.</li>
<li>@BsonAlias on the getter allows user the specify a name for bson document instead of the default java bean property name.</li>
<li>@BsonIgnore on the getter marks a property to be transient when serialization and deserialization.</li>
</ul>
<p>Then you need a <i>bason.properties</i> at the root of classpath which looks like</p>
<div class="codecolorer-container properties default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="properties codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080; font-weight:bold;">bason.managerClassName</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">info.sunng.bason.BasonManager</span></div></div>
<p>You specify the manager class name here. This name can not be duplicated if you use Bason in multiple modules.</p>
<p>Take maven configuration as an example:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>info.sunng.bason<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>bason-annotation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>compile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>info.sunng.bason<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>bason-internal<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>compile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.mongodb<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mongo-java-driver<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${project.artifactId}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-compiler-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;compilerArguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;processor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>info.sunng.bason.internal.BasonProcessor<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/processor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/compilerArguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>when everything is ready, run <i>mvn compile</i> to generate the manager source file. By default, in a standard maven project, the generated file will be placed at:<br />
<i>/bason-example/target/generated-sources/annotations/</i></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">info.sunng.bason</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.bson.*</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.annotation.Generated</span><span style="color: #339933;">;</span><br />
@Generated<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;info.sunng.bason.BasonManager&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> BasonManager<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> BSONObject toBson<span style="color: #009900;">&#40;</span>info.<span style="color: #006633;">sunng</span>.<span style="color: #006633;">bason</span>.<span style="color: #006633;">example</span>.<span style="color: #006633;">Passenger</span> o<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anullpointerexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">NullPointerException</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; BSONObject bson <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BasicBSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; bson.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ticket&quot;</span>,o.<span style="color: #006633;">getTicketId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; bson.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span>,o.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; bson.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;createdDate&quot;</span>,o.<span style="color: #006633;">getCreatedDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; bson.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;flight&quot;</span>,toBson<span style="color: #009900;">&#40;</span>o.<span style="color: #006633;">getFlight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> bson<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> info.<span style="color: #006633;">sunng</span>.<span style="color: #006633;">bason</span>.<span style="color: #006633;">example</span>.<span style="color: #006633;">Passenger</span> fromBson<span style="color: #009900;">&#40;</span>info.<span style="color: #006633;">sunng</span>.<span style="color: #006633;">bason</span>.<span style="color: #006633;">example</span>.<span style="color: #006633;">Passenger</span> o, BSONObject bson<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> bson <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anullpointerexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">NullPointerException</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; o.<span style="color: #006633;">setTicketId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">lang</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Long</span></a><span style="color: #009900;">&#41;</span>bson.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ticket&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; o.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">lang</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#41;</span>bson.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; o.<span style="color: #006633;">setCreatedDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">util</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Date</span></a><span style="color: #009900;">&#41;</span>bson.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;createdDate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; o.<span style="color: #006633;">setFlight</span><span style="color: #009900;">&#40;</span>fromBson<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> info.<span style="color: #006633;">sunng</span>.<span style="color: #006633;">bason</span>.<span style="color: #006633;">example</span>.<span style="color: #006633;">Flight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #009900;">&#40;</span>BSONObject<span style="color: #009900;">&#41;</span>bson.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;flight&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> o<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> BSONObject toBson<span style="color: #009900;">&#40;</span>info.<span style="color: #006633;">sunng</span>.<span style="color: #006633;">bason</span>.<span style="color: #006633;">example</span>.<span style="color: #006633;">Flight</span> o<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anullpointerexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">NullPointerException</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; BSONObject bson <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BasicBSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; bson.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;company&quot;</span>,o.<span style="color: #006633;">getCompany</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; bson.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;flightId&quot;</span>,o.<span style="color: #006633;">getFlightId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> bson<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> info.<span style="color: #006633;">sunng</span>.<span style="color: #006633;">bason</span>.<span style="color: #006633;">example</span>.<span style="color: #006633;">Flight</span> fromBson<span style="color: #009900;">&#40;</span>info.<span style="color: #006633;">sunng</span>.<span style="color: #006633;">bason</span>.<span style="color: #006633;">example</span>.<span style="color: #006633;">Flight</span> o, BSONObject bson<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> bson <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anullpointerexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">NullPointerException</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; o.<span style="color: #006633;">setCompany</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">lang</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#41;</span>bson.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;company&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; o.<span style="color: #006633;">setFlightId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">lang</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#41;</span>bson.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;flightId&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> o<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The project is hosted at<br />
<a href="http://github.com/sunng87/bason">http://github.com/sunng87/bason</a></p>
<p>If you have any ideas, just let me know.</p>
<p>The post is brought to you by <a href="http://fedorahosted.org/lekhonee">lekhonee</a> v0.7</p>
]]></content:encoded>
			<wfw:commentRss>http://sunng.info/blog/2010/08/bason-a-bson-serialization-code-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RPC, Serialization and Schema</title>
		<link>http://sunng.info/blog/2010/06/rpc-serialization-and-schema/</link>
		<comments>http://sunng.info/blog/2010/06/rpc-serialization-and-schema/#comments</comments>
		<pubDate>Sat, 12 Jun 2010 08:16:45 +0000</pubDate>
		<dc:creator>Sunng</dc:creator>
				<category><![CDATA[手艺]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sergent]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://sunng.info/blog/?p=591</guid>
		<description><![CDATA[The post is brought to you by lekhonee v0.7 糖果项目的后端用Java编写，我负责service gateway的开发（暂且叫sergent），服务以Java接口+Annotation的形式声明，与Spring集成使用，Java对象被序列化为JSON和XML（通过jackson和castor）与外部系统交互。专门的JSON Schema和XML Schema是可选的，系统交互通过简明的文档和人工确认。 RPC框架是跨进程、跨系统交互的重要工具，RPC框架中又包括远程调用、网络传输和序列化反序列化等等部分。流行的工具包括Facebook的thrift，Google的Protobuf和原先Hadoop项目下的avro。其中thrift包含远程调用、反序列化、网络等等全部的功能。Protobuf本身是一个序列化反序列化库，另有很多第三方RPC实现，avro目前除了序列化和反序列化的功能，也包含了ipc的HTTP Server和SocketServer等实现。在序列化的格式方面，Thrift支持JSON和二进制协议，Protobuf本身仅有二进制支持，但已经存在第三方的其他格式实现。 avro原生支持二进制和JSON格式。 从效率上来说，二进制方式的序列化要比文本方式的快。Google Code上（最近迁往了github）有一个tpc项目（thrift-protobuf-compare），根据这个项目的最新的比较结果（与原先不同）： protobuf成为了三者中耗时最少的框架，之后是thrift和avro，这次avro的耗时甚至超过了文本方式的jackson（主要在反序列化上）。 但是二进制协议通常都需要定义Schema，thrift / protobuf / avro三者各自定义了Schema的格式，没有类似XSD和Json Schema的统一标准，也就是说，当你需要传输一个对象，就要为它编写一个Schema文件。按照通常的习惯，都是先编写Schema，然后通过命令行工具或者自动构建工具来生成Java source。对于新系统还好说，对旧系统这个改造就比较麻烦了。另外，二进制协议不便于调试，所以各个thrift/protobuf/avro先后也都有JSON的实现，在文本的序列化格式上，JSON对XML的优势是全方位的。 所以综合起来，很难说有一种完美的解决方案。二进制协议的效率高，但是改造、编写Schema的代价并不小，还要面对核心Model被绑架到具体框架的风险。文本协议开发简便，不需要Schema，直接POJO就可以序列化和反序列化，但是在时间和空间上都不如二进制的方式。 补充 从tpc项目的结果上看，kryo在时间、空间上都击败了所有对手，而且，kryo的API非常简洁，不需要Schema文件就可以序列化POJO，听起来太完美了，看来以后sergent要借鉴一下的。 补充 2010-06-14 发现avro现在也有ReflectDatumReader和ReflectDatumWriter，可以通过反射内部自动映射生成Schema，可以尝试一下。]]></description>
			<content:encoded><![CDATA[<p>The post is brought to you by <a href="http://fedorahosted.org/lekhonee">lekhonee</a> v0.7</p>
<p>糖果项目的后端用Java编写，我负责service gateway的开发（暂且叫sergent），服务以Java接口+Annotation的形式声明，与Spring集成使用，Java对象被序列化为JSON和XML（通过jackson和castor）与外部系统交互。专门的JSON Schema和XML Schema是可选的，系统交互通过简明的文档和人工确认。</p>
<p>RPC框架是跨进程、跨系统交互的重要工具，RPC框架中又包括远程调用、网络传输和序列化反序列化等等部分。流行的工具包括Facebook的<a href="http://incubator.apache.org/thrift/">thrift</a>，Google的<a href="http://code.google.com/p/protobuf/">Protobuf</a>和原先Hadoop项目下的<a href="http://avro.apache.org/">avro</a>。其中thrift包含远程调用、反序列化、网络等等全部的功能。Protobuf本身是一个序列化反序列化库，另有很多<a href="http://code.google.com/p/protobuf/wiki/ThirdPartyAddOns#RPC_Implementations">第三方RPC实现</a>，avro目前除了序列化和反序列化的功能，也包含了ipc的HTTP Server和SocketServer等实现。在序列化的格式方面，Thrift支持JSON和二进制协议，Protobuf本身<a href="http://code.google.com/p/protobuf/issues/detail?id=82">仅有二进制支持</a>，但已经存在<a href="http://code.google.com/p/protobuf-java-format/">第三方的其他格式实现</a>。 avro原生支持二进制和JSON格式。</p>
<p>从效率上来说，二进制方式的序列化要比文本方式的快。Google Code上（最近迁往了github）有一个<a href="http://wiki.github.com/eishay/jvm-serializers/">tpc项目</a>（thrift-protobuf-compare），根据这个项目的最新的比较结果（与原先不同）：<br />
<img src="http://chart.apis.google.com/chart?chtt=total&#038;chf=c||lg||0||FFFFFF||1||76A4FB||0|bg||s||EFEFEF&#038;chs=700x426&#038;chd=t:3234,4375,4583,5094,5712,6949,7199,7817,9228,9532,11160,11265,12575,12698,13233,14117,14950,16244,17187,23282,28258,31018,37012,43989,61663,74099,113707,141689,218143,225905,227399,227943,228237&#038;chds=0,251061.7483&#038;chxt=y&#038;chxl=0:|woodstox-xstream|fastinfo-xstream|xml%2Fxstream|aalto-xstream|json%2Fgoogle-gson|json%2Fprotobuf|scala%2Fjava-built-in|java-built-in|xml%2Fxstream%2Bc|fastinfo-xstream%2Bc|woodstox-xstream%2Bc|xml%2Fmanual-fastinfo|aalto-xstream%2Bc|hessian|json%2Fjackson-databind|xml%2Fmanual-woodstox|xml%2Fjavolution|json%2Fprotostuff-runtime|avro-generic|json%2Fprotostuff-core|avro|json%2Fjackson-manual|xml%2Fmanual-aalto|thrift-compact|thrift|scala%2Fsbinary|protobuf%2Factivemq%2Balt|protobuf|protobuf%2Fprotostuff-runtime|protobuf%2Fprotostuff-core|kryo|kryo-opt|java-manual&#038;chm=N%20*f*,000000,0,-1,10&#038;lklk&#038;chdlp=t&#038;chco=660000|660033|660066|660099|6600CC|6600FF|663300|663333|663366|663399|6633CC|6633FF|666600|666633|666666&#038;cht=bhg&#038;chbh=6,0,6&#038;nonsense=aaa.png" title="" alt="" /></p>
<p>protobuf成为了三者中耗时最少的框架，之后是thrift和avro，这次avro的耗时甚至超过了文本方式的jackson（主要在反序列化上）。</p>
<p>但是二进制协议通常都需要定义Schema，thrift / protobuf / avro三者各自定义了Schema的格式，没有类似XSD和Json Schema的统一标准，也就是说，当你需要传输一个对象，就要为它编写一个Schema文件。按照通常的习惯，都是先编写Schema，然后通过命令行工具或者自动构建工具来生成Java source。对于新系统还好说，对旧系统这个改造就比较麻烦了。另外，二进制协议不便于调试，所以各个thrift/protobuf/avro先后也都有JSON的实现，在文本的序列化格式上，JSON对XML的优势是全方位的。</p>
<p>所以综合起来，很难说有一种完美的解决方案。二进制协议的效率高，但是改造、编写Schema的代价并不小，还要面对核心Model被绑架到具体框架的风险。文本协议开发简便，不需要Schema，直接POJO就可以序列化和反序列化，但是在时间和空间上都不如二进制的方式。</p>
<p>补充<br />
从tpc项目的结果上看，kryo在时间、空间上都击败了所有对手，而且，kryo的API非常简洁，不需要Schema文件就可以序列化POJO，听起来太完美了，看来以后sergent要借鉴一下的。</p>
<p><strong>补充 2010-06-14</strong><br />
发现avro现在也有ReflectDatumReader和ReflectDatumWriter，可以通过反射内部自动映射生成Schema，可以尝试一下。</p>
]]></content:encoded>
			<wfw:commentRss>http://sunng.info/blog/2010/06/rpc-serialization-and-schema/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>ibatis infinite loop when getFirstResultSet</title>
		<link>http://sunng.info/blog/2010/06/ibatis-infinite-loop/</link>
		<comments>http://sunng.info/blog/2010/06/ibatis-infinite-loop/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 14:01:03 +0000</pubDate>
		<dc:creator>Sunng</dc:creator>
				<category><![CDATA[手艺]]></category>
		<category><![CDATA[ibatis]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://sunng.info/blog/?p=587</guid>
		<description><![CDATA[前几天上线后老大发现几台负载非常高，dump线程状态后发现多个线程死循环在同一处，于是发现了ibatis的这个bug： https://issues.apache.org/jira/browse/IBATIS-384 https://issues.apache.org/jira/browse/IBATIS-587 在mysql数据库上，没有结果集时，stmt.getUpdateCount()会返回0，而非-1. ibatis 2.4.3 &#160; private ResultSet getFirstResultSet&#40;StatementScope scope, Statement stmt&#41; throws SQLException &#123; &#160; &#160; ResultSet rs = null; &#160; &#160; boolean hasMoreResults = true; &#160; &#160; while &#40;hasMoreResults&#41; &#123; &#160; &#160; &#160; rs = stmt.getResultSet&#40;&#41;; &#160; &#160; &#160; if &#40;rs != null&#41; &#123; &#160; &#160; &#160; &#160; break; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>前几天上线后老大发现几台负载非常高，dump线程状态后发现多个线程死循环在同一处，于是发现了ibatis的这个bug：</p>
<p>https://issues.apache.org/jira/browse/IBATIS-384</p>
<p>https://issues.apache.org/jira/browse/IBATIS-587</p>
<p>在mysql数据库上，没有结果集时，<em>stmt.getUpdateCount()</em>会返回0，而非-1.<br />
ibatis 2.4.3</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aresultset+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ResultSet</span></a> getFirstResultSet<span style="color: #009900;">&#40;</span>StatementScope scope, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Statement</span></a> stmt<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asqlexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">SQLException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aresultset+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ResultSet</span></a> rs <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> hasMoreResults <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>hasMoreResults<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; rs <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">getResultSet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rs <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; hasMoreResults <span style="color: #339933;">=</span> moveToNextResultsIfPresent<span style="color: #009900;">&#40;</span>scope, stmt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> rs<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> moveToNextResultsIfPresent<span style="color: #009900;">&#40;</span>StatementScope scope, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Statement</span></a> stmt<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asqlexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">SQLException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> moreResults<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// This is the messed up JDBC approach for determining if there are more results</span><br />
&nbsp; &nbsp; moreResults <span style="color: #339933;">=</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>moveToNextResultsSafely<span style="color: #009900;">&#40;</span>scope, stmt<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>stmt.<span style="color: #006633;">getUpdateCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> moreResults<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> moveToNextResultsSafely<span style="color: #009900;">&#40;</span>StatementScope scope, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Statement</span></a> stmt<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asqlexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">SQLException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>forceMultipleResultSetSupport<span style="color: #009900;">&#40;</span>scope<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> stmt.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMetaData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">supportsMultipleResultSets</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> stmt.<span style="color: #006633;">getMoreResults</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>moreResults恒为真，程序出现死循环。</p>
<p>在mybatis 2.5的代码里，这部分已经修改为：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> moveToNextResultsIfPresent<span style="color: #009900;">&#40;</span>StatementScope scope, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Statement</span></a> stmt<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asqlexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">SQLException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> moreResults<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// This is the messed up JDBC approach for determining if there are more results</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> movedToNextResultsSafely <span style="color: #339933;">=</span> moveToNextResultsSafely<span style="color: #009900;">&#40;</span>scope, stmt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> updateCount <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">getUpdateCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; moreResults <span style="color: #339933;">=</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>movedToNextResultsSafely <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>updateCount <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//ibatis-384: workaround for mysql not returning -1 for stmt.getUpdateCount()</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>moreResults <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; moreResults <span style="color: #339933;">=</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>movedToNextResultsSafely <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>isMultipleResultSetSupportPresent<span style="color: #009900;">&#40;</span>scope, stmt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> moreResults<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> moveToNextResultsSafely<span style="color: #009900;">&#40;</span>StatementScope scope, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Statement</span></a> stmt<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asqlexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">SQLException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isMultipleResultSetSupportPresent<span style="color: #009900;">&#40;</span>scope, stmt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> stmt.<span style="color: #006633;">getMoreResults</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp;* checks whether multiple result set support is present - either by direct support of the database driver or by forcing it<br />
&nbsp; &nbsp;*/</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> isMultipleResultSetSupportPresent<span style="color: #009900;">&#40;</span>StatementScope scope,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Statement</span></a> stmt<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asqlexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">SQLException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> forceMultipleResultSetSupport<span style="color: #009900;">&#40;</span>scope<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> stmt.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMetaData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">supportsMultipleResultSets</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>这部分条件判断实在很极致了。</p>
<p>新的实现当getUpdateResult是0时，moreResults恒为真，这时再进行一个判断，如果是由于isMultipleResultSetSupportPresent为false导致了moveToNextResultsSafely为false，那么实际moreResults应是false</p>
<p>MyBatis 2.5还有一个issue没有解决，离发布还有一些时间，这个问题只好签出新版本代码自己build了</p>
]]></content:encoded>
			<wfw:commentRss>http://sunng.info/blog/2010/06/ibatis-infinite-loop/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	<georss:point>31.2013666 121.6192502</georss:point>	</item>
		<item>
		<title>Maven recipe #1: Test Java code with groovy</title>
		<link>http://sunng.info/blog/2010/05/maven-recipe-1-test-java-code-with-groovy/</link>
		<comments>http://sunng.info/blog/2010/05/maven-recipe-1-test-java-code-with-groovy/#comments</comments>
		<pubDate>Tue, 18 May 2010 17:43:40 +0000</pubDate>
		<dc:creator>Sunng</dc:creator>
				<category><![CDATA[把戏]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://sunng.info/blog/?p=573</guid>
		<description><![CDATA[问题： 受够了，不想写Java了，写个含有数据的map还要new出来一个一个put进去，想用groovy解决单元测试 解决： gmaven+groovy eclipse插件可以解决这个需求 在pom.xml中添加gmaven的依赖，注意，仅用来测试。老大不让生产代码里有不可靠的东西。 ... &#160; &#160; &#160; &#160; &#60;dependency&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;groupId&#62;org.codehaus.groovy.maven.runtime&#60;/groupId&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;artifactId&#62;gmaven-runtime-default&#60;/artifactId&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;version&#62;1.0-rc-3&#60;/version&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;scope&#62;test&#60;/scope&#62; &#160; &#160; &#160; &#160; &#60;/dependency&#62; ... &#160; &#160; &#160; &#160; &#160; &#160; &#60;plugin&#62; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>问题： 受够了，不想写Java了，写个含有数据的map还要new出来一个一个put进去，想用groovy解决单元测试<br />
解决： gmaven+groovy eclipse插件可以解决这个需求<br />
在pom.xml中添加gmaven的依赖，注意，仅用来测试。老大不让生产代码里有不可靠的东西。</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">...<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.codehaus.groovy.maven.runtime<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>gmaven-runtime-default<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0-rc-3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.codehaus.groovy.maven<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>gmaven-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0-rc-3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>generateStubs<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>compile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>generateTestStubs<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>testCompile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>在eclipse中新建source folder, <em>src/test/groovy</em>，在其中创建groovy test case即可</p>
]]></content:encoded>
			<wfw:commentRss>http://sunng.info/blog/2010/05/maven-recipe-1-test-java-code-with-groovy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<georss:point>31.203057 121.6174866</georss:point>	</item>
		<item>
		<title>Maven recipe #0</title>
		<link>http://sunng.info/blog/2010/05/maven-recipe-0/</link>
		<comments>http://sunng.info/blog/2010/05/maven-recipe-0/#comments</comments>
		<pubDate>Sat, 15 May 2010 17:02:26 +0000</pubDate>
		<dc:creator>Sunng</dc:creator>
				<category><![CDATA[手艺]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[recipe]]></category>

		<guid isPermaLink="false">http://sunng.info/blog/?p=571</guid>
		<description><![CDATA[问题： 多个root pom的dependencyManagement有重复的内容，希望统一管理。 解决： 新建一个空pom.xml，在dependencyManagement中指定这些依赖，如 &#60;project xmlns=&#34;http://maven.apache.org/POM/4.0.0&#34; xmlns:xsi=&#34;http://www.w3.org/2001/XMLSchema-instance&#34; &#160; &#160; xsi:schemaLocation=&#34;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&#34;&#62; &#160; &#160; &#60;modelVersion&#62;4.0.0&#60;/modelVersion&#62; &#160; &#160; &#60;groupId&#62;info.sunng&#60;/groupId&#62; &#160; &#160; &#60;artifactId&#62;root&#60;/artifactId&#62; &#160; &#160; &#60;packaging&#62;pom&#60;/packaging&#62; &#160; &#160; &#60;version&#62;0.0.1-SNAPSHOT&#60;/version&#62; &#160; &#160; &#60;dependencyManagement&#62; &#160; &#160; &#160; &#160; &#60;dependencies&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;dependency&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#60;groupId&#62;info.sunng&#60;/groupId&#62; &#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>问题： 多个root pom的dependencyManagement有重复的内容，希望统一管理。<br />
解决：<br />
新建一个空pom.xml，在dependencyManagement中指定这些依赖，如</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>info.sunng<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>root<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>pom<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.0.1-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencyManagement<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>info.sunng<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>X<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.0.1-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencyManagement<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>在其他root pom中添加这样的dependencyManagement</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">....<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>info.sunng<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>root<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.0.1-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>pom<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>import<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
....</div></div>
]]></content:encoded>
			<wfw:commentRss>http://sunng.info/blog/2010/05/maven-recipe-0/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	<georss:point>31.203057 121.6174866</georss:point>	</item>
		<item>
		<title>My First Hello World Web App Using Compojure</title>
		<link>http://sunng.info/blog/2010/04/my-first-hello-world-web-app-using-compojure/</link>
		<comments>http://sunng.info/blog/2010/04/my-first-hello-world-web-app-using-compojure/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 00:36:57 +0000</pubDate>
		<dc:creator>Sunng</dc:creator>
				<category><![CDATA[把戏]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[compojure]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[lein]]></category>

		<guid isPermaLink="false">http://sunng.info/blog/?p=550</guid>
		<description><![CDATA[Compojure是一个用Clojure写成的类似Sinatra的Web框架。Leiningen是一个新的Clojure构建工具，它用Maven来处理依赖管理，而通过封装Lancet（基于Ant）来实现build-in的task。 以上是背景介绍。以下是HelloWorld。 创建一个目录作为工程目录 mkdir compojure-app cd compojure-app 像创建build.xml和pom.xml一样创建project.clj &#40;defproject info.sunng/compojure-app &#34;0.0.1&#34; &#160; &#160; &#160; &#160; :description &#34;A demo app running on compojure framework&#34; &#160; &#160; &#160; &#160; :dependencies &#91;&#91;org.clojure/clojure &#34;1.1.0&#34;&#93; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#91;org.clojure/clojure-contrib &#34;1.1.0&#34;&#93; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/weavejester/compojure">Compojure</a>是一个用<a href="http://clojure.org/">Clojure</a>写成的类似<a href="http://www.sinatrarb.com/">Sinatra</a>的Web框架。<a href="http://github.com/technomancy/leiningen">Leiningen</a>是一个新的Clojure构建工具，它用Maven来处理依赖管理，而通过封装<a href="http://github.com/stuarthalloway/lancet">Lancet</a>（基于Ant）来实现build-in的task。</p>
<p>以上是背景介绍。以下是HelloWorld。</p>
<p>创建一个目录作为工程目录</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> compojure-app<br />
<span style="color: #7a0874; font-weight: bold;">cd</span> compojure-app</div></div>
<p>像创建build.xml和pom.xml一样创建project.clj</p>
<div class="codecolorer-container lisp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>defproject info<span style="color: #66cc66;">.</span>sunng/compojure-app <span style="color: #ff0000;">&quot;0.0.1&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">:</span><span style="color: #555;">description</span> <span style="color: #ff0000;">&quot;A demo app running on compojure framework&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">:</span><span style="color: #555;">dependencies</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>org<span style="color: #66cc66;">.</span>clojure/clojure <span style="color: #ff0000;">&quot;1.1.0&quot;</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>org<span style="color: #66cc66;">.</span>clojure/clojure-contrib <span style="color: #ff0000;">&quot;1.1.0&quot;</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>compojure <span style="color: #ff0000;">&quot;0.3.2&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">:</span><span style="color: #555;">dev-dependencies</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>leiningen/lein-swank <span style="color: #ff0000;">&quot;1.1.0&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">:</span><span style="color: #555;">main</span> info<span style="color: #66cc66;">.</span>sunng<span style="color: #66cc66;">.</span>compojureapp<span style="color: #66cc66;">.</span>helloworld<span style="color: #66cc66;">&#41;</span></div></div>
<p>首行定义了项目的groupId, artifactId和version，其后的是maven风格的依赖定义，最后我们还定义了程序的主类。</p>
<p>这个helloworld只有一个文件，被放在src/info/sunng/compojureapp/目录下</p>
<div class="codecolorer-container lisp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>ns info<span style="color: #66cc66;">.</span>sunng<span style="color: #66cc66;">.</span>compojureapp<span style="color: #66cc66;">.</span>helloworld <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">gen-class</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">use</span> compojure<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #66cc66;">&#40;</span>defroutes example-routes<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">GET</span> <span style="color: #ff0000;">&quot;/&quot;</span> <span style="color: #ff0000;">&quot;Hello W0rld&quot;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>ANY <span style="color: #ff0000;">&quot;*&quot;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">404</span> <span style="color: #ff0000;">&quot;Page Not Found&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #66cc66;">&#40;</span>defn -main <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>run-server <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">:</span><span style="color: #555;">port</span> <span style="color: #cc66cc;">8080</span><span style="color: #66cc66;">&#125;</span> <span style="color: #ff0000;">&quot;/*&quot;</span> <span style="color: #66cc66;">&#40;</span>servlet example-routes<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>中间就是Sinatra风格的URL映射定义，最后在主类中通过一个run-server方法以嵌入式的方式运行一个jetty</p>
<p>回到工程目录，执行构建</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">lein deps<br />
lein compile<br />
lein uberjar</div></div>
<p>通过uberjar可以将依赖通通打入一个jar包中，接着就可以通过<br />
<em>java -jar compojure-app-standalone.jar </em><br />
启动你的Web程序了，很酷吧</p>
<p>除了这种方式，还可以通过<br />
<em>lein swank</em><br />
启动一个<a href="http://github.com/technomancy/leiningen/tree/master/lein-swank">swank</a> server（project.clj中定义了dev-dependency），再用Emacs的<a href="http://common-lisp.net/project/slime/">SLIME</a>交互式地运行程序。</p>
<p>参考</p>
<ul>
<li><a href="http://kzar.co.uk/blog/view/simple-compojure-project">http://kzar.co.uk/blog/view/simple-compojure-project</a></li>
<li><a href="http://schipplock.de/2010/01/31/clojure-web-framework-compojure/">http://schipplock.de/2010/01/31/clojure-web-framework-compojure/</a></li>
<li><a href="http://zef.me/2470/building-clojure-projects-with-leiningen">http://zef.me/2470/building-clojure-projects-with-leiningen</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://sunng.info/blog/2010/04/my-first-hello-world-web-app-using-compojure/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<georss:point>31.203057 121.6174866</georss:point>	</item>
		<item>
		<title>We are using Hudson for continuous integration</title>
		<link>http://sunng.info/blog/2010/04/we-are-using-hudson-for-continuous-integration/</link>
		<comments>http://sunng.info/blog/2010/04/we-are-using-hudson-for-continuous-integration/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 13:49:19 +0000</pubDate>
		<dc:creator>Sunng</dc:creator>
				<category><![CDATA[装备]]></category>
		<category><![CDATA[CI]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[hudson]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[tool]]></category>

		<guid isPermaLink="false">http://sunng.info/blog/?p=533</guid>
		<description><![CDATA[经历了持续两周的人肉集成，今天上午抢得一台Linux机器，终于尝试用hudson来替代人肉构建。 Hudson的安装和配置远比想象的简单，只要下载发布的war包，在相应的目录执行 nohup java -jar hudson.war > hudson.log 2>&#038;1 &#038; 即可启动到后台 hudson的web图形界面可以胜任几乎全部工作。我们主要使用maven来构建项目，hudson提供了非常强大的功能：邮件提醒（通过插件支持twitter/jabber/irc提醒）；自动构建，除了基本的定时构建以外，hudson还会自动解析其管理的项目之间的依赖关系，从而实现级联的构建，这个功能非常震撼。 刚刚上手以后我还安装了两个插件。一个是build-timeout插件，可以之间一次构建的超时时间：我们的项目中有老大写的交互式的maven配置插件，一旦这个插件在自动构建时运行会阻塞构建的流程。另一个是scp发布插件，可以自动scp一个文件到远程服务器上，我用这个插件来把构建版本发布到运行环境中，只要在适当的时候重启一下运行环境的服务器就可以实现部署了。不过scp插件由于上游依赖的问题貌似不支持putty生成的privatekey，这是暂时的美中不足。 配置了整整一天，终于有了CI工具，我就彻底解放出来可以做其他事了。]]></description>
			<content:encoded><![CDATA[<p>经历了持续两周的人肉集成，今天上午抢得一台Linux机器，终于尝试用hudson来替代人肉构建。</p>
<p>Hudson的安装和配置远比想象的简单，只要下载发布的war包，在相应的目录执行<br />
nohup java -jar hudson.war > hudson.log 2>&#038;1 &#038;<br />
即可启动到后台</p>
<p>hudson的web图形界面可以胜任几乎全部工作。我们主要使用maven来构建项目，hudson提供了非常强大的功能：邮件提醒（通过插件支持twitter/jabber/irc提醒）；自动构建，除了基本的定时构建以外，hudson还会自动解析其管理的项目之间的依赖关系，从而实现级联的构建，这个功能非常震撼。</p>
<p>刚刚上手以后我还安装了两个插件。一个是build-timeout插件，可以之间一次构建的超时时间：我们的项目中有老大写的交互式的maven配置插件，一旦这个插件在自动构建时运行会阻塞构建的流程。另一个是scp发布插件，可以自动scp一个文件到远程服务器上，我用这个插件来把构建版本发布到运行环境中，只要在适当的时候重启一下运行环境的服务器就可以实现部署了。不过scp插件由于上游依赖的问题貌似不支持putty生成的privatekey，这是暂时的美中不足。</p>
<p>配置了整整一天，终于有了CI工具，我就彻底解放出来可以做其他事了。</p>
]]></content:encoded>
			<wfw:commentRss>http://sunng.info/blog/2010/04/we-are-using-hudson-for-continuous-integration/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	<georss:point>31.203057 121.6174866</georss:point>	</item>
		<item>
		<title>Update</title>
		<link>http://sunng.info/blog/2010/04/update/</link>
		<comments>http://sunng.info/blog/2010/04/update/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 01:00:40 +0000</pubDate>
		<dc:creator>Sunng</dc:creator>
				<category><![CDATA[自话]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[snda]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://sunng.info/blog/?p=528</guid>
		<description><![CDATA[太忙了，真的太忙了，每天重复无数遍 mvn clean install -Dmaven.test.skip， 一不小心再遇上&#8221;cidaemon.exe&#8221;锁死文件无法删除，或者SVN报点Tree conflict，无法见证奇迹就只剩下悲剧了。都是重复的工作，但是在Windows下想做automation还是一筹莫展，比如要构建多个maven项目，也不知道怎么自动化地判断maven构建成功还是失败；再者在Windows里也不知道怎么自动化的scp文件到服务器上去，还要ssh执行重启服务的命令；手足无措啊。 最严重的问题是每天忙的要命，但是并没有感觉有应有的收获或者长进什么的，折腾了半天做的事情还是自己熟悉的小方面，只是做得相对变态而已。每天新的情况让你根本来不及重构，而且严重的问题是，重构好了又有什么用呢，谁能想到后面又有什么变化把你自己引以为豪的设计通通推翻。程序员要考虑的事情太多了，常常要揣摩一些需求方的心思，偷偷摸摸地提前做一些准备。 我现在已经有了一个annotation driven的自动暴露Java接口的框架。之前在CRL实习时，在Jazz上开发也是用一个类似的机制，当时是使用接口的命名规范来确定暴露，比如说有以get和post开头的接口都暴露为HTTP服务，就有很多类似postUpdate、postAdd之类的名字，昨天同事说这个名字太诡异。后来参考了淘宝Open平台的实现，通通的XML描述接口，服务名、方法名、参数名、约束通通写XML配置，我懒，也放弃这个方案了。最后选择用annotation，通过反射解析Java接口，实现的效果和前两者差不多，自认为相对名称的约定要灵活一些，相对无数XML描述符要简洁一些。 后来还为方法的每一步调用都加上监听器，每一个环节都可以注册外部自定义的监听器，所有的核心功能之外的需求都放到自定义的监听器里处理，和框架核心剥离。 再者，框架原本直接序列化接口返回的对象，后来需求有变，要加各种各样的包装。OK，弄一个WrapperFacotry的接口出去，想怎么搞也放到框架核心之外了。Spring的@Authwire(required=false)真是解决了许多问题，在实现InitializingBean初始化一个默认实现就可以了，这样在applicationContext里可以轻松控制外部功能。 还有的发现，序列化的框架分别用的是castor和jackson，结果做profile的时候一比吓一跳，jackson序列话的时间占整个调用的20%，castor的就占到50%了，太可怕了，恐怕要试试其他实现了。 细节扯多了，其实框架的核心都是一天写成的，后面加各种各样的功能花了很大精力。 最近升级到UBuntu10.04已经可以见证奇迹了，眼看着Bug越来越少，之前启动打在tty1上的ureadahead错误日志现在也隐藏了。一切趋于完美，新indicator菜单很好用。 清明回家，下午的火车。因为回家周一不能加班了，少了3倍的工资，悲凉啊。]]></description>
			<content:encoded><![CDATA[<p>太忙了，真的太忙了，每天重复无数遍 mvn clean install -Dmaven.test.skip， 一不小心再遇上&#8221;cidaemon.exe&#8221;锁死文件无法删除，或者SVN报点Tree conflict，无法见证奇迹就只剩下悲剧了。都是重复的工作，但是在Windows下想做automation还是一筹莫展，比如要构建多个maven项目，也不知道怎么自动化地判断maven构建成功还是失败；再者在Windows里也不知道怎么自动化的scp文件到服务器上去，还要ssh执行重启服务的命令；手足无措啊。</p>
<p>最严重的问题是每天忙的要命，但是并没有感觉有应有的收获或者长进什么的，折腾了半天做的事情还是自己熟悉的小方面，只是做得相对变态而已。每天新的情况让你根本来不及重构，而且严重的问题是，重构好了又有什么用呢，谁能想到后面又有什么变化把你自己引以为豪的设计通通推翻。程序员要考虑的事情太多了，常常要揣摩一些需求方的心思，偷偷摸摸地提前做一些准备。</p>
<p>我现在已经有了一个annotation driven的自动暴露Java接口的框架。之前在CRL实习时，在Jazz上开发也是用一个类似的机制，当时是使用接口的命名规范来确定暴露，比如说有以get和post开头的接口都暴露为HTTP服务，就有很多类似postUpdate、postAdd之类的名字，昨天同事说这个名字太诡异。后来参考了淘宝Open平台的实现，通通的XML描述接口，服务名、方法名、参数名、约束通通写XML配置，我懒，也放弃这个方案了。最后选择用annotation，通过反射解析Java接口，实现的效果和前两者差不多，自认为相对名称的约定要灵活一些，相对无数XML描述符要简洁一些。</p>
<p>后来还为方法的每一步调用都加上监听器，每一个环节都可以注册外部自定义的监听器，所有的核心功能之外的需求都放到自定义的监听器里处理，和框架核心剥离。</p>
<p>再者，框架原本直接序列化接口返回的对象，后来需求有变，要加各种各样的包装。OK，弄一个WrapperFacotry的接口出去，想怎么搞也放到框架核心之外了。Spring的@Authwire(required=false)真是解决了许多问题，在实现InitializingBean初始化一个默认实现就可以了，这样在applicationContext里可以轻松控制外部功能。</p>
<p>还有的发现，序列化的框架分别用的是castor和jackson，结果做profile的时候一比吓一跳，jackson序列话的时间占整个调用的20%，castor的就占到50%了，太可怕了，恐怕要试试其他实现了。</p>
<p>细节扯多了，其实框架的核心都是一天写成的，后面加各种各样的功能花了很大精力。</p>
<p>最近升级到UBuntu10.04已经可以见证奇迹了，眼看着Bug越来越少，之前启动打在tty1上的ureadahead错误日志现在也隐藏了。一切趋于完美，新indicator菜单很好用。</p>
<p>清明回家，下午的火车。因为回家周一不能加班了，少了3倍的工资，悲凉啊。</p>
]]></content:encoded>
			<wfw:commentRss>http://sunng.info/blog/2010/04/update/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>New features in JTS 1.11</title>
		<link>http://sunng.info/blog/2010/03/new-features-in-jts-1-11/</link>
		<comments>http://sunng.info/blog/2010/03/new-features-in-jts-1-11/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 05:16:08 +0000</pubDate>
		<dc:creator>Sunng</dc:creator>
				<category><![CDATA[装备]]></category>
		<category><![CDATA[GIS]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://sunng.info/blog/?p=505</guid>
		<description><![CDATA[JTS最近发布了1.11版本，新增了： 对Delaunay三角网、Voronoi多边形的支持； 把Geometry对象转换为AWT的Shape对象的功能 对几何对象进行densify的操作（增加结点）； 计算Hausdorff相似度和Area相似度的支持 计算Delaunay三角网和Voronoi多边形： import java.util.ArrayList; import java.util.Collection; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryCollection; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.triangulate.DelaunayTriangulationBuilder; import com.vividsolutions.jts.triangulate.VoronoiDiagramBuilder; /** &#160;* &#160;* @author Sun Ning/SNDA &#160;* @since 2010-3-3 &#160;*/ public class DelaunayAndVoronoiApp &#123; &#160; &#160; /** &#160; &#160; &#160;* create some predefined sites &#160; &#160; &#160;* @return &#160; &#160; &#160;*/ &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://lin-ear-th-inking.blogspot.com/2010/03/jts-version-111-released.html">JTS最近发布了1.11版本</a>，新增了：</p>
<ul>
<li>对<a href="http://en.wikipedia.org/wiki/Delaunay_triangulation">Delaunay三角网</a>、<a href="http://en.wikipedia.org/wiki/Voronoi">Voronoi多边形</a>的支持；</li>
<li>把Geometry对象转换为AWT的Shape对象的功能</li>
<li>对几何对象进行densify的操作（增加结点）；</li>
<li>计算<a href="http://en.wikipedia.org/wiki/Hausdorff_distance">Hausdorff相似度</a>和Area相似度的支持</li>
</ul>
<p>计算Delaunay三角网和Voronoi多边形：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Collection</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.vividsolutions.jts.geom.Coordinate</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.vividsolutions.jts.geom.Geometry</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.vividsolutions.jts.geom.GeometryCollection</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.vividsolutions.jts.geom.GeometryFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.vividsolutions.jts.triangulate.DelaunayTriangulationBuilder</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.vividsolutions.jts.triangulate.VoronoiDiagramBuilder</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author Sun Ning/SNDA<br />
&nbsp;* @since 2010-3-3<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DelaunayAndVoronoiApp <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* create some predefined sites<br />
&nbsp; &nbsp; &nbsp;* @return<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Collection<span style="color: #339933;">&lt;</span>coordinate<span style="color: #339933;">&gt;</span> getPredefinedSites<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> coords <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">27</span><span style="color: #009900;">&#125;</span>,<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">28</span>, <span style="color: #cc66cc;">50</span><span style="color: #009900;">&#125;</span>,<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">29</span>, <span style="color: #cc66cc;">40</span><span style="color: #009900;">&#125;</span>,<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">32</span>, <span style="color: #cc66cc;">90</span><span style="color: #009900;">&#125;</span>, <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">12</span>, <span style="color: #cc66cc;">26</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<span style="color: #339933;">&lt;</span>coordinate<span style="color: #339933;">&gt;</span> coordinates <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>coordinate<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>coords.<span style="color: #006633;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>coords.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; coordinates.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Coordinate<span style="color: #009900;">&#40;</span>coords<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>, coords<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> coordinates<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @param coords<br />
&nbsp; &nbsp; &nbsp;* @return a geometry collection of triangulations<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Geometry buildDelaunayTriangulation<span style="color: #009900;">&#40;</span>Collection<span style="color: #339933;">&lt;</span>coordinate<span style="color: #339933;">&gt;</span> coords<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; DelaunayTriangulationBuilder builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DelaunayTriangulationBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; builder.<span style="color: #006633;">setSites</span><span style="color: #009900;">&#40;</span>coords<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> builder.<span style="color: #006633;">getTriangles</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> GeometryFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @param coords<br />
&nbsp; &nbsp; &nbsp;* @return a collection of polygons<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Geometry buildVoronoiDiagram<span style="color: #009900;">&#40;</span>Collection<span style="color: #339933;">&lt;</span>coordinate<span style="color: #339933;">&gt;</span> coords<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; VoronoiDiagramBuilder builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> VoronoiDiagramBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; builder.<span style="color: #006633;">setSites</span><span style="color: #009900;">&#40;</span>coords<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> builder.<span style="color: #006633;">getDiagram</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> GeometryFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @param args<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Collection<span style="color: #339933;">&lt;</span>coordinate<span style="color: #339933;">&gt;</span> coordinates <span style="color: #339933;">=</span> getPredefinedSites<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Delauny<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; GeometryCollection triangulations<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>GeometryCollection<span style="color: #009900;">&#41;</span>buildDelaunayTriangulation<span style="color: #009900;">&#40;</span>coordinates<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> total <span style="color: #339933;">=</span> triangulations.<span style="color: #006633;">getNumGeometries</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Total triangulations: %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, total<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>total<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Geometry g <span style="color: #339933;">=</span> triangulations.<span style="color: #006633;">getGeometryN</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Coordinate<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> coords <span style="color: #339933;">=</span> g.<span style="color: #006633;">getCoordinates</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Triangulation %d: &quot;</span>, i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>Coordinate c <span style="color: #339933;">:</span> coords<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(%.3f, %.3f) &quot;</span>, c.<span style="color: #006633;">x</span>, c.<span style="color: #006633;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Voronoi<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; GeometryCollection diagram <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>GeometryCollection<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buildVoronoiDiagram<span style="color: #009900;">&#40;</span>coordinates<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> totalDia <span style="color: #339933;">=</span> diagram.<span style="color: #006633;">getNumGeometries</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>totalDia<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Geometry g <span style="color: #339933;">=</span> diagram.<span style="color: #006633;">getGeometryN</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Coordinate<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> coords <span style="color: #339933;">=</span> g.<span style="color: #006633;">getCoordinates</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Diagram %d: &quot;</span>, i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>Coordinate c <span style="color: #339933;">:</span> coords<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(%.3f, %.3f) &quot;</span>, c.<span style="color: #006633;">x</span>, c.<span style="color: #006633;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>输出：</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Total triangulations: 5<br />
Triangulation 0: (32.000, 90.000) (12.000, 26.000) (28.000, 50.000) (32.000, 90.000)<br />
Triangulation 1: (32.000, 90.000) (28.000, 50.000) (100.000, 27.000) (32.000, 90.000)<br />
Triangulation 2: (100.000, 27.000) (28.000, 50.000) (29.000, 40.000) (100.000, 27.000)<br />
Triangulation 3: (100.000, 27.000) (29.000, 40.000) (12.000, 26.000) (100.000, 27.000)<br />
Triangulation 4: (12.000, 26.000) (29.000, 40.000) (28.000, 50.000) (12.000, 26.000)<br />
Diagram 0: (-76.000, 88.625) (-76.000, 178.000) (176.713, 178.000) (72.699, 65.730) (-38.235, 76.824) (-76.000, 88.625)<br />
Diagram 1: (-76.000, -62.000) (-76.000, 88.625) (-38.235, 76.824) (11.978, 43.348) (56.422, -10.619) (57.006, -62.000) (-76.000, -62.000)<br />
Diagram 2: (11.978, 43.348) (-38.235, 76.824) (72.699, 65.730) (67.316, 48.882) (11.978, 43.348)<br />
Diagram 3: (176.713, 178.000) (188.000, 178.000) (188.000, -62.000) (57.006, -62.000) (56.422, -10.619) (67.316, 48.882) (72.699, 65.730) (176.713, 178.000)<br />
Diagram 4: (11.978, 43.348) (67.316, 48.882) (56.422, -10.619) (11.978, 43.348)</div></div>
<p>将Geometry对象转换成Shape对象，绘制在JPanel上：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Color</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Dimension</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Graphics</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Graphics2D</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Shape</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Collection</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.JFrame</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.JPanel</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.vividsolutions.jts.awt.ShapeWriter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.vividsolutions.jts.geom.Coordinate</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.vividsolutions.jts.geom.Geometry</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Random</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;*<br />
&nbsp;* @author Sun Ning/SNDA<br />
&nbsp;* @since 2010-3-3<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JTS2Awt <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> showUI<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ashape+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Shape</span></a>... <span style="color: #006633;">shape</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajframe+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JFrame</span></a> jframe <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajframe+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JFrame</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JTS Geometry to AWT Shape&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajpanel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JPanel</span></a> jp <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajpanel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JPanel</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> paint<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Agraphics+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Graphics</span></a> g<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">paint</span><span style="color: #009900;">&#40;</span>g<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Agraphics2d+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Graphics2D</span></a> g2d <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Agraphics2d+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Graphics2D</span></a><span style="color: #009900;">&#41;</span>g<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>shape <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ashape+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Shape</span></a> s<span style="color: #339933;">:</span> shape<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acolor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Color</span></a><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acolor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Color</span></a>.<span style="color: #006633;">HSBtoRGB</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arandom+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Random</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">nextFloat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, 1f, 0.6f<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.<span style="color: #006633;">draw</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jp.<span style="color: #006633;">setPreferredSize</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adimension+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Dimension</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">150</span>, <span style="color: #cc66cc;">150</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; jframe.<span style="color: #006633;">getContentPane</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>jp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jframe.<span style="color: #006633;">setDefaultCloseOperation</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajframe+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JFrame</span></a>.<span style="color: #006633;">EXIT_ON_CLOSE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jframe.<span style="color: #006633;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jframe.<span style="color: #006633;">setVisible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ashape+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Shape</span></a> toShape<span style="color: #009900;">&#40;</span>Geometry geom<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ShapeWriter writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ShapeWriter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> writer.<span style="color: #006633;">toShape</span><span style="color: #009900;">&#40;</span>geom<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Collection<span style="color: #339933;">&lt;</span>coordinate<span style="color: #339933;">&gt;</span> coords <span style="color: #339933;">=</span> DelaunayAndVoronoiApp.<span style="color: #006633;">getPredefinedSites</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Geometry geomT <span style="color: #339933;">=</span> DelaunayAndVoronoiApp.<span style="color: #006633;">buildDelaunayTriangulation</span><span style="color: #009900;">&#40;</span>coords<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Geometry geomD <span style="color: #339933;">=</span> DelaunayAndVoronoiApp.<span style="color: #006633;">buildVoronoiDiagram</span><span style="color: #009900;">&#40;</span>coords<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; showUI<span style="color: #009900;">&#40;</span>toShape<span style="color: #009900;">&#40;</span>geomT<span style="color: #009900;">&#41;</span>, toShape<span style="color: #009900;">&#40;</span>geomD<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p><a href="http://www.flickr.com/photos/40741608@N08/4403349474/" title="jts by 贝小塔, on Flickr"><img src="http://farm5.static.flickr.com/4010/4403349474_cc3db44369_o.png" width="158" height="177" alt="jts" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://sunng.info/blog/2010/03/new-features-in-jts-1-11/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<georss:point>31.203057 121.6174866</georss:point>	</item>
		<item>
		<title>Suppressing simple-xml&#039;s class attribute</title>
		<link>http://sunng.info/blog/2010/02/suppressing-simple-xmls-class-attribute/</link>
		<comments>http://sunng.info/blog/2010/02/suppressing-simple-xmls-class-attribute/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 07:07:12 +0000</pubDate>
		<dc:creator>Sunng</dc:creator>
				<category><![CDATA[手艺]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://sunng.info/blog/?p=452</guid>
		<description><![CDATA[Simple-xml is an object-xml serialization and de-serialization framework. It&#8217;s featured by annotation-driven, light-weight and self-contained. In simple-xml, pojos that will be serialized are annotated with @Root, @Element, @Attribute or @Text. The problem is, when handling with inheritance, if the actual type is different from declared type, simple-xml will add a &#8220;class&#8221; attribute for de-serialization consideration. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://simple.sourceforge.net" target="_blank">Simple-xml</a> is an object-xml serialization and de-serialization framework. It&#8217;s featured by annotation-driven, light-weight and self-contained.</p>
<p>In simple-xml, pojos that will be serialized are annotated with @Root, @Element, @Attribute or @Text. The problem is, when handling with inheritance, if the actual type is different from declared type, simple-xml will add a &#8220;class&#8221; attribute for de-serialization consideration. That is, the parser will use this attribute to determine the schema of the xml element. However, this is of no useful when we do not parse xml by simple-xml. And there is also risk that the class attribute exposes our program&#8217;s internal structure.</p>
<p>Take following code as example:</p>
<pre class="brush:java">@Root
public class Response{
    @Element
    private Object entry;

    // getter and setter ...
}

public class OrderItem{
    @Element
    private String name;

    // getter and setter ...
}</pre>
<p>When you generate xml from classes above, you will have the entry element of response with an attribute &#8220;class&#8221;, which value is the qualified class name, like &#8220;package.OrderItem&#8221;.</p>
<p>This is done by simple-xml&#8217;s Strategy interface. By default, the Persister uses TreeStrategy which has an implementation of setElement like:</p>
<pre class="brush:java">/**
 * This is used to attach a attribute to the provided element
 * that is used to identify the class. The attribute name is
 * "class" and has the value of the fully qualified class
 * name for the object provided. This will only be invoked
 * if the object class is different from the field class.
 *
 * @param type this is the declared class for the field used
 * @param value this is the instance variable being serialized
 * @param node this is the element used to represent the value
 * @param map this is used to maintain contextual information
 *
 * @return this returns true if serialization is complete
 */
public boolean setElement(Type type, Object value, NodeMap node, Map map){
    Class actual = value.getClass();
    Class expect = type.getType();
    Class real = actual;
    if(actual.isArray()) {
        real = setArray(expect, value, node);
    }
    if(actual != expect) {
        node.put(label, real.getName());
    }
    return false;
}</pre>
<p>This is where class attributed. So to suppress the attribute, we simply override this method by inherit TreeStrategy. I take a inline class for convenience here.</p>
<pre class="brush:java">Serializer s = new Persister(new TreeStrategy(){
    @Override
    public boolean setElement(Type type, Object value, NodeMap node, Map map){
        return false;
    }
});</pre>
<p>Now it works, however, simple-xml won&#8217;t be able to deserialize xml generated by this modified strategy.</p>
]]></content:encoded>
			<wfw:commentRss>http://sunng.info/blog/2010/02/suppressing-simple-xmls-class-attribute/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	<georss:point>31.203057 121.6174866</georss:point>	</item>
	</channel>
</rss>
