<?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>Fernando J. Pereda</title>
	<atom:link href="http://blogs.gentoo.org/ferdy/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.gentoo.org/ferdy</link>
	<description>Just another Gentoo Blogs site</description>
	<lastBuildDate>Fri, 15 Oct 2010 03:06:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>On &#8220;How can someone miss a meeting?&#8221;</title>
		<link>http://blogs.gentoo.org/ferdy/2008/05/16/on_how_can_someone_miss_a_meeting/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://blogs.gentoo.org/ferdy/2008/05/16/on_how_can_someone_miss_a_meeting/#comments</comments>
		<pubDate>Fri, 16 May 2008 18:57:37 +0000</pubDate>
		<dc:creator>ferdy</dc:creator>
				<category><![CDATA[Gentoo]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Since Diego is deleting my comments from his moderation queue, I&#8217;ll try to give my impression of his post here. But first I&#8217;ll state my opinion on the matter. I think the Council made a mistake (admittedly, not a big one) by missing their latest meeting. And I don&#8217;t particularly care about why it happened. [...]]]></description>
				<content:encoded><![CDATA[<p>Since Diego is deleting my comments from his moderation queue, I&#8217;ll try to give my impression of his post here. But first I&#8217;ll state my opinion on the matter.</p>
<p>I think the Council made a mistake (admittedly, not a big one) by missing their latest meeting. And I don&#8217;t particularly care about <i>why</i> it happened. Also, I don&#8217;t think they have to give either reasons or excuses, this kind of stuff might happen, and that&#8217;s all.</p>
<p>However, there&#8217;s one reason the Council exists: because we all voted for what today is <a href="http://www.gentoo.org/proj/en/glep/glep-0039.html">GLEP39</a> (which wasn&#8217;t a GLEP by that time, as far as I recall). And said document states it clearly:</p>
<p><q>If <b><i>any</i></b> meeting has less than 50% attendance by council members, a new election for all places must be held within a month. The &#8216;one year&#8217; is then reset from that point.</q></p>
<p>There is no point in <b>enforcing</b> such a rule when Council members are late even for half an hour (or even a couple of hours). But there is no enforcing here, you guys missed the meeting, rules say you have to call an election. Hell, those of you who got my vote will get it again. (And I&#8217;m quite sure that&#8217;s the situation for lots of people).</p>
<p>Just follow the rules that give you the <i>power</i> you have.</p>
<p>However, I find it interesting that Diego, who is a Council member, didn&#8217;t read the summary of that meeting:</p>
<p><q>Did I read summary or log the day after? Sincerely, no. I did think I was there till the end, as we were already late, and I don</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/ferdy/2008/05/16/on_how_can_someone_miss_a_meeting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Slacker Council</title>
		<link>http://blogs.gentoo.org/ferdy/2008/05/15/slacker_council/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://blogs.gentoo.org/ferdy/2008/05/15/slacker_council/#comments</comments>
		<pubDate>Thu, 15 May 2008 20:44:55 +0000</pubDate>
		<dc:creator>ferdy</dc:creator>
				<category><![CDATA[Gentoo]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[As per http://archives.gentoo.org/gentoo-dev/msg_19892c04f0e6cf4c24629f13718e45cb.xml there was a meeting council scheduled for 20:00 UTC today (that&#8217;s a bit more than half an hour ago). For some reason, only amne and dberkholz showed up. As per GLEP39&#8216;s Specification: If any meeting has less than 50% attendance by council members, a new election for all places must be held [...]]]></description>
				<content:encoded><![CDATA[<p>As per http://archives.gentoo.org/gentoo-dev/msg_19892c04f0e6cf4c24629f13718e45cb.xml there was a meeting council scheduled for 20:00 UTC today (that&#8217;s a bit more than half an hour ago).</p>
<p>For some reason, only amne and dberkholz showed up. As per <a href="http://www.gentoo.org/proj/en/glep/glep-0039.html">GLEP39</a>&#8216;s <b>Specification</b>:</p>
<ul>
<li>If any meeting has less than 50% attendance by council members, a new election for <i>all</i> places must be held within a month. The &#8216;one year&#8217; is then reset from that point.</li>
</ul>
<p>What&#8217;s the council going to do? Place your bets.</p>
<p>&mdash; ferdy</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/ferdy/2008/05/15/slacker_council/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On cooperating and paludis vulnerability</title>
		<link>http://blogs.gentoo.org/ferdy/2008/05/03/on_cooperating_and_paludis_vulnerability/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://blogs.gentoo.org/ferdy/2008/05/03/on_cooperating_and_paludis_vulnerability/#comments</comments>
		<pubDate>Sat, 03 May 2008 22:24:10 +0000</pubDate>
		<dc:creator>ferdy</dc:creator>
				<category><![CDATA[Gentoo]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[A serious security issue in paludis was brought to my attention recently, and I feel I should make you all aware. Apparently someone, with root access to a machine, can gain root access by installing or editing paludis config files. For those interested, this is how it happened (times are GMT+1): 22:34 &#60;@ferdy&#62; bonsaikitten: can [...]]]></description>
				<content:encoded><![CDATA[<p>A serious security issue in paludis was brought to my attention recently, and I feel I should make you all aware. Apparently someone, with root access to a machine, can gain root access by installing or editing paludis config files.</p>
<p>For those interested, this is how it happened (times are GMT+1):</p>
<pre>22:34 &lt;@ferdy&gt; bonsaikitten: can you give me any details regarding that
 security bug in paludis?
22:35 &lt;+bonsaikitten&gt; ferdy: it's so obvious you should have found it already
22:37 &lt;@ferdy&gt; bonsaikitten: I should, but I probably haven't
22:37 &lt;+bonsaikitten&gt; ferdy: well, as I am a moron I'm unable to coherently explain <img src='http://blogs.gentoo.org/ferdy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> 
22:37 &lt;@ferdy&gt; bonsaikitten: I mean, depends on whether we are talking about
a real security issue or about something we should document to avoid people
shooting themselves in the foot
22:39 &lt;@ferdy&gt; bonsaikitten: is that all you are going to tell me?
22:39 &lt;+bonsaikitten&gt; ferdy: come on, it's obvious. You're supposed to be smart ...
22:39 * bonsaikitten is not in a mood to explain
22:40 &lt;@ferdy&gt; bonsaikitten: you aren't really talking about the paludisbuild issue, are you?
22:41 &lt;+bonsaikitten&gt; mmh no, that's a different one
22:41 &lt;@ferdy&gt; k
22:41 &lt;@ferdy&gt; bonsaikitten: what are we talking about?
22:42 &lt;@ferdy&gt; bonsaikitten: you don't need to explain it... just say, in general 
terms, what the issue is
22:50 &lt;@ferdy&gt; bonsaikitten: so? care to give any useful hint?
22:50 &lt;+bonsaikitten&gt; ferdy: doesn't happen in portage compatibility mode
22:51 &lt;+bonsaikitten&gt; but I blame the vodka, hard to explain when *burp* *giggle*
22:52 &lt;@ferdy&gt; bonsaikitten: what's the impact?
22:53 &lt;+bonsaikitten&gt; ferdy: depends on how annoying the other person is
22:54 &lt;+bonsaikitten&gt; ferdy: worst case random file modification
22:58 &lt;@ferdy&gt; bonsaikitten: and we already agreed that we aren't talking about
the paludisbuild issue, right?
22:59 &lt;@ferdy&gt; bonsaikitten: if we aren't, I'll need more hints....
23:05 &lt;@ferdy&gt; bonsaikitten: can I get an attack vector?
23:05 &lt;@ferdy&gt; that shouldn't need lots of explaining... I can figure out that
part myself
23:19 &lt;@ferdy&gt; bonsaikitten: have you got that attack vector for me?
23:24 &lt;+bonsaikitten&gt; ferdy: look at configuration files, maybe you notice that
there's some exquisit code execution possible there
23:29 &lt;@ferdy&gt; bonsaikitten: you mean those config files that only root can
edit? I must be missing something here
23:29 &lt;+bonsaikitten&gt; ferdy: you are <img src='http://blogs.gentoo.org/ferdy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> 
23:29 &lt;+bonsaikitten&gt; not much, and it's basically the same flaw bashrc is
for portage
23:29 &lt;+bonsaikitten&gt; only that bashrc is config_protect'ed ...
23:30 &lt;@ferdy&gt; bonsaikitten: but for a package to clover those files, it must be
in a repo root added, right?
23:31 &lt;+bonsaikitten&gt; someone in the package mangler group, but yes
23:35 &lt;@ferdy&gt; bonsaikitten: but if you can change those files in the first place,
why clover them by adding a malicious repo with a malicious package that changes
those files?
23:35 &lt;+bonsaikitten&gt; ferdy: because it's very subtle
23:36 &lt;@ferdy&gt; moreover, if you can already do that, why not just make the
package install whatever backdoor you want?
23:37 &lt;@ferdy&gt; I mean, it is subtle, but why would anyone go the 'convoluted'
route? he is already able to edit those files (since he had to add that repo)
23:38 &lt;+bonsaikitten&gt; 'cause only paludis is affected and you will find it very
hard to trace
23:38 &lt;+bonsaikitten&gt; that makes it so tempting ...
23:40 &lt;+bonsaikitten&gt; just don't be surprised if it suddenly unmerges itself <img src='http://blogs.gentoo.org/ferdy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> 
23:41 &lt;@ferdy&gt; yeah... well...
23:41 &lt;@ferdy&gt; bonsaikitten: mind if I disclose this vulnerability in
 planet.gentoo.org?
23:42 &lt;+bonsaikitten&gt; go ahead
23:42 &lt;@ferdy&gt; ta
23:42 &lt;+bonsaikitten&gt; 't is even on the features page of the package mangler <img src='http://blogs.gentoo.org/ferdy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </pre>
<p>This is a good lesson to learn today:</p>
<p><cite>If you can edit files owned by root in a machine, you can get root access to that machine.</cite></p>
<p>So the bottom line is: There is no vulnerability, if you can mangle paludis config files, you are already root so you don&#8217;t need to edit a file to run any command you want. Another lesson one can learn by reading that log is how to be really cooperative.</p>
<p>Ah, and before someone with a need to use cheap psychology asks, the intention of this blag post is to stop the <a href="http://gentooexperimental.org/~patrick/weblog/archives/2008-05.html#e2008-05-03T21_04_56.txt">FUD</a>.</p>
<p>- ferdy</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/ferdy/2008/05/03/on_cooperating_and_paludis_vulnerability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When you think you&#8217;ve seen everything&#8230;</title>
		<link>http://blogs.gentoo.org/ferdy/2008/04/28/when_you_think_you_ve_seen_everything/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://blogs.gentoo.org/ferdy/2008/04/28/when_you_think_you_ve_seen_everything/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 08:07:11 +0000</pubDate>
		<dc:creator>ferdy</dc:creator>
				<category><![CDATA[Gentoo]]></category>
		<category><![CDATA[Rants]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[&#8230;you get up, fire your mail client just to find: https://bugs.gentoo.org/show_bug.cgi?id=64840 https://bugs.gentoo.org/show_bug.cgi?id=168573 https://bugs.gentoo.org/show_bug.cgi?id=161368 Please take the time to read those &#8216;Excerpts&#8217;, they are really enlightening. Politics is so much fun. Some facts: All of them work in Paludis Some of them have been vocal against how #gentoo and forums.gentoo.org are managed and its politics. Using [...]]]></description>
				<content:encoded><![CDATA[<p>&#8230;you get up, fire your mail client just to find:</p>
<p>https://bugs.gentoo.org/show_bug.cgi?id=64840</p>
<p>https://bugs.gentoo.org/show_bug.cgi?id=168573</p>
<p>https://bugs.gentoo.org/show_bug.cgi?id=161368</p>
<p>Please take the time to read those &#8216;Excerpts&#8217;, they are really enlightening. Politics is so much fun.</p>
<p>Some facts:</p>
<ul>
<li>All of them work in Paludis</li>
<li>Some of them have been vocal against how #gentoo and forums.gentoo.org are managed and its politics.</li>
<li>Using the image I posted yesterday as an avatar is one of the reasons one of them got the boot.</li>
</ul>
<p>I wonder who is going to be next. I also wonder who &#8216;started&#8217; the process. It is difficult to know, because the bug is restricted ( https://bugs.gentoo.org/show_bug.cgi?id=216219 ).</p>
<p>Way to go Gentoo. No love for you today.</p>
<p>- ferdy</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/ferdy/2008/04/28/when_you_think_you_ve_seen_everything/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Doing It Wrong</title>
		<link>http://blogs.gentoo.org/ferdy/2008/04/27/doing_it_wrong/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://blogs.gentoo.org/ferdy/2008/04/27/doing_it_wrong/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 19:39:16 +0000</pubDate>
		<dc:creator>ferdy</dc:creator>
				<category><![CDATA[Gentoo]]></category>
		<category><![CDATA[Rants]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I hear this image got someone a Developer Relations complain. Way to go, Gentoo. Way to go. You are clearly doing it wrong. (For those that aren&#8217;t aware of this rather old issue: have fun). - ferdy]]></description>
				<content:encoded><![CDATA[<p>I hear this image got someone a Developer Relations complain. Way to go, Gentoo. Way to go.</p>
<p><center><img src="http://forums.gentoo.org/images/avatars/150511225947d5a05181db1.png" /></center></p>
<p>You are clearly doing it wrong.</p>
<p>(For those that aren&#8217;t aware of this rather old issue: have <a href="http://forums.gentoo.org/viewtopic-t-480537.html">fun</a>).</p>
<p>- ferdy</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/ferdy/2008/04/27/doing_it_wrong/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>From yoswink: 1st Technological days of Isla Cristina</title>
		<link>http://blogs.gentoo.org/ferdy/2008/04/13/from_yoswink_1st_technological_days_of_i/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://blogs.gentoo.org/ferdy/2008/04/13/from_yoswink_1st_technological_days_of_i/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 12:44:44 +0000</pubDate>
		<dc:creator>ferdy</dc:creator>
				<category><![CDATA[Gentoo]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I&#8217;m proxy-posting this to Gentoo Planet from yoswink: &#8212; During the last week, the people of Isla Cristina (a beautiful town in the Atlantic coast with probably the best beaches of all Spain) organized an event called &#8217;1st Technological days of Isla Cristina&#8217;. They invited me to participate and give a talk about Gentoo. On [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;m proxy-posting this to Gentoo Planet from yoswink:</p>
<p>&#8212;</p>
<p>During the last week, the people of <a href="http://www.terra.es/personal/xilcar/">Isla Cristina</a>  (a beautiful town in the Atlantic coast with probably the best beaches of all Spain) organized an event called <a href="http://www.iesmirabent.com/jornadas/index2.html">&#8217;1st Technological days of Isla Cristina&#8217;</a>. They invited me to participate and give a talk about Gentoo.</p>
<p>On Friday I was there explaining what makes our distribution different, what the &#8216;All about choice&#8217; is and the technology behind it. I shared the day with Spanish developers of other open source projects such as <a href="http://www.openbsd.org">OpenBSD</a>, <a href="http://www.netbsd.org">NetBSD</a> and <a href="http://www.kde.org">KDE</a>.</p>
<p>The day organization was perfect, any more to say, it couldn&#8217;t be better. I want to thanks the people (teachers and students) from <a href="http://iesmirabent.com/enlaces.html">IES Mirabent</a>, who fought to make this idea a reality in a small town of 22k people (is not so common to find this kind of event in Spain with official devs from big OSS projects even in the big cities or universities), and to the youth department of Isla Cristina for all they have done for me.</p>
<p>If someone want to take an example about how an event should treat a lecturer, please contact with them.</p>
<p>P.D: thanks to Julio Merio from NetBSD for lending me the computer (I forgot the damn Mini-DVI cable) and sorry to Antonio Larrosa from KDE, I ate some minutes of his time <img src='http://blogs.gentoo.org/ferdy/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>&#8212;</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/ferdy/2008/04/13/from_yoswink_1st_technological_days_of_i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On tool boxes</title>
		<link>http://blogs.gentoo.org/ferdy/2008/01/15/on_tool_boxes/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://blogs.gentoo.org/ferdy/2008/01/15/on_tool_boxes/#comments</comments>
		<pubDate>Tue, 15 Jan 2008 10:12:00 +0000</pubDate>
		<dc:creator>ferdy</dc:creator>
				<category><![CDATA[Gentoo]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Apparently, people are bashing Joshua Jackson (a.k.a. tsunam) for posting his opinion (which happens to be shared among lots of us). So just in case someone hasn&#8217;t read what he said yet, I&#8217;m going to link his posts here. Please, do read them: http://tsunam.org/2008/01/12/in-response/ http://tsunam.org/2008/01/14/clarifications/ http://tsunam.org/2008/01/14/tool-box/ I simply can&#8217;t trust Daniel Robbins after what he [...]]]></description>
				<content:encoded><![CDATA[<p>Apparently, people are bashing Joshua Jackson (a.k.a. tsunam) for posting his opinion (which happens to be shared among lots of us). So just in case someone hasn&#8217;t read what he said yet, I&#8217;m going to link his posts here. Please, do read them:</p>
<p>http://tsunam.org/2008/01/12/in-response/</p>
<p>http://tsunam.org/2008/01/14/clarifications/</p>
<p>http://tsunam.org/2008/01/14/tool-box/</p>
<p>I simply can&#8217;t trust Daniel Robbins after what he tried to do the last time he tried to come back. Has everybody forgotten that? I hope not.</p>
<p>No love.</p>
<p>- ferdy</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/ferdy/2008/01/15/on_tool_boxes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Paludis meets Java, part III</title>
		<link>http://blogs.gentoo.org/ferdy/2007/11/29/paludis_meets_java_part_iii/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://blogs.gentoo.org/ferdy/2007/11/29/paludis_meets_java_part_iii/#comments</comments>
		<pubDate>Wed, 28 Nov 2007 21:14:28 +0000</pubDate>
		<dc:creator>ferdy</dc:creator>
				<category><![CDATA[Gentoo]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Once we&#8217;ve converted native types we face the task of converting arbitrary classes and types. Another thing we&#8217;ll need is extract the C++ type of a Java object (jobject in JNI). For that task, we will follow the convention of calling the native pointers in Java classes _ptr. With that, we can define the following [...]]]></description>
				<content:encoded><![CDATA[<p>Once we&#8217;ve converted native types we face the task of converting arbitrary classes and types. Another thing we&#8217;ll need is extract the C++ type of a Java object (<tt>jobject</tt> in JNI).</p>
<p>For that task, we will follow the convention of calling the native pointers in Java classes <tt>_ptr</tt>. With that, we can define the following templates:</p>
<pre>template &lt;typename T_&gt;
inline T_ * get_native_ptr(JNIEnv * env, const char * const class_name, jobject obj)
{
    jclass cls(env-&gt;FindClass(class_name));
    jfieldID ptr_field(env-&gt;GetFieldID(cls, "_ptr", "J"));
    jlong ptr(env-&gt;GetLongField(obj, ptr_field));
    env-&gt;DeleteLocalRef(cls);
    return from_java_ptr&lt;T_&gt;(ptr);
}

template &lt;typename T_&gt;
inline tr1::shared_ptr&lt;T_&gt; get_native_sptr(JNIEnv * env, const char * const class_name, jobject obj)
{
    jclass cls(env-&gt;FindClass(class_name));
    jfieldID ptr_field(env-&gt;GetFieldID(cls, "_ptr", "J"));
    jlong ptr(env-&gt;GetLongField(obj, ptr_field));
    env-&gt;DeleteLocalRef(cls);
    return from_java_ptr_sptr&lt;T_&gt;(ptr);
}</pre>
<p>This should really be doing more error checking, but it is good enough for ilustrating how nice are things when using proper tools (both languages and libraries).</p>
<p>Converting arbitrary types also uses some template magic:</p>
<pre>template &lt;typename T_&gt;
struct NativeToJavaTypeMapper
{
    jobject operator() (JNIEnv *, const T_ &amp;);
};</pre>
<p>Now we need specializations for each type we want to convert, for instance, converting a paludis&#8217; <tt>FSEntry</tt> into a <tt>java.io.File</tt> looks like the following:</p>
<pre>template&lt;&gt;
struct NativeToJavaTypeMapper&lt;FSEntry&gt;
{
    jobject operator() (JNIEnv * env, const FSEntry &amp; f)
    {
        jclass cls(env-&gt;FindClass("java/io/File"));
        jmethodID constructor(env-&gt;GetMethodID(cls, "&lt;init&gt;", "(Ljava/lang/String;)V"));
        jobject ret(env-&gt;NewObject(cls, constructor, to_java_string(env, stringify(f))));
        env-&gt;DeleteLocalRef(cls);
        return ret;
    }
};</pre>
<p>Neat and clean.</p>
<p>For containers, a <tt>java.util.LinkedList</tt> would be used for paludis&#8217; <tt>Sequence</tt>; and <tt>java.util.TreeSet</tt> for paludis&#8217; <tt>Set</tt>:</p>
<pre>template &lt;typename T_, typename It_&gt;
jobject range_to_list(JNIEnv * env, It_ begin, It_ end)
{
    jclass list_class(env-&gt;FindClass("java/util/LinkedList"));
    jmethodID constructor(env-&gt;GetMethodID(list_class, "&lt;init&gt;", "()V"));
    jobject our_list(env-&gt;NewObject(list_class, constructor));
    jmethodID add_method(env-&gt;GetMethodID(list_class, "add", "(Ljava/lang/Object;)Z"));

    for (It_ i(begin) ; i != end ; ++i)
        env-&gt;CallBooleanMethod(our_list, add_method, NativeToJavaTypeMapper&lt;T_&gt;()(env, *i));

    env-&gt;DeleteLocalRef(list_class);

    return our_list;
}

template &lt;typename T_, typename It_&gt;
jobject range_to_set(JNIEnv * env, It_ begin, It_ end)
{
    jclass set_class(env-&gt;FindClass("java/util/TreeSet"));
    jmethodID constructor(env-&gt;GetMethodID(set_class, "&lt;init&gt;", "()V"));
    jobject our_set(env-&gt;NewObject(set_class, constructor));
    jmethodID add_method(env-&gt;GetMethodID(set_class, "add", "(Ljava/lang/Object;)Z"));

    for (It_ i(begin) ; i != end ; ++i)
        env-&gt;CallBooleanMethod(our_set, add_method, NativeToJavaTypeMapper&lt;T_&gt;()(env, *i));

    env-&gt;DeleteLocalRef(set_class);

    return our_set;
}</pre>
<p>And now defining <tt>NativeToJavaTypeMapper</tt> specializations for containers is quite easy:</p>
<pre>template&lt;&gt;
template &lt;typename T_&gt;
struct NativeToJavaTypeMapper&lt;tr1::shared_ptr&lt;const Sequence&lt;T_&gt; &gt; &gt;
{
    jobject operator() (JNIEnv * env, const tr1::shared_ptr&lt;const Sequence&lt;T_&gt; &gt; &amp; s)
    {
        return range_to_list&lt;T_&gt;(env, s-&gt;begin(), s-&gt;end());
    }
};

template&lt;&gt;
template &lt;typename T_&gt;
struct NativeToJavaTypeMapper&lt;tr1::shared_ptr&lt;const Set&lt;T_&gt; &gt; &gt;
{
    jobject operator() (JNIEnv * env, const tr1::shared_ptr&lt;const Set&lt;T_&gt; &gt; &amp; s)
    {
        return range_to_set&lt;T_&gt;(env, s-&gt;begin(), s-&gt;end());
    }
};</pre>
<p>I&#8217;ve spent a fair amount of the time fighting with make and the build system. It looks mostly sane now, though.</p>
<p>Dealing with exceptions has been a bit tricky, however, I think I have a good <i>system</i> to deal with it now, even  though Ciaran tagged it as <i>icky</i> and <i>ugly</i> <tt> <img src='http://blogs.gentoo.org/ferdy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </tt>. That&#8217;d be the topic of the next part of the series.</p>
<p>The documentation is currently at http://dev.gentoo.org/~ferdy/paludis-jni/ . All of that has been accomplished in:</p>
<pre>[ $ ~/git/paludis/jni(jni) ] git diff --shortstat trunk..
 65 files changed, 4371 insertions(+), 0 deletions(-)</pre>
<p>Which is not a lot of code for what&#8217;s exposed.</p>
<p>&mdash; ferdy</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/ferdy/2007/11/29/paludis_meets_java_part_iii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Paludis meets Java, part II</title>
		<link>http://blogs.gentoo.org/ferdy/2007/11/19/paludis_meets_java_part_ii/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://blogs.gentoo.org/ferdy/2007/11/19/paludis_meets_java_part_ii/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 08:04:39 +0000</pubDate>
		<dc:creator>ferdy</dc:creator>
				<category><![CDATA[Gentoo]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[After showing how is the structure of a regular Paludis class using JNI, next thing is showing part of the magic behind paludis_java.hh. What&#8217;s there is functions to convert Java types into Paludis (and C++ native) types and viceversa (this conversions exist only when needed, not for every single type). So, for instance, one of [...]]]></description>
				<content:encoded><![CDATA[<p>After showing how is the structure of a regular Paludis class using JNI, next thing is showing part of the magic behind <tt>paludis_java.hh</tt>.</p>
<p>What&#8217;s there is functions to convert Java types into Paludis (and C++ native) types and viceversa (this conversions exist only when needed, not for every single type). So, for instance, one of those conversions would be <tt>jboolean &lt;--&gt; bool</tt> and it looks like this:</p>
<pre>inline jboolean to_java_boolean(bool b)
{
    return b ? JNI_TRUE : JNI_FALSE;
}

inline bool from_java_boolean(jboolean b)
{
    return b == JNI_TRUE;
}</pre>
<p>That was easy, let me show you the <em>dirty</em> part. I call it dirty not because it is difficult or tricky to understand, but because it is a bit <em>icky</em>. It is the way we store the native Paludis pointers in Java classes:</p>
<pre>inline jlong to_java_ptr(void * const ptr)
{
    return reinterpret_cast&lt;jlong&gt;(ptr);
}

template &lt;typename T_&gt;
inline T_ * from_java_ptr(jlong ptr)
{
    return reinterpret_cast&lt;_ *&gt;(ptr);
}

template &lt;typename T_&gt;
inline tr1::shared_ptr&lt;T_&gt; from_java_ptr_sptr(jlong ptr)
{
    return * reinterpret_cast&lt;tr1::shared_ptr&lt;T_&gt; *&gt;(ptr);
}</pre>
<p>Although dirty, it is quite easy aswell. Something this bindings will be converting a lot is strings:</p>
<pre>std::string
from_java_string(JNIEnv * const env, jstring s)
{
    const char * const c_s(env-&gt;GetStringUTFChars(s, 0));
    std::string result(c_s);
    env-&gt;ReleaseStringUTFChars(s, c_s);
    return result;
}

jstring
to_java_string(JNIEnv * const env, const std::string &amp; s)
{
    return env-&gt;NewStringUTF(s.c_str());
}</pre>
<p>And since C++ is such a nice language comparing and stringifying arbitrary types was just as easy (credits for this go to Mr. McCreesh):</p>
<pre>template &lt;typename T_&gt;
jstring common_stringify(JNIEnv * const env, jlong ptr)
{
    return to_java_string(env, stringify(*from_java_ptr&lt;T_&gt;(ptr)));
}

template &lt;typename T_&gt;
jint common_compare(jlong lhs_ptr, jlong rhs_ptr)
{
    T_ * const lhs(from_java_ptr&lt;T_&gt;(lhs_ptr)), * const rhs(from_java_ptr&lt;T_&gt;(rhs_ptr));
    if (*lhs &lt; *rhs)
        return -1;
    else if (*rhs &lt; *lhs)
        return 1;
    else
        return 0;
}</pre>
<p>This wasn&#8217;t quite difficult and it certainly makes working with JNI easier. However, there is still stuff to do (actually, to show, since it is implemented and working in my git repository) like exception handling, converting arbitrary Paludis types and typesafe containers.</p>
<p>During the weekend I&#8217;ve written bindings for almost every core Paludis class and the patch is not that big:</p>
<pre>[ $ ~/git/paludis(jni) ] git diff --shortstat trunk..
 30 files changed, 1975 insertions(+), 0 deletions(-)</pre>
<p>Now it is time to stop the bindings for a while and start writing documentation, examples and integrating the bindings into the Paludis codebase properly.</p>
<p>Next part of the series will be about how I am converting arbitrary types and containers into Java types and typesafe collections respectively.</p>
<p>&mdash; ferdy</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/ferdy/2007/11/19/paludis_meets_java_part_ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Paludis meets Java, part I</title>
		<link>http://blogs.gentoo.org/ferdy/2007/11/18/paludis_meets_java_1/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://blogs.gentoo.org/ferdy/2007/11/18/paludis_meets_java_1/#comments</comments>
		<pubDate>Sun, 18 Nov 2007 11:28:48 +0000</pubDate>
		<dc:creator>ferdy</dc:creator>
				<category><![CDATA[Gentoo]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Some days ago, the Wearer of the shiny hat started tinkering with JNI bindings for Paludis. Even if we all think Java is perverse and should be avoided at all costs. Having used Java for the last four years and having studied part of the Java5 code I thought I could use everything I learnt [...]]]></description>
				<content:encoded><![CDATA[<p>Some days ago, the <em>Wearer of the shiny hat</em> started tinkering with JNI bindings for Paludis.</p>
<p>Even if we all think Java is perverse and should be avoided at all costs. Having used Java for the last four years and having studied part of the Java5 code I thought I could use everything I learnt and took the task of developing and maintaining the JNI bindings.</p>
<p>It has all been relatively easy. It&#8217;s good that Ciaran started them; because his experience with the Ruby bindings gave the key to developing the Java bindings <em>cleanly</em>.</p>
<p>What we are currently doing is storing a C++ pointer in Java classes and passing it around in native methods. Then we delete it when Java&#8217;s garbage collector decides it is good to call finalize.</p>
<p>So it mostly looks like this:</p>
<pre>package paludis;
import paludis.Paludis;

public class Foo {
    private static Paludis _load_the_frickin_paludis_library = new Paludis();
    private long _ptr;

    private static native long _construct_string(String s);
    private static native void _destruct(long ptr);

    private static native boolean _is_foo(long ptr);

    protected void finalize() throws Throwable {
        _destruct(_ptr);
        super.finalize();
    }

    public Foo(String s) {
        _ptr = _construct_string(s);
    }

    public boolean isFoo() {
        return _is_foo(_ptr);
    }
}</pre>
<p>Then the C++ part looks like this:</p>
<pre>/* vim: set sw=4 sts=4 et foldmethod=syntax : */

#include "foo.hh"
#include "paludis_java.hh"
#include &lt;jni.h&gt;
#include &lt;paludis/foo.hh&gt;

using namespace paludis;
using namespace paludis::java;

JNIEXPORT jlong JNICALL
Java_paludis_Foo__1construct_1string(JNIEnv * env, jclass, jstring s)
{
    return to_java_ptr(new Foo(from_java_string(env, s)));
}

JNIEXPORT void JNICALL
Java_paludis_Foo__1destruct(JNIEnv *, jclass, jlong ptr)
{
    delete from_java_ptr&lt;Foo&gt;(ptr);
}

JNIEXPORT jboolean JNICALL
Java_paludis_Foo__1is_1foo(JNIEnv *, jclass, jlong ptr)
{
    return to_java_boolean(from_java_ptr&lt;Foo&gt;(ptr)->is_foo());
}</pre>
<p>Here, all the magic is happening behind the scenes in the <tt>paludis_java.hh</tt> header which I&#8217;ll show in a future post of the <em>Paludis meets Java</em> series.</p>
<p>&mdash; ferdy</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/ferdy/2007/11/18/paludis_meets_java_1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
