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

📄 machinebean.java

📁 EJB_原代码多例-好好东西啊
💻 JAVA
字号:
package com.wiley.compBooks.roman.session.fazuul;

import javax.ejb.*;
import java.util.*;
import javax.naming.*;

/**
 * Stateless Session Bean.  Simple factory for generating random Components.
 */
public class MachineBean implements SessionBean {

  // Constants used for properties file reading
  public static final String COMPONENT_LIST = "COMPONENT_LIST";

  private transient SessionContext ctx;

  //
  // EJB-required methods
  //

  public void ejbCreate() {
  }
 
  public void ejbRemove() {
  }

  public void ejbActivate() {
  }

  public void ejbPassivate() {
  }

  public void setSessionContext(SessionContext ctx) {
	this.ctx = ctx;
  }

  //
  // Business methods
  //

  /**
   * Makes a new, random component
   */
  public Component makeComponent() throws MachineException {
	/*
	 * Get properties from Session Context, and retrieve app-specific
	 * property that lists the available components for creation.
	 */
	Properties props = ctx.getEnvironment();
	String componentString = (String) props.get(COMPONENT_LIST);
	StringTokenizer components = new StringTokenizer(componentString, ",");


	/*
	 * Find a random component name
	 */
	int componentNumber = Math.abs(
		new Random().nextInt() % components.countTokens());

	String componentName = null;
	for (int i=0; i <= componentNumber; i++) {
		componentName = (String) components.nextToken();
	}

	try {
		/*
		 * Get a reference to the ComponentHome Object via JNDI.  We
		 * need the Component's Home Object to create Components.
		 *
		 * We rely on app-specific properties to define
		 * the Initial Context params which JNDI needs.
		 */
		Context ctx = new InitialContext(props);
		ComponentHome home = (ComponentHome) ctx.lookup("ComponentHome");

		/*
		 * Use the factory to create the new, merged component, and
		 * return it. 
		 */
		return home.create(componentName);
	}
	catch (Exception e) {
		throw new MachineException(e);
	}
  }
}

⌨️ 快捷键说明

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