Near Field Communication (NFC)

Near Field Communication (NFC) is a wireless/radio-frequency technology that works over short-range. This involves communicating data between devices(“initiator” and “target”) in close proximty normally requiring less than 10 centimeters. In the realm of mobile phones, the “initiator” is the mobile handset and the target is typically a RFID(Radio Frequency Identification) Tag for passive communication. To understand it; RFID Tags, for passive communication can be thought as QR/barcodes and the smartphone as a “reader”. But with NFC it is also possible to have an “active” communication, which requires powered RFID Tags enabling peer-to-peer communication between itself and the NFC-enabled device, similar to Bluetooth.

Now this technology opens up a realm of possibilities, from reading messages from RFID Smart-Posters, using the mobile phone as keys, to mobile payment services, in which your smart phone becomes your smart wallet making your phone serve as a credit card or, depending on the payment model, charge a credit card with an embedded RFID Tag. This technology is still new but is being widely adopted by smart phone hardware & software vendors; and Android 2.3 has already provided a high-level API to write NFC applications. Google’s Nexus-S is now available in most countries, with built-in NFC support, which enables Nexus-S to read RFID Tags; iPhone 5 & iPad 2 will also be launched with NFC chips. And soon as this technology is well understood, fully standardized with the security issues resolved, we will see it being adopted by more and more companies and financial institutions.

A free software license

There are many free software licenses available these days, from viral and stringent licenses like GPL, with some grey and confusing like LGPL to business friendly licenses like ASL and X11. All of these licenses are “open source”, which means the software source is also available to public along with its binaries. Which is great for open source and I personally like ASL among the lot and tend to use it for most of my open source software. But sometimes you might feel that these licenses are not “free” enough, and pack tons of terms and conditions in a pretty complicated language. There should be a software specific license which doesn’t restrict the user in anyway and allows to freely use, modify and distribute the software in any way the user likes. And the derived work should be allowed for both commercial and personal use, with or without a charge.

Now with the above situation in mind and as a developer of free software, here is my attempt to create a re-usable free software license. Lets call it, for obvious reasons, The Free Software License (TFSL). I must also mention that this license is inspired from GPL, ASL and WTF licenses and I have borrowed a few things from them. This license is also DRM-free and is composed of the following three important things:

  1. Terms of use
  2. Warranty
  3. Liability

I think these three things explain fully how the software should be used and also protects the developer/vendor from any damages inured from the use of the software. This license also makes sure that the software, licensed under TFSL, is distributed for free.

THE FREE SOFTWARE LICENSE
Version 1, April 2011

Copyright (C) 2011 Kamran Zafar <kamran@kamranzafar.org>

Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing is allowed as long as the name is changed.

THE FREE SOFTWARE LICENSE
TERMS AND CONDITIONS FOR USE, MODIFICATION, REPRODUCTION AND DISTRIBUTION

0. You can use, modify, reproduce and distribute the software and any of its components in any way you want.
1. The software is available on “AS IS” basis and comes with absolutely NO warranty, either express or implied, to the extent permitted by applicable law.
2. In any event and under any legal theory, the copyright owner of the software will NOT be liable for any direct or in-direct damages incurred by the existence or the use of the software.

This “free software license” is available for free and can be used by any software vendor to distribute software as long as it meets the terms and conditions outlined in it’s text. In order to apply this license to the software, each source file of the software MUST contain the following wording.

Copyright [year] [name of copyright owner]

This program is a free software and comes with absolutely no warranty. You can use, modify, reproduce and distribute this software under the terms and conditions of The Free Software License.

You can obtain the copy of The Free Software License at

http://kamranzafar.org/licenses/tfsl.txt

Now like I said, it is just an attempt to create such a free software license and I am sure this is not perfect but it can be modified as per vendor requirements. This license is also available for download here.

Why is git better than svn

Well I am not going to start debating over this question, as most people on the web are doing so. I am not going to list pros and cons of both svn and git and make a vague conclusion in the end. I being a user of both svn and git, found git better for the kind of work I do and the way I do it. I was mainly a svn user before I heard people talking about git being easier etc. than svn; after I started using git I agree with these people. With git, it is much easier, simpler, faster and natural(in the realm of development) to manage your code, to create and clone repos; branch, tag and merge code. Apart from being simple and powerful, git is also decentralized and distributed in nature, which I think gives it that extra edge and undoubtedly the main advantage over svn.

Now I found git better because I no-longer have to think about problems and limitations associated with svnsync; with git I can clone my repositories, pull and push my changes from anywhere in the world. In fact git allows ubiquitous transactions, so one doesn’t have to be connected to the repo (as in svn) to check in changes. One also doesn’t have to remember branch or tag locations like with svn. I sometimes work while I am traveling, when I am in a different city or country; so now, with git, I don’t have to care from where in the world I am pushing files to which mirror of which repository; in other words I won’t indulge in the master-slave repo setups like in svn, in git every clone of a repo is truly “equal”, master and slave at the same time. Security is one area where git needs to focus on more, because it is decentralized; but the current asymmetric encryption for all transactions seem to suffice.

There are many other things one can talk about, there are pros and cons of both SCMs. But in the end I would say that gradually people should try and move away from a centralized repo to a more distributed SCM because of many advantages, some of which are described above.

Crawl, index and search

Sometimes you need to search for files or pages on content-rich websites or browser-based information software/encyclopedias that don’t really have a search functionality. And it could be a pain to find what you are looking for. I wrote a little crawler once in python that works well to search for stuff, on websites, on the fly. But sometimes a real “index” is needed for searching. There are a few libraries available and among them is the open source Apache Lucene, an excellent high-performance text search engine library that one can use for free. Lucene coupled with a multi-threaded web crawler and you have a pretty good index and search functionality; though not as good as google, but close.

Below is an example of how you can use Lucene to build searchable indexes for websites.

// Create an index directory
FSDirectory dir = FSDirectory.open( new File( "C:/test/myindex") );

// Use the IndexWriter to write text documents to the above directory
IndexWriter writer = new IndexWriter( dir, new StandardAnalyzer( Version.LUCENE_CURRENT ),
  true, IndexWriter.MaxFieldLength.LIMITED );
.
.
// For each crawled URL, create a document and add to index.
// You can add as many attributes you want
Document doc = new Document();
doc.add( new Field( "contents", new StringReader("...the content...") ) );
doc.add( new Field( "url", "http://the-crawled-url", Field.Store.YES, Field.Index.NOT_ANALYZED ) );
doc.add( new Field( "mime", "text/plain", Field.Store.YES, Field.Index.NOT_ANALYZED ) );

writer.addDocument( doc );

writer.commit(); // Commit changes

Now once the index is created we can start searching it for content. Lucene provides IndexSearcher class that is used to search the index using a Query. Below is an example that searches for results in the above created index, and prints the website URL where the required content is found.

// Open the index directory
FSDirectory index = FSDirectory.open( new File( "C:/test/myindex" ) );

// Create a search query
String querystr = "hello world";
Query q = new QueryParser( Version.LUCENE_CURRENT, "contents",
  new StandardAnalyzer( Version.LUCENE_CURRENT ) ).parse( querystr );

int hitsPerPage = 10; // Used for pagination
IndexSearcher searcher = new IndexSearcher( index, true );
TopScoreDocCollector collector = TopScoreDocCollector.create( hitsPerPage, true );
searcher.search( q, collector );
ScoreDoc[] hits = collector.topDocs().scoreDocs;

// For each result print the URL
for( int i = 0; i &lt; hits.length; ++i ) {
  int docId = hits[i].doc;
  Document d = searcher.doc( docId );
  System.out.println( ( i + 1 ) + ". " + d.get( "url" ) );
}

The full source of this example, including the web crawler, can be found here and is available under GPL.

Project migration from Sourceforge to Googlecode

I have been using googlecode for some of my recent open source development work, and I was surprised how googlecode speeds up development. The SCM is very fast and gave me no troubles, it is easy to create wiki pages and documentation for projects etc. etc. Although it offers limited features, compared to sourceforge for example, but the real power is in its simplicity. Sourceforge offers more features like, hosting web pages and shell services and if you are smart you can also create your own little maven repository for your artifacts; one might argue that all these features make sourceforge very complex. But recently sourceforge has become slow as hell, and it is bit of a pain to manage your work, SCM is slow, web pages are not served with a desired speed, shell services (although more secure) but slower and the whole shell-creation process takes too long. So to cut the story short, I finally decided to migrate some of my work from sourceforge to googlecode, simply because googlecode is faster and simple.

In the beginning I had no clue how to achieve this task. But it was much simpler than I anticipated. My only concern was to get the code migrated fully, safely and with all the version history. This is done by synch’ing the project’s SVN repository on googlecode with the repository on sourceforge. First I reset the googlecode repository to enable svn synch’ing. This is done under Administrator->Source tab on your project’s homepage on googlecode. Then I began the synch’ing process.

The first step is to initialize the googlecode’s subversion repository

svnsync init –username [user] –password [pass]https://[project].googlecode.com/svn/trunk/ https://[project].svn.sourceforge.net/svnroot/[project]

After this we just start synch’ing the repositories.

svnsync sync –username [user] –password [pass]https://[project].googlecode.com/svn/trunk/

The above command will fetch all the code, with the version history and including tags and branches.

And this is all to it. For more information on the svnsync refer to the subversion redbook.

Oracle-IBM pact and Android

IBM-Oracle pact is a good news for Java developers and for the open source community in general. OpenJDK is a more natural open alternative to Oracle J2SE and is well backed by the Java community, so IBM’s move to shift “its development effort from the Apache project Harmony to OpenJDK” makes sense. And remember IBM also wanted to acquire Sun mainly because of Java. Apache Harmony on the other hand never gained enough popularity because of the TCK-issuance tussle between Sun and Apache. But Harmony remains as another open-source implementation of Java, free from legal infringements.

Now a lot of people see this pact as a threat to Google’s Android platform, and associate it with Oracle’s lawsuit against Google, but I don’t think this is the case (although Oracle may think otherwise). First of all OpenJDK is not built for mobile platforms, it could be a threat to Oracle’s own J2SE but not to Android. Secondly Google uses Apache Harmony, which has been rewritten and is Open. Google also uses a Dalvik VM, which is a special virtual machine written from scratch for mobile devices, and is also backed by the Open Handset Alliance. Dalvik VM is also open-source and uses it’s own form of bytecode, which is “distinct and different from Java bytecode“. So Google is not using any Java component in their Android platform, which would obligate them to require a license from Oracle.

As a developer, I think IBM’s move is good for Java being Open. To Google it is kind of an opportunity to start owning Harmony and to control development on it; and Google should seriously start investing in Harmony and capitalize on this opportunity. And for the lawsuit, I think Google can win it, if it is not thrown out of the court, may be fined for “some incompatibilities” in their version of Java. But it is the time this suit is going to take, “may” cause some hardware vendors to slowdown their production of android phones, but the demand will drive it and I don’t see it happening because Android phones are much cheaper; a lot of android phones are lined up for 2011 with Android-3-based tablets.

Oracle, a patent troll, just wants money and will try to prolong the lawsuit as much as they can to get some leverage from “time” and “doubts” about future of android. But in the end it is not going to matter much because Oracle is not Android’s competitor and doesn’t have a “real” mobile platform except for the dying-j2me, which they inherited from Sun.

Open Source “Duke Nukem Forever”

One of my favorite games of all time, Duke Nukem 3D had me waiting “forever”, for DNF (Duke Nukem Forever). Then in May 2009 the wait finally ended, when the DNF team got axed. And the game was then kicked into the back burner of Apogee. I think the fans of Duke deserve better, we waited 13 years for the game and in the end it was all in vein.

I think it is time to make DNF Open Source and let the community and fans develop it, and I am pretty sure it won’t take that long for the game to be completed. The Open Source guys have done it in the past by developing the High Resolution Pack for Duke Nukem 3D, and I am sure they can do it again.

I’ll end this entry with my favorite dialog from DN3D: “it’s time to kick ass and chew bubble gum, and I’m all outta gum.”

Cyber Sanctions: “Freedom” the American way

US government has recently asked Sourceforge to deny content to certain countries, including Cuba, Iran, North Korea, Syria and Sudan. This means that users in these countries trying to post or access content on Sourceforge will get a big 403 error. Here is what Sourceforge is saying about it, although not happy but they don’t seem to have a choice:

Since 2003, the SourceForge.net Terms and Conditions of Use have prohibited certain persons from receiving services pursuant to U.S. laws, including, without limitations, the Denied Persons List and the Entity List, and other lists issued by the U.S. Department of Commerce, Bureau of Industry and Security. The specific list of sanctions that affect our users concern the transfer and export of certain technology to foreign persons and governments on the sanctions list. This means users residing in countries on the United States Office of Foreign Assets Control (OFAC) sanction list, including Cuba, Iran, North Korea, Sudan, and Syria, may not post content to, or access content available through, SourceForge.net. Last week, SourceForge.net began automatic blocking of certain IP addresses to enforce those conditions of use.

This move will only divide the open source community and will not achieve anything. There are other ways to access content through mirrors and other project hosts; and people will be forced to use anonymizers etc. to hide their identity. But will the US government stop Microsoft, Apple etc. to sell their applications to businesses in these countries? I doubt that. So why target Open Source alone? And what is next? denying free email and web hosting services?

On a more serious note, we all know that most of the affected countries are poor and belong to the third world, so it will just push the users towards piracy. It also means that Internet and WWW are still not global and have a lot of American influence. The US seem to have their own definition of freedom, which just serves the US interests with hypocrisy written all over it. On one side US is trying to battle the Chinese censorship on Google and on the other hand they are doing the same thing by denying people services, which are licensed to be free for all.

Log4J: Emailing specific errors only

Log4J’s SMTPAppender provides enough basic functionality to send out error messages as emails. Although emailing error messages is not always a good idea, unless something really goes wrong in system. Sometimes it is required to send out only specific errors in specific areas of the system. It is possible to do that with the SMTPAppender, even if these specific errors lie in the same category(WARN, ERROR etc.) as other errors. This is achieved with TriggeringEventEvaluator. Here is a sample config for the SMTPAppender.

    <appender name="EMAIL" class="org.apache.log4j.net.SMTPAppender">
        <param name="To" value="kamran.zafar@xeustechnologies.org" />
        <param name="From" value="server-errors@xeustechnologies.org" />
        <param name="Subject" value="SERIOUS ERROR" />
        <param name="SMTPHost" value="mysmtphost" />
        <param name="SMTPUsername" value="server-errors" />
        <param name="SMTPPassword" value="password" />
        <param name="Threshold" value="ERROR" />
        <param name="BufferSize" value="1" />
        <param name="EvaluatorClass" value="org.xeustechnologies.test.log4j.SmtpTrigger" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{dd MMM yyyy HH:mm:ss.SSS}][%p][%t][%c] - %m%n%n" />
        </layout>
    </appender>

The parameter EvaluatorClass is where we specify the implementation of TriggeringEventEvaluator, which acts like a filter to allow emailing only specific errors:

import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.TriggeringEventEvaluator;

public class SmtpTrigger implements TriggeringEventEvaluator {

    public boolean isTriggeringEvent(LoggingEvent event) {
        /*
         * Email errors
         */
        if( event.getLoggerName().equals( "SomeSeriousErrorLogger" )
                &amp;&amp; event.getLevel().equals( Level.ERROR ) ) {
            return true;
        }

        return false;
    }
}

Now only ERRORs from “SomeSeriousErrorLogger” will be emailed.

Merging arrays in Java

I was playing around with java arrays and ran into a problem where I wanted to merge smaller arrays into a single big array. So the first thing I thought was to make a new array of size equal to the total length of all smaller arrays and then populating the values using loops. Then I went on to make a generic method to merge Generic arrays of same type but I realized that I cannot create a Generic array. So I thought of another way to merge Generic arrays using List.

  public <T> T[] merge(T[]... arrays) {
      List<T> list = new ArrayList<T>();

      for( T[] array : arrays )
          list.addAll( Arrays.asList( array ) );

      return list.toArray( (T[]) Array.newInstance( arrays[0][0].getClass(), list.size() ) );
  }

This code here will merge any number of generic arrays into one single array. The last line gives an unchecked cast warning, but can be ignored because we already assumed that the arrays are of same type.