indiWiz.com

Subhash's Tech Log

Archive for the ‘Java’ tag

Guava Libraries from Google

without comments

Discovered from a blog post Apache Commons-like Java library from Google: Guava Libraries. Check out their presentation which briefs the functionality.

Written by Subhash Chandran

October 15th, 2009 at 2:05 pm

Posted in Java

Tagged with ,

New releases in Java space

without comments

There have been some prominent releases in the past few weeks:

Written by Subhash Chandran

October 13th, 2009 at 1:08 pm

Posted in Java, news

Tagged with ,

Reduce Java boiler-plate code using Lombok

without comments

We all hate the verbocity of Java. We all hate getters and setters and toString and hashCode and equals. We also hate the lack of closure support for proper resource handling. Now you can still love your favorite language without doing these manual chores!

There is a fine OpenSource project, oddly named as Project Lombok which fixes the boiler plate code writing (or generation) using annotations. Beautifully designed, this is some thing which could save you lot of time. Checkout their video (available in the home page).

Written by Subhash Chandran

October 8th, 2009 at 12:55 pm

Posted in Java

Tagged with ,

XSL Transform using Java

without comments

import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

...

// The XSL source:
StreamSource xsl = new StreamSource(...);

// Create the transformer:
TransformerFactory factory = TransformerFactory.newInstance();
Templates template = factory.newTemplates(xsl); // can throw TransformerConfigurationException (parent exception is: TransformerException)
Transformer transformer = template.newTransformer(); // can throw TransformerConfigurationException

// The data XML source
StreamSource data = new StreamSource(...);

// The transformed output:
StreamResult out = new StreamResult(...);

// Transform using the Transformer instance:
transformer.transform(data, out); // can throw TransformerException

Have a look at the StreamSource API and StreamResult API to understand the various ways by which it can be instantiated.

Written by Subhash Chandran

September 14th, 2009 at 4:56 pm

Posted in Java

Tagged with , , , ,

Maven Profiles

without comments

Thanks to Arun Jeganath for making me understand Maven Profiles.

Often we are forced to specify different build parameters in different environments. In the ages of Make, these parameters used to be specified as environment variables. In the world of Maven, we have profiles.

Profiles: Introduction

A sample profile information:

  <profiles>

    <!-- DEV Profile (Default) -->
    <profile>
      <id>dev-profile</id>
      <activation>
        <property>
          <name>env</name>
          <value>dev</value>
        </property>
        <activeByDefault>true</activeByDefault> <!-- Note this -->
      </activation>
      <!-- Liferay Home in DEV -->
      <properties>
        <liferay-home>${user.home}/apps/liferay-portal-tomcat-6.0-5.1.2</liferay-home>
      </properties>
    </profile>

    <!-- TEST Profile -->
    <profile>
      <id>test-profile</id>
      <activation>
        <property>
          <name>env</name>
          <value>test</value>
        </property>
      </activation>
      <!-- Liferay Home in TEST -->
      <properties>
        <liferay-home>/opt/apps/liferay-portal-tomcat-6.0-5.1.2</liferay-home>
      </properties>
    </profile>
  </profiles>

Quick learning from the above example:

  • There can be number of profiles defined.
  • A profile can be marked as <activeByDefault>. This will enforce the configuration defined in the activated profile to take effect during build.
  • To enable a specific profile during build, the value defined in XPath /profiles/profile/activation/property needs to be passed the Maven as System Property (in our case: -Denv=test).
  • The property defined in the profile (liferay-home) can be referenced elsewhere in the pom.xml as ${liferay-home}.

Placing profile information

Profile information is generally placed in:

  1. In the project POM itself.
  2. Inside {user.home}/.m2/settings.xml.

In the first case (in this particular example), the deployment information is placed in the project’s POM. This, some might feel improper because the module/project build has hardcoded environment dependencies which might not work in other systems. The second option externalizes the platform specific build information. But a new developer checking out the project will not be aware of this external dependency causing his build to fail. Both these problems might be addressed using documentation: some projects have README and BUILD files explaining environment setup for build.

As we saw, both methods have advantages and disadvantages. Choose the method appropriate to your situation.

Parameters that can be overridden in profiles

Note: This list is shamelessly copied from the official Maven profiles guide. Refer to this guide for detailed information.

  • <repositories>
  • <pluginRepositories>
  • <dependencies>
  • <plugins>
  • <properties> (not actually available in the main POM, but used behind the scenes)
  • <modules>
  • <reporting>
  • <dependencyManagement>
  • <distributionManagement>
  • a subset of the <build> element, which consists of:
    • <defaultGoal>
    • <resources>
    • <testResources>
    • <finalName>

Written by Subhash Chandran

September 1st, 2009 at 3:46 pm

Posted in Java, build

Tagged with , ,

Netbeans, Eclipse new releases

without comments

Eclipse Foundation released Eclipse Galileo on 24th June. Netbeans released version 6.7 on 29th June. Nice to see healthy competition from both sides :-) . Download the latest versions of Netbeans and Eclipse.

Written by Subhash Chandran

June 30th, 2009 at 5:11 am

Posted in Java, news

Tagged with , ,

Maven managed projects: Generating dependency graph

without comments

When using Maven from commandline, use this command:

Maven Dependency Graph

If you are using Netbeans 6.7, you can generate a much more visual graph:

Netbeans Maven Dependency Graph

For bringing up this graph, just right click on the project and select Show Dependency Graph.

The screenshots above show the dependency graph for my project WizTools.org RESTClient.

Written by Subhash Chandran

June 22nd, 2009 at 1:38 am

Posted in Java, build

Tagged with , ,

JavaFX 1.2 for Linux, Mac and Solaris

without comments

JavaFX is now available for Linux, Solaris and Mac. Finally, now I can try this technology (I use Linux at work and home)!

Written by Subhash Chandran

June 4th, 2009 at 5:11 am

Posted in Java, news

Tagged with , ,

Java Multi-touch API

without comments

Multi-touch is one of the areas where there is no standard in Java. I believe some of the vendors might have developed custom multi-touch APIs for Java. But wouldn’t we, as developers, prefer some standard API which would work across multiple vendors? Richard Monson-Haefel, an eccentric genius whose work spans Java and multi-touch world, has taken the initiative to call for participation in this standardization process. Interested people, please join him in creating history!

Written by Subhash Chandran

June 1st, 2009 at 10:51 am

Posted in Java, news

Tagged with , , ,

JDK 6 Update 14 Released

without comments

The important feature update in this release is the inclusion of Garbage-First Garbage Collector (aka G1). Download JDK 6 update 14.

Written by Subhash Chandran

June 1st, 2009 at 5:58 am

Posted in Java, news

Tagged with ,

JUnit 4.6 Released

without comments

Check the release notes and download.For those using Maven, here is the dependency:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.6</version>
</dependency>

Written by Subhash Chandran

May 11th, 2009 at 11:43 am

Posted in Java

Tagged with , ,

Standardized DI Annotations for Java

with one comment

At last, at last, some action is going on in this direction!

Written by Subhash Chandran

May 6th, 2009 at 10:11 am

Posted in news

Tagged with ,

Clojure reaches 1.0

without comments

Clojure 1.0 released. About Clojure from the Clojure website:

Clojure is a dialect of Lisp, and shares with Lisp the code-as-data philosophy and a powerful macro system. Clojure is predominantly a functional programming language, and features a rich set of immutable, persistent data structures. When mutable state is needed, Clojure offers a software transactional memory system and reactive Agent system that ensure clean, correct, multithreaded designs.

Written by Subhash Chandran

May 6th, 2009 at 9:12 am

Posted in news

Tagged with , ,

James Gosling writes about Netbeans Kenai.com support

without comments

The post is interesting. Kenai.com as a cloud based project hosting infrastructure is interesting. But …

Given Oracle’s acquisition of SUN, I am quite unsure if Oracle would continue investing in a Kenai.com. Or, for that matter, in Netbeans (I am a Netbeans addict, and pray that they do it).

So what have I done? I have created an account in Kenai.com. But, for the moment I am not going to host any of my OpenSource initiatives in Kenai.com. Wait and see is the only thing I can do now.

Written by Subhash Chandran

April 30th, 2009 at 9:56 am

Posted in Innovation, Java

Tagged with , ,

JMX Code Snippet

without comments

This snippet uses MX4J.

        String urlAddr = "service:jmx:rmi://" + host;
        logger.info("Address: "+urlAddr);

        JMXServiceURL address = new JMXServiceURL(urlAddr);
        Map environment = null;
        MBeanServer server = MBeanServerFactory.createMBeanServer();
        JMXConnectorServer cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(address, environment, server);
        cntorServer.start();

        // HTTP Adapter

        HttpAdaptor adapter = new HttpAdaptor();
        ObjectName name = new ObjectName("Server:name=HttpAdaptor");
        server.registerMBean(adapter, name);
        logger.info("jmx-port: "+ MBeanConfig.ADAPTOR_PORT);
        logger.info("jmx-host: "+ MBeanConfig.ADAPTOR_HOST);
        adapter.setPort( MBeanConfig.ADAPTOR_PORT );
        adapter.setHost( MBeanConfig.ADAPTOR_HOST );
        adapter.setProcessor( new XSLTProcessor());
        adapter.start();

        // Register MBean

        ObjectName objectName = new ObjectName("org.wiztools.project.mbean:type=WizTools");
        logger.info("ObjectName: " + objectName);
        WizToolsMonitorMBean mb = new WizToolsMonitor();
        server.registerMBean(mb, objectName);

Written by Subhash Chandran

April 25th, 2009 at 10:17 am

Posted in Java

Tagged with ,