<?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>Moritz Haarmann&#039;s Blog &#187; java</title>
	<atom:link href="http://momo.brauchtman.net/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://momo.brauchtman.net</link>
	<description>random thoughts.</description>
	<lastBuildDate>Fri, 27 Jan 2012 15:54:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>bored summary of the past day. / 750words, 1buckapp, java coding tipps</title>
		<link>http://momo.brauchtman.net/2010/01/09/bored-summary-of-the-past-day-750words-1buckapp-java-coding-tipps/</link>
		<comments>http://momo.brauchtman.net/2010/01/09/bored-summary-of-the-past-day-750words-1buckapp-java-coding-tipps/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 09:24:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[personal]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[idea]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[stuttgart]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://momo.brauchtman.net/?p=485</guid>
		<description><![CDATA[So, on this very exceptionally snowy day here in Stuttgart, i decided to procrastinate a bit and do some fun stuff. Because going outside would have been at least life-threatening, I decided that the most exciting adventure would be to &#8230; <a href="http://momo.brauchtman.net/2010/01/09/bored-summary-of-the-past-day-750words-1buckapp-java-coding-tipps/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So, on this very exceptionally snowy day here in Stuttgart, i decided to procrastinate a bit and do some fun stuff. Because going outside would have been at least life-threatening, I decided that the most exciting adventure would be to clean the kitchen take a panorama image out of my 3 living room windows. Here it is.</p>
<p><a href="http://momo.brauchtman.net/wp-content/uploads/2010/01/result.jpg" rel="lightbox[485]" title="result"><img class="aligncenter size-medium wp-image-486" title="result" src="http://momo.brauchtman.net/wp-content/uploads/2010/01/result-300x68.jpg" alt="" width="300" height="68" /></a>After having mastered the fun part of the day, I had a really interesting skype call with a former colleague of mine who is now enrolled at UCL. We spoke about some projects we have been doing, and of course the upcoming ones. And I guess a sideproject of his, <a href="http://1bucketapp.appspot.com/">1bucketapp.appspot.com</a>, is really worth mentioning. I don&#8217;t know what to do with it, but maybe you can put it to use.</p>
<p>Another, at first sight, useless site is <a href="http://750words.com/">http://750words.com/</a>, a project where everyone is encouraged to write 750 words every day. The texts are not published nor accessible to anyone else but you, so it&#8217;s a private diary for everyone who has a need for one. From my personal attempt to use it I can tell that 750 words is a lot.</p>
<p>The last interesting snippet of the day was a collection of <a href="http://www.squarebox.co.uk/download/javatips.html">tipps for keeping Java code clean and maintainable</a>. Although the text dates back to 2001, it&#8217;s still of remarkabel relevance and definitely worth reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://momo.brauchtman.net/2010/01/09/bored-summary-of-the-past-day-750words-1buckapp-java-coding-tipps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Character Encoding for the rest of us: UTF-8</title>
		<link>http://momo.brauchtman.net/2009/01/19/character-encoding-for-the-rest-of-us-utf-8/</link>
		<comments>http://momo.brauchtman.net/2009/01/19/character-encoding-for-the-rest-of-us-utf-8/#comments</comments>
		<pubDate>Mon, 19 Jan 2009 12:58:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[improvingtheworld]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[useful]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://momo.brauchtman.net/?p=257</guid>
		<description><![CDATA[I spent a fair part of my past life not understanding character encodings in its entirety. While this was totally unimportant in past times, when e.g. a dataformat or file was written or created by the same program reading it, &#8230; <a href="http://momo.brauchtman.net/2009/01/19/character-encoding-for-the-rest-of-us-utf-8/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I spent a fair part of my past life not understanding character encodings in its entirety. While this was totally unimportant in past times, when e.g. a dataformat or file was written or created by the same program reading it, most likely not crossing country or language borders, nowadays it is. Very. So what is this all about?<span id="more-257"></span></p>
<p><span>Let‘s have a look at how characters can be encoded. The most simple approach is to simply put up a table with characters and assign each character a number between 0 and 255. By doing this, you get a character pool with 255 elements and can address each element with 1 byte. This approach is widely still in use, the most popular implementation is by far the ASCII format. These 255 characters did a great job for a long time. By dividing it into a lower and upper part system manufacturers were able to provide e.g. Germany with their beloved special characters, but were as well able to provide language-specific special characters to other nationalities, like French or Spain. By doing this, you gain the ability to use one byte to encode the most important subset of characters, but lose portability. A file created on a german-speaking workstation wouldn‘t be displayed correctly somewhere outside this language zone. That‘s where the concept of codemaps has it‘s origin. Each system was equipped with a codemap, which was nothing more but a table mapping byte-values to chars. </span></p>
<p><span>So there was a need growing, in times of internet and global networking, to encode characters so that they were viewable everywhere, in a consistent manner. Many approaches exist, but the one that is most widely used today and the de-facto standard for encodings is called UTF-8. Fundamentally, it is a multi-byte encoding, that is, each character can be encoded using up to 4 bytes. The exact byte count is determined by the highest order bits, if it‘s 0, only 1 byte is present, if it‘s 1, two bytes and so on. This is a clever approach, since the lower table of ASCII characters is properly UTF-8 encoded, just because the implementation is cool. The lower case table, for your interest, includes all basic numbers, whitespaces, and the alphabet in lower- and uppercase. </span></p>
<p><span>So nowadays, one should definitely use UTF-8. Why? The best thing would be to have everything in UTF-8, e.g. XML-Documents, Text Documents. Most moden languages handle Strings as being UTF-8 by default, only changing the external presentation on request. UTF-8 simplifies development of easily localizable application tremendously, while being simple to manage on the other hand. As exaggerated as it may sound: UTF-8 is indeed the answer to most encoding problems. </span></p>
<p><span>If you want to use it in Java, you are already equipped with what you need. If you want to use C to build a UTF-8-capable application, use the iconv-library, it is open-source and included on every distribution, including Cygwin. Iconv is able to encode almost any format that is there into any other format, and it‘s able to read UTF-8, or work with it to extend the C Standard Library ( which is at the date of this writing not able to do so . </span></p>
<p><span>Hopefully I could give you a brief introduction to what encodings are, and not are, and why UTF-8 should be used. Remind yourself!</span></p>
]]></content:encoded>
			<wfw:commentRss>http://momo.brauchtman.net/2009/01/19/character-encoding-for-the-rest-of-us-utf-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sharer, Giver, whatever, Filesharing</title>
		<link>http://momo.brauchtman.net/2009/01/15/sharer-giver-whatever-filesharing/</link>
		<comments>http://momo.brauchtman.net/2009/01/15/sharer-giver-whatever-filesharing/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 22:09:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[improvingtheworld]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[simpleyeteffective]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://momo.brauchtman.net/?p=242</guid>
		<description><![CDATA[A short note on a previous post where I talked about my project of a simple File-Sharing applications that basically works using drag&#8217;n'drop and some zeroconf to find other peers. I wasn&#8217;t exactly surprised to find something that matches that &#8230; <a href="http://momo.brauchtman.net/2009/01/15/sharer-giver-whatever-filesharing/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A short note on a previous post where I talked about my project of a simple File-Sharing applications that basically works using drag&#8217;n'drop and some zeroconf to find other peers. I wasn&#8217;t exactly surprised to find something that matches that description pretty well, but here it is, it&#8217;s <a href="http://code.google.com/p/giver/">called giver</a> and should run on any platform that has support for some kind of .net/mono framework. I haven&#8217;t tried it yet, but I&#8217;ll sure give it a shot and tell you about it.</p>
<p>I&#8217;m already thinking about dropping the Java-project and instead do a client for the giver-protocol in Cocoa. I would be excited to have someone reporting about the actual use of giver! Moritz.</p>
]]></content:encoded>
			<wfw:commentRss>http://momo.brauchtman.net/2009/01/15/sharer-giver-whatever-filesharing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Still no Java 6 for Intel 32 bit and PowerPC based Macs</title>
		<link>http://momo.brauchtman.net/2009/01/14/still-no-java-6-for-intel-32-bit-and-powerpc-based-macs/</link>
		<comments>http://momo.brauchtman.net/2009/01/14/still-no-java-6-for-intel-32-bit-and-powerpc-based-macs/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 14:02:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[industry]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[stupid]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://momo.brauchtman.net/?p=236</guid>
		<description><![CDATA[Running Mac OS X, i forgot to mention that in the headline. Why? Apple is most likely busy updating it&#8217;s own stuff, promoting it&#8217;s platform, and since the iPhone came up, many people started learning Cocoa, so the developer base &#8230; <a href="http://momo.brauchtman.net/2009/01/14/still-no-java-6-for-intel-32-bit-and-powerpc-based-macs/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Running Mac OS X, i forgot to mention that in the headline. Why? Apple is most likely busy updating it&#8217;s own stuff, promoting it&#8217;s platform, and since the iPhone came up, many people started learning Cocoa, so the developer base there has grown, too. Additionally, the general user base is also growing, making it a more interesting target for Software Companies. And there are not many basic rules, valid everywhere, but one is for sure that Java Desktop applications just don&#8217;t integrate well. There is maybe an exception for programs using SWT, but still, the native look and feel is something different.</p>
<p>So the question I&#8217;m asking is whether it&#8217;s that bad that there is no Java support? Yes! Absolutely! At least if you either use programs or build programs depending on it. Of course, most programs just work fine with Java 5, but there are some that just don&#8217;t. And so one of the main reasons for Java is obsolete: write once, run everywhere. And while Sun, the company behind Java, is providing Runtime Environments for Windows and Linux, it&#8217;s not for Mac. So it&#8217;s not Apples fault alone. But careless of who&#8217;s fault it is, it just sucks, clearly spoken.</p>
]]></content:encoded>
			<wfw:commentRss>http://momo.brauchtman.net/2009/01/14/still-no-java-6-for-intel-32-bit-and-powerpc-based-macs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Zeroconf made easy: Bonjour for Java, Part 2, Client Implementation</title>
		<link>http://momo.brauchtman.net/2009/01/09/zeroconf-made-easy-bonjour-for-java-client-implementation/</link>
		<comments>http://momo.brauchtman.net/2009/01/09/zeroconf-made-easy-bonjour-for-java-client-implementation/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 08:04:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[simpleyeteffective]]></category>
		<category><![CDATA[useful]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://momo.brauchtman.net/?p=190</guid>
		<description><![CDATA[I&#8217;ve been writing about the server side implementation aspects of Bonjour for Java a while ago, and I promised to explain the client side, which is a bit more tricky. The reasons therefore is that it makes heavy use of &#8230; <a href="http://momo.brauchtman.net/2009/01/09/zeroconf-made-easy-bonjour-for-java-client-implementation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been writing about the<a href="http://momo.brauchtman.net/2008/12/zeroconf-made-easy-using-bonjour-for-java/"> server side implementation aspects of Bonjour for Java </a>a while ago, and I promised to explain the client side, which is a bit more tricky. The reasons therefore is that it makes heavy use of asynchronous callbacks, leaving you no other choice but to implement some interfaces and understand the way things work behind the curtain.<span id="more-190"></span></p>
<p>So what is the client&#8217;s task? In our example it is to find, that is discover, services and make them accessible to our application. Of course, it uses the same mDNSResponder facilites discussed previously. To mention it again, mDNSResponder is, from our point of view, the single instance taking care about all actions required to either register, discover or browse services. And caused by the way it works ( by &#8220;talking&#8221; to the other peers on your network ) it can&#8217;t provide instant answers to requests made, that&#8217;s why asynchronous callbacks are required.</p>
<p>We used that killerapp example, and I&#8217;m going to continue with that, though any other name can of course be used.</p>
<p>I assume you already imported the apple package and read about how to install it if you&#8217;re unable to compile your class file.</p>
<pre lang="JAVA">import com.apple.dnssd.*;</pre>
<p>Before actually starting to putting it all together, let&#8217;s have a look at two interfaces that are important for our work. The one is BrowseListener, which actually defines the callbacks to call when a service is either lost or found.</p>
<pre lang="JAVA">public void serviceFound(DNSSDService browser,
                         int flags,
                         int ifIndex,
                         java.lang.String serviceName,
                         java.lang.String regType,
                         java.lang.String domain)

public void serviceLost(DNSSDService browser,
                        int flags,
                        int ifIndex,
                        java.lang.String serviceName,
                        java.lang.String regType,
                        java.lang.String domain)</pre>
<p>The other interface which is used to provide our application with the data necessary to connect to a discovered service using standard Java Sockets, is ResolveListener. This interface defines only one callback, serviceResolved</p>
<pre lang="JAVA">public void serviceResolved(DNSSDService resolver,
                            int flags,
                            int ifIndex,
                            java.lang.String fullName,
                            java.lang.String hostName,
                            int port,
                            TXTRecord txtRecord)</pre>
<p>which is called whenever Bonjour was apple to resolve a service. Of course, because of the BaseListener interface, operationFailed must be implemented, too. Let&#8217;s do something useful now.</p>
<p>First of all, we have to enable Bonjour and tell it what to call in case it finds something. It&#8217;s best done by having a Class which handles all the discovery stuff. So whatever your class is, just implement the interface BrowseListener, as the ResolveListener callbacks will be implemented using anonymous classes later. For me, the skeleton looks like this:</p>
<pre lang="JAVA">public class Discover implements BrowseListener {
	// the constructor.
	public Discover(){

	}

	public void serviceLost(DNSSDService browser, int flags, int ifIndex,
				String serviceName, String regType, String domain) {
	}

	public void serviceFound(DNSSDService browser, int flags, int ifIndex,
				String serviceName, String regType, String domain) {
	}

        public void operationFailed(DNSSDService arg0, int arg1) {
		// this one is required by BaseListener, which is the parent of all other Listener Interfaces.
	}
}</pre>
<p>Now I&#8217;m going to initialise Bonjour&#8217;s browsing capabilities in the constructor. Take care, It throws an exception if something goes wrong. It&#8217;s up to you what to do with it.</p>
<pre lang="JAVA">	public Discover(){
		try {
			browser = DNSSD.browse("_killerapp._tcp", this);
		} catch (DNSSDException e) {
			// do something fancy here.
		}
	}</pre>
<p>The method browse requires two arguments, the name of the service you are browsing for, and the callback handler, which is the calling object in that case. Bonjour handles the rest for us, and either one of the methods serviceFound or serviceLost gets called accordingly.</p>
<p>Assuming you are lucky and Bonjour finds a service with that name, serviceFound will be called. To resolve the found service to an IP address and a port, you need to call DNSSD.resolve, a method with the following signature</p>
<pre lang="JAVA">public static DNSSDService resolve(int flags,
                                   int ifIndex,
                                   java.lang.String serviceName,
                                   java.lang.String regType,
                                   java.lang.String domain,
                                   ResolveListener listener)
                            throws DNSSDException</pre>
<p>It&#8217;s okay to fill 0 in flags, for further meaning just read the <a href="http://developer.apple.com/documentation/Java/Reference/DNSServiceDiscovery_JavaRef/index.html">Javadoc</a>. Because our serviceFound method is already called with most of the required arguments, it&#8217;s quite easy to implement the resolve process.</p>
<pre lang="JAVA">DNSSD.resolve(0, ifIndex, serviceName, regType, domain, new ResolveListener(){
				public void serviceResolved(DNSSDService resolver, int flags, int ifIndex,
				String fullname, String hostname, int port, TXTRecord txtRecord){
					InetAddress theAddress;
					try {
						theAddress = InetAddress.getByName(hostname);
					} catch (UnknownHostException e) {
						// ouch..
					}
				}

				public void operationFailed(DNSSDService arg0, int arg1) {
					// ouch, again!
				}
			});</pre>
<p>So what&#8217;s happening here is that we create an anonymous class to handle the callbacks, which should look familiar if you&#8217;ve been doing some AWT stuff. Once the resolve process was successful, the serviceResolved method is called, and we resolve the hostname given there to create an InetAddress object filled with all the information we want to know.</p>
<p>And yes, that&#8217;s it. The same applies for the serviceLost method, which has the same signature, and it&#8217;s up to you what to do with that address object from now on! Hope this helped you a bit.</p>
]]></content:encoded>
			<wfw:commentRss>http://momo.brauchtman.net/2009/01/09/zeroconf-made-easy-bonjour-for-java-client-implementation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Antiusability at its best: Language Documentation</title>
		<link>http://momo.brauchtman.net/2008/12/30/antiusability-at-its-best-language-documentation/</link>
		<comments>http://momo.brauchtman.net/2008/12/30/antiusability-at-its-best-language-documentation/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 19:23:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[antiusability]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[industry]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[RailsMerb]]></category>
		<category><![CDATA[stupid]]></category>

		<guid isPermaLink="false">http://momo.brauchtman.net/?p=117</guid>
		<description><![CDATA[Whether you&#8217;re an active developer busy doing some Java, Ruby, Perl or ( fill in your language here ) based projects, you&#8217;ll most likely have some kind of browser windows opened sometimes providing you with the necessary documentation for libraries &#8230; <a href="http://momo.brauchtman.net/2008/12/30/antiusability-at-its-best-language-documentation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Whether you&#8217;re an active developer busy doing some Java, Ruby, Perl or ( fill in your language here ) based projects, you&#8217;ll most likely have some kind of browser windows opened sometimes providing you with the necessary documentation for libraries or ( but hopefully not ) language basics. </p>
<p>Because I&#8217;m not in love with one language and used to switching back and forth between several of them, I tend to forget some details about built-in classes etc.. It&#8217;s certainly o.k. to have the documentation for such features available somewhere on the web, but I&#8217;d love to be able to simply download bundles for a language in a &#8220;documentation reader&#8221;. Open format ( there are lots of them that would suit this application ), simple to transform anything to it via xsl or something comparable, and it would just work ( even offline ). </p>
<p>I won&#8217;t claim to start a new project here as I&#8217;m already quite busy doing the rest, and celebrating new years eve, but if somebody is looking for a challenge, here it is. </p>
<p>This subject shows another important aspect of usability. Usability shouldn&#8217;t stop at the Users place. Developers are users, too. And happy users tend to be more loyal and happy, a goal certainly worth achieving.</p>
<p>Edit: Now the night has finally arrived, and I think the following tasks should be completed before any such project can be forged. </p>
<ol>
<li>Define an open format for efficiently saving Language Documentation. Keep in mind that language documentation differs in structure depending on what kind of language is documented. Object oriented languages need other formats or present other structures than procedural or functional ones. </li>
<li>Create scripts that convert existing documentation into that new format. XSL is a powerful friend here. No one will ever take care about a cool project without a funky demo.</li>
<li>Build clients that are eaasssyy to use for all major platforms. I&#8217;m talking about smooth integration ( e.g. a spotlight plugin for OS X ), not some dirty hack.</li>
<li>Tell all your programmer friends.</li>
</ol>
<p>Thats it. But I&#8217;m still not ( yet ) interested in doing it by myself, but if anybody wants to do it, don&#8217;t bother contacting me. Sleep well.</p>
<p>Edit: found <a href="http://halfhourhacks.blogspot.com/2008/08/documentation-viewer.html">this link</a>, like it.</p>
]]></content:encoded>
			<wfw:commentRss>http://momo.brauchtman.net/2008/12/30/antiusability-at-its-best-language-documentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pass-by-reference workaround in Java</title>
		<link>http://momo.brauchtman.net/2008/12/18/pass-by-reference-workaround-in-java/</link>
		<comments>http://momo.brauchtman.net/2008/12/18/pass-by-reference-workaround-in-java/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 14:08:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[simpleyeteffective]]></category>
		<category><![CDATA[useful]]></category>
		<category><![CDATA[workaround]]></category>

		<guid isPermaLink="false">http://momo.brauchtman.net/?p=60</guid>
		<description><![CDATA[As anybody knows, java passes variables by value. and i haven&#8217;t found the magic compiler switch to change that. of course, there are problems caused by this restriction, but in most cases, pass-by-value works just fine.  If you end up &#8230; <a href="http://momo.brauchtman.net/2008/12/18/pass-by-reference-workaround-in-java/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As anybody knows, java passes variables by value. and i haven&#8217;t found the magic compiler switch to change that. of course, there are problems caused by this restriction, but in most cases, pass-by-value works just fine. </p>
<p>If you end up in a situation where it&#8217;d come in handy to have a pass-by-reference facility, think about the way java is storing objects, and how these objects are then passed-by-value. </p>
<p>First of all, an object variable contains only a ( typed ) memory address. Thus, comparing e.g. two strings for equality will yield only true if they are in fact the same strings, same applies for all other types. And this is the value. So in fact you pass a ( kind of ) pointer to a function, enabling to manipulate the original object, as the object is not cloned or something else.</p>
<p>Its also common to encapsulate basic types like int into a object just for the sake of manipulating it in some methods.</p>
<p>So let&#8217;s break it down to a simple example:</p>
<pre lang="JAVA">class MInt {
	int x;
}

public void inc(MInt what){
	what.x++;
}</pre>
<p>This will result in the int x of a MInt object being indeed incremented. Let&#8217;s imagine calling a increase method with a plain integer. This will change just nothing, at least not outside the methods scope.</p>
]]></content:encoded>
			<wfw:commentRss>http://momo.brauchtman.net/2008/12/18/pass-by-reference-workaround-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zeroconf made easy: using Bonjour for Java, Part 1</title>
		<link>http://momo.brauchtman.net/2008/12/16/zeroconf-made-easy-using-bonjour-for-java/</link>
		<comments>http://momo.brauchtman.net/2008/12/16/zeroconf-made-easy-using-bonjour-for-java/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 01:45:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://momo.brauchtman.net/?p=50</guid>
		<description><![CDATA[It&#8217;s really easy. If you have some clue about network programming and Java, this shouldn&#8217;t be extra hard to understand. It&#8217;s about zero-configuration, the ability of devices to e.g. discover them independently of any server. There are several implementations, I &#8230; <a href="http://momo.brauchtman.net/2008/12/16/zeroconf-made-easy-using-bonjour-for-java/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s really easy. If you have some clue about network programming and Java, this shouldn&#8217;t be extra hard to understand. It&#8217;s about zero-configuration, the ability of devices to e.g. discover them independently of any server. There are several implementations, I choose to use Bonjour, which is provided and developed by Apple. The Java-Bindings are available for Windows and, of course, Mac. <span id="more-50"></span></p>
<p>To use them, you have to install.. nothing on a Mac ( I actually don&#8217;t know when they introduced it to be a part of OS X, but anyone not using a 10 year old Mac should be fine ), and <a href="http://developer.apple.com/networking/bonjour/download/">Bonjour for Windows</a> for Windows. Install your JRE prior to running the Bonjour installer, as it won&#8217;t install the Java Bindings if it doesn&#8217;t find a working JRE.</p>
<p>Bonjour is one implementation of Zeroconf. It uses Multicast DNS in combination with DNS Service Discovery to allow for service browsing and discovery. Many Windows-Users may have wondered about the mDNSResponder.exe process running all the time. It&#8217;s Bonjour.</p>
<p>So, let&#8217;s pretend you&#8217;re writing a killer-network-application that wants to notify other instances of it running in the network of its existence. It would have to call it out, which is done quite simply as follows.</p>
<p>First of all you have to do the import stuff, thats self-explaining i guess</p>
<pre lang="Java">import com.apple.dnssd.*;</pre>
<p>now let&#8217;s call the class thats responsible for announcing the service ServiceAnnouncer. The interface is as follows:</p>
<pre lang="Java">public interface IServiceAnnouncer {
	public void registerService();
	public void unregisterService();
	public boolean isRegistered();
}</pre>
<p>It&#8217;s quite simple and I intentionally didn&#8217;t include any methods for setting the service&#8217; name etc. for simplicity. Now let&#8217;s go over to the implementation part.</p>
<pre lang="Java">public class ServiceAnnouncer implements IServiceAnnouncer, RegisterListener {
	private DNSSDRegistration serviceRecord;
	private boolean registered;

	public boolean isRegistered(){
		return registered;
	}

	public void registerService()  {
		try {
			serviceRecord = DNSSD.register(0,0,null,"_killerapp._tcp", null,null,1234,null,this);
		} catch (DNSSDException e) {
			// error handling here
		}
	}

	public void unregisterService(){
		serviceRecord.stop();
                registered = false;
	}

	public void serviceRegistered(DNSSDRegistration registration, int flags,String serviceName, String regType, String domain){
		registered = true;
	}

	public void operationFailed(DNSSDService registration, int error){
		// do error handling here if you want to.
	}
}</pre>
<p>The first thing you&#8217;ll probably notice is the interface RegisterListener. It contains the callbacks operationFailed and serviceRegistered that are called by the Bonjour library if registering your service either fails or succeeds. Caused by the way Bonjour works this is required.</p>
<p>Let&#8217;s have a look at the registerService method. As you can see, it consists basically of calling one static method from the DNSSD class. It&#8217;s the method registerService. If you want to have a closer look just look at it&#8217;s <a href="http://developer.apple.com/documentation/Java/Reference/DNSServiceDiscovery_JavaRef/index.html">Javadoc</a>. This method is the core of the whole service-register orgy, and it requires quite a few arguments, although using default values will work for you most of the times. What are these arguments? The method signature looks like this</p>
<pre lang="JAVA">public static DNSSDRegistration register(int flags,
                                         int ifIndex,
                                         java.lang.String serviceName,
                                         java.lang.String regType,
                                         java.lang.String domain,
                                         java.lang.String host,
                                         int port,
                                         TXTRecord txtRecord,
                                         RegisterListener listener)
                                  throws DNSSDException</pre>
<p>Well, the first field is flags, the only flag that is valid there is NO_AUTO_RENAME, which will cause two services with the same name not being renamed automatically ( like &#8220;Killerapp 2&#8243; ). Passing 0 is fine if you don&#8217;t want this behaviour. The next argument, interface index, is more interesting, as it allows you to specify on what interfaces to announce a service. Passing 0 will register your service an all available interfaces, -1 just for your local machine and other values for the specific device you want. 0 is mostly fine here, too.</p>
<p>Now it comes to the customisable part, serviceName is simply a string containing your applications name followed by some identifier, e.g. &#8220;Killerapp running on Killer Computer&#8221;. If you pass null, Bonjour will simply use the computers name. Next is the regType field, should be something like &#8220;_killer._tcp&#8221;. Important is the dot and the underscores. The protocol can, of course, be also udp if you want to.</p>
<p>Domain let&#8217;s you choose the domain on which to announce the service, using null is fine here, as well as for the next argument, host. It just specifies the target host-name, but if you pass null, things will work. Next argument is the port on which your application accepts incoming connections.</p>
<p>Bonjour features the ability to have some &#8220;payload&#8221; associated with your service record, so that not only a name and port are announced, but there is also the possibility for another computer to get a so-called TXTRecord associated with your registered service. This can be quite useful, and if you want to use it, just read further through Apple&#8217;s documentation.</p>
<p>The last argument is simply the listener, this is, our class. So passing this will be fine, as we implement the RegisterListener interface. You can use some other object here if you want to, but I think it&#8217;s most clean to have it here.</p>
<p>The method returns a service handle of the type DNSSDRegistration, used in the unregisterService to cancel the service announcement.</p>
<p>That&#8217;s it for the server part.The client part is <a href="http://momo.brauchtman.net/2009/01/zeroconf-made-easy-bonjour-for-java-client-implementation/">explained here!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://momo.brauchtman.net/2008/12/16/zeroconf-made-easy-using-bonjour-for-java/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

