FileDocCategorySizeDatePackage
ProfileClient.javaAPI DocExample2263Wed Apr 05 11:25:42 BST 2000entity.containerManaged

ProfileClient.java

/*
 * This example is from the book "Java Enterprise in a Nutshell".
 * Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

package entity.containerManaged;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import java.util.Properties;
import NoSuchPersonException;

public class ProfileClient {
  public static void main(String[] args) {
    String name = args[0];

    try {
      // Get a JNDI context for our EJB server
      Properties p = new Properties();
      p.put(Context.INITIAL_CONTEXT_FACTORY,
	    "weblogic.jndi.T3InitialContextFactory");
      p.put(Context.PROVIDER_URL, "t3://localhost:7001");
      Context context = new InitialContext(p);

      // Create a profile server
      ProfileHome pHome =
	(ProfileHome)context.lookup("entity.containerManaged.ProfileHome");

      // Ask the server for the person's profile
      // First, see if this person already has a profile
      System.out.println("Looking for profile for " + name);
      Profile profile = null;
      try {
	profile = pHome.findByPrimaryKey(new ProfilePK(name));
      }
      catch (Exception e) {
	System.out.println("Exception while looking up profile.");
      }

      if (profile == null) {
	System.out.println("Creating profile for " + name);
	profile = pHome.create(name);
	// Set some entries in the profile
	System.out.println("Setting profile entries for " + name);
	profile.setEntry("favoriteColor", "blue");
	profile.setEntry("language", "German");
      }

      // Get the entries from the profile
      System.out.println("Getting profile entries for " + name);
      System.out.println("\tFavorite color: " +
			 profile.getEntry("favoriteColor"));
      System.out.println("\tLanguage: " + profile.getEntry("language"));
    }
    catch (NoSuchPersonException nspe) {
      System.out.println("Invalid person: " + name);
    } 
    catch (Exception e) {
      System.out.println("Error while creating/using profile.");
      e.printStackTrace();
    }
  }
}