THE PLM STATE

Agile SDK and Inversion of Control

The SpringFramework can make your Agile PLM development go quickerLooking for a better way to handle configuration for your Agile PLM development projects? Make your way over to SpringSource and read up on inversion of control (IoC) or dependency injection. I was first introduced to the Spring Framework back in 2006 and have always been quite fond of it. It is a sophisticated tool to have in your arsenal and can really speed up development time by allowing you to focus on the business rules you need to implement rather than the plumbing that orchestrates your application.

This blog will only scratch the surface but should provide enough insight as to what dependency injection is and the basics of getting started. The example I will be walking through is the creation of an Agile Session with NO CODE!!! That's right, my session will be created by configuration alone and available to me when I ask for it. Let's get started.


The Code

At first I was simply going to create a new template based project in the SpringSource Tool Suite, but decided to go old school so we stayed focused on the concepts. Here is the listing to my only class:

{codecitation style="brush: Java;auto-links: false"}
package zws;

import com.agile.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.*;

public final class AgileSessionBlog {

private static final Logger log = LoggerFactory.getLogger(AgileSessionBlog.class);

public static void main(final String... args) throws Exception {
final AbstractApplicationContext context = new ClassPathXmlApplicationContext("classpath:config.xml");

IAgileSession agileSession = (IAgileSession) context.getBean("agileSession");

if (agileSession.isOpen()) {
log.info( "The session is open..." );
agileSession.close();
} else {
log.info( "The session is closed..." );
}
}

}
{/codecitation}

As you can see, it is pretty straight forward. There are only two notable lines of code in this little application (lines 13 and 15). The first is our call to load the config.xml by way of an ApplicationContext. This opens the config.xml file and automatically instantiates Java beans and "autowires" them together. This means that based on the configuration one dynamically created Java object can be used in the creation of another.

The second notable line contains the call to context.getBean(). Notice it doesn't say create bean. Why? Because our session has already been created, we just need to go get it! Pretty awesome stuff.

I chose to illustrate the power of Spring using the Agile session object because it is a little more complicated to get an object instantiated from a Factory class than just a plain old Java object (POJO). Now let's review the config.xml and see how this is all defined.

The Configuration

 

{codecitation style="brush: xml;auto-links: false"}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans

">


Agile PLM Configuration
================================================== -->

key-type="Java.lang.Integer" value-type="Java.lang.String">

factory-method="getInstanceEx">

factory-bean="agileSessionFactory" factory-method="createSession">

</beans>
{/codecitation}

 

Starting in the Agile PLM Configuration section lets look at each block, starting with the one on line 15. This block will create a HashMap Java object, as specified by the map-class attribute with the values of the entries enclosed within the block.

Next let's move on to the block on line 22 (id="agileSessionFactory"). Here we are going to get an instance of the AgileFactory. Because it is a singleton, we will need to call getInstanceEx by way of the factory-method attribute, providing the appropriate parameters. Normally I would not make the call from Java, but through the framework, it doesn't cost me anything.

With our factory class in hand, we can now create our session object (line 27). To do so we simply need to call the createSession method on agileSessionFactory object that was just created. If you are familiar with the SDK calls to createSession, you know that you need to pass in the parameters. With Spring, all we need to do is point to our agileSessionMap as a reference for the required argument.

With that all three objects are available to me whenever I need them. Anywhere in my application that the context object is available, I can simply call context.getBean("agileSessionMap") to get my connection parameters or make similar calls for the other JavaBeans.

This is equivalent to the following code:

 

{codecitation style="brush: Java;auto-links: false"}
HashMap params = new HashMap();
params.put(AgileSessionFactory.USERNAME, "rmccabe");
params.put(AgileSessionFactory.PASSWORD, "agile");
params.put(AgileSessionFactory.URL, "http://dev-agile9311c:7001/Agile");

// no I would not normally get a session like this ... normally I would do
// IAgileSession session = AgileSessionFactory.createSessionEx(params);

AgileSessionFactory agileSessionFactory = agileSessionFactory.getInstanceEx(params);
IAgileSession session = agileSessionFactory.createSession(params);
{/codecitation}

 

Summary

So what did this buy us ... on the surface, nothing. If you understand what the Spring container can do for you though and look at the other packages available, you can build very sophisticated integration solutions with less effort. That can be both exciting and rewarding.

Subscribe to the ZWS Blog

Recent Posts