converterbean.java
来自「weblogic应用全实例」· Java 代码 · 共 221 行
JAVA
221 行
//声明这个类在包examples.wlec.ejb.simpapp中
package examples.wlec.ejb.simpapp;
//声明这个类引入的其它类
import javax.ejb.*;
import java.io.Serializable;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.omg.CORBA.*;
import com.beasys.Tobj.*;
import com.beasys.*;
/*这些类来自WebLogic Enterprise Simpapp 实例 */
import SimpleFactory;
import SimpleFactoryHelper;
import Simple;
/**
* 这是个无状态的会话bean
* 这个bean演示:
* 访问ISL/ISH进程和WebLogic Enterprise 服务器
* 没有状态保持
* 用户定义的异常
*
*/
//会话bean必须实现接口SessionBean
public class ConverterBean implements SessionBean {
static SimpleFactory simple_factory_ref;
// -----------------------------------------------------------------
// 私有变量
private SessionContext ctx;
private Context rootCtx;
// -----------------------------------------------------------------
// SessionBean 实现
/**
* 这是EJB规范定义的方法,这个实例中没用到
*/
public void ejbActivate() {}
/**
* 这是EJB规范定义的方法,这个实例中没用到
*/
public void ejbRemove() {}
/**
* 这是EJB规范定义的方法,这个实例中没用到
*/
public void ejbPassivate() {}
/**
* 设置会话上下文
*
* @参数 ctx SessionContext 会话上下文
*/
public void setSessionContext(SessionContext ctx) {
this.ctx = ctx;
}
/**
* 这是EJB规范定义的方法
*/
public void ejbCreate () throws CreateException {
try {
try {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
InitialContext ic = new InitialContext(p);
rootCtx = (Context)ic.lookup("java:comp/env");
}
catch (NamingException ne) {
throw new CreateException("Could not lookup context");
}
initIIOPpool();
}
catch (Exception e) {
throw new CreateException("ejbCreate called: " + e);
}
}
/**
* 远程方法实现
*
* @参数 mixed string 输入
* @返回 ConverterResult 转化结果
* @exception examples.wlec.ejb.simpapp.ProcessingErrorException
* 异常
*/
public ConverterResult toUpper(String mixed)
throws ProcessingErrorException
{
return convert("UPPER", mixed);
}
/**
* 远程方法实现
*
* @参数 mixed string 输入
* @返回 ConverterResult 转化结果
* @exception examples.wlec.ejb.simpapp.ProcessingErrorException
* 异常
*/
public ConverterResult toLower(String mixed)
throws ProcessingErrorException
{
return convert("LOWER", mixed);
}
//本类使用的方法
protected ConverterResult convert (String changeCase, String mixed)
throws ProcessingErrorException
{
String result;
try {
// 查找simple对象
Simple simple = simple_factory_ref.find_simple();
if (changeCase.equals("UPPER")) {
// 大写操作
org.omg.CORBA.StringHolder buf = new org.omg.CORBA.StringHolder(mixed);
simple.to_upper(buf);
result = buf.value;
}
else
{
//下写操作
result = simple.to_lower(mixed);
}
}
catch (org.omg.CORBA.SystemException e) {
//异常处理
throw new ProcessingErrorException("Converter error: Corba system exception: " + e);
}
catch (Exception e) {
//异常处理,抛出用户定义的异常
throw new ProcessingErrorException("Converter error: " + e);
}
return new ConverterResult(result);
}
// 私有方法
/**
* 返回WebLogic Enterprise连接池名
*
* @返回 String IIOP 连接池名
*/
private String getIIOPPoolName() throws ProcessingErrorException {
try {
return (String) rootCtx.lookup("IIOPPoolName");
}
catch (NamingException ne) {
//异常处理
throw new ProcessingErrorException ("IIOPPoolName not found in context");
}
}
/**
* 初始化IIOP连接池
*/
private void initIIOPpool() throws Exception {
try {
// 创建bootstrap对象
Tobj_Bootstrap bootstrap = BootstrapFactory.getClientContext(getIIOPPoolName());
// 使用bootstrap对象查找factory finder.
org.omg.CORBA.Object fact_finder_oref =
bootstrap.resolve_initial_references("FactoryFinder") ;
// 验证factory finder.
FactoryFinder fact_finder_ref =
FactoryFinderHelper.narrow(fact_finder_oref);
// 使用factory finder查找simple factory.
org.omg.CORBA.Object simple_fact_oref =
fact_finder_ref.find_one_factory_by_id(SimpleFactoryHelper.id());
// 验证simple factory.
simple_factory_ref =
SimpleFactoryHelper.narrow(simple_fact_oref);
}
catch (org.omg.CosLifeCycle.NoFactory e) {
//异常处理
throw new Exception("Can't find the simple factory: " +e);
}
catch (CannotProceed e) {
//异常处理
throw new Exception("FactoryFinder internal error: " +e);
}
catch (RegistrarNotAvailable e) {
//异常处理
throw new Exception("FactoryFinder Registrar not available: " +e);
}
catch (InvalidName e) {
//异常处理
throw new Exception("Invalid name from resolve_initial_reference(): " +e);
}
catch (org.omg.CORBA.BAD_PARAM e) {
//异常处理
throw new Exception("Invalid TOBJADDR=//host:port property specified: " +e);
}
catch (org.omg.CORBA.UserException e) {
//异常处理
throw new Exception("Unexpected CORBA user exception: " +e);
}
catch (org.omg.CORBA.SystemException e) {
//异常处理
throw new Exception("CORBA system exception: " +e);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?