fridgebuzz

6.12.2006

Configuring a JXTA Peer in Spring

The new net.jxta.platform.NetworkConfigurator class provides a clean and straightforward way to configure an instance of the JXTA platform without a UI. It can be used programmatically (see the main() method for an example of stand-alone use) or—because it is a Java Bean—declaratively deployed inside containers like Spring.

Read the full post for a brief JXTA/Spring tutorial.

Categories: ,

Here is a simple example of a Spring context file, applicationContext-rdv.xml, which configures a Rendezvous Server:


    <bean id="netConfig" class="net.jxta.platform.NetworkConfigurator">
        <!-- 
            Location of the home directory 
        -->
        <property name="home"><value>.jxta</value></property>
        <!-- 
             Modes determine node properties. 2572 corresponds to a
             Rendezvous Server node. See NetworkConfigurator javadocs
             for details.
         -->
        <property name="mode"><value>2752</value></property>
        <!-- 
             This is a managed RDV, so the peer ID is hard-coded in order to 
             maintain synch with rendezvousACL.xml
         -->
        <property name="peerId">
        <value>urn:jxta:uuid-XXXXXXXXXXXXXXXXXXXXXXXXXX...</value></property>
        <!-- 
            Details of private Net Peer Group 
        -->
        <property name="infrastructureID">
            <value>uuid-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</value>
        </property>
        <property name="infrastructureName"><value>MyNetPG</value></property>
        <property name="infrastructureDescription">
            <value>My Infrastructure Group</value><
        /property>
        <!--
            Some basic information about the Node
        -->
        <property name="name"><value>com.mycompany RDV1</value></property>
        <property name="description"><value>Rendezvous Node</value></property>
        <property name="principal"><value>user</value></property>
        <property name="password"><value>pwd</value></property>
        <!--
            Because it's a managed peer, we may want control over the exact
            ports being used.
        -->
        <property name="httpPort"><value>9700</value></property>
        <property name="tcpPort"><value>9701</value></property>
    </bean>

    <bean id="myJxtaBean" class="com.mycompany.MyJxtaBean">
        <property name="netConfig"><ref bean="netConfig"/></property>
    </bean
And here's what MyJxtaBean.java might look like, in part:

public class MyJxtaBean {

    private NetworkConfigurator netConfig;
    
    private PeerGroup infrastructureGroup;
    
    public MyJxtaBean() {}
    
    public setNetConfig(NetworkConfigurator netConfig) {
        this.netConfig = netConfig;
    }
    
    public synchronized void start() {

        /*
        * Simplest case, we don't overwrite an existing configuration, nor
        * do we load and alter/ammend it. Only if there is no PlatformConfig
        * available, construct a new one using the bean's properties.
        */
        if (!netConfig.exists()) {
            try {
                netConfig.save();
            } catch (IOException io) {
                System.out.println("Error saving PlatformConfig");
            }
        }
          
        try {
            infrastructureGroup = PeerGroupFactory.newNetPeerGroup();
        } catch (PeerGroupException pge) {
            System.out.println("Couldn't create NetPeerGroup");
            System.exit(1);
        }
    }
}
Now all your applicaton has to do is get the MyJxtaBean from the context and call start() to start the JXTA platform.

    applicationContext = 
        new ClassPathXmlApplicationContext("applicationContext-rdv.xml");
    myPlatform = (MyJxtaBean)applicationContext.getBean("myJxtaBean");
    myPlatform.start();
There's more to writing a JXTA application, of course, but I hope this example demonstrates how NetworkConfigurator takes some of the mystery out of JXTA plaform configuration, while making it easy to combine JXTA and Spring.

4 Comments:

  • Thanks for posting this. I believe this is a JXTA <> Spring first.

    Can you say a bit about why you are using Spring in this overall context? Spring neophytes may learn something from your choices.

    Mark

    By Mark Petrovic, at 5:39 PM  

  • There is a problem in that if you set the home property in Spring (2.x) and that directory does not exist, Spring will error when loading the configuration. This is not a direct problem with JXTA, but one that could bite you the first time you run a JXTA application configured in this manner.

    I am looking at way to work around this and I'll let you know if there is one.

    By Christopher, at 9:15 PM  

  • Vanessa, the solution to the Spring/JXTA issue can be found here.

    By Christopher, at 2:55 AM  

  • Thanks for the bug report and patch, Christopher!

    By vanessa, at 8:21 AM  

Post a Comment

<< Home