User: vharcq
Date: 01/06/20 15:01:15
Added: src/examples/org/jboss/docs/cmp/cd List.java Lookup.java
Remove.java Upload.java
Log:
Docs chapter 4 CD examples
Revision Changes Path
1.1 manual/src/examples/org/jboss/docs/cmp/cd/List.java
Index: List.java
===================================================================
package org.jboss.docs.cmp.cd;
import org.jboss.docs.cmp.cd.interfaces.CD;
import org.jboss.docs.cmp.cd.interfaces.CDHome;
import org.jboss.docs.cmp.cd.interfaces.CDCollectionHome;
import org.jboss.docs.cmp.cd.interfaces.CDCollection;
import javax.rmi.PortableRemoteObject;
import javax.naming.InitialContext;
import java.util.Hashtable;
import java.util.Properties;
import java.io.FileInputStream;
/**
* This is a simple client for the "CD" EJB; it lists (to standard output) all
* the "CD" instances in the system. The "main" method allows this class to be
* run from the command line.
*/
public class List
{
public static void main(String[] args)
{
// Get information about the Bean server from the properties file
Properties props = new Properties();
Properties sysProps = System.getProperties();
try
{
props.load (new FileInputStream ("cd.properties"));
sysProps.putAll(props);
}
catch (Exception e)
{
System.err.println ("Can't read cd.properties");
System.exit (-1);
}
System.setProperties (sysProps);
// Enclosing the whole process in a single "try" block is not an ideal way
// to do exception handling, but I don't want to clutter the program up
// with catch blocks
try
{
// Get a naming context
InitialContext jndiContext = new InitialContext();
// Get a reference to a CD Bean
Object ref = jndiContext.lookup("cd/CDCollection");
// Get a reference from this to the Bean's Home interface
CDCollectionHome home = (CDCollectionHome)
PortableRemoteObject.narrow (ref, CDCollectionHome.class);
CDCollection cdCollection = home.create();
CD[] cds = cdCollection.findAll();
for (int i = 0; i < cds.length; i++)
{
System.out.println (cds[i].getId() + "\t" + cds[i].getTitle() + "\t" +
cds[i].getArtist() + "\t" + cds[i].getType());
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
1.1 manual/src/examples/org/jboss/docs/cmp/cd/Lookup.java
Index: Lookup.java
===================================================================
package org.jboss.docs.cmp.cd;
import org.jboss.docs.cmp.cd.interfaces.CDCollectionHome;
import org.jboss.docs.cmp.cd.interfaces.CDCollection;
import org.jboss.docs.cmp.cd.interfaces.CD;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Properties;
import java.io.FileInputStream;
/**
* This is a simple client for the `CD' EJB; it lists (to standard output) all
* the `CD' instances that have any attribute (artist, title,...) that matches
* the string supplied. The `main' method allows this class to be executed on
* the command line.
*/
public class Lookup
{
public static void main(String[] args)
{
// Check that the command line has specified a string to lookup
if (args.length < 1)
{
System.err.println("Usage: java com.web_tomorrow.cd.Lookup [text]");
System.exit(-1);
}
String text = args[0];
// Get information about the Bean server from the properties file
Properties props = new Properties();
Properties sysProps = System.getProperties();
try
{
props.load (new FileInputStream ("cd.properties"));
sysProps.putAll(props);
}
catch (Exception e)
{
System.err.println ("Can't read `cd.proprties'");
System.exit (-1);
}
System.setProperties (sysProps);
// Enclosing the whole process in a single `try' block is not an ideal way
// to do exception handling, but I don't want to clutter the program up
// with catch blocks
try
{
// Get a naming context
InitialContext jndiContext = new InitialContext();
// Get a reference to a CD Bean
Object ref = jndiContext.lookup("cd/CDCollection");
// Get a reference from this to the Bean's Home interface
CDCollectionHome home = (CDCollectionHome)
PortableRemoteObject.narrow (ref, CDCollectionHome.class);
CDCollection cdCollection = home.create();
CD[] cds = cdCollection.lookupInAnyField(text);
for (int i = 0; i < cds.length; i++)
{
System.out.println (cds[i].getId() + "\t" + cds[i].getTitle()
+ "\t" +
cds[i].getArtist() + "\t" + cds[i].getType());
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
1.1 manual/src/examples/org/jboss/docs/cmp/cd/Remove.java
Index: Remove.java
===================================================================
package org.jboss.docs.cmp.cd;
import org.jboss.docs.cmp.cd.interfaces.CDHome;
import org.jboss.docs.cmp.cd.interfaces.CD;
import org.jboss.docs.cmp.cd.interfaces.CDCollectionHome;
import org.jboss.docs.cmp.cd.interfaces.CDCollection;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.util.Hashtable;
import java.util.Properties;
import java.io.FileInputStream;
/**
* This is a simple client for the `CD' EJB; it removes all the `CD' instances
* from the system. The `main' method allows this class to be run from the
* command line.
*/
public class Remove {
public static void main(String[] args) {
// Get information about the Bean server from the properties file
Properties props = new Properties();
Properties sysProps = System.getProperties();
try
{
props.load (new FileInputStream ("cd.properties"));
sysProps.putAll(props);
} catch (Exception e) {
System.err.println ("Can't read `cd.properties'");
System.exit (-1);
}
System.setProperties (sysProps);
// Enclosing the whole process in a single `try' block is not an ideal
way
// to do exception handling, but I don't want to clutter the program up
// with catch blocks
try {
// Get a naming context
InitialContext jndiContext = new InitialContext();
// Need two object references, one for the collection,
// one for each CD.
// Get a reference to a CD Bean
Object refCD = jndiContext.lookup("cd/CD");
// Get a reference to the CD Bean's Home interface
CDHome cdHome = (CDHome)
PortableRemoteObject.narrow (refCD, CDHome.class);
// Get a reference to a CDCollection Bean
Object ref = jndiContext.lookup("cd/CDCollection");
// Get a reference to the collection Bean's Home interface
CDCollectionHome cdCollectionHome = (CDCollectionHome)
PortableRemoteObject.narrow (ref,
CDCollectionHome.class);
CDCollection cdCollection = cdCollectionHome.create();
CD[] cds = cdCollection.findAll();
for (int i = 0; i < cds.length; i++)
{
System.out.println ("Removing " +
cds[i].getId() + "\t" +
cds[i].getTitle());
CD eachCD = cdHome.findByPrimaryKey(cds[i].getId());
eachCD.remove();
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
1.1 manual/src/examples/org/jboss/docs/cmp/cd/Upload.java
Index: Upload.java
===================================================================
package org.jboss.docs.cmp.cd;
import org.jboss.docs.cmp.cd.interfaces.CDCollectionHome;
import org.jboss.docs.cmp.cd.interfaces.CDCollection;
import org.jboss.docs.cmp.cd.interfaces.CDExistsException;
import org.jboss.docs.cmp.cd.utils.FileLineReader;
import org.jboss.docs.cmp.cd.utils.TokenizerWithBlanks;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Properties;
import java.io.FileInputStream;
/**
* Uploads a tab-delimited file of CD data to the database. There is a `main()'
* method so this class can be run from the command line. `main()' takes one
* command-line argument: the name of the file to upload. See the documentation
* for the `uploadFile()' method for details of the file format. Note that the
* uploading process may be very slow on some machines, especially if you are
* using an interpreting JVM. Expect to have a cup of coffee if you are
* uploading more than a hundred CDs.
* Note that the uploadFile method deletes the existing database before it
* uploads.
*/
public class Upload
{
/**
* Run the upload process from the command line.
*/
public static void main(String[] args)
{
// Check that the command line has specified a filename
if (args.length < 1)
{
System.err.println("Usage: java com.web_tomorrow.cd.Upload
[filename]");
System.exit(-1);
}
String filename = args[0];
// Get information about the Bean server from the properties file
Properties props = new Properties();
Properties sysProps = System.getProperties();
try
{
props.load (new FileInputStream ("cd.properties"));
sysProps.putAll(props);
}
catch (Exception e)
{
System.err.println ("Can't read `cd.proprties'");
System.exit (-1);
}
System.setProperties (sysProps);
try
{
uploadFile (filename);
}
catch (java.io.IOException e)
{
System.err.println ("Upload: can't read file `" + filename + "'");
}
catch(Exception e)
{
System.err.println("Upload: " + e.toString());
}
System.out.println ("OK");
}
/**
* This method uploads a database of CDs specified as a text file. The file
* must contain one line for each CD, with 5 fields on each line. The
* ordering of the fields is id-title-artist-type-notes. The fields are
* separated with tabs. Note that a tab on its own must exist as a
* placeholder for an empty field.
*/
public static void uploadFile (String filename)
throws java.io.IOException, javax.naming.NamingException,
java.rmi.RemoteException, javax.ejb.CreateException
{
// Get a naming context
InitialContext jndiContext = new InitialContext();
// Get a reference to a CD Bean
Object ref = jndiContext.lookup("cd/CDCollection");
// Get a reference from this to the Bean's Home interface
CDCollectionHome home = (CDCollectionHome)
PortableRemoteObject.narrow (ref, CDCollectionHome.class);
CDCollection cdCollection = home.create();
cdCollection.deleteAll();
FileLineReader flr = new FileLineReader(filename);
boolean more = true;
int i = 0;
do
{
String s = flr.readLine();
i++;
if (s == null)
more = false;
else
{
if (s == "") continue;
String[] tokens = TokenizerWithBlanks.tokenize(s, '\t');
int numTokens = tokens.length;
if (numTokens == 5)
{
String id = tokens[0];
String title = tokens[1];
String artist = tokens[2];
String type = tokens[3];
String notes = tokens[4];
try
{
cdCollection.addCd(id, title, artist, type,
notes);
}
catch (CDExistsException e)
{
System.err.println ("uploadFile: duplicate ID
on line "
+ i + " (ID is " + id + ")");
}
}
else
System.err.println ("uploadFile: wrong number of
fields on line "
+ i
+ " in file `" + filename + "'");
}
} while (more);
} // End of uploadFile()
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development