indiWiz.com

Subhash's Tech Log

Archive for the ‘Java’ Category

Oracle JDK 7 Available for Mac

without comments

Written by Subhash Chandran

April 29th, 2012 at 8:41 pm

Posted in Java,Mac

IntelliJ IDEA OpenSourced :-)

without comments

IntelliJ IDEA 9.0 will be OpenSourced under Apache 1.0 License. There will be a fully-loaded commercial edition also available. Check out the feature differences between the Community Edition and Ultimate Edition.

Written by Subhash Chandran

October 19th, 2009 at 2:27 pm

Posted in Java

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 build,Java

Tagged with , ,

Building Alfresco AMPs Using Maven

without comments

[2009-12-03] Updated for new Maven repository.

Thanks to Ganesh Gembali and Velrajan for helping me with this post.

One thing I hate about Alfresco is it’s SDK. I hate to learn new methodologies and configurations specific to new software. When it comes to developing Alfresco modules and extensions, it was difficult to do it without configuring Alfresco SDK. But that is history ;-)

Alfresco Maven Archetypes

I will discuss the steps I followed to configure a new Alfresco module project using Maven Alfresco AMP Archetype.

Step 1: Configuring Alfresco Repositories

Add the following repositories to your Maven configuration:

<repositories>
    <repository>
        <id>alf-public</id>
        <url>http://maven.alfresco.com/nexus/content/repositories/releases</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>alf-public-plugin</id>
        <url>http://maven.alfresco.com/nexus/content/repositories/releases</url>
    </pluginRepository>
</pluginRepositories>

Step 2: Using archetype:generate to generate a new project structure

mvn archetype:generate \
  -DarchetypeCatalog=http://maven.alfresco.com/nexus/content/repositories/releases/alfresco-archetype-catalog.xml


Actually the above code did not work in our proxy environment (I have configured Maven for Proxy usage correctly). So I saved the alfresco-archetype-catalog.xml to local filesystem and referenced it thus:

mvn archetype:generate \
  -DarchetypeCatalog=file:///home/subhash/alfresco-archetype-catalog.xml


The application quizzes you about artifactId, groupId, package name and version. Give the details. It will create the project directory.

Step 3: Placement of files

The following table will help you understand where to place your source files. The third column (AMP path) describes the path where the source file will be packaged inside the AMP. The fourth column (Deployment Path) the path where the file will be installed inside Alfresco deployment.

Source Path Description AMP path Deployment Path
src/main/config Place your module configuration files (module-context.xml) here. config/alfresco/module/{groupId}.{artifactId}/ WEB-INF/classes/alfresco/module/{groupId}.{artifactId}/
src/main/java Place your Java source code lib/{artifactId}-{version}.jar WEB-INF/lib/{artifactId}-{version}.jar
src/main/resources All other files like JavaScript, FTL templates, WebScript XMLs, etc. config/ WEB-INF/classes/
module.properties module.properties is stored in the root directory. module.properties WEB-INF/classes/alfresco/module/{artifactId}/module.properties

Step 4: Configuring Alfresco Dependencies

As an example, our module depended on alfresco-webscript-framework and alfresco-core APIs:

<dependency>
        <groupId>org.alfresco</groupId>
        <artifactId>alfresco-webscript-framework</artifactId>
        <version>3.2r</version>
        <classifier>community</classifier>
        <scope>provided</scope>
</dependency>
<dependency>
        <groupId>org.alfresco</groupId>
        <artifactId>alfresco-core</artifactId>
        <version>3.2r</version>
        <classifier>community</classifier>
        <scope>provided</scope>
</dependency>

Two things to note:

  • The <classifier> element is needed. The Jar files in the repository is named as {artifactId}-{version}-{classifier}.jar
  • Always give the scope as provided. This ensures Alfresco Jars do not get bundled with your AMP.

Written by Subhash Chandran

August 25th, 2009 at 9:29 am

Posted in Java

Tagged with

Terracotta Announces Purchase of Ehcache

without comments

What happens when two respectable enterprise Java scalability providers (Ehcache and Terracotta) join together? I am eager to witness the interesting time ahead! Get the news here.

Written by Subhash Chandran

August 19th, 2009 at 7:46 pm

Posted in Java,news

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 build,Java

Tagged with , ,

Netbeans 6.7 RC 2

without comments

I have been test-driving Netbeans 6.7 RC 2 for two days now. Has been pretty stable. It is also the first version with native Maven support.

NetBeansIDE 6.7 RC2

Written by Subhash Chandran

June 10th, 2009 at 5:56 pm

Posted in Java,news

Tagged with , ,

“Hello World” (GUI) example in JavaFX

without comments

package tutorial;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

def WIDTH = 160;
def HEIGHT = 80;
def MESSAGE = "Hello World!";

Stage {
    title: MESSAGE
    width: WIDTH
    height: HEIGHT
    scene: Scene {
        fill: Color.BLACK
        content:
            Text {
                font: Font.font("Serif", FontWeight.BOLD, 16)
                fill: Color.WHITE
                x: 10
                y: 30
                content: MESSAGE
            }
    }
}

Written by Subhash Chandran

June 8th, 2009 at 8:39 am

Posted in Java

Tagged with

Command line “Hello World!” in JavaFX

without comments

Download JavaFX SDK. Install it, and have its bin folder in your PATH.

Open your favorite text editor, and write:

println("Hello World!");
java.lang.System.exit(0);

Save the file as A.fx. To compile the code:

$ javafxc A.fx

This will generate the class file A.class. To run the application:

$ javafx A

Isn’t it simple?

Written by Subhash Chandran

June 4th, 2009 at 6:33 am

Posted in Java

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 , ,