Archive for the 'Java' Category

Core Java: Volume I, Fundamentals

With its many code samples and short explanations, this is both an excellent resource for learning and a great reference book.

From Java Platform Improvements to Better Teaching: A Conversation With Java Champion Cay Horstmann

Java Champion Cay Horstmann, a computer science professor and author of noted books on Java programming, discusses needed platform improvements, developer challenges, and ways to inspire students.

Ending the Spam: SDN Newsletters Migrate to RSS

SDN has moved to a blog/RSS model for developer communications. Find out why.

Getting to Know System Tray

Learn how to add applications to the Microsoft Windows taskbar, the Gnome notification area, or KDE's system tray.

Ask the Experts Transcript: NetBeans IDE 6.0

Is there an obfuscator function in NetBeans? How can I run/invoke JavaFX code from a NetBeans module? What is the current status of PHP support in NetBeans? Get answers to these and a wide variety of other questions about NetBeans in this transcript.

Installing, Configuring, and Deploying Sun Java System Access Manager the Simple Way

Sun Java System Access Manager 7.1 integrates authentication and authorization services, policy agents, identity management, and identity federation for protecting network resources.

Video Interview With Roman Strobl

In this video interview, Ed Ort chats with NetBeans evangelist Roman Strobl at Sun Tech Days in Milan. Roman highlights what he considers to be the most significant new features in NetBeans IDE 6.0.

Becoming a Better Programmer: A Conversation With Java Champion Heinz Kabutz

Dr. Heinz Kabutz, Java Champion and creator of the Java Specialists' Newsletter, discusses the importance of design patterns and unit testing, the 10 laws of Java concurrency, and life as a developer on the island of Crete.

Sun and MySQL: How It Stacks Up for Developers

MySQL, the world's most popular open-source database, fills an important niche in Sun's software stack. With Sun's reach and resources, MySQL is poised for even wider adoption.

Tech Tip: Client-Side Polling With Dynamic Faces

See how the Dynamic Faces framework brings the power of Ajax to a real-time, stock query application that does client-side polling.

LINQ for Java

Would you like to see LINQ (Language INtegrated Queries) implemented for Java? This is an invitation to vote for it and to contribute.

Java: Beautiful enums

Whatever complaints you may have against java, I think you have to grant that at least enums are a thing of beauty.

Updates on Modularity in the Java platform

java.net Weblogs (37 reads)

There have been lots of exciting development and changes going on in the modularity areas recently.

Getting File System Details in Java

Javalobby - The heart of the Java developer community (33 reads)

Due to a number of differences between various platforms it is very difficult to present system specific information in a consistent manner. When getting closer to system specific details, like file system information, a Java programmer has to become aware of the operating system hosting his program in order to make sense of the information returned by some of the Java APIs.

Java Architecture for XML Binding - JAXB2 kick-off tutorial

Java Hobby (36 reads)

Java Architecture for XML Binding (JAXB) allows Java developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects. JAXB2 is a very good reference implementation of the specification.

There are 2 main approaches to work with XML binding:

a)start with a XSD schema definition as input and generate a collection of Java classes from the given XML schema through the schema compiler.
b)start with writing the Java model classes, mark the classes with specific JAXB 2.0 annotations and compile the model classes using JAXB2 in the classpath.

Here I will demonstrate the last approach as I find much easy for myself to think in "java classes" than to write a XSD schema definition file. The scope is to write the XML binding code for the following document:

<?xml version="1.0" encoding="utf-8"?>
<documentburster>
<settings>
<defaultfilename>defaultBurst</defaultfilename>
<emailserver>
<host>your_email_server</host>
<port>your_email_port</port>
<userid>your_email_user_id</userid>
<userpassword>your_email_password</userpassword>
<usessl>false</usessl>
<usetls>false</usetls>
<debug>false</debug>
<keyfile />
<rootcertfile />
<servercertfile />
<emailaddressfrom>your_email_address</emailaddressfrom>
<emailaddressfromlabel>your_name</emailaddressfromlabel>
</emailserver>
<defaultemailmessage>
<emailmessage>
Default Message
</emailmessage>
<emailsubject>
Default Subject
</emailsubject>
</defaultemailmessage>
</settings>
</documentburster>

All the Java mapping classes are in package burster.settings.model;
First step is to write the "documentburster" root class. Here is the code:

package burster.settings.model;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="documentburster")
public class DocumentBursterSettings {

@XmlElement(name = "settings")
public BursterSettings settings;

}

I think the code is self explanatory with @XmlRootElement(name="documentburster") the most important thing to notice. <settings> is a child for the root so the @XmlElement(name = "settings") annotation is used. The code for the class BursterSettings is:

package burster.settings.model;

import javax.xml.bind.annotation.XmlElement;

public class BursterSettings {

@XmlElement(name = "defaultfilename")
public String defaultFileName;

@XmlElement(name = "emailserver")
public EmailServer emailServer;

@XmlElement(name = "defaultemailmessage")
public DefaultEmailMessage defaultEmailMessage;

}

EmailServer and DefaultEmailMessage are classic plain java bean classes with getters and setters and without any annotation inside. Because of space issues I will show only the code for DefaultEmailMessage.java

package burster.settings.model;

public class DefaultEmailMessage {

private String emailsubject;
private String emailmessage;

public String getEmailmessage() {
return emailmessage;
}

public void setEmailmessage(String emailmessage) {
this.emailmessage = emailmessage;
}

public String getEmailsubject() {
return emailsubject;
}

public void setEmailsubject(String emailsubject) {
this.emailsubject = emailsubject;
}
}

The code for EmailServer class is very simple as it is a plain java bean class with getters and setters.

But where is the Unmarshaller code? Where is the code that actually reads/write into the XML file? The final piece of the code is the class Settings which is a "facade" for all my model XML binding classes. From my application I access all the XML binding through the Settings class and not through each model class individually . Here is the code for Settings.java:

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import burster.settings.model.BursterSettings;
import burster.settings.model.DefaultEmailMessage;
import burster.settings.model.DocumentBursterSettings;
import burster.settings.model.EmailServer;

public class Settings {
private static Settings instance = new Settings();

private DocumentBursterSettings docSettings;

private Settings(String xmlSettingsFile) {
loadSettings( xmlSettingsFile );
}

public static Settings getInstance() {
return instance;
}

public void loadSettings(String xmlSettingsFile )
{

try {

JAXBContext jc = JAXBContext.newInstance(DocumentBursterSettings.class);
Unmarshaller u = jc.createUnmarshaller();

docSettings = (DocumentBursterSettings)u.unmarshal(new File( xmlSettingsFile ));
} catch(Exception e){
System.out.println("Settings.loadSettings: " + e);
}
}

private BursterSettings getSettings() {
return docSettings.settings;
}

public EmailServer getEmailServer() {
return getSettings().emailServer;
}

public DefaultEmailMessage getDefaultEmailMessage() {
return getSettings().defaultEmailMessage;
}

public String getDefaultFileName() {
return getSettings().defaultFileName;
}

}

In this post I experimented only with a small part of what JAXB2 is capable. By the way JAXB2 is included with Java SE6 but you can use it in earlier versions of Java if you download it from here.

I would appreciate a comment if you feel you have your own noteworthy xml binding tips.

Further resources:
1)DocumentBurster project
2)Another Good JAXB2 tutorial
3)XML Data Binding Resources

The Power of the JVM

In the past couple days, a new project release was announced that has shown once again the potential of the Java platform. Shown how the awesome JVM has not yet begun to flex its muscles and really hit its stride in this project's domain. Made clear that even projects with serious issues can correct them, harnessing much more of the JVM with only a modest amount of rework. And demonstrated there's a lot more around the corner.

Spring Web Services 1.5.1 Released

Springframework.org (39 reads)

Dear Spring community,

I'm pleased to announce that Spring Web Services 1.5.1 has been released!

Downloads | Site | Changelog | Announcement

Even SpringSource Caught By OSS Licensing Hell?

Guru Meditation (39 reads)

TheServerSide posted a story about the new SpringSource Application Platform (S2AP) that generated a high number of comments. Unlike the Spring Framework, which uses the Apache License v2.0, the new S2AP will be dual licensed under the GPL v3 and a commercial license. Rod Johnson in a comment said of course, we took legal advice about our [...]

Hadoop Vs GridGain

Comparing GridGain to Hadoop.

Asynchronous method calls - II

Java Tips Blog (34 reads)

Do read the first part of this post. In this post, I will prenset another example of asynchronous method calls in EJBeans.

Next Page »