This section provides you sample code that creates an entry in the MySQL Database for the ControlLogix PLC with which the iTapestry EJBs communicate.
In order to use our sample code, perform these steps.
Create a new Java package and a new Java Class under the Java project titled Starthis which you created in Section 5.3, “Setting-up Eclipse 3.0.2”. You can name your Java package and Java class according to your preference. This document names the Java class as ClgxCreator.java.
Copy-paste the following code fragment into ClgxCreator.java. If you chose a different name for your Java class, make sure that you edit the code fragment below to reflect the same.
/*
* Created on Oct 5, 2005
* Copyright Starthis Inc.
*/
package com.starthis.example;
import com.starthis.hardware.rockwell.ab.clgx.*;
import javax.ejb.*;
import com.starthis.enip.iec.types.*;
import com.starthis.enip.iec.types.segments.Port;
import java.util.Properties;
import javax.naming.*;
import javax.rmi.*;
import java.rmi.*;
/**
* EXAMPLE:
* Create a new Entry for a PLC in the database.
* Use the Admin session bean. It supports the creation of a
* PLC bean identified by its EtherNet/IP path (EPATH).
* Also ensure that all of the data tag names and their
* corresponding types are read from the PLC and stored in
* the database.
*
*/
public class ClgxCreator{
public static void main(String[] args){
/* Step 1: Initial Context */
System.setProperty(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
System.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
try
{
/* Step 2: Fetching the remote home */
ControlLogixAdminHome home = ControlLogixAdminUtil.getHome();
/* Step 3: Fetching the remote interface */
ControlLogixAdmin admin = home.create();
/* Step 4: Creating the EPATH */
EPATH path = new EPATH();
// Add a port to the path. This is the PLC's ethernet port. The IP
// address is that of the PLC.
path.add(new Port(0xAF12, "10.10.2.26"));
// Add a path thet represents a hop (from the ethernet port) to
// the PLC CPU Unit.
path.add(new Port(1, (byte)0));
// Create a ControlLogixAdmin session bean to administer
// The PLC/CPU at the given path
admin.create(path);
// Force an update of the name of the PLC as stored in the
// database. This does NOT change the PLC's name. It merely
// reads the PLC's name (from the PLC) and inserts that name
// in the database record representing the PLC.
System.out.println("Retrieve PLC's name to store in DB");
admin.updateName();
// Read the tag names and descriptors from the PLC and store them
// in the database.
System.out.println("Retrieve tag names and descriptor; store in DB");
admin.updateTagInformation();
// Release the reference to the bean
System.out.println("Let go of reference");
admin.remove();
}
catch (NamingException e) {
e.printStackTrace();
}
catch (ClassCastException e) {
e.printStackTrace();
}
catch (CreateException e) {
e.printStackTrace();
}
catch (RemoteException e) {
e.printStackTrace();
}
catch (ClgxException e) {
e.printStackTrace();
}
catch (RemoveException e) {
e.printStackTrace();
}
}
public static Context getInitialContext() throws NamingException{
Properties env = new Properties();
env.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
env.setProperty("java.naming.provider.url", "localhost:1099");
env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
return new InitialContext(env);
}
}
Run ClgxCreator.java as a Java application to perform the operation.