BMPBEANTestClient1.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:10k
源码类别:
Java编程
开发平台:
Java
- package userjmssesenbdb;
- import com.cwj.userjmssesenbdb.*;
- import javax.naming.*;
- import java.util.Properties;
- import javax.rmi.PortableRemoteObject;
- public class BMPBEANTestClient1 extends Object {
- private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
- private static final int MAX_OUTPUT_LINE_LENGTH = 100;
- private boolean logging = true;
- private BMPBEANRemoteHome bMPBEANRemoteHome = null;
- private BMPBEANRemote bMPBEANRemote = null;
- //Construct the EJB test client
- public BMPBEANTestClient1() {
- initialize();
- }
- public void initialize() {
- long startTime = 0;
- if (logging) {
- log("Initializing bean access.");
- startTime = System.currentTimeMillis();
- }
- try {
- //get naming context
- Context context = getInitialContext();
- //look up jndi name
- Object ref = context.lookup("BMPBEANRemote");
- //look up jndi name and cast to Home interface
- bMPBEANRemoteHome = (BMPBEANRemoteHome) PortableRemoteObject.narrow(ref, BMPBEANRemoteHome.class);
- bMPBEANRemoteHome.ejbInsertAa11("1", "J");
- if (logging) {
- long endTime = System.currentTimeMillis();
- log("Succeeded initializing bean access through Home interface.");
- log("Execution time: " + (endTime - startTime) + " ms.");
- }
- }
- catch(Exception e) {
- if (logging) {
- log("Failed initializing bean access.");
- }
- e.printStackTrace();
- }
- }
- private Context getInitialContext() throws Exception {
- String url = "t3://127.0.0.1:7001";
- String user = null;
- String password = null;
- Properties properties = null;
- try {
- properties = new Properties();
- properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
- properties.put(Context.PROVIDER_URL, url);
- if (user != null) {
- properties.put(Context.SECURITY_PRINCIPAL, user);
- properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
- }
- return new InitialContext(properties);
- }
- catch(Exception e) {
- log("Unable to connect to WebLogic server at " + url);
- log("Please make sure that the server is running.");
- throw e;
- }
- }
- //----------------------------------------------------------------------------
- // Methods that use Home interface methods to generate a Remote interface reference
- //----------------------------------------------------------------------------
- public BMPBEANRemote create(String shancbz, String xiwbz) {
- long startTime = 0;
- if (logging) {
- log("Calling create(" + shancbz + ", " + xiwbz + ")");
- startTime = System.currentTimeMillis();
- }
- try {
- bMPBEANRemote = bMPBEANRemoteHome.create(shancbz, xiwbz);
- if (logging) {
- long endTime = System.currentTimeMillis();
- log("Succeeded: create(" + shancbz + ", " + xiwbz + ")");
- log("Execution time: " + (endTime - startTime) + " ms.");
- }
- }
- catch(Exception e) {
- if (logging) {
- log("Failed: create(" + shancbz + ", " + xiwbz + ")");
- }
- e.printStackTrace();
- }
- if (logging) {
- log("Return value from create(" + shancbz + ", " + xiwbz + "): " + bMPBEANRemote + ".");
- }
- return bMPBEANRemote;
- }
- public BMPBEANRemote findByPrimaryKey(BMPBEANPK pk) {
- long startTime = 0;
- if (logging) {
- log("Calling findByPrimaryKey(" + pk + ")");
- startTime = System.currentTimeMillis();
- }
- try {
- bMPBEANRemote = bMPBEANRemoteHome.findByPrimaryKey(pk);
- if (logging) {
- long endTime = System.currentTimeMillis();
- log("Succeeded: findByPrimaryKey(" + pk + ")");
- log("Execution time: " + (endTime - startTime) + " ms.");
- }
- }
- catch(Exception e) {
- if (logging) {
- log("Failed: findByPrimaryKey(" + pk + ")");
- }
- e.printStackTrace();
- }
- if (logging) {
- log("Return value from findByPrimaryKey(" + pk + "): " + bMPBEANRemote + ".");
- }
- return bMPBEANRemote;
- }
- public BMPBEANPK ejbInsertAa11(String shancbz, String xiwbz) {
- BMPBEANPK returnValue = null;
- long startTime = 0;
- if (logging) {
- log("Calling ejbInsertAa11(" + shancbz + ", " + xiwbz + ")");
- startTime = System.currentTimeMillis();
- }
- try {
- returnValue = bMPBEANRemoteHome.ejbInsertAa11(shancbz, xiwbz);
- if (logging) {
- long endTime = System.currentTimeMillis();
- log("Succeeded: ejbInsertAa11(" + shancbz + ", " + xiwbz + ")");
- log("Execution time: " + (endTime - startTime) + " ms.");
- }
- }
- catch(Exception e) {
- if (logging) {
- log("Failed: ejbInsertAa11(" + shancbz + ", " + xiwbz + ")");
- }
- e.printStackTrace();
- }
- if (logging) {
- log("Return value from ejbInsertAa11(" + shancbz + ", " + xiwbz + "): " + returnValue + ".");
- }
- return returnValue;
- }
- public BMPBEANPK ejbdeleteAa11(String shancbz) {
- BMPBEANPK returnValue = null;
- long startTime = 0;
- if (logging) {
- log("Calling ejbdeleteAa11(" + shancbz + ")");
- startTime = System.currentTimeMillis();
- }
- try {
- returnValue = bMPBEANRemoteHome.ejbdeleteAa11(shancbz);
- if (logging) {
- long endTime = System.currentTimeMillis();
- log("Succeeded: ejbdeleteAa11(" + shancbz + ")");
- log("Execution time: " + (endTime - startTime) + " ms.");
- }
- }
- catch(Exception e) {
- if (logging) {
- log("Failed: ejbdeleteAa11(" + shancbz + ")");
- }
- e.printStackTrace();
- }
- if (logging) {
- log("Return value from ejbdeleteAa11(" + shancbz + "): " + returnValue + ".");
- }
- return returnValue;
- }
- //----------------------------------------------------------------------------
- // Methods that use Remote interface methods to access data through the bean
- //----------------------------------------------------------------------------
- public void setShancbz(String shancbz) {
- if (bMPBEANRemote == null) {
- System.out.println("Error in setShancbz(): " + ERROR_NULL_REMOTE);
- return ;
- }
- long startTime = 0;
- if (logging) {
- log("Calling setShancbz(" + shancbz + ")");
- startTime = System.currentTimeMillis();
- }
- try {
- bMPBEANRemote.setShancbz(shancbz);
- if (logging) {
- long endTime = System.currentTimeMillis();
- log("Succeeded: setShancbz(" + shancbz + ")");
- log("Execution time: " + (endTime - startTime) + " ms.");
- }
- }
- catch(Exception e) {
- if (logging) {
- log("Failed: setShancbz(" + shancbz + ")");
- }
- e.printStackTrace();
- }
- }
- public String getShancbz() {
- String returnValue = "";
- if (bMPBEANRemote == null) {
- System.out.println("Error in getShancbz(): " + ERROR_NULL_REMOTE);
- return returnValue;
- }
- long startTime = 0;
- if (logging) {
- log("Calling getShancbz()");
- startTime = System.currentTimeMillis();
- }
- try {
- returnValue = bMPBEANRemote.getShancbz();
- if (logging) {
- long endTime = System.currentTimeMillis();
- log("Succeeded: getShancbz()");
- log("Execution time: " + (endTime - startTime) + " ms.");
- }
- }
- catch(Exception e) {
- if (logging) {
- log("Failed: getShancbz()");
- }
- e.printStackTrace();
- }
- if (logging) {
- log("Return value from getShancbz(): " + returnValue + ".");
- }
- return returnValue;
- }
- public void setXiwbz(String xiwbz) {
- if (bMPBEANRemote == null) {
- System.out.println("Error in setXiwbz(): " + ERROR_NULL_REMOTE);
- return ;
- }
- long startTime = 0;
- if (logging) {
- log("Calling setXiwbz(" + xiwbz + ")");
- startTime = System.currentTimeMillis();
- }
- try {
- bMPBEANRemote.setXiwbz(xiwbz);
- if (logging) {
- long endTime = System.currentTimeMillis();
- log("Succeeded: setXiwbz(" + xiwbz + ")");
- log("Execution time: " + (endTime - startTime) + " ms.");
- }
- }
- catch(Exception e) {
- if (logging) {
- log("Failed: setXiwbz(" + xiwbz + ")");
- }
- e.printStackTrace();
- }
- }
- public String getXiwbz() {
- String returnValue = "";
- if (bMPBEANRemote == null) {
- System.out.println("Error in getXiwbz(): " + ERROR_NULL_REMOTE);
- return returnValue;
- }
- long startTime = 0;
- if (logging) {
- log("Calling getXiwbz()");
- startTime = System.currentTimeMillis();
- }
- try {
- returnValue = bMPBEANRemote.getXiwbz();
- if (logging) {
- long endTime = System.currentTimeMillis();
- log("Succeeded: getXiwbz()");
- log("Execution time: " + (endTime - startTime) + " ms.");
- }
- }
- catch(Exception e) {
- if (logging) {
- log("Failed: getXiwbz()");
- }
- e.printStackTrace();
- }
- if (logging) {
- log("Return value from getXiwbz(): " + returnValue + ".");
- }
- return returnValue;
- }
- //----------------------------------------------------------------------------
- // Utility Methods
- //----------------------------------------------------------------------------
- private void log(String message) {
- if (message == null) {
- System.out.println("-- null");
- return ;
- }
- if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
- System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");
- }
- else {
- System.out.println("-- " + message);
- }
- }
- //Main method
- public static void main(String[] args) {
- BMPBEANTestClient1 client = new BMPBEANTestClient1();
- // Use the client object to call one of the Home interface wrappers
- // above, to create a Remote interface reference to the bean.
- // If the return value is of the Remote interface type, you can use it
- // to access the remote interface methods. You can also just use the
- // client object to call the Remote interface wrappers.
- }
- }