Friday, August 29, 2008

Load jar files and Java classes dynamically

In this post I present a way to load jar files and Java classes dynamically from java code. It can be very useful, if you want to create module, for example.

Design Pattern
The solution build up from three part, the
Loader - loads and uses the jar files and Java class dynamically,
Communication interface(s) - defines the interface, what can be used to communicate the program and the dynamic library,
Dynamic libraries - the Loader loads and uses this libraries through the communication interface(s).

The loader, the communication interface(s) and the dynamic libraries must be separated in different projects, because
  • Loader imports the communication interface(s)
  • Dynamic libraries imports the communication interface(s)
  • Loader can't know anything about the Dynamic libraries
  • Dynamic libraries can't know anything about the Loader



Sample application
As usually, I made a sample application, what can help to you to understand my idea. The sample application is very similar now. The main program can be invocated with two parameter, the first is the name of the jar file to load, the second is the name of the class to load. The application loads the given jar file and class and say hello in two different language, depend on, which jar file is loaded.

Creating the communication interface(s)
There is only one communication interface, what defines only one simple method:



Creating a dynamic library
The sample dynamic library is as simple as the communication interface:


Creating the Loader
Sample application has a main class, what processes the program arguments:


and a class, where the codes are separated, that load the jar file and class dynamically:


Downloads
You can download sample projects here as NetBeans project.

Saturday, August 2, 2008

Spring Framework - XML configuration file - Part I

In this post the Spring framework's XML configuration file is presented. This files are the heart of Spring framework. Understand using these files is very important to understand Spring framework itself.


Part I - This part of post is about the XML scheme, the root element, the tags inside the root element and the properties of the <bean/> tag.

XML Scheme
To use the XML file with Spring framework, we have to start our XML file like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
These lines mean our XML file version is 1.0 and the encoding of the file is UTF-8. It's recommended keep these settings.

The next two lines is a DTD import. It can be used to check the validation of our XML file.

Root element
All XML file have to be a root element. If we create XML configuration file to Spring framework then the root element must be:
<beans>
...
</beans>
All bean definition, configuration code and everything have to be between the beans open and close tag.

The following tags are allowed inside the <beans/> element:
  • alias,
  • bean,
  • description,
  • import.

<import/> tag: Importing beans from other XML files
It can be very useful when we can separate the beans to several XML files based on the business logic. Other benefit is the support of the team work. Everybody can work with its own beans and program code and using import tag, the XML files can be logically merged.

Importing beans from other XML file:
<import resource="an_other_xml_file.xml" />


<alias/> tag: Aliasing beans
Using <alias/> tag we can give alias names to beans. This means we can give new reference-name to the bean:
<alias name="the_exist_name_of_bean" alias="the_new_name_of_bean" />
<description/> tag: Writing descriptions, comments
Using <description/> tag we can write descriptions, comments to our XML configuration file:
...
<description>
Some important description...
<description/>
...

<bean/> tag: Defining beans
A Spring IoC container manages one or more beans. These beans are created using the configuration metadata that has been supplied to the container
.
Within the container itself, these bean definitions are represented as BeanDefinition objects, which contain (among other information) the following metadata:
  • a package-qualified class name: typically this is the actual implementation class of the bean being defined,
  • bean behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth),
  • references to other beans which are needed for the bean to do its work; these references are also called collaborators or dependencies,
  • other configuration settings to set in the newly created object. An example would be the number of connections to use in a bean that manages a connection pool, or the size limit of the pool.
The <bean/> tag has the following attributes:
  • abstract
  • autowire
  • class
  • dependency-check
  • depends-on
  • destroy-method
  • factory-bean
  • factory-method
  • id
  • init-method
  • lazy-init
  • name
  • parent
  • singleton
<bean/> tag, abstract property
We can mark a bean explicit abstract. In this case we can define bean template without specifying its class. This abstract beans can use as parent of well-defined beans.

Example:
<bean id="beanId" class="pack.example.MyClass" abstract="true">
<property name="myProp" value="sample"/>
<bean/>

<bean/> tag, autowire attribute
The Spring container is able to autowire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for your bean by inspecting the contents of the BeanFactory. The autowiring functionality has five modes. Autowiring is specified per bean and can thus be enabled for some beans, while other beans will not be autowired. Using autowiring, it is possible to reduce or eliminate the need to specify properties or constructor arguments, thus saving a significant amount of typing. When using XML-based configuration metadata, the autowire mode for a bean definition is specified
by using the autowire attribute of the <bean/> element. The following values are allowed:

  • no - No autowiring at all. Bean references must be defined via a ref element. This is the default, and changing this is discouraged for larger deployments, since explicitly specifying collaborators gives greater control and clarity. To some extent, it is a form of documentation about the structure of a system.
  • byName - Autowiring by property name. This option will inspect the container and look for a bean named exactly the same as the property which needs to be autowired. For example, if you have a bean definition which is set to autowire by name, and it contains a master property (that is, it has a setMaster(..) method), Spring will look for a bean definition named master, and use it to set the property.
  • byType - Allows a property to be autowired if there is exactly one bean of the property type in the container. If there is more than one, a fatal exception is thrown, and this indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set. If this is not desirable, setting the dependency-check="objects" attribute value specifies that an error should be thrown in this case.
  • constructor - This is analogous to byType, but applies to constructor arguments. If there isn't exactly one bean of the constructor argument type in the container, a fatal error is raised.
  • autodetect - Chooses constructor or byType through introspection of the bean class. If a default constructor is found, the byType mode will be applied.
Example:
<bean id="beanId" class="pack.example.MyClass" autowire="no"/>


<bean/> tag, class attribute
Using class property we can specify the class of the bean. The class definition must be contain the package name and the class name
Example:
<bean id="beanId" class="pack.example.MyClass"/>

<bean/> tag, dependency-check attribute
The Spring IoC container also has the ability to check for the existence of unresolved dependencies of a bean deployed into the container. These are JavaBeans properties of the bean, which do not have actual values set for them in the bean definition, or alternately provided automatically by the autowiring feature. This feature is sometimes useful when you want to ensure that all properties (or all properties of a certain type) are set on a bean. Of course, in many cases a bean class will have default values for many properties, or some properties do not apply to all usage scenarios, so this feature is of limited use. Dependency checking can also be enabled and disabled per bean, just as with the autowiring functionality. The default is to not check dependencies. Dependency checking can be handled in several different modes. When using XML-based configuration metadata, this is specified via the 'dependency-check' attribute in a bean definition, which may have the following values:
  • none - No dependency checking. Properties of the bean which have no value specified for them are simply not set.
  • simple - Dependency checking is performed for primitive types and collections (everything except collaborators).
  • object - Dependency checking is performed for collaborators only.
  • all - Dependency checking is done for collaborators, primitive types and collections.
Example:
<bean id="beanId" class="pack.example.MyClass" dependency-check="none"/>

<bean/> tag, depends-on attribute
For most situations, the fact that a bean is a dependency of another is expressed by the fact that one bean is set as a property of another.

For the relatively infrequent situations where dependencies between beans are less direct (for
example, when a static initializer in a class needs to be triggered, such as database driver registration), the 'depends-on' attribute may be used to explicitly force one or more beans to be initialized before the bean using this element is initialized. Find below an example of using the 'depends-on' attribute to express a dependency on a single bean:
<bean id="myClass" class="pack.example.MyClass"/>
<bean id="myOtherClass" class="pack.example.MyOtherClass"/>
<bean id="beanId" class="pack.example.MyImportantClass" depends-on="myClass,myOtherClass"/>

<bean/> tag, destroy-method attribute
Implementing the org.springframework.beans.factory.DisposableBean interface allows a bean to get a callback when the container containing it is destroyed. The DisposableBean interface specifies a single method:
void destroy() throws Exception;

Other way to define destruction callback is the destroy-method attribute. We can set a method of the class, that is invoced before it is destroyed:
<bean id="myClass" class="pack.example.MyClass" destroy-method="cleanup"/>

In this case the cleanup() method is invoced before the bean is destroyed.

<bean/> tag, factory-bean, factory-method attributes
We can use factory method instead of constructor. We have two options to do that:
  • static factory method
  • instance factory method
Static factory method
When defining a bean which is to be created using a static factory method, along with the class attribute which specifies the class containing the static factory method, another attribute named factory-method is needed to specify the name of the factory method itself. Spring expects to be able to call this method (with an optional list of arguments as described later) and get back a live object, which from that point on is treated as if it had been created normally via a constructor. One use for such a bean definition is to call static factories in legacy code.

The following example shows a bean definition which specifies that the bean is to be created by calling a factory-method. Note that the definition does not specify the type (class) of the returned object, only the class containing the factory method. In this example, the createInstance() method must be a static method:
<bean id="exampleBean" class="examples.FactoryBean" factory-method="createInstance"/>

Instance factory method
Instantiation using an instance factory method is where a non-static method of an existing bean from the container is invoked to create a new bean. To use this mechanism, the 'class' attribute must be left empty, and the 'factory-bean' attribute must specify the name of a bean in the current (or parent/ancestor) container that contains the instance method that is to be invoked to
create the object. The name of the factory method itself must be set using the 'factory-method' attribute. The createInstance method is an instance method of examples.FactoryBean class here:
<bean id="factoryBean" class="examples.FactoryBean"/>
<bean id="exampleBean" factory-bean="factoryBean" factory-method="createInstance"/>
<bean/> tag, id, name attributes
Every bean has one or more ids (also called identifiers, or names; these terms refer to the same thing). These ids must be unique within the container the bean is hosted in. A bean will almost always have only one id, but if a bean has more than one id, the extra ones can essentially be considered aliases.

When using XML-based configuration metadata, you use the 'id' or 'name' attributes to specify the bean identifier(s). The 'id' attribute allows you to specify exactly one id, and as it is a real XML element ID attribute, the XML parser is able to do some extra validation when other elements reference the id; as such, it is the preferred way to specify a bean id. However, the XML specification does limit the characters which are legal in XML IDs. This is usually not a constraint, but if you have a need to use one of these special XML characters, or want to introduce other aliases to the bean, you may also or instead specify one or more bean ids,
separated by a comma (,), semicolon (;), or whitespace in the 'name' attribute.
Please note that you are not required to supply a name for a bean. If no name is supplied explicitly, the container will generate a unique name for that bean.

Example:
<bean id="exampleBean" class="examples.ExampleClass"/>

<bean/> tag, init-method attribute
Implementing the org.springframework.beans.factory.InitializingBean interface allows a bean to perform initialization work after all necessary properties on the bean have been set by the container. The InitializingBean interface specifies exactly one method:
void afterPropertiesSet() throws Exception;

Other way to define initialization callback is the init-method attribute. We can set a method of the class, that is invoced after all necessary properties on the bean have been set.
<bean id="myClass" class="pack.example.MyClass" init-method="initBean"/>

In this case the initBean() method is invoced after all necessary properties on the bean have been set.

<bean/> tag, lazy-init attribute
The default behavior for ApplicationContext implementations is to eagerly pre-instantiate all singleton beans at startup. Pre-instantiation means that an ApplicationContext will eagerly create and configure all of its singleton beans as part of its initialization process. Generally this is a good thing, because it means that any errors in the configuration or in the surrounding environment will be discovered immediately.

However, there are times when this behavior is not what is wanted. If we do not want a singleton bean to be pre-instantiated when using an ApplicationContext, you can selectively control this by marking a bean definition as lazy-initialized. A lazily-initialized bean indicates to the IoC container whether or not a bean instance should be created at startup or when it is first equested.

When configuring beans via XML, this lazy loading is controlled by the 'lazy-init' attribute on the <bean/> element; for example:
<bean id="myClass" class="pack.example.MyClass" lazy-init="true"/>

<bean/> tag, parent attribute
Use of parent attribute is based on inheritance.

A child bean definition will use the bean class from the parent definition if none is specified, but can also override it. In the latter case, the child bean class must be compatible with the parent, that is it must accept the parent's property values.

A child bean definition will inherit constructor argument values, property values and method overrides from the parent, with the option to add new values. If any init-method, destroy-method and/or static factory method settings are specified, they will override the corresponding parent settings.

Example:
<bean id="beanId" class="pack.example.MyClass" abstract="true">
<property name="myProp" value="sample"/>
<bean/>
<bean id="child" class="pack.example.MyClass" parent="beanId">

<bean/> tag, singleton attribute
If we mark a bean as singleton then if we get it through its id, always the same object is referred. The created beans are implicit singletons. If we want to get a new instance when we refer to the bean, we have to set explicit to no singleton:
<bean id="child" class="pack.example.MyClass" singleton="false">

Spring Framework - First Steps

This post teaches you some useful basic information and function of Spring framework. Writing XML configuration files, wiring beans, loading XMLs, using ApplicationContext. Creating, publishing and listening events are discussed in this post too.

TaskbarNotifier example application
The functions above are represented through an example application. This application has a GUI frame, where we can create animals, given the genus, the name and the color:


and when user click to the Create and notify button, then the created animal properties are showed in a tooltip message on taskbar:


Step 1: Creating the project
Create a new java application project with your favorite IDE and add the following files from Spring to the application classpath:
  • spring.jar
  • spring-core.jar
  • spring-context.jar
  • commons-logging.jar
  • log4j-1.2.xy.jar
Step 2: Creating the model
Now we create the model, the objects, on our application operates. We have a simple JavaBean, named Animal with three simple properties:
  • genus - genus of the animal
  • name - name of the animal
  • color - color of the animal
Our Animal JavaBean:


Step 3: Creating the service interfaces
Services are very important in Spring. They implement the business logic, operate with the application data. To create a service, first we create an interface with methods, that the service will implement (it's function will be discussed in another post).

Our example application has two service interfaces:
  • AnimalService - Creates Animal instance from its properties.
  • TaskbarService - Shows the taskbar icon, displays tooltip message on taskbar and closes the taskbar icon.
Our AnimalService interface:

Our TaskbarService interface:

Step 4: Implementing the service interfaces
Implementing the service interface is a simple interface implementation, its complexity depends on the complexity of the business logic.
Our application has two service interfsce implementations:
  • AnimalServiceImp
  • TaskbarServiceImp

AnimalServiceImp source code:


TaskbarServiceImp source code:

Step 5: Creating and publishing events
Events can be very useful when we want to inform other parts of our application, something interesting happened.

To create an event, we have to extend the org.springframework.context.ApplicationEvent abstract class and create a constructor, what call a super() with a parameter. This parameter is usually the object, what is behind to the published event.

If we have an event instance then we can publish it through an ApplicationContext instance, using the publishEvent() method, like this (where context is an ApplicationContext instance):
context.publishEvent(new TaskbarNotificationEvent(animal)); //This code segment is from AnimalServiceImp class

Working of events is represented by the following chart:


Our application have only one publishable event:
  • TaskbarNotificationEvent - This event is published when a new Animal instance created.
TaskbarNotificationEvent Source code:


Step 6 : Creating the Listener class
As I discussed above, ApplicationListeners are waiting for published events and they (of course we, programmers) decide, is the actual event important to this listener or not. If it is then the listener can run some event-specific code, otherwise it should do nothing (but can, if it wants).

To create an own listener, we need implement the org.springframework.context.ApplicationListener interface. This interface has a single method:
public void onApplicationEvent(ApplicationEvent event);


Through the event parameter we can access the actual published event. With the instanceof operator we can decide the type of the event.

The example application has only one Listener:
  • TaskbarNotifier.
TaskbarNotifier Source Code:


Step 7: Creating GUI
There's a simple thing, that I discuss in this step:
I write the windowClosing event to my Frame:
private void formWindowClosing(java.awt.event.WindowEvent evt) {
/**
* Close the application, using the AppController.exit() method.
* The defaultCloseOperation of form is set to EXIT_ON_CLOSE.
*/
AppController.exit();
}


I did it because i want to run some code before the application is closed (remove the taskbar icon for example).

Our application has only one frame:
  • MainFrame.
MainFrame Source Code:


Step 8: The XML configuration file
As I said, we can use XML configuration files to define beans, wire beans, set configurations. In a separated post I will write only about the XML configuration file, now see only the required information to create our sample application.

Our sample application's XML configuration file looks like this:



XML Scheme
To use the XML file with Spring framework, we have to start our XML file like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
These lines mean our XML file version is 1.0 and the encoding of the file is UTF-8. It's recommended keep these settings.

The next two lines is a DTD import. It can be used to check the validation of our XML file.

Root element
All XML file have to be a root element. If we create XML configuration file to Spring framework then the root element must be:
<beans>
...
</beans>
All bean definition, configuration code and everything have to be between the beans open and close tag.

Simple bean definition
The most easiest way to define a bean is the following:
<bean id="animalService" class="example.animal.AnimalServiceImp" />
This definition means:
  • create an instance of class example.animal.AnimalServiceImp
  • assign the animalService identification to the created instance
  • if we refering the created instance through the animalService identification, we always access to the same object instance (singleton) it can be changed, it will be discussed later
Wiring beans, dependency injection
We define a bean to access an example.taskbar.TaskbarServiceImp instance:
<bean id="taskbarService" class="example.taskbar.TaskbarServiceImp" />

And now, we define an other bean, what is a little more complex, because we set the taskbarService properties of the created bean:
<bean id="taskbarNotifier" class="example.taskbar.TaskbarNotifier">
<property name="taskbarService">
<ref bean="taskbarService" />
</property>
</bean>

This definition means:
  • create an instance of class example.taskbar.TaskbarNotifier
  • assign the taskbarNotifier identification to the created instance
  • if we refering the created instance through the taskbarNotifier identification, we always access to the same object instance (singleton) it can be changed, it will be discussed later
  • get the example.taskbar.TaskbarServiceImp instance with taskbarService identification and set the created bean's taskbarService property through its setter method to this taskbarService object
This way to creating and connecting beans can be little strange, but it is very very useful, there are so many thing, that we can't make without this solution.

Summary
If your read this post, you can
  • add the required files to use Spring framework
  • create service interfaces
  • implement service interfaces
  • create and publish events
  • listen and handle events
Using XML configuration files can be strange first, but I will create a post only about it. Understand using these files is very important to understand Spring framework itself.

Downloads
You can download example code here as NetBeans project.