⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example5.java

📁 BEA WebLogic Server 8.1大全 = BEA webLogic server 8.1 unleashed (美) Mark Artiges等著 袁毅 ... [等] 译 eng
💻 JAVA
字号:
package com.wlsunleashed.jndi;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

/**
 * This Example demonstrates adding and deleting a binding from a JNDI
 * tree.
 * 
 * No warranties, explicit or implied are made about this source code.
 * This is provided as-is and 
 *
 * @version 1.0
 * @since July 2002
 */

public class Example5 {

	/**
	 *  Constant representing the Initial context factory to be used 
	 */
	private static final String INITIAL_CONTEXT_FACTORY = 
					"weblogic.jndi.WLInitialContextFactory" ;
		
	/**
	 * Constant representing the Provider URL 
	 */			
	private static final String PROVIDER_URL = 
					"t3://localhost:7001" ;


	/**
	 * Name of the binding
	 */
	private static final String BINDING_NAME =
					"ExampleBinding" ;
					
	/**
	 * Object to be bound
	 */
	private static final String BOUND_OBJECT = 
					"TestObject" ;					

	/**
	 * Stores the initial context object
	 */
	private Context ctx = null ;
	
	/**
	 * The main method is invoked when the class file is executed 
	 * @param args : list of command line arguments
	 */
	public static void main(String[] args) {
		Example5 object = new Example5();
		try
		{
			System.out.println("*** listing before doing anything");
			object.listSubTree("");
			object.addBinding(BINDING_NAME, BOUND_OBJECT);
			System.out.println("*** listing after adding " );
			object.listSubTree("");
			object.deleteBinding(BINDING_NAME);
			System.out.println("*** listing after deleting" );
			object.listSubTree("");
			object.getInitialContext().close();
		}
		catch (NamingException ne)
		{
			ne.printStackTrace();
		}
	}


	/**
 	 * Returns the initial context to the server
 	 * @return The intial Context
 	 */ 
	private Context getInitialContext() 
	throws NamingException
	{
		if (ctx == null)
		{
			Hashtable props = new Hashtable() ;
			props.put( Context.INITIAL_CONTEXT_FACTORY, 
				INITIAL_CONTEXT_FACTORY );
			props.put( Context.PROVIDER_URL,
				PROVIDER_URL );
			ctx = new InitialContext( props );
		}
		return ctx ;	
	}

	/**
	 * Prints the subtree denoted by the passed tree name
	 * the client's environment
	 * @param subTree The Tree name
	 * @return Object
	 */
	private void listSubTree(String subTree )
	throws NamingException
	{
		
        System.out.println("Listing Bindings ");
        NamingEnumeration enum = getInitialContext().listBindings(subTree);    
        while (enum.hasMore())
        {
            System.out.println( enum.next() );    
        }
        System.out.println("listing done");       
	}
	

	/**
	 * adds a new binding using the name and object passed as parameters
	 * @param bindingName Name of the binding
	 * @param bindingObject Object to bind
	 */
	private void addBinding( String bindingName, 
					Object bindingObject )
	throws NamingException
	{
		System.out.println( "Attempting to bind " + 
							bindingObject + 
							" to name " +
							bindingName ) ;
		getInitialContext().bind(bindingName, bindingObject);
		System.out.println( "done." );		
	}	

	/**
	 * deletes a binding with the given name
	 * @param bindingName The binding to delete
	 */
	private void deleteBinding( String bindingName ) 
	throws NamingException
	{
		System.out.println( "Attempting to unbind " + 
							bindingName ) ;
		getInitialContext().unbind(bindingName);
		System.out.println( "done." );		
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -