1

我有在桌面上运行的 java SE 示例客户端(下面的代码)。但是我已经访问了WebSphere 被称为EJB 的部署。如何重写以下代码以在 WebSphere 上工作?(当我离开这段代码就像它是程序一样工作时,但我认为这可以做得更简单明了)

主要方法:

WSConn connection = new WSConn();
final Plan plan = connection.getPlanBean();
com.ibm.websphere.security.auth.WSSubject.doAs(connection.getSubject(), new java.security.PrivilegedAction<Object>() {
public Object run() {
try {
    // App logic
} catch (Throwable t) {
    System.err.println("PrivilegedAction - Error calling EJB: " + t);
t.printStackTrace();
   }
     return null;
  }
}); // end doAs

WSConn 类:

public class WSConn {
    private static final String INITIAL_CONTEXT_FACTORY = "com.ibm.websphere.naming.WsnInitialContextFactory";
    private static final String JAAS_MODULE = "WSLogin";
    private static final String MODEL_EJB_NAME_LONG = "ejb/com/ibm/ModelHome";
    private static final String PLAN_EJB_NAME_LONG = "ejb/com/ibm/PlanHome";
    private Subject subject;
    private InitialContext initialContext;
    private String serverName;
    private String serverPort;
    private String uid;
    private String pwd;
    private String remoteServerName;
    private Model modelBean;
    private Plan planBean;
    public WSConn() {
        Properties props = new Properties();
        try {
            props.load(WSConn.class.getClassLoader().getResourceAsStream("WSConn.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        serverName = props.getProperty("WSConn.serverName");
        serverPort = props.getProperty("WSConn.serverPort");
        uid = props.getProperty("WSConn.userID");
        pwd = props.getProperty("WSConn.password");
        remoteServerName = props.getProperty("WSConn.remoteServerName");
    }

    private void init() {
        if (subject == null || initialContext == null) {
            subject = login();
        }
    }

    private Subject login() {

        Subject subject = null;
        try {
            LoginContext lc = null;

            // CRATE LOGIN CONTEXT
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
            env.put(Context.PROVIDER_URL, "corbaloc:iiop:" + serverName + ":" + serverPort);

            initialContext = new InitialContext(env);

            // Just to test the connection
            initialContext.lookup("");

            lc = new LoginContext(JAAS_MODULE, new WSCallbackHandlerImpl(uid, pwd));
            lc.login();

            subject = lc.getSubject();


        } catch (javax.naming.NoPermissionException exc) {
            System.err.println("[WSConn] - Login Error: " + exc);

        } catch (Exception exc) {
            System.err.println("[WSConn] - Error: " + exc);
        }
        return subject;
    }

    public wModel getModelBean() {
        if (modelBean == null) {
            init();
            modelBean = (wModel) com.ibm.websphere.security.auth.WSSubject.doAs(subject,
                    new java.security.PrivilegedAction<wModel>() {
                        public wModel run() {
                            wModel session = null;
                            try {
                                Object o = initialContext.lookup(MODEL_EJB_NAME_LONG);
                                wModelHome home = (wModelHome) PortableRemoteObject.narrow(o, wModelHome.class);

                                if (home != null) {
                                    session = home.create(remoteServerName);
                                }

                            } catch (Exception exc) {
                                System.err.println("Error getting model bean: " + exc);
                            }
                            return session;
                        }
                    }); // end doAs
        }

        return modelBean;
    }

    public wPlan getPlanBean() {
        if (planBean == null) {
            init();
            planBean = (wPlan) com.ibm.websphere.security.auth.WSSubject.doAs(subject,
                    new java.security.PrivilegedAction<wPlan>() {
                        public wPlan run() {
                            wPlan session = null;
                            try {
                                Object o = initialContext.lookup(PLAN_EJB_NAME_LONG);
                                wPlanHome home = (wPlanHome) PortableRemoteObject.narrow(o, wPlanHome.class);

                                if (home != null) {
                                    session = home.create(remoteServerName);
                                }

                            } catch (Exception exc) {
                                System.err.println("Error getting plan bean: " + exc);
                            }
                            return session;
                        }
                    }); // end doAs
        }

        return planBean;
    }

    public Subject getSubject() {
        if (subject == null) {
            init();
        }

        return subject;
    }
}
4

2 回答 2

1

如果您尝试从 POJO 类访问 EJB,那么没有什么比查找+窄更简单的了。但是,如果 POJO 包含在应用程序(EAR 或 WAR)中,那么您可以声明和查找 EJB 引用 ( java:comp/ejb/myEJB),然后容器将执行窄而不是您的代码。如果您将代码更改为托管类,如 servlet、另一个 EJB 或 CDI bean,那么您可以使用@EJB注入,然后您甚至不需要查找。

于 2015-03-27T15:10:30.653 回答
1

如另一个答案所示,经典机制是查找和缩小主界面。

获取初始上下文

final InitialContext initialContext = new InitialContext();

按 jndi 名称查找 home,指定完整的 jndi 名称

Object obj = initialContext.lookup("ejb/com/ibm/tws/conn/plan/ConnPlanHome");

或者您可以在您的 WAR 中创建 e 引用并使用 java:comp/env/ yourname

然后将home缩小到home接口类

ConnPlanHome planHome = (ConnPlanHome)PortableRemoteObject.narrow(obj, ConnPlanHome.class);

然后创建 EJB 远程接口

ConnPlan plan = planHome.create();

关于调用应该适用于 IBM Workload Scheduler 分布式。对于 IBM Workload Scheduler z/OS,JNDI 名称和类名称不同:

final InitialContext initialContext = new InitialContext();
String engineName = "XXXX";
Object obj = initialContext.lookup("ejb/com/ibm/tws/zconn/plan/ZConnPlanHome");
ZConnPlanHome planHome = (ZConnPlanHome)PortableRemoteObject.narrow(obj, ZConnPlanHome.class);
ZConnPlan plan = planHome.create(engineName);

用户凭据从客户端传播到引擎,需要对客户端进行身份验证,否则引擎将拒绝请求。

于 2016-06-01T16:04:30.363 回答