Java Tutorial - Java Scipt : CMP 2.x Entity Bean Tests and Sample Code

Java Tutorial - Java Scipt :

CMP 2.x Entity Bean Tests and Sample Code

moAddButton = new JButton( “add” );
moAddButton.addActionListener( this );
moRemoveButton = new JButton( “remove by name” );
moRemoveButton.addActionListener( this );
moLoadButton = new JButton( “load by name” );
moLoadButton.addActionListener( this );
moMessageArea = new JTextArea();
moNameField = new JTextField();
moNumberField = new JTextField();
JPanel oLabelPanel = new JPanel( new GridLayout( 2, 1 ) );
oLabelPanel.add( new JLabel( “Name:” ) );
oLabelPanel.add( new JLabel( “Number:” ) );
JPanel oFieldPanel = new JPanel( new GridLayout( 2, 1 ) );
oFieldPanel.add( moNameField );
oFieldPanel.add( moNumberField );
JPanel oButtonPanel = new JPanel();
oButtonPanel.add( moAddButton, BorderLayout.SOUTH );
oButtonPanel.add( moRemoveButton, BorderLayout.SOUTH );
oButtonPanel.add( moLoadButton, BorderLayout.SOUTH );
JPanel oTopPanel = new JPanel( new BorderLayout() );
oTopPanel.add( oLabelPanel, BorderLayout.WEST );
oTopPanel.add( oFieldPanel );
oTopPanel.add( oButtonPanel, BorderLayout.SOUTH );
JPanel oPanel = new JPanel( new BorderLayout() );
oPanel.add( oTopPanel, BorderLayout.NORTH );
oPanel.add( new JScrollPane( moMessageArea ) );
getContentPane().add( oPanel );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void actionPerformed( ActionEvent oActionEvent ) {
if( oActionEvent.getSource() == moAddButton ) {
addSimpleEntity();
}
if( oActionEvent.getSource() == moRemoveButton ) {
removeSimpleEntity();
}
if( oActionEvent.getSource() == moLoadButton ) {
loadSimpleEntity();
}
}
protected void addSimpleEntity() {
try {
SimpleEntityHome oHome = (SimpleEntityHome)
PortableRemoteObject.narrow(
moContext.lookup( “ejb/SimpleEntity” ),
SimpleEntityHome.class );
SimpleEntity oSimpleEntity = (SimpleEntity)
PortableRemoteObject.narrow(
oHome.create( moNameField.getText(),
Integer.parseInt( moNumberField.getText() ) ),
SimpleEntity.class );
moMessageArea.setText( “Added “ + oSimpleEntity );
}
catch( Exception e ) {
JOptionPane.showMessageDialog(
null,
e.toString(),
“Whoops!”,
JOptionPane.ERROR_MESSAGE );
}
}
protected void removeSimpleEntity() {
try {
SimpleEntityHome oHome = (SimpleEntityHome)
PortableRemoteObject.narrow(
moContext.lookup( “ejb/SimpleEntity” ),
SimpleEntityHome.class );
String sPK = moNameField.getText();
SimpleEntity oSimpleEntity = (SimpleEntity)
PortableRemoteObject.narrow(
oHome.findByPrimaryKey( sPK ),
SimpleEntity.class );
oSimpleEntity.remove();
moMessageArea.setText( “Removed “ + oSimpleEntity );
}
catch( Exception e ) {
JOptionPane.showMessageDialog( null,
e.toString(),
“Whoops!”,
JOptionPane.ERROR_MESSAGE );
}
}
protected void loadSimpleEntity() {
try {
SimpleEntityHome oHome = (SimpleEntityHome)
PortableRemoteObject.narrow(
moContext.lookup( “ejb/SimpleEntity” ),
SimpleEntityHome.class );
String sPK = moNameField.getText();
SimpleEntity oSimpleEntity = (SimpleEntity)
PortableRemoteObject.narrow(
oHome.findByPrimaryKey( sPK ),
SimpleEntity.class );
moNumberField.setText( “” + oSimpleEntity.getNumber() );
moMessageArea.setText( “Loaded “ + oSimpleEntity );
}
catch( Exception e ) {
JOptionPane.showMessageDialog(
null,
e.toString(),
“Whoops!”,
JOptionPane.ERROR_MESSAGE );
}
}
public static void main( String args[] ) {
SimpleEntityClient client = new SimpleEntityClient();
client.show();
}
}

To compile and run the client, the jbossall-client.jar, log4j.jar, and simple-entity-ejb.jar files will be required. These .jar files contain classes that are referenced within the client. Here’s the command for compiling the client in Windows:
javac -classpath .;%JBOSS_HOME%\client\jbossall-client.jar;simple-entity-ejb.jar

SimpleEntityClient.java And here’s the command for compiling the client in Linux:

javac -classpath .:$JBOSS_HOME/client/jbossall-client.jar:simple-entity-ejb.jar

SimpleEntityClient.java Now, to run the client we also need to have those .jar files available. Here’s the command to run the client in Windows:
 java -classpath .;%JBOSS_HOME%\client\jbossallclient.
jar;%JBOSS_HOME%\client\log4j.jar;simple-entity-ejb.jar

 SimpleEntityClient And here’s the command to run the client in Linux:
 java -classpath .:$JBOSS_HOME/client/jbossallclient.
jar:$JBOSS_HOME/client/log4j.jar:simple-entity-ejb.jar

 SimpleEntityClientThe interface is fairly self-explanatory. Text fields are available to set values. The Add button adds the CMP based on those values. The Remove button removes a CMP with the name specified in the field. The load button loads a CMP based on the name field. Because the client will print out exceptions, you can try out combinations such as removing nonexistent CMPs and creating duplicates to see what the exception will look like in those instances. Finally, the List button just dumps a list of all SimpleEntity CMP beans into the message area. With the client, you can now see that the CMP bean is deployed and works as expected. So there you have it! We’re able to get a simple CMP EJB deployed on JBoss. This is just the tip of the iceberg as far as CMP is concerned. There are more CMP features and deployment optimizations to explore, as well as a plethora of design patterns that can make your CMP beans better. All this is beyond the scope of this chapter, but you’ll see some of these techniques put to use as we build the sample application in the later part of this book.