为什么受保护方法中的对象有实现?

时间:2014-05-27 18:07:26

标签: java object

它的方法是invokeBackend(String request,HashMap context)。

为什么它有一个名为java.security.PrivilegedAction的对象createController = new java.security.PrivilegedAction() 它有一个实现?我看到它有一个run()方法,这意味着它是一个线程。

  1. run方法是否返回" controller"?它返回" controller"什么?
  2. 这是什么类型的实现?一个具有实现代码的对象? 3.什么是invokeBackend方法的主要用途

    也是对象

  3. java.security.PrivilegedAction processRequest = new java.security.PrivilegedAction()

    非常感谢!

    protected static String invokeBackend(String request, HashMap context)
            throws CommonModelException {
    
        if (request.equals("")) {
            return null;
        }
    
        if (logger.isDebugEnabled()) {
            logger.debug("request: \r\n" + request);
        }
    
        Properties clientAuthenticationEnv = CommonProperties
                .getClientAuthenticationProperties();
        if (wccClientId == null) {
            wccClientId = clientAuthenticationEnv.getProperty(CLIENT_ID);
        }
        if (wccClientPassword == null) {
            wccClientPassword = clientAuthenticationEnv
                    .getProperty(CLIENT_PASSWORD);
        }
    
        controllerHome = getControllerHome();
        String response = null;
    
        try {
    
            if (controllerHome == null) {
                throw new CommonModelException(ResourceBundleHelper.resolve(
                        CommonResourceBundleNames.COMMON_STRINGS,
                        LOG_REMOTE_EXCEPTION));
            }
            if (isWASImpl) {
                java.security.PrivilegedAction createController = new java.security.PrivilegedAction() {
                    public Object run() {
                        Object controller = null;
                        try {
                            controller = controllerHome.create();
                        } catch (RemoteException e) {
                            logger.error(ResourceBundleHelper.resolve(
                                    CommonResourceBundleNames.COMMON_STRINGS,
                                    LOG_REMOTE_EXCEPTION), e);
                        } catch (CreateException e) {
                            logger.error(ResourceBundleHelper.resolve(
                                    CommonResourceBundleNames.COMMON_STRINGS,
                                    LOG_FAIL_GET_EJB_INSTANCE), e);
                        }
                        return controller;
                    }
                }; // PrivilegedAction
    
                validateSecurityToken();
                final DWLServiceController controller = (DWLServiceController) WSSubject
                        .doAs(subject, createController);
    
                final String req = request;
                final HashMap cxt = context;
    
                java.security.PrivilegedAction processRequest = new java.security.PrivilegedAction() {
                    public Object run() {
                        Object response = null;
                        try {
                            response = (String) controller.processRequest(cxt,
                                    req);
                        } catch (com.dwl.base.exception.DWLResponseException e) {
                            response = e.getLocalizedMessage();
                        } catch (RemoteException e) {
                            logger.error(ResourceBundleHelper.resolve(
                                    CommonResourceBundleNames.COMMON_STRINGS,
                                    LOG_REMOTE_EXCEPTION), e);
                        }
                        return response;
                    }
                }; // PrivilegedAction
    
                validateSecurityToken();
                response = (String) WSSubject.doAs(subject, processRequest);
            } else {
                // DWLServiceController controller = controllerHome.create();
                // response = (String) controller.processRequest(context,
                // request);
    
                java.security.PrivilegedAction createController = new java.security.PrivilegedAction() {
                    public Object run() {
                        Object controller = null;
                        try {
                            controller = controllerHome.create();
                        } catch (RemoteException e) {
                            logger.error(ResourceBundleHelper.resolve(
                                    CommonResourceBundleNames.COMMON_STRINGS,
                                    LOG_REMOTE_EXCEPTION), e);
                        } catch (CreateException e) {
                            logger.error(ResourceBundleHelper.resolve(
                                    CommonResourceBundleNames.COMMON_STRINGS,
                                    LOG_FAIL_GET_EJB_INSTANCE), e);
                        }
                        return controller;
                    }
                }; // PrivilegedAction
    
                //reflection invoke to avoid compile dependency on weblogic library
                ClassLoader cl = Thread.currentThread().getContextClassLoader();
                Class securityClazz = cl.getClass().getClassLoader().loadClass("weblogic.security.Security");
                Method runAs = securityClazz.getMethod("runAs", new Class[]{Subject.class, java.security.PrivilegedAction.class});
                final DWLServiceController controller = (DWLServiceController) runAs.invoke(securityClazz, new Object[]{subject, createController});
                //final DWLServiceController controller = (DWLServiceController) Security.runAs(subject, createController);
    
                final String req = request;
                final HashMap cxt = context;
    
                java.security.PrivilegedAction processRequest = new java.security.PrivilegedAction() {
                    public Object run() {
                        Object response = null;
                        try {
                            response = (String) controller.processRequest(cxt,
                                    req);
                        } catch (com.dwl.base.exception.DWLResponseException e) {
                            response = e.getLocalizedMessage();
                        } catch (RemoteException e) {
                            logger.error(ResourceBundleHelper.resolve(
                                    CommonResourceBundleNames.COMMON_STRINGS,
                                    LOG_REMOTE_EXCEPTION), e);
                        }
                        return response;
                    }
                }; // PrivilegedAction
    
                response = (String)runAs.invoke(securityClazz, subject, processRequest);
                //response = (String) Security.runAs(subject, processRequest);
    
            }
        } catch (Exception e) {
            response = e.getLocalizedMessage();
            // for non IBM WebSphere Server, we have one more chance to redo
            // lookup for EJB server restart, clear the session first
            controllerHome = getControllerHome();
    
            if (controllerHome == null) {
                throw new CommonModelException(ResourceBundleHelper.resolve(
                        CommonResourceBundleNames.COMMON_STRINGS,
                        LOG_REMOTE_EXCEPTION));
            }
    
            try {
    
                DWLServiceController controller = controllerHome.create();
                response = (String) controller.processRequest(context, request);
            } catch (DWLResponseException e1) {
                response = e1.getLocalizedMessage();
            } catch (RemoteException e1) {
                logger.error(ResourceBundleHelper.resolve(
                        CommonResourceBundleNames.COMMON_STRINGS,
                        LOG_REMOTE_EXCEPTION), e1);
                throw new CommonModelException(e1);
            } catch (CreateException e1) {
                logger.error(ResourceBundleHelper.resolve(
                        CommonResourceBundleNames.COMMON_STRINGS,
                        LOG_FAIL_GET_EJB_INSTANCE), e1);
                throw new CommonModelException(e1);
            }
        }
    
        if (logger.isDebugEnabled()) {
            logger.debug("response: \r\n" + response);
        }
    
        return response;
    }
    

1 个答案:

答案 0 :(得分:0)

您正在查看的声明是一个初始化为引用匿名内部类实例的局部变量。实施的课程是PrivilegedAction。当您想要创建一个您不希望在其他地方使用的接口或类的一次性实现时,会使用该语法,因此没有必要为其命名。代码从接口实现run方法,并创建一个实现该接口的对象,并将其分配给局部变量。

run方法将控制器对象返回给调用它的人。这里没有显示,它作为此行上runAs方法调用的参数之一传入:

final DWLServiceController controller = (DWLServiceController) runAs.invoke(
    securityClazz, new Object[]{subject, createController});

其中代码使用反射来查找类weblogic.security.Security上的runAs方法。