Archive for the ‘Java’ tag
Guava Libraries from Google
Discovered from a blog post Apache Commons-like Java library from Google: Guava Libraries. Check out their presentation which briefs the functionality.
New releases in Java space
There have been some prominent releases in the past few weeks:
- Apache POI 3.5-FINAL: For reading/writing Microsoft Office format files from Java.
- Jetty 7.0: A servlet container akin to Tomcat. Easily embeddable and ideal for use in testing.
- XOM 1.2.3: New version of the XML parser written by Elliotte Rusty Harold.
Reduce Java boiler-plate code using Lombok
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).
XSL Transform using Java
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.
Maven Profiles
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:
- In the project POM itself.
- 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>
Netbeans, Eclipse new releases
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.
Maven managed projects: Generating dependency graph
When using Maven from commandline, use this command:
If you are using Netbeans 6.7, you can generate a much more visual 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.
JavaFX 1.2 for Linux, Mac and Solaris
JavaFX is now available for Linux, Solaris and Mac. Finally, now I can try this technology (I use Linux at work and home)!
Java Multi-touch API
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!
JDK 6 Update 14 Released
The important feature update in this release is the inclusion of Garbage-First Garbage Collector (aka G1). Download JDK 6 update 14.
JUnit 4.6 Released
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>
Standardized DI Annotations for Java
At last, at last, some action is going on in this direction!
Clojure reaches 1.0
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.
James Gosling writes about Netbeans Kenai.com support
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.
JMX Code Snippet
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);

