WebLogic 上的集群故障转移异常

发布于 2024-09-06 04:13:47 字数 15958 浏览 5 评论 0原文

我在具有两个节点的 WebLogic 10.3.2 集群上部署了一个应用程序,并在集群前面有一个负载均衡器。

我已将

<core:init distributable="true" debug="true" />

“我的会话”和“对话”类设置为可序列化。

我开始使用第一个节点提供的应用程序。控制台显示会话复制正在运行。

<Jun 17, 2010 11:43:50 AM EEST> <Info> <Cluster> <BEA-000128> <Updating  5903057688359791237S:xxx.yyy.gr:[7002,7002,-1,-1,-1,-1,-1]:xxx.yyy.gr:7002,xxx.yyy.gr:7002:prs_domain:PRS_Server_2 in the cluster.>
<Jun 17, 2010 11:43:50 AM EEST> <Info> <Cluster> <BEA-000128> <Updating 5903057688359791237S:xxx.yyy.gr:[7002,7002,-1,-1,-1,-1,-1]:xxx.yyy.gr:7002,xxx.yyy.gr:7002:prs_domain:PRS_Server_2 in the cluster.>

当我从管理控制台关闭第一个节点时,我在另一个节点中得到以下信息:

<Jun 17, 2010 11:23:46 AM EEST> <Error> <Kernel> <BEA-000802> <ExecuteRequest failed
 java.lang.NullPointerException.
java.lang.NullPointerException
        at org.jboss.seam.intercept.JavaBeanInterceptor.callPostActivate(JavaBeanInterceptor.java:165)
        at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:73)
        at com.myproj.beans.SortingFilteringBean_$$_javassist_seam_2.sessionDidActivate(SortingFilteringBean_$$_javassist_seam_2.java)
        at weblogic.servlet.internal.session.SessionData.notifyActivated(SessionData.java:2258)
        at weblogic.servlet.internal.session.SessionData.notifyActivated(SessionData.java:2222)
        at weblogic.servlet.internal.session.ReplicatedSessionData.becomePrimary(ReplicatedSessionData.java:231)
        at weblogic.cluster.replication.WrappedRO.changeStatus(WrappedRO.java:142)
        at weblogic.cluster.replication.WrappedRO.ensureStatus(WrappedRO.java:129)
        at weblogic.cluster.replication.LocalSecondarySelector$ChangeSecondaryInfo.run(LocalSecondarySelector.java:542)
        at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:516)
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
>

我做错了什么?

这是SortingFilteringBean:

import java.util.HashMap;
import java.util.LinkedHashMap;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.myproj.model.crud.Filtering;
import com.myproj.model.crud.Sorting;
import com.myproj.model.crud.SortingOrder;

/**
 * Managed bean aggregating the sorting and filtering values for all the
 * application's lists. A light-weight bean to always keep in the session with
 * minimum impact.
 */
@Name("sortingFilteringBean")
@Scope(ScopeType.SESSION)
public class SortingFilteringBean extends BaseManagedBean {

    private static final long serialVersionUID = 1L;
    private Sorting applicantProductListSorting;
    private Filtering applicantProductListFiltering;
    private Sorting homePageSorting;
    private Filtering homePageFiltering;

    /**
     * Creates a new instance of SortingFilteringBean.
     */
    public SortingFilteringBean() {

        // **********************
        // Applicant Product List
        // **********************
        // Sorting
        LinkedHashMap<String, SortingOrder> applicantProductListSortingValues = new LinkedHashMap<String, SortingOrder>();
        applicantProductListSortingValues.put("applicantName",
                SortingOrder.ASCENDING);
        applicantProductListSortingValues.put("applicantEmail",
                SortingOrder.ASCENDING);
        applicantProductListSortingValues.put("productName",
                SortingOrder.ASCENDING);
        applicantProductListSortingValues.put("productEmail",
                SortingOrder.ASCENDING);
        applicantProductListSorting = new Sorting(
                applicantProductListSortingValues);
        // Filtering
        HashMap<String, String> applicantProductListFilteringValues = new HashMap<String, String>();
        applicantProductListFilteringValues.put("applicantName", "");
        applicantProductListFilteringValues.put("applicantEmail", "");
        applicantProductListFilteringValues.put("productName", "");
        applicantProductListFilteringValues.put("productEmail", "");
        applicantProductListFiltering = new Filtering(
                applicantProductListFilteringValues);
        // *********
        // Home page
        // *********
        // Sorting
        LinkedHashMap<String, SortingOrder> homePageSortingValues = new LinkedHashMap<String, SortingOrder>();
        homePageSortingValues.put("productName", SortingOrder.ASCENDING);
        homePageSortingValues.put("productId", SortingOrder.ASCENDING);
        homePageSortingValues.put("productAtcCode", SortingOrder.UNSORTED);
        homePageSortingValues.put("productEmaNumber", SortingOrder.UNSORTED);
        homePageSortingValues.put("productOrphan", SortingOrder.UNSORTED);
        homePageSortingValues.put("productRap", SortingOrder.UNSORTED);
        homePageSortingValues.put("productCorap", SortingOrder.UNSORTED);
        homePageSortingValues.put("applicationTypeDescription",
                SortingOrder.ASCENDING);
        homePageSortingValues.put("applicationId", SortingOrder.ASCENDING);
        homePageSortingValues
                .put("applicationEmaNumber", SortingOrder.UNSORTED);
        homePageSortingValues
                .put("piVersionImportDate", SortingOrder.ASCENDING);
        homePageSortingValues.put("piVersionId", SortingOrder.ASCENDING);
        homePageSorting = new Sorting(homePageSortingValues);
        // Filtering
        HashMap<String, String> homePageFilteringValues = new HashMap<String, String>();
        homePageFilteringValues.put("productName", "");
        homePageFilteringValues.put("productAtcCode", "");
        homePageFilteringValues.put("productEmaNumber", "");
        homePageFilteringValues.put("applicationTypeId", "");
        homePageFilteringValues.put("applicationEmaNumber", "");
        homePageFilteringValues.put("piVersionImportDate", "");
        homePageFiltering = new Filtering(homePageFilteringValues);
    }

    /**
     * @return the applicantProductListFiltering
     */
    public Filtering getApplicantProductListFiltering() {

        return applicantProductListFiltering;
    }

    /**
     * @param applicantProductListFiltering
     *            the applicantProductListFiltering to set
     */
    public void setApplicantProductListFiltering(
            Filtering applicantProductListFiltering) {

        this.applicantProductListFiltering = applicantProductListFiltering;
    }

    /**
     * @return the applicantProductListSorting
     */
    public Sorting getApplicantProductListSorting() {

        return applicantProductListSorting;
    }

    /**
     * @param applicantProductListSorting
     *            the applicantProductListSorting to set
     */
    public void setApplicantProductListSorting(
            Sorting applicantProductListSorting) {

        this.applicantProductListSorting = applicantProductListSorting;
    }

    /**
     * @return the homePageSorting
     */
    public Sorting getHomePageSorting() {

        return homePageSorting;
    }

    /**
     * @param homePageSorting
     *            the homePageSorting to set
     */
    public void setHomePageSorting(Sorting homePageSorting) {

        this.homePageSorting = homePageSorting;
    }

    /**
     * @return the homePageFiltering
     */
    public Filtering getHomePageFiltering() {

        return homePageFiltering;
    }

    /**
     * @param homePageFiltering
     *            the homePageFiltering to set
     */
    public void setHomePageFiltering(Filtering homePageFiltering) {

        this.homePageFiltering = homePageFiltering;
    }

    /**
     * For convenience to view in the Seam Debug page.
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder("");
        sb.append("\n\n");
        sb.append("applicantProductListSorting");
        sb.append(applicantProductListSorting);
        sb.append("\n\n");
        sb.append("applicantProductListFiltering");
        sb.append(applicantProductListFiltering);
        sb.append("\n\n");
        sb.append("homePageSorting");
        sb.append(homePageSorting);
        sb.append("\n\n");
        sb.append("homePageFiltering");
        sb.append(homePageFiltering);
        return sb.toString();
    }
}

这是BaseManagedBean,继承了AbstractMutable。

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ArrayUtils;
import org.jboss.seam.core.AbstractMutable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.myproj.common.exceptions.WebException;
import com.myproj.common.util.FileUtils;
import com.myproj.common.util.StringUtils;
import com.myproj.web.messages.Messages;

public abstract class BaseManagedBean extends AbstractMutable {

    private static final Logger logger = LoggerFactory
            .getLogger(BaseManagedBean.class);
    private FacesContext facesContext;

    /**
     * Set a message to be displayed for a specific component.
     * 
     * @param resourceBundle
     *            the resource bundle where the message appears. Either base or
     *            id may be used.
     * @param summaryResourceId
     *            the id of the resource to be used as summary. For the detail
     *            of the element, the element to be used will be the same with
     *            the suffix {@code _detail}.
     * @param parameters
     *            the parameters, in case the string is parameterizable
     * @param severity
     *            the severity of the message
     * @param componentId
     *            the component id for which the message is destined. Note that
     *            an appropriate JSF {@code <h:message for="myComponentId">} tag
     *            is required for the to appear, or alternatively a {@code
     *            <h:messages>} tag.
     */
    protected void setMessage(String resourceBundle, String summaryResourceId,
            List<Object> parameters, Severity severity, String componentId,
            Messages messages) {

        FacesContext context = getFacesContext();
        FacesMessage message = messages.getMessage(resourceBundle,
                summaryResourceId, parameters);
        if (severity != null) {
            message.setSeverity(severity);
        }
        context.addMessage(componentId, message);
    }

    /**
     * Copies a byte array to the response output stream with the appropriate
     * MIME type and content disposition. The response output stream is closed
     * after this method.
     * 
     * @param response
     *            the HTTP response
     * @param bytes
     *            the data
     * @param filename
     *            the suggested file name for the client
     * @param mimeType
     *            the MIME type; will be overridden if the filename suggests a
     *            different MIME type
     * @throws IllegalArgumentException
     *             if the data array is <code>null</code>/empty or both filename
     *             and mimeType are <code>null</code>/empty
     */
    protected void printBytesToResponse(HttpServletResponse response,
            byte[] bytes, String filename, String mimeType)
            throws WebException, IllegalArgumentException {

        if (response.isCommitted()) {
            throw new WebException("HTTP response is already committed");
        }
        if (ArrayUtils.isEmpty(bytes)) {
            throw new IllegalArgumentException("Data buffer is empty");
        }
        if (StringUtils.isEmpty(filename) && StringUtils.isEmpty(mimeType)) {
            throw new IllegalArgumentException(
                    "Filename and MIME type are both null/empty");
        }
        // Set content type (mime type)
        String calculatedMimeType = FileUtils.getMimeType(filename);
        // not among the known ones
        String newMimeType = mimeType;
        if (calculatedMimeType == null) {
            // given mime type passed
            if (mimeType == null) {
                // none available put default mime-type
                newMimeType = "application/download";
            } else {
                if ("application/octet-stream".equals(mimeType)) {
                    // small modification
                    newMimeType = "application/download";
                }
            }
        } else { // calculated mime type has precedence over given mime type
            newMimeType = calculatedMimeType;
        }
        response.setContentType(newMimeType);
        // Set content disposition and other headers
        String contentDisposition = "attachment;filename=\"" + filename + "\"";
        response.setHeader("Content-Disposition", contentDisposition);
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "max-age=30");
        response.setHeader("Pragma", "public");
        // Set content length
        response.setContentLength(bytes.length);
        // Write bytes to response
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            out.write(bytes);
        } catch (IOException e) {
            throw new WebException("Error writing data to HTTP response", e);
        } finally {
            try {
                out.close();
            } catch (Exception e) {
                logger.error("Error closing HTTP stream", e);
            }
        }
    }

    /**
     * Retrieve a session-scoped managed bean.
     * 
     * @param sessionBeanName
     *            the session-scoped managed bean name
     * @return the session-scoped managed bean
     */
    protected Object getSessionBean(String sessionBeanName) {

        Object sessionScopedBean = FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap().get(sessionBeanName);
        if (sessionScopedBean == null) {
            throw new IllegalArgumentException("No such object in Session");
        } else {
            return sessionScopedBean;
        }
    }

    /**
     * Set a session-scoped managed bean
     * 
     * @param sessionBeanName
     *            the session-scoped managed bean name
     * @return the session-scoped managed bean
     */
    protected boolean setSessionBean(String sessionBeanName, Object sessionBean) {

        Object sessionScopedBean = FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap().get(sessionBeanName);
        if (sessionScopedBean == null) {
            FacesContext.getCurrentInstance().getExternalContext()
                    .getSessionMap().put(sessionBeanName, sessionBean);
        } else {
            throw new IllegalArgumentException(
                    "This session-scoped bean was already initialized");
        }
        return true;
    }

    /**
     * For testing (enables mock of FacesContext)
     * 
     * @return the faces context
     */
    public FacesContext getFacesContext() {

        if (facesContext == null) {
            return FacesContext.getCurrentInstance();
        }
        return facesContext;
    }

    /**
     * For testing (enables mocking of FacesContext).
     * 
     * @param aFacesContext
     *            a - possibly mock - faces context.
     */
    public void setFacesContext(FacesContext aFacesContext) {

        this.facesContext = aFacesContext;
    }
}

I deploy an application on a WebLogic 10.3.2 cluster with two nodes, and a load balancer in front of the cluster.

I have set the

<core:init distributable="true" debug="true" />

My Session and Conversation classes implement Serializable.

I start using the application being served by the first node. The console shows that the session replication is working.

<Jun 17, 2010 11:43:50 AM EEST> <Info> <Cluster> <BEA-000128> <Updating  5903057688359791237S:xxx.yyy.gr:[7002,7002,-1,-1,-1,-1,-1]:xxx.yyy.gr:7002,xxx.yyy.gr:7002:prs_domain:PRS_Server_2 in the cluster.>
<Jun 17, 2010 11:43:50 AM EEST> <Info> <Cluster> <BEA-000128> <Updating 5903057688359791237S:xxx.yyy.gr:[7002,7002,-1,-1,-1,-1,-1]:xxx.yyy.gr:7002,xxx.yyy.gr:7002:prs_domain:PRS_Server_2 in the cluster.>

When I shutdown the first node from the Administration console, I get this in the other node:

<Jun 17, 2010 11:23:46 AM EEST> <Error> <Kernel> <BEA-000802> <ExecuteRequest failed
 java.lang.NullPointerException.
java.lang.NullPointerException
        at org.jboss.seam.intercept.JavaBeanInterceptor.callPostActivate(JavaBeanInterceptor.java:165)
        at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:73)
        at com.myproj.beans.SortingFilteringBean_$_javassist_seam_2.sessionDidActivate(SortingFilteringBean_$_javassist_seam_2.java)
        at weblogic.servlet.internal.session.SessionData.notifyActivated(SessionData.java:2258)
        at weblogic.servlet.internal.session.SessionData.notifyActivated(SessionData.java:2222)
        at weblogic.servlet.internal.session.ReplicatedSessionData.becomePrimary(ReplicatedSessionData.java:231)
        at weblogic.cluster.replication.WrappedRO.changeStatus(WrappedRO.java:142)
        at weblogic.cluster.replication.WrappedRO.ensureStatus(WrappedRO.java:129)
        at weblogic.cluster.replication.LocalSecondarySelector$ChangeSecondaryInfo.run(LocalSecondarySelector.java:542)
        at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:516)
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
>

What am I doing wrong?

This is the SortingFilteringBean:

import java.util.HashMap;
import java.util.LinkedHashMap;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.myproj.model.crud.Filtering;
import com.myproj.model.crud.Sorting;
import com.myproj.model.crud.SortingOrder;

/**
 * Managed bean aggregating the sorting and filtering values for all the
 * application's lists. A light-weight bean to always keep in the session with
 * minimum impact.
 */
@Name("sortingFilteringBean")
@Scope(ScopeType.SESSION)
public class SortingFilteringBean extends BaseManagedBean {

    private static final long serialVersionUID = 1L;
    private Sorting applicantProductListSorting;
    private Filtering applicantProductListFiltering;
    private Sorting homePageSorting;
    private Filtering homePageFiltering;

    /**
     * Creates a new instance of SortingFilteringBean.
     */
    public SortingFilteringBean() {

        // **********************
        // Applicant Product List
        // **********************
        // Sorting
        LinkedHashMap<String, SortingOrder> applicantProductListSortingValues = new LinkedHashMap<String, SortingOrder>();
        applicantProductListSortingValues.put("applicantName",
                SortingOrder.ASCENDING);
        applicantProductListSortingValues.put("applicantEmail",
                SortingOrder.ASCENDING);
        applicantProductListSortingValues.put("productName",
                SortingOrder.ASCENDING);
        applicantProductListSortingValues.put("productEmail",
                SortingOrder.ASCENDING);
        applicantProductListSorting = new Sorting(
                applicantProductListSortingValues);
        // Filtering
        HashMap<String, String> applicantProductListFilteringValues = new HashMap<String, String>();
        applicantProductListFilteringValues.put("applicantName", "");
        applicantProductListFilteringValues.put("applicantEmail", "");
        applicantProductListFilteringValues.put("productName", "");
        applicantProductListFilteringValues.put("productEmail", "");
        applicantProductListFiltering = new Filtering(
                applicantProductListFilteringValues);
        // *********
        // Home page
        // *********
        // Sorting
        LinkedHashMap<String, SortingOrder> homePageSortingValues = new LinkedHashMap<String, SortingOrder>();
        homePageSortingValues.put("productName", SortingOrder.ASCENDING);
        homePageSortingValues.put("productId", SortingOrder.ASCENDING);
        homePageSortingValues.put("productAtcCode", SortingOrder.UNSORTED);
        homePageSortingValues.put("productEmaNumber", SortingOrder.UNSORTED);
        homePageSortingValues.put("productOrphan", SortingOrder.UNSORTED);
        homePageSortingValues.put("productRap", SortingOrder.UNSORTED);
        homePageSortingValues.put("productCorap", SortingOrder.UNSORTED);
        homePageSortingValues.put("applicationTypeDescription",
                SortingOrder.ASCENDING);
        homePageSortingValues.put("applicationId", SortingOrder.ASCENDING);
        homePageSortingValues
                .put("applicationEmaNumber", SortingOrder.UNSORTED);
        homePageSortingValues
                .put("piVersionImportDate", SortingOrder.ASCENDING);
        homePageSortingValues.put("piVersionId", SortingOrder.ASCENDING);
        homePageSorting = new Sorting(homePageSortingValues);
        // Filtering
        HashMap<String, String> homePageFilteringValues = new HashMap<String, String>();
        homePageFilteringValues.put("productName", "");
        homePageFilteringValues.put("productAtcCode", "");
        homePageFilteringValues.put("productEmaNumber", "");
        homePageFilteringValues.put("applicationTypeId", "");
        homePageFilteringValues.put("applicationEmaNumber", "");
        homePageFilteringValues.put("piVersionImportDate", "");
        homePageFiltering = new Filtering(homePageFilteringValues);
    }

    /**
     * @return the applicantProductListFiltering
     */
    public Filtering getApplicantProductListFiltering() {

        return applicantProductListFiltering;
    }

    /**
     * @param applicantProductListFiltering
     *            the applicantProductListFiltering to set
     */
    public void setApplicantProductListFiltering(
            Filtering applicantProductListFiltering) {

        this.applicantProductListFiltering = applicantProductListFiltering;
    }

    /**
     * @return the applicantProductListSorting
     */
    public Sorting getApplicantProductListSorting() {

        return applicantProductListSorting;
    }

    /**
     * @param applicantProductListSorting
     *            the applicantProductListSorting to set
     */
    public void setApplicantProductListSorting(
            Sorting applicantProductListSorting) {

        this.applicantProductListSorting = applicantProductListSorting;
    }

    /**
     * @return the homePageSorting
     */
    public Sorting getHomePageSorting() {

        return homePageSorting;
    }

    /**
     * @param homePageSorting
     *            the homePageSorting to set
     */
    public void setHomePageSorting(Sorting homePageSorting) {

        this.homePageSorting = homePageSorting;
    }

    /**
     * @return the homePageFiltering
     */
    public Filtering getHomePageFiltering() {

        return homePageFiltering;
    }

    /**
     * @param homePageFiltering
     *            the homePageFiltering to set
     */
    public void setHomePageFiltering(Filtering homePageFiltering) {

        this.homePageFiltering = homePageFiltering;
    }

    /**
     * For convenience to view in the Seam Debug page.
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder("");
        sb.append("\n\n");
        sb.append("applicantProductListSorting");
        sb.append(applicantProductListSorting);
        sb.append("\n\n");
        sb.append("applicantProductListFiltering");
        sb.append(applicantProductListFiltering);
        sb.append("\n\n");
        sb.append("homePageSorting");
        sb.append(homePageSorting);
        sb.append("\n\n");
        sb.append("homePageFiltering");
        sb.append(homePageFiltering);
        return sb.toString();
    }
}

And this is the BaseManagedBean, inheriting the AbstractMutable.

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ArrayUtils;
import org.jboss.seam.core.AbstractMutable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.myproj.common.exceptions.WebException;
import com.myproj.common.util.FileUtils;
import com.myproj.common.util.StringUtils;
import com.myproj.web.messages.Messages;

public abstract class BaseManagedBean extends AbstractMutable {

    private static final Logger logger = LoggerFactory
            .getLogger(BaseManagedBean.class);
    private FacesContext facesContext;

    /**
     * Set a message to be displayed for a specific component.
     * 
     * @param resourceBundle
     *            the resource bundle where the message appears. Either base or
     *            id may be used.
     * @param summaryResourceId
     *            the id of the resource to be used as summary. For the detail
     *            of the element, the element to be used will be the same with
     *            the suffix {@code _detail}.
     * @param parameters
     *            the parameters, in case the string is parameterizable
     * @param severity
     *            the severity of the message
     * @param componentId
     *            the component id for which the message is destined. Note that
     *            an appropriate JSF {@code <h:message for="myComponentId">} tag
     *            is required for the to appear, or alternatively a {@code
     *            <h:messages>} tag.
     */
    protected void setMessage(String resourceBundle, String summaryResourceId,
            List<Object> parameters, Severity severity, String componentId,
            Messages messages) {

        FacesContext context = getFacesContext();
        FacesMessage message = messages.getMessage(resourceBundle,
                summaryResourceId, parameters);
        if (severity != null) {
            message.setSeverity(severity);
        }
        context.addMessage(componentId, message);
    }

    /**
     * Copies a byte array to the response output stream with the appropriate
     * MIME type and content disposition. The response output stream is closed
     * after this method.
     * 
     * @param response
     *            the HTTP response
     * @param bytes
     *            the data
     * @param filename
     *            the suggested file name for the client
     * @param mimeType
     *            the MIME type; will be overridden if the filename suggests a
     *            different MIME type
     * @throws IllegalArgumentException
     *             if the data array is <code>null</code>/empty or both filename
     *             and mimeType are <code>null</code>/empty
     */
    protected void printBytesToResponse(HttpServletResponse response,
            byte[] bytes, String filename, String mimeType)
            throws WebException, IllegalArgumentException {

        if (response.isCommitted()) {
            throw new WebException("HTTP response is already committed");
        }
        if (ArrayUtils.isEmpty(bytes)) {
            throw new IllegalArgumentException("Data buffer is empty");
        }
        if (StringUtils.isEmpty(filename) && StringUtils.isEmpty(mimeType)) {
            throw new IllegalArgumentException(
                    "Filename and MIME type are both null/empty");
        }
        // Set content type (mime type)
        String calculatedMimeType = FileUtils.getMimeType(filename);
        // not among the known ones
        String newMimeType = mimeType;
        if (calculatedMimeType == null) {
            // given mime type passed
            if (mimeType == null) {
                // none available put default mime-type
                newMimeType = "application/download";
            } else {
                if ("application/octet-stream".equals(mimeType)) {
                    // small modification
                    newMimeType = "application/download";
                }
            }
        } else { // calculated mime type has precedence over given mime type
            newMimeType = calculatedMimeType;
        }
        response.setContentType(newMimeType);
        // Set content disposition and other headers
        String contentDisposition = "attachment;filename=\"" + filename + "\"";
        response.setHeader("Content-Disposition", contentDisposition);
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "max-age=30");
        response.setHeader("Pragma", "public");
        // Set content length
        response.setContentLength(bytes.length);
        // Write bytes to response
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            out.write(bytes);
        } catch (IOException e) {
            throw new WebException("Error writing data to HTTP response", e);
        } finally {
            try {
                out.close();
            } catch (Exception e) {
                logger.error("Error closing HTTP stream", e);
            }
        }
    }

    /**
     * Retrieve a session-scoped managed bean.
     * 
     * @param sessionBeanName
     *            the session-scoped managed bean name
     * @return the session-scoped managed bean
     */
    protected Object getSessionBean(String sessionBeanName) {

        Object sessionScopedBean = FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap().get(sessionBeanName);
        if (sessionScopedBean == null) {
            throw new IllegalArgumentException("No such object in Session");
        } else {
            return sessionScopedBean;
        }
    }

    /**
     * Set a session-scoped managed bean
     * 
     * @param sessionBeanName
     *            the session-scoped managed bean name
     * @return the session-scoped managed bean
     */
    protected boolean setSessionBean(String sessionBeanName, Object sessionBean) {

        Object sessionScopedBean = FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap().get(sessionBeanName);
        if (sessionScopedBean == null) {
            FacesContext.getCurrentInstance().getExternalContext()
                    .getSessionMap().put(sessionBeanName, sessionBean);
        } else {
            throw new IllegalArgumentException(
                    "This session-scoped bean was already initialized");
        }
        return true;
    }

    /**
     * For testing (enables mock of FacesContext)
     * 
     * @return the faces context
     */
    public FacesContext getFacesContext() {

        if (facesContext == null) {
            return FacesContext.getCurrentInstance();
        }
        return facesContext;
    }

    /**
     * For testing (enables mocking of FacesContext).
     * 
     * @param aFacesContext
     *            a - possibly mock - faces context.
     */
    public void setFacesContext(FacesContext aFacesContext) {

        this.facesContext = aFacesContext;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文