<?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/"
	>

<channel>
	<title>Cyberborean Chronicles &#187; Coding</title>
	<atom:link href="http://blog.cyberborean.org/tag/coding/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.cyberborean.org</link>
	<description>by Alex Alishevskikh</description>
	<lastBuildDate>Wed, 18 Jan 2012 07:52:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Dennis MacAlistair Ritchie</title>
		<link>http://blog.cyberborean.org/2011/10/14/dennis-macalistair-ritchie</link>
		<comments>http://blog.cyberborean.org/2011/10/14/dennis-macalistair-ritchie#comments</comments>
		<pubDate>Fri, 14 Oct 2011 20:42:05 +0000</pubDate>
		<dc:creator>Alex Alishevskikh</dc:creator>
				<category><![CDATA[Essays]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://blog.cyberborean.org/?p=875</guid>
		<description><![CDATA[I read the K&#38;R book when I was a kid and it was likely the most influencing reading I ever had on programming. At least, there were two stunning truths which, as I think, determined my life and profession: Programming is fun Programming is simple It was an astonishing &#8220;You can do that&#8221; idea: you [...]]]></description>
			<content:encoded><![CDATA[<p>I read the <a href="http://en.wikipedia.org/wiki/The_C_Programming_Language">K&amp;R book</a> when I was a kid and it was likely the most influencing reading I ever had on programming. At least, there were two stunning truths which, as I think, determined my life and profession:</p>
<p><span id="more-875"></span></p>
<ul>
<li><em>Programming is fun</em></li>
<li><em>Programming is simple</em></li>
</ul>
<p>It was an astonishing &#8220;<em>You can do that</em>&#8221; idea: you don&#8217;t have to be a mature, qualified engineer to start doing things with the computer right now. I still don&#8217;t know whether it&#8217;s a property of the C language itself, or of the book (in very good Russian translation); but even complex subjects, like pointers etc., were explained in an amusing way, intuitive for an average 13 y.o. schoolboy.</p>
<p>I don&#8217;t know another programming language specification that were such fundamental and detailed but simple and inspiring at the same time. I really owe to it in particular and to the C in general for I had not been lost in the jungles of something like BASIC (as it was suggested by the most of the &#8220;beginner&#8221; books of those days) and got a taste for a good language and for a fun of programming from the very beginning.</p>
<p>Unfortunately, I never remember Mr. Ritchie when I turn on my Linux (UNIX!) computer for doing my everyday work (those curly braces and all that stuff). And it&#8217;s sad.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cyberborean.org/2011/10/14/dennis-macalistair-ritchie/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RDFBeans 2.0</title>
		<link>http://blog.cyberborean.org/2011/04/07/rdfbeans-2-0</link>
		<comments>http://blog.cyberborean.org/2011/04/07/rdfbeans-2-0#comments</comments>
		<pubDate>Thu, 07 Apr 2011 05:46:56 +0000</pubDate>
		<dc:creator>Alex Alishevskikh</dc:creator>
				<category><![CDATA[Howtos]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[RDF]]></category>
		<category><![CDATA[RDFBeans]]></category>
		<category><![CDATA[Semantic Web]]></category>

		<guid isPermaLink="false">http://blog.cyberborean.org/?p=695</guid>
		<description><![CDATA[The second edition of RDFBeans Java-to-RDF databinding framework is finally out. In 2.0 version the library underwent major refactoring to clean and enhance the API and provide new databinding techniques. RDFBeans is an open-source object-RDF databinding and persistence library for the Semantic Web development with Java language. It provides a framework for mapping an object-oriented [...]]]></description>
			<content:encoded><![CDATA[<p>The second edition of <a href="http://rdfbeans.sourceforge.net">RDFBeans</a> Java-to-RDF databinding framework is finally out. In 2.0 version the library underwent major refactoring to clean and enhance the API and provide new databinding techniques.</p>
<div class="excerpt fullwidth">
RDFBeans is an open-source object-RDF databinding and persistence library for the Semantic Web development with Java language. It provides a framework for mapping an object-oriented domain model to RDF resource descriptions. </p>
<p>For more information, see:</p>
<ul>
<li><a href="http://rdfbeans.sf.net">The project web-site</a></li>
<li><a href="http://rdfbeans.sourceforge.net/apidocs/index.html">Javadocs</a></li>
<li><a href="http://rdfbeans.sourceforge.net/download.html">How to get the sourcecode or configure Maven dependency</a></li>
</ul>
</div>
<p><span id="more-695"></span></p>
<h2>RDFBean interfaces and dynamic proxying</h2>
<p>Along with <a href="http://rdfbeans.sourceforge.net/usage.html#Working_with_RDFBean_classes">traditional JavaBean-like persisting technique</a> using RDFBean classes, the version 2.0 introduces new approach to object-RDF mapping: <a href="http://rdfbeans.sourceforge.net/usage.html#Working_with_RDFBean_interfaces">RDFBean interfaces</a>.</p>
<p>It works simply: you define an interface of your domain model object and annotate it just like an RDFBean class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RDFNamespaces<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foaf = http://xmlns.com/foaf/0.1/&quot;</span><span style="color: #009900;">&#41;</span>
@RDFBean<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foaf:Person&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> IPerson <span style="color: #009900;">&#123;</span>
&nbsp;
    @RDFSubject
    <span style="color: #003399;">String</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    @RDF<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foaf:name&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    @RDF<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foaf:mbox&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #003399;">String</span> getEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">void</span> setEmail<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> email<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then you instantiate the <code>RDFBeanManager</code> (was <code>RDFBinding</code> in RDFBeans 1.x) upon an RDF2Go Model and create a <em>dynamic proxy object</em> with your interface:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ModelFactory modelFactory <span style="color: #339933;">=</span> RDF2Go.<span style="color: #006633;">getModelFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Model model <span style="color: #339933;">=</span> modelFactory.<span style="color: #006633;">createModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
model.<span style="color: #006633;">open</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
RDFBeanManager manager <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RDFBeanManager<span style="color: #009900;">&#40;</span>model<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
IPerson person <span style="color: #339933;">=</span> manager.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://example.com/persons/jdoe&quot;</span>, IPerson.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>The methods of this proxy object are directed immediately to the underlying RDF triples, so that</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">person.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;John Doe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
person.<span style="color: #006633;">setEmail</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;john@example.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>will cause adding the statements to the model:</p>
<pre>
&lt;http://example.com/persons/jdoe&gt; a &lt;http://xmlns.com/foaf/0.1/Person&gt; ;
	&lt;http://xmlns.com/foaf/0.1/name&gt; "John Doe" ;
	&lt;http://xmlns.com/foaf/0.1/mbox&gt; "john@example.com" ;
</pre>
<p>Similarly, the getter methods will return values retrieved directly from these statements.</p>
<h2>RDFBean annotations</h2>
<p>The <a href="http://rdfbeans.sourceforge.net/rdfbean.html">annotations scheme</a> has changed, so the old classes will be incompatible with RDFBeans 2.0. As you can see in the example above, <code>@RDF</code> annotation is now applied to the getter methods but not to the field declaration like in the previous versions.</p>
<p>You also can notice new annotation, <code>@RDFNamespaces</code>, which defines a list of URI prefixes to be used in other RDFBean annotations declared in a given class or interface. This includes class and property URIs, as well as RDFBean identifiers:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RDFNamespaces<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> 
	<span style="color: #0000ff;">&quot;foaf = http://xmlns.com/foaf/0.1/&quot;</span>,
	<span style="color: #0000ff;">&quot;persons = http://example.com/persons/&quot;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
@RDFBean<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foaf:Person&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> IPerson <span style="color: #009900;">&#123;</span>
&nbsp;
	@RDFSubject<span style="color: #009900;">&#40;</span>prefix <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;persons:&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #003399;">String</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
...</pre></div></div>

<p>Annotations are now inherited from superclasses and interfaces that makes the RDFBeans markup compatible with complex domain-specific object models. See how the <code>Thing</code>, <code>Agent</code> and <code>Person</code> classes of the <a href="http://rdfbeans.svn.sourceforge.net/viewvc/rdfbeans/tags/2.0/src/test/java/com/viceversatech/rdfbeans/test/foafexample/entities/">FOAF example</a> are organized to mimic the <code>owl:Thing → foaf:Agent → foaf:Person</code> subclassing in RDF-Schema.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cyberborean.org/2011/04/07/rdfbeans-2-0/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RDFBeans: Now on Sourceforge</title>
		<link>http://blog.cyberborean.org/2009/08/04/rdfbeans-now-on-sourceforge</link>
		<comments>http://blog.cyberborean.org/2009/08/04/rdfbeans-now-on-sourceforge#comments</comments>
		<pubDate>Tue, 04 Aug 2009 10:44:07 +0000</pubDate>
		<dc:creator>Alex Alishevskikh</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[RDF]]></category>
		<category><![CDATA[RDFBeans]]></category>
		<category><![CDATA[Semantic Web]]></category>

		<guid isPermaLink="false">http://blog.cyberborean.org/?p=471</guid>
		<description><![CDATA[RDFBeans framework (see &#8220;Simple RDF data binding&#8220;) is a Sourceforge project now: http://rdfbeans.sourceforge.net]]></description>
			<content:encoded><![CDATA[<p>RDFBeans framework (see &#8220;<a rel="bookmark" href="../2009/02/06/simple-rdf-data-binding">Simple RDF data binding</a>&#8220;) is a Sourceforge project now:</p>
<p class="hand"><a href="http://rdfbeans.sourceforge.net">http://rdfbeans.sourceforge.net</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cyberborean.org/2009/08/04/rdfbeans-now-on-sourceforge/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple RDF data binding</title>
		<link>http://blog.cyberborean.org/2009/02/06/simple-rdf-data-binding</link>
		<comments>http://blog.cyberborean.org/2009/02/06/simple-rdf-data-binding#comments</comments>
		<pubDate>Fri, 06 Feb 2009 19:52:45 +0000</pubDate>
		<dc:creator>Alex Alishevskikh</dc:creator>
				<category><![CDATA[Howtos]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[RDF]]></category>
		<category><![CDATA[RDFBeans]]></category>
		<category><![CDATA[Semantic Web]]></category>

		<guid isPermaLink="false">http://blog.cyberborean.org/?p=315</guid>
		<description><![CDATA[A large part of SW development is representing the information as RDF for persistence and interoperability. It&#8217;s usually done with lots of the glue code to map the programming object model to RDF triples and vice versa. Update: RDFBeans is an open-source object-RDF databinding and persistence library for the Semantic Web development with Java language. [...]]]></description>
			<content:encoded><![CDATA[<p>A large part of SW development is representing the information as RDF for persistence and interoperability. It&#8217;s usually done with lots of the glue code to map the programming object model to RDF triples and vice versa.</p>
<p><span id="more-315"></span></p>
<div class="excerpt">
<p><span class="upd"><strong>Update:</strong></span> RDFBeans is an open-source object-RDF databinding and persistence library for the Semantic Web development with Java language. It provides a framework for mapping an object-oriented domain model to RDF resource descriptions. </p>
<p>Information on this page is likely outdated. Please refer to:</p>
<ul>
<li><a href="http://rdfbeans.sf.net">The project web-site</a></li>
<li><a href="http://rdfbeans.sourceforge.net/apidocs/index.html">Javadocs</a></li>
<li><a href="http://rdfbeans.sourceforge.net/download.html">How to get the sourcecode or configure Maven dependency</a></li>
</ul>
<p>All posts about RDFBeans framework are <a href="/tag/rdfbeans">here</a>.
</div>
<p>Working on my current project, I had to deal with a rich object model  which is persisted into a RDF triple-store, so quickly I stuck in writing the object-triples translation code. I thought it would be a good idea to automate this by providing a simple data binding framework for transparent mapping of the Java objects to RDF (like <a href="http://www.hibernate.org/">Hibernate</a> and other tools does so for SQL or XML) with the following requirements in mind:</p>
<ul>
<li>It should be easy to make existing classes compatible with the framework with minimum modifications. This should not affect the business object model (no special interfaces and superclasses) and should not interfere with common <a class="zem_slink" title="JavaBean" rel="wikipedia" href="http://en.wikipedia.org/wiki/JavaBean">JavaBeans</a>-oriented frameworks (like Spring, etc.). Any <a class="zem_slink" title="JavaBean" rel="wikipedia" href="http://en.wikipedia.org/wiki/JavaBean">JavaBean</a>-like <a class="zem_slink" title="Plain Old Java Object" rel="wikipedia" href="http://en.wikipedia.org/wiki/Plain_Old_Java_Object">POJO</a> class can be  RDF-serializable just with few <a class="zem_slink" title="Java annotation" rel="wikipedia" href="http://en.wikipedia.org/wiki/Java_annotation">Java annotations</a> added.</li>
<li>No predefined ontologies and RDF-schemas are required.</li>
<li>Class information is stored in the RDF model for transparent instantiation of the objects.</li>
<li>Cascade binding for related objects should be supported.</li>
<li>Basic Java Collection types should be supported.</li>
</ul>
<p>This framework is implemented using Java annotations and Reflection APIs and works with major RDF triple-stores  (via <a href="http://semanticweb.org/wiki/RDF2Go">RDF2GO</a> abstraction layer).</p>
<h2>RDF Beans</h2>
<p>In order to be mapped to an RDF resource, a Java class must obey certain conventions:</p>
<ul>
<li>The class must have a public default (no-argument) constructor.</li>
<li>The class declaration must have a <code>@RDFBean</code> annotation to associate it with appropriate <a class="zem_slink" title="RDF Schema" rel="wikipedia" href="http://en.wikipedia.org/wiki/RDF_Schema">RDFS</a> class:

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">@RDFBean<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://xmlns.com/foaf/0.1/Person&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span> ...</pre></div></div>

</li>
</ul>
<p>See also: <a href="http://cyberborean.org/files/rdfbeans/1.0/examples/Person.java">A RDFBean class example</a>.</p>
<h3>Properties</h3>
<p>The class properties must be accessible using standard getter and setter methods. The properties are mapped to their RDF counterparts with <code>@RDF</code> annotations:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RDF<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://xmlns.com/foaf/0.1/name&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
@RDF<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://xmlns.com/foaf/0.1/mbox&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #003399;">String</span> email<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<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><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <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> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> email<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setEmail<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> email<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">email</span> <span style="color: #339933;">=</span> email<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The argument of <code>@RDF</code> is a valid URI of a RDF property (predicate) in a domain of the specified RDFS class.  The following types of properties are permitted:</p>
<ul>
<li>A Literal (<code>String, Boolean, Date, Integer, Float, Double, Long, Short, Byte, java.net.URI</code>)</li>
<li>Another RDFBean class</li>
<li>A Java Collection (List or Set) of Literals or RDFBeans</li>
</ul>
<p>Literal values are represented in RDF with corresponding <a class="zem_slink" title="XML schema" rel="wikipedia" href="http://en.wikipedia.org/wiki/XML_schema">XML-Schema</a> datatypes and Collections with RDF Containers (<a href="http://www.w3.org/TR/rdf-schema/#ch_bag">rdf:Bag</a> or <a href="http://www.w3.org/TR/rdf-schema/#ch_seq">rdf:Seq</a> depending on a Collection type). References to other RDFBean objects will be kept as RDF object properties and the values will be represented as separate RDF resources.</p>
<h3>RDF subject</h3>
<p>There must be a special String property to hold an unique URI value to identify the RDF resource (the subject URI). It must be accessible with getter and setter methods and be marked with <code>@RDFSubject</code> annotation:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RDFSubject
<span style="color: #003399;">String</span> id<span style="color: #339933;">;</span></pre></div></div>

<p>The property value must be a valid absolute URI, otherwise you can use the <code>prefix</code> argument for a common prefix to construct URIs from arbitrary String identifiers:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RDFSubject<span style="color: #009900;">&#40;</span>prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://example.com/persons#&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #003399;">String</span> id<span style="color: #339933;">;</span></pre></div></div>

<h2>Data Binding</h2>
<p>RDFBeans data binding framework is based on <a href="http://semanticweb.org/wiki/RDF2Go">RDF2Go API</a> which creates an abstraction layer above physical RDF storages. It makes an RDF model independent from specific RDF frameworks (such as <a class="zem_slink" title="Sesame (framework)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Sesame_%28framework%29">Sesame</a> and <a class="zem_slink" title="Jena (framework)" rel="homepage" href="http://jena.sourceforge.net/">Jena</a>), thus it can work with any, if a RDF2Go adapter is implemented.  The data binding is performed using single <code>RDFBinding</code> class, which is initialized with existing RDF2Go model:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.cyberborean.rdfbeans.RDFBinding</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ontoware.rdf2go.RDF2Go</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ontoware.rdf2go.model.Model</span><span style="color: #339933;">;</span>
...
<span style="color: #006633;">Model</span> model <span style="color: #339933;">=</span> RDF2Go.<span style="color: #006633;">getModelFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">createModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
model.<span style="color: #006633;">open</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
RDFBinding binding <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RDFBinding<span style="color: #009900;">&#40;</span>model<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <code>marshal</code> method converts a Java object into a RDF resource in the model:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
person.<span style="color: #006633;">setId</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://example.com/staff#johndoe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
person.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;John Doe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
person.<span style="color: #006633;">setEmail</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;johndoe@example.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
...
<span style="color: #006633;">binding</span>.<span style="color: #006633;">marshal</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This code will create a set of RDF statements representing the properties of the given object in the model:</p>

<div class="wp_syntax"><div class="code"><pre class="" style="font-family:monospace;">&lt;http://example.com/staff#johndoe&gt; &lt;http://www.w3.org/<span style="">1999</span>/02/<span style="">22</span>-rdf-syntax-ns#type&gt; &lt;http://xmlns.com/foaf/<span style="">0.1</span>/Person&gt; .
&lt;http://example.com/staff#johndoe&gt; &lt;http://xmlns.com/foaf/<span style="">0.1</span>/name&gt; &quot;John Doe&quot;^^&lt;http://www.w3.org/<span style="">2001</span>/XMLSchema#string&gt; .
&lt;http://example.com/staff#johndoe&gt; &lt;http://xmlns.com/foaf/<span style="">0.1</span>/mbox&gt; &quot;johndoe@example.com&quot;^^&lt;http://www.w3.org/<span style="">2001</span>/XMLSchema#string&gt; .</pre></div></div>

<p>Additionally, it adds a special statement to associate the RDFS class with Java:</p>

<div class="wp_syntax"><div class="code"><pre class="" style="font-family:monospace;">&lt;http://xmlns.com/foaf/<span style="">0.1</span>/Person&gt; &lt;http://cyberborean.org/rdfbeans/<span style="">1.0</span>/bindingClass&gt; &quot;com.example.Person&quot; .</pre></div></div>

<p>The <code>marshal</code> method inspects the properties which link the object with other RDFBeans and initiates their cascade binding to the RDF model, thus simplifying the development and ensuring referential integrity of the RDFBeans.</p>
<p>To reconstruct a Java object from RDF model, there is <code>unmarshal</code> method:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.ontoware.rdf2go.model.node.URI</span><span style="color: #339933;">;</span>
...
<span style="color: #006633;">URI</span> subject <span style="color: #339933;">=</span> model.<span style="color: #006633;">createURI</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://example.com/staff#johndoe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Person person <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Person<span style="color: #009900;">&#41;</span> binding.<span style="color: #006633;">unmarshal</span><span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The method creates new instance of the Java class (defined by <code>http://cyberborean.org/rdfbeans/1.0/bindingClass</code> property) and fill the properties with data from the RDF model. If a property holds a reference to another RDFBean, it results into cascade unmarshalling of related objects, thus restoring the original object model.</p>
<h2>Code</h2>
<ul>
<li><a href="http://cyberborean.org/files/rdfbeans/1.0/">RDFBeans JAR and source code</a></li>
<li><a href="http://cyberborean.org/files/rdfbeans/1.0/apidocs/">Javadocs</a></li>
</ul>
<p><strong>[Update: 2009-08-04]</strong></p>
<p>RDFBeans framework is released on SourceForge: <a href="http://rdfbeans.sourceforge.net/">http://rdfbeans.sourceforge.net</a>. The API has been changed, please check the <a href="http://rdfbeans.sourceforge.net/usage.html">documentation</a> and <a href="http://rdfbeans.sourceforge.net/apidocs/index.html">Javadocs</a> for details.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cyberborean.org/2009/02/06/simple-rdf-data-binding/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java AntiPatterns blog</title>
		<link>http://blog.cyberborean.org/2007/11/22/java-antipatterns-blog</link>
		<comments>http://blog.cyberborean.org/2007/11/22/java-antipatterns-blog#comments</comments>
		<pubDate>Thu, 22 Nov 2007 22:43:53 +0000</pubDate>
		<dc:creator>Alex Alishevskikh</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[antipattern]]></category>
		<category><![CDATA[bad practice]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://cyberborean.wordpress.com/2007/11/22/java-antipatterns-blog/</guid>
		<description><![CDATA[I started another blog here on WordPress.com: Java AntiPatterns. It is created to be a &#8220;collection of bad practices&#8221; and short tips how to avoid them in everyday Java coding. The antipatterns are organized both by categories and by tags and some initial set is already on there. If I meet or remember another antipattern, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://javaantipatterns.wordpress.com/"><img src="http://cyberborean.org/blog/wp-content/uploads/2007/11/jap.jpg" border="0" alt="jap.jpg" hspace="0" vspace="0" align="right" /></a>I started another blog here on WordPress.com: <a href="http://javaantipatterns.wordpress.com/">Java AntiPatterns</a>. It is created to be a &#8220;collection of bad practices&#8221; and short tips how to avoid them in everyday Java coding. The antipatterns are organized both by categories and by tags and some initial set is already on there. If I meet or remember another antipattern, I will post it to that blog.</p>
<p>I&#8217;m not thinking about it as of my personal stuff and welcome everyone to <a href="http://javaantipatterns.wordpress.com/suggest-yours/">suggest new </a> antipatterns and their solutions. if you want to post to that blog yourself, please <a href="http://cyberborean.wordpress.com/feedback/">contact me</a> (you have to be a <a href="http://wordpress.com/signup/">registered</a> WordPress user).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cyberborean.org/2007/11/22/java-antipatterns-blog/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sourcecode posting on WordPress</title>
		<link>http://blog.cyberborean.org/2007/09/06/sourcecode-posting-on-wordpress</link>
		<comments>http://blog.cyberborean.org/2007/09/06/sourcecode-posting-on-wordpress#comments</comments>
		<pubDate>Thu, 06 Sep 2007 20:27:01 +0000</pubDate>
		<dc:creator>Alex Alishevskikh</dc:creator>
				<category><![CDATA[Ongoing]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://cyberborean.wordpress.com/2007/09/06/sourcecode-posting-on-wordpress/</guid>
		<description><![CDATA[Thanks to new WordPress feature, sourcecode quotes in the blog posts now looks awesome: [sourcecode language='java'] /** * HelloWorld */ public class HelloWorld { public static void main(String[] args) { System.out.println(&#8220;Hello world!&#8221;); } } [/sourcecode] See also this and this examples of Java code, this example of Python and this XML. Actually 12 programming/markup languages [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks to new <a href="http://faq.wordpress.com/2007/09/03/how-do-i-post-source-code/">WordPress feature</a>, sourcecode quotes in the blog posts now looks awesome:</p>
<p>[sourcecode language='java']<br />
/**<br />
 * HelloWorld<br />
 */<br />
public class HelloWorld {<br />
    public static void main(String[] args) {<br />
        System.out.println(&#8220;Hello world!&#8221;);<br />
    }<br />
}<br />
[/sourcecode]</p>
<p><span id="more-177"></span><br />
See also <a href="http://cyberborean.wordpress.com/2007/07/04/custom-classloaders-the-black-art-of-java/">this</a> and <a href="http://cyberborean.wordpress.com/2006/05/07/tips-tricks-required-text-fields-in-swing/">this</a> examples of Java code, <a href="http://cyberborean.wordpress.com/2006/09/12/getting-new-mail-onto-the-desktop/">this example of Python</a> and <a href="http://cyberborean.wordpress.com/2006/01/23/a-sol-example/">this XML</a>. Actually 12 programming/markup languages are supported by the syntax highlighter.</p>
<h3>What&#8217;s good</h3>
<ul>
<li>It&#8217;s an eye-candy</li>
<li>Language-dependent syntax highlighting, of course</li>
<li>Horizontal scrolling of the code block, if it&#8217;s needed. Now I don&#8217;t have to bother how the lines are long</li>
<li>You can copy my code into your clipboard by a single click</li>
</ul>
<h3>What&#8217;s bad</h3>
<p>There was a bug when HTML-reserved characters (&#8220;<code>&lt;</code>&#8220;, &#8220;<code>&gt;</code>&#8220;, &#8220;<code>&amp;</code>&#8220;) were displayed as HTML entities (&#8220;<code>&amp;lt;</code>&#8221; etc). It has been fixed just today. <del datetime="2007-09-07T08:04:29+00:00">I also noticed that the single backslash chars (&#8220;\&#8221;) are not displayed and must be escaped with another backslash (&#8220;\\&#8221;) &#8211; hi, PHP geeks!</del></p>
<p><strong>[upd]</strong> The problem with backslash has also been fixed. Thanks to Joseph Scott from WordPress for fast responding and fixing it!<strong>[/upd]</strong></p>
<p>Sadly enough, code posting is incompatible with WYSIWYG, if you bother about indentation of your code. <a href="http://tinymce.moxiecode.com">TinyMCE</a> visual editor will eat all your leading spaces and tabs.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cyberborean.org/2007/09/06/sourcecode-posting-on-wordpress/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>URL&#8217;s in Java: Another pitfall</title>
		<link>http://blog.cyberborean.org/2007/08/22/urls-in-java-another-pitfall</link>
		<comments>http://blog.cyberborean.org/2007/08/22/urls-in-java-another-pitfall#comments</comments>
		<pubDate>Wed, 22 Aug 2007 12:58:52 +0000</pubDate>
		<dc:creator>Alex Alishevskikh</dc:creator>
				<category><![CDATA[Essays]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://cyberborean.wordpress.com/2007/08/22/urls-in-java-another-pitfall/</guid>
		<description><![CDATA[If ClassLoader Delegation, though esoteric and counterintuitive, is an example of some design, there are some things in Java that are confusing and just poorly designed at once. When testing my new Java application, I noticed strange freezing sometimes. It happened irregularly and the program did nothing serious at that time &#8211; no complex calculations, [...]]]></description>
			<content:encoded><![CDATA[<p><em>If <a href="http://cyberborean.wordpress.com/2007/07/04/custom-classloaders-the-black-art-of-java/">ClassLoader Delegation</a>, though esoteric and counterintuitive, is an example of some design, there are some things in Java that are  confusing and just poorly designed at once.</em></p>
<p><span id="more-174"></span></p>
<p>When testing my new Java application, I noticed strange freezing sometimes. It happened irregularly and the program did nothing serious at that time &#8211; no complex calculations, no heavy data transfers, etc. Nevertheless, I noticed that those freezings where related to my internet connection glitches &#8211; unfortunately, it occasionally happens here. But my program didn&#8217;t do anything with internet at those moments!</p>
<p>I did some profiling and found the bottleneck &#8211; to my surprise, it was <code>get()</code> method of a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashMap.html"><code>HashMap</code></a> instance. Sometimes it took about 20 seconds only for just checking if an object exists in the map. I was shocked. HashMaps! I used it for ages and was sure they are reasonably fast kind of collections.</p>
<p>One way or another, I found the problem. The point was that I used <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html"><code>java.net.URL</code></a> instances as the HashMap keys. <code>URL</code> objects in Java, in comparison with simple strings, are convenient as they store URLs as the sets of separate fields (protocol, host, path etc), guarantee URL well-formedness and enable to create a connection with a single <code>connect()</code> method.</p>
<p>But its implementation is horrible. As it <a href="http://kickjava.com/src/java/net/URL.java.htm">turned out</a>, two generic object methods &#8211; <code>equals()</code> and <code>hashCode()</code> needed for internet connection and domain name resolving just to check equality of two URLs or to compute object&#8217;s hash code.</p>
<blockquote><p>Two URL objects are equal if they have the same protocol, reference equivalent hosts, have the same port number on the host, and the same file and fragment of the file.</p>
<p>Two hosts are considered equivalent if both host names can be resolved into the same IP addresses; else if either host name can&#8217;t be resolved, the host names must be equal without regard to case; or both host names equal to null.</p>
<p>Since hosts comparison requires name resolution, this operation is a blocking operation.</p>
<p>(<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html"><code>java.net.URL</code> javadoc</a>)</p></blockquote>
<p>I have no idea what were the reasons to do that, but it is bad. Simply bad. Such common operations as equality test (and hash code computation) should rely on an object data only. Making it dependent on external unreliable resources is dangerous. Also, those operations must be fast as possible, because they are called frequently (often inside the loops). When the time of their execution is equal to the connection time-out, your program simply don&#8217;t work.</p>
<p>Even if there were some reasons for that, it works wrong.</p>
<blockquote><p>Note: The defined behavior for equals is known to be inconsistent with virtual hosting in HTTP.</p>
<p>(<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html">Ibid.</a>)</p></blockquote>
<p>Sure. For instance, URLs of all WordPress blogs are equal from point of view of <code>java.net.URL</code>, as long as all of them are resolved to the same IP address. And a whole lot of other web-sites, unless they are hosted on the dedicated servers.</p>
<p>URLs are just the text strings with known syntax. They should work offline, no matter they are &#8220;http:&#8221; or &#8220;file:&#8221;. They don&#8217;t need to be resolved to the IP addresses, unless a referenced resource is requested.</p>
<p>Do not use <code>java.net.URL</code>. Store URLs as simple String objects. If you need to keep URLs structured and well-formed, you can use <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html"><code>java.net.URI</code></a> class instead, but the strings are faster anyway. You always can parse a string into <code>java.net.URI</code> object when it is needed. You can parse it into <code>java.net.URL</code> as well, but.. better don&#8217;t do it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cyberborean.org/2007/08/22/urls-in-java-another-pitfall/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Custom ClassLoaders: The Black Art of Java</title>
		<link>http://blog.cyberborean.org/2007/07/04/custom-classloaders-the-black-art-of-java</link>
		<comments>http://blog.cyberborean.org/2007/07/04/custom-classloaders-the-black-art-of-java#comments</comments>
		<pubDate>Wed, 04 Jul 2007 07:19:12 +0000</pubDate>
		<dc:creator>Alex Alishevskikh</dc:creator>
				<category><![CDATA[Essays]]></category>
		<category><![CDATA[Howtos]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://cyberborean.wordpress.com/2007/07/04/custom-classloaders-the-black-art-of-java/</guid>
		<description><![CDATA[Java is great platform for component development but there are some odd and counterintuitive things on the way. One of these hidden pitfalls waiting for a developer who is going to replace default system ClassLoader with a custom one. A common case when you might have an idea of using a custom ClassLoader is to [...]]]></description>
			<content:encoded><![CDATA[<p><em>Java is great platform for component development but there are some odd and counterintuitive things on the way. One of these hidden pitfalls waiting for a developer who is going to replace default system ClassLoader with a custom one.</em></p>
<p><span id="more-172"></span></p>
<p>A common case when you might have an idea of using a custom ClassLoader is to avoid the classpath issues using <code><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLClassLoader.html">URLClassLoader</a></code> for loading classes instead of the default one. Classpath is a list of locations (JAR-files or directories) where default ClassLoader is looking for the classes to load. Declaring Classpath explicitly (either with <code>CLASSPATH</code> environment variable, or as a command-line argument of Java virtual machine) is tedious and error-prone job and it works only in the case of monolithic, statically-linked applications with known set of libraries.</p>
<p>But imagine a complex component-based system with a number of replaceable modules (plug-ins). It is obvious that classpath for that system should be built automatically to reflect the configuration changes before each running. It is often  done with system-specific startup scripts which analyzes the modules configuration and generates an appropriate classpath before starting the application. However, this method has its own drawbacks &#8211; especially in the case of poor Microsoft systems as their rudimentary command interpreter makes this task hardly possible. And this is a way system-dependent, of course.</p>
<p><code>URLClassLoader</code>, from a distant point of view, looks like an elegant, simple and &#8220;100%-pure-java&#8221; solution for the classpath headache. It is instantiated with an array of locations (URL&#8217;s) so each class loaded with that ClassLoader will use this array as a classpath. An advantage is that the classpath can be generated on the fly, automatically by an application itself.</p>
<p>So, let&#8217;s see how it works. Imagine we have an application with main executable class <code>Foo</code> (in package <code>com.example.foo</code>) and a lot of other classes in different locations. We will develop an invocation wrapper for loading <code>Foo</code> using URLClassLoader with an array of locations, generated automatically:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
 * FooInvoker.java
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.foo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URLClassLoader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Method</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Modifier</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FooInvoker <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> FOO_CLASS_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com.example.foo.Foo&quot;</span><span style="color: #339933;">;</span>
&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><span style="color: #003399;">String</span><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>
        <span style="color: #003399;">URL</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> classpathURLs <span style="color: #339933;">=</span> getClasspathURLs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">ClassLoader</span> loader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URLClassLoader</span><span style="color: #009900;">&#40;</span>classpathURLs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">Class</span> fooClass <span style="color: #339933;">=</span> loader.<span style="color: #006633;">loadClass</span><span style="color: #009900;">&#40;</span>FOO_CLASS_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Method</span> main <span style="color: #339933;">=</span> fooClass.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;main&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>.<span style="color: #000000; font-weight: bold;">class</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> modifiers <span style="color: #339933;">=</span> main.<span style="color: #006633;">getModifiers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Modifier</span>.<span style="color: #006633;">isPublic</span><span style="color: #009900;">&#40;</span>modifiers<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #003399;">Modifier</span>.<span style="color: #006633;">isStatic</span><span style="color: #009900;">&#40;</span>modifiers<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                main.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">// In this example, we assume Foo.main() takes no</span>
                <span style="color: #666666; font-style: italic;">// command-line arguments</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">NoSuchMethodException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error running class &quot;</span> <span style="color: #339933;">+</span> FOO_CLASS_NAME
                    <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> getClasspathURLs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">URL</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> urls <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Here we're looking for all project-related libraries</span>
        <span style="color: #666666; font-style: italic;">// and constructing the classpath array</span>
        <span style="color: #666666; font-style: italic;">// ...</span>
        <span style="color: #000000; font-weight: bold;">return</span> urls<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Run it (assuming <code>Foo</code> and <code>FooInvoker</code> are packaged both into <code>foo.jar</code> file, which is the core library of our app):</p>
<pre>$ java -classpath ./foo.jar com.example.foo.FooInvoker</pre>
<p>Alas! Everything located outside the <code>foo.jar</code> (that is, the classpath set in JVM arguments) is failed to load. For instance, if <code>Foo</code> uses class <code>Bar</code> in it&#8217;s own <code>bar.jar</code> library, <code>Foo</code> will throw <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassNotFoundException.html"><code>ClassNotFoundException</code> </a> despite of the fact that <code>bar.jar</code> has been passed to <code>URLClassLoader</code> argument. It seems we have no progress at all &#8211; all classes are loaded from default JVM classpath as with the system ClassLoader.</p>
<p>Let&#8217;s do some debugging. Add this to <code>Foo.main()</code> code to see which ClassLoader loads <code>Foo</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Foo is loaded with: &quot;</span> <span style="color: #339933;">+</span>
                           Foo.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getClassLoader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<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></pre></div></div>

<p>Instead of &#8220;<code>java.net.URLClassLoader</code>&#8221; in output (as we hoped), we will see:</p>
<pre><code>Foo is loaded with: sun.misc.Launcher$AppClassLoader</code></pre>
<p>This is the default system ClassLoader. We really have no progress &#8211; it seems our <code>URLClassLoader</code> simply does not come into play. Our code is right but it doesn&#8217;t work. Is it a Java bug? Should we give up? Or report to Sun?</p>
<p>Wait a minute. The problem is not in code but in how Java VM executes it. Let&#8217;s try a trick: change <code>FooInvoker.getClasspathURLs()</code> algorythm to include <code>foo.jar</code> path in the resulted array (to be passed to <code>URLClassLoader</code> among other locations) and compile <code>FooInvoker</code> as a separate class file (not inside the <code>foo.jar</code>). Then run it (no classpath is declared!):</p>
<pre><code>$ java com.example.foo.FooInvoker
...
Foo is loaded with: java.net.URLClassLoader
</code></pre>
<p>Voila! Our <code>URLClassLoader</code> has been used to load <code>Foo</code> as well as other classes, including those from locations in our dynamic classpath. It works! So what is happened?</p>
<p>In this case we found ourselves in one of the dark and odd Java territories ruled by the Masters of The Black Java Art (also known as the Heavy Java Geeks) who believe the programs should not behave as their author expects, but following the mysterious Rules instead. One of those Rules is called &#8220;ClassLoader Delegation Model&#8221; and it <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html">reads</a>:</p>
<blockquote><p> The ClassLoader class uses a delegation model to search for classes and resources. Each instance of ClassLoader has an associated parent class loader. When requested to find a class or resource, a ClassLoader instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself.</p></blockquote>
<p>A parent of <code>URLClassLoader</code> is the default system ClassLoader (as we used single-argument constructor). When we tried to load <code>Foo</code> with an instance of <code>URLClassLoader</code>, it asked the parent if  the latter could load <code>Foo</code>. As <code>Foo</code> was in the default classpath, the system ClassLoader, of course, could. By this way, it took a position of a default ClassLoader in a chain of subsequent class invocations and left our <code>URLClassLoader</code> out of business.</p>
<p>When we moved <code>Foo</code> and everything out the default classpath, the system ClassLoader was not able to load anything (except <code>FooInvoker</code> itself). So, it had no chance to took precedence over our custom ClassLoader.</p>
<p>Well, if you like rules, there is another one:</p>
<p><strong>If you use custom ClassLoader, do not give the system one a chance to come into play.</strong></p>
<p>Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cyberborean.org/2007/07/04/custom-classloaders-the-black-art-of-java/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>There is a button, stupid</title>
		<link>http://blog.cyberborean.org/2006/10/18/there-is-a-button-stupid</link>
		<comments>http://blog.cyberborean.org/2006/10/18/there-is-a-button-stupid#comments</comments>
		<pubDate>Wed, 18 Oct 2006 14:56:19 +0000</pubDate>
		<dc:creator>Alex Alishevskikh</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[Life]]></category>

		<guid isPermaLink="false">http://cyberborean.wordpress.com/2006/10/18/there-is-a-button-stupid/</guid>
		<description><![CDATA[Long tweaking and hacking an open source software code can lead to a sort of very special menthal syndrome. This is a real life dialog in the kitchen: Me: This microwave has very annoying, shrill beep. I hate it! I gonna break it and cut that damned beeper off. My wife: Don&#8217;t be surprised but [...]]]></description>
			<content:encoded><![CDATA[<p>Long tweaking and hacking an open source software code can lead to a sort of very special menthal syndrome. This is a real life dialog in the kitchen:</p>
<p>Me: This microwave has very annoying, shrill beep. I hate it! I gonna break it and cut that damned beeper off.<br />
My wife: Don&#8217;t be surprised but there is a button sequence which makes the owen silent.</p>
<p>Probably, I have to take a rest.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cyberborean.org/2006/10/18/there-is-a-button-stupid/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting new mail onto the desktop</title>
		<link>http://blog.cyberborean.org/2006/09/12/getting-new-mail-onto-the-desktop</link>
		<comments>http://blog.cyberborean.org/2006/09/12/getting-new-mail-onto-the-desktop#comments</comments>
		<pubDate>Tue, 12 Sep 2006 15:24:56 +0000</pubDate>
		<dc:creator>Alex Alishevskikh</dc:creator>
				<category><![CDATA[Essays]]></category>
		<category><![CDATA[Howtos]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[desktop]]></category>
		<category><![CDATA[KDE]]></category>
		<category><![CDATA[PIM]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://cyberborean.wordpress.com/2006/09/12/getting-new-mail-onto-the-desktop/</guid>
		<description><![CDATA[I had nice last weekend gathering new harvest of apples, drinking fresh apple juice and playing with SuperKaramba widgets &#8211; a good opportunity to take a sort of &#8220;recreational programming&#8221;. Perhaps all modern KDE users know those nice resource eaters eye-candies which are living right on the desktop surface and displaying the clocks, calendars, weather [...]]]></description>
			<content:encoded><![CDATA[<p>I had nice last weekend gathering new harvest of apples, drinking fresh apple juice and playing with <a href="http://netdragon.sourceforge.net">SuperKaramba</a> widgets &#8211; a good opportunity to take a sort of &#8220;recreational programming&#8221;. Perhaps all modern KDE users know those nice <s>resource eaters</s> eye-candies which are living right on the desktop surface and displaying the clocks, calendars, weather forecasts, system monitors and so on.</p>
<p>Instead of developing some Yet Another Big Animated Clock, I decided to write something practical. What I&#8217;d like to have is a widget which would ask my e-mail client (<a href="http://kmail.kde.org">KMail</a>) for the headers of the latest unread messages to show them on the desktop.<br />
<span id="more-133"></span></p>
<p><img src="http://cyberborean.org/blog/wp-content/uploads/2006/09/skkmail.jpg" alt="skkmail.jpg" /></p>
<p>Two buttons in the top bar are for checking new mail from a server and for opening new message window. Clicking on the top bar somewhere else will open main KMail window.</p>
<p>SuperKaramba comes with a <a href="http://netdragon.sourceforge.net/api.html">Python API</a>, so everyone who is familiar with Pyton basics can write her own widget (aka &#8220;theme&#8221;) as a Python script. In my case, I wrote two scripts &#8211; one is for widget output and another as a simple KMail folder API wrapped around DCOP calls.</p>
<h3>Patching KMail</h3>
<p>There was a major problem &#8211; KMail DCOP interface had no a call to get unread message headers; only getting a number of them is possible. So I have had to dig into the KMail sourcecode (my current version is 1.9.4) for to add new function to FolderIFace interface (<code>folderIface.cpp</code>).</p>
<h4><code>kmail.diff:</code></h4>

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;">diff kmail/folderIface.cpp kmail_patched/folderIface.cpp
<span style="color: #440088;">147a148,162</span>
<span style="color: #00b000;">&gt; QStringList</span>
<span style="color: #00b000;">&gt; FolderIface::unreadMessageHeaders<span style="">&#40;</span><span style="">&#41;</span></span>
<span style="color: #00b000;">&gt; <span style="">&#123;</span></span>
<span style="color: #00b000;">&gt;      QStringList msgs;</span>
<span style="color: #00b000;">&gt;      mFolder-&gt;open<span style="">&#40;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">&gt;      for<span style="">&#40;</span> int i = <span style="">0</span>; i &amp;amp;lt; mFolder-&gt;count<span style="">&#40;</span><span style="">&#41;</span>; i++<span style="">&#41;</span> <span style="">&#123;</span></span>
<span style="color: #00b000;">&gt;          KMMsgBase *msg = mFolder-&gt;getMsgBase<span style="">&#40;</span>i<span style="">&#41;</span>;</span>
<span style="color: #00b000;">&gt; 	     if <span style="">&#40;</span>msg-&gt;isNew<span style="">&#40;</span><span style="">&#41;</span> || msg-&gt;isUnread<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span> <span style="">&#123;</span></span>
<span style="color: #00b000;">&gt; 	       msgs.append<span style="">&#40;</span>msg-&gt;fromStrip<span style="">&#40;</span><span style="">&#41;</span> + &quot;\\t&quot; + msg-&gt;subject<span style="">&#40;</span><span style="">&#41;</span> + &quot;\\t&quot; + msg-&gt;dateStr<span style="">&#40;</span><span style="">&#41;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">&gt;          <span style="">&#125;</span></span>
<span style="color: #00b000;">&gt;      <span style="">&#125;</span></span>
<span style="color: #00b000;">&gt;      mFolder-&gt;close<span style="">&#40;</span><span style="">&#41;</span>;</span>
<span style="color: #00b000;">&gt;      return msgs;</span>
<span style="color: #00b000;">&gt; <span style="">&#125;</span></span>
<span style="color: #00b000;">&gt;</span>
diff kmail/folderIface.h kmail_patched/folderIface.h
<span style="color: #440088;">61c61,62</span>
<span style="color: #991111;">&lt;</span>
<span style="color: #888822;">---
<span style="color: #00b000;">&gt;     virtual QStringList unreadMessageHeaders<span style="">&#40;</span><span style="">&#41;</span>;</span></span>
<span style="color: #00b000;">&gt;</span></pre></div></div>

<p>After adding new function header to <code>folderIface.h</code> and rebuilding KMail, new DCOP call &#8220;<code>kmail FolderIface unreadMessageHeaders</code>&#8221; became available to get the list of message headers in format &#8220;From/Subject/Date&#8221; divided by tab characters (<code>\t</code>).</p>
<h3>Sourcecode</h3>
<h4><code>skkmail.py</code>:</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> karamba
<span style="color: #ff7700;font-weight:bold;">import</span> kmail
&nbsp;
text = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
images = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
clickAreas = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Define widget area width here (height is adjusted automatically)</span>
s_width = <span style="color: #ff4500;">350</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># If an actual height is above this value, a widget area will be clipped</span>
maxHeight = <span style="color: #ff4500;">600</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Margins inside a widget area</span>
s_x = <span style="color: #ff4500;">10</span>
s_y = <span style="color: #ff4500;">10</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _update<span style="color: black;">&#40;</span>widget<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">global</span> text, images, clickAreas, s_width, s_x, s_y
    x_gap = <span style="color: #ff4500;">10</span>
    y_gap = <span style="color: #ff4500;">4</span>
&nbsp;
    s_height = <span style="color: #ff4500;">14</span>
    s_col1 = <span style="color: black;">&#40;</span>s_width-x_gap<span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">0.66</span>
    s_col2 = s_width
    s_col3 = <span style="color: black;">&#40;</span>s_width-x_gap-<span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">0.33</span>
&nbsp;
    folderFont = <span style="color: #483d8b;">&quot;Franklin Gothic Medium&quot;</span>
    folderFontSize = <span style="color: #ff4500;">14</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> t <span style="color: #ff7700;font-weight:bold;">in</span> text:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
          karamba.<span style="color: black;">deleteText</span><span style="color: black;">&#40;</span>widget, t<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span>:
          <span style="color: #ff7700;font-weight:bold;">pass</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> images:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
          karamba.<span style="color: black;">deleteImage</span><span style="color: black;">&#40;</span>widget, i<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span>:
          <span style="color: #ff7700;font-weight:bold;">pass</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> clickAreas:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
          karamba.<span style="color: black;">removeClickArea</span><span style="color: black;">&#40;</span>widget, c<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span>:
          <span style="color: #ff7700;font-weight:bold;">pass</span>
    text = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    images = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    clickAreas = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    folders = kmail.<span style="color: black;">getFolders</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    y = s_y + <span style="color: #ff4500;">25</span><span style="color: #66cc66;">;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> f <span style="color: #ff7700;font-weight:bold;">in</span> folders:
        msgs = f.<span style="color: black;">unreadMessages</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>msgs<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
            y0 = y
            t = karamba.<span style="color: black;">createText</span><span style="color: black;">&#40;</span>widget, s_x+<span style="color: #ff4500;">15</span>, y, s_width, s_height,
                f.<span style="color: black;">name</span>+<span style="color: #483d8b;">&quot; (&quot;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>msgs<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>+<span style="color: #483d8b;">&quot;)&quot;</span><span style="color: black;">&#41;</span>
            karamba.<span style="color: black;">changeTextFont</span><span style="color: black;">&#40;</span>widget, t, folderFont<span style="color: black;">&#41;</span>
            karamba.<span style="color: black;">changeTextSize</span><span style="color: black;">&#40;</span>widget, t, folderFontSize<span style="color: black;">&#41;</span>
            text.<span style="color: black;">append</span><span style="color: black;">&#40;</span>t<span style="color: black;">&#41;</span>
            images.<span style="color: black;">append</span><span style="color: black;">&#40;</span>karamba.<span style="color: black;">createImage</span><span style="color: black;">&#40;</span>widget, s_x-<span style="color: #ff4500;">10</span>, y, <span style="color: #483d8b;">&quot;icons/folder.png&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            y = y + s_height + y_gap<span style="color: #66cc66;">*</span><span style="color: #ff4500;">2</span> +<span style="color: #ff4500;">10</span>
            <span style="color: #ff7700;font-weight:bold;">for</span> m <span style="color: #ff7700;font-weight:bold;">in</span> msgs:
                t = karamba.<span style="color: black;">createText</span><span style="color: black;">&#40;</span>widget, s_x+<span style="color: #ff4500;">20</span>, y, s_col1, s_height, m<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
                text.<span style="color: black;">append</span><span style="color: black;">&#40;</span>t<span style="color: black;">&#41;</span>
                t = karamba.<span style="color: black;">createText</span><span style="color: black;">&#40;</span>widget, s_x, y+s_height, s_col2, s_height, m<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
                text.<span style="color: black;">append</span><span style="color: black;">&#40;</span>t<span style="color: black;">&#41;</span>
                t = karamba.<span style="color: black;">createText</span><span style="color: black;">&#40;</span>widget, s_x+<span style="color: #ff4500;">20</span>+s_col1+x_gap, y, s_col3, s_height, m<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
                karamba.<span style="color: black;">setTextAlign</span><span style="color: black;">&#40;</span>widget, t, <span style="color: #483d8b;">&quot;RIGHT&quot;</span><span style="color: black;">&#41;</span>
                text.<span style="color: black;">append</span><span style="color: black;">&#40;</span>t<span style="color: black;">&#41;</span>
                images.<span style="color: black;">append</span><span style="color: black;">&#40;</span>karamba.<span style="color: black;">createImage</span><span style="color: black;">&#40;</span>widget, s_x, y, <span style="color: #483d8b;">&quot;icons/mail.png&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                y = y + s_height<span style="color: #66cc66;">*</span><span style="color: #ff4500;">2</span> + y_gap<span style="color: #66cc66;">*</span><span style="color: #ff4500;">2</span>
            clickAreas.<span style="color: black;">append</span><span style="color: black;">&#40;</span>karamba.<span style="color: black;">createClickArea</span><span style="color: black;">&#40;</span>widget, s_x-<span style="color: #ff4500;">10</span>, y0, s_width, y-y0,
                <span style="color: #483d8b;">&quot;dcop kmail KMailIface selectFolder &quot;</span>+f.<span style="color: black;">path</span>+<span style="color: #483d8b;">&quot; &amp;&amp; kmail&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            y = y + y_gap<span style="color: #66cc66;">*</span><span style="color: #ff4500;">2</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>y <span style="color: #66cc66;">&lt;</span> maxHeight - <span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>:
        karamba.<span style="color: black;">resizeWidget</span><span style="color: black;">&#40;</span>widget, s_width+s_x<span style="color: #66cc66;">*</span><span style="color: #ff4500;">2</span>, y+<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#this is called when you widget is initialized</span>
<span style="color: #ff7700;font-weight:bold;">def</span> initWidget<span style="color: black;">&#40;</span>widget<span style="color: black;">&#41;</span>:
    bgImage = karamba.<span style="color: black;">createBackgroundImage</span><span style="color: black;">&#40;</span>widget, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">5</span>, <span style="color: #483d8b;">&quot;icons/bg.png&quot;</span><span style="color: black;">&#41;</span>
    karamba.<span style="color: black;">resizeImage</span><span style="color: black;">&#40;</span>widget, bgImage, s_width+s_x<span style="color: #66cc66;">*</span><span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>
    logo = karamba.<span style="color: black;">createImage</span><span style="color: black;">&#40;</span>widget, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #483d8b;">&quot;icons/kmail.png&quot;</span><span style="color: black;">&#41;</span>
    t = karamba.<span style="color: black;">createText</span><span style="color: black;">&#40;</span>widget, <span style="color: #ff4500;">35</span>, <span style="color: #ff4500;">5</span>, <span style="color: #ff4500;">100</span>, <span style="color: #ff4500;">20</span>, <span style="color: #483d8b;">&quot;new mail&quot;</span><span style="color: black;">&#41;</span>
    karamba.<span style="color: black;">changeTextFont</span><span style="color: black;">&#40;</span>widget, t, <span style="color: #483d8b;">&quot;Franklin Gothic Medium&quot;</span><span style="color: black;">&#41;</span>
    karamba.<span style="color: black;">changeTextSize</span><span style="color: black;">&#40;</span>widget, t, <span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span>
    karamba.<span style="color: black;">createClickArea</span><span style="color: black;">&#40;</span>widget, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">100</span>, <span style="color: #ff4500;">30</span>, <span style="color: #483d8b;">&quot;kmail&quot;</span><span style="color: black;">&#41;</span>
    karamba.<span style="color: black;">createImage</span><span style="color: black;">&#40;</span>widget, s_width+s_x<span style="color: #66cc66;">*</span><span style="color: #ff4500;">2</span>-<span style="color: #ff4500;">18</span>, <span style="color: #ff4500;">7</span>, <span style="color: #483d8b;">&quot;icons/mail_get.png&quot;</span><span style="color: black;">&#41;</span>
    karamba.<span style="color: black;">createClickArea</span><span style="color: black;">&#40;</span>widget, s_width+s_x<span style="color: #66cc66;">*</span><span style="color: #ff4500;">2</span>-<span style="color: #ff4500;">18</span>, <span style="color: #ff4500;">7</span>, <span style="color: #ff4500;">16</span>, <span style="color: #ff4500;">16</span>, <span style="color: #483d8b;">&quot;dcop kmail KMailIface checkMail&quot;</span><span style="color: black;">&#41;</span>
    karamba.<span style="color: black;">createImage</span><span style="color: black;">&#40;</span>widget, s_width+s_x<span style="color: #66cc66;">*</span><span style="color: #ff4500;">2</span>-<span style="color: #ff4500;">40</span>, <span style="color: #ff4500;">7</span>, <span style="color: #483d8b;">&quot;icons/mail_new.png&quot;</span><span style="color: black;">&#41;</span>
    karamba.<span style="color: black;">createClickArea</span><span style="color: black;">&#40;</span>widget, s_width+s_x<span style="color: #66cc66;">*</span><span style="color: #ff4500;">2</span>-<span style="color: #ff4500;">40</span>, <span style="color: #ff4500;">7</span>, <span style="color: #ff4500;">16</span>, <span style="color: #ff4500;">16</span>,
        <span style="color: #483d8b;">&quot;dcop kmail KMailIface openComposer '' '' '' '' '' ''&quot;</span><span style="color: black;">&#41;</span>
    _update<span style="color: black;">&#40;</span>widget<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#this is called everytime your widget is updated</span>
<span style="color: #808080; font-style: italic;">#the update inverval is specified in the .theme file</span>
<span style="color: #ff7700;font-weight:bold;">def</span> widgetUpdated<span style="color: black;">&#40;</span>widget<span style="color: black;">&#41;</span>:
    _update<span style="color: black;">&#40;</span>widget<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#This gets called everytime our widget is clicked.</span>
<span style="color: #808080; font-style: italic;"># Middle button click forces to check new mail</span>
<span style="color: #ff7700;font-weight:bold;">def</span> widgetClicked<span style="color: black;">&#40;</span>widget, x, y, button<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>button == <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
        kmail.<span style="color: black;">dcopCall</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;kmail KMailIface checkMail&quot;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<h4><code>kmail.py:</code></h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">subprocess</span>, <span style="color: #dc143c;">string</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> dcopCall<span style="color: black;">&#40;</span>call<span style="color: black;">&#41;</span>:
  <span style="color: #dc143c;">cmd</span> = <span style="color: #483d8b;">&quot;dcop &quot;</span>+call
  pipe = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span>, shell=<span style="color: #008000;">True</span>, bufsize=<span style="color: #ff4500;">1024</span>, stdout=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span><span style="color: black;">&#41;</span>.<span style="color: black;">stdout</span>
  res = pipe.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  pipe.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> res
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Folder:
  path = <span style="color: #483d8b;">&quot;&quot;</span>
  name = <span style="color: #483d8b;">&quot;&quot;</span>
  unreadCount = <span style="color: #ff4500;">0</span>
  unreadMessages = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, path<span style="color: black;">&#41;</span>:
    dcopCall<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;kmail KMailIface getFolder &quot;</span>+path<span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">path</span> = path
    <span style="color: #008000;">self</span>.<span style="color: black;">unreadMessages</span> = <span style="color: #008000;">self</span>.<span style="color: black;">getUnreadMessages</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> getUnreadMessages<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    r = dcopCall<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;kmail FolderIface unreadMessageHeaders&quot;</span><span style="color: black;">&#41;</span>
    ss = <span style="color: #dc143c;">string</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span>r, <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>n&quot;</span><span style="color: black;">&#41;</span>
    msg = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> s <span style="color: #ff7700;font-weight:bold;">in</span> ss:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
            msg.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span>s, <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>t&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    unreadCount = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>unreadCount <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">name</span> = <span style="color: #dc143c;">string</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span>dcopCall<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;kmail FolderIface displayName&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    msg.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> msg
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> getFolders<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    r = dcopCall<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;kmail KMailIface folderList&quot;</span><span style="color: black;">&#41;</span>
    folders = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> f <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">string</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span>r, <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>n&quot;</span><span style="color: black;">&#41;</span>:
        folders.<span style="color: black;">append</span><span style="color: black;">&#40;</span>Folder<span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> folders</pre></td></tr></table></div>

<h4><code>skkmail.theme:</code></h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># you can change initial widget position, area size and updatin interval (default = 1min)</span>
karamba x=<span style="color: #ff4500;">100</span> y=<span style="color: #ff4500;">100</span> w=<span style="color: #ff4500;">400</span> h=<span style="color: #ff4500;">200</span> interval=<span style="color: #ff4500;">60000</span>
defaultfont font=<span style="color: #483d8b;">&quot;Franklin Gothic Book&quot;</span> fontsize=<span style="color: #ff4500;">12</span> color=<span style="color: #ff4500;">230</span>,<span style="color: #ff4500;">230</span>,<span style="color: #ff4500;">230</span></pre></td></tr></table></div>

<p>All code above is <a href="http://www.gnu.org/licenses/gpl.html">GPL</a>&#8216;ed.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cyberborean.org/2006/09/12/getting-new-mail-onto-the-desktop/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

