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

📄 invokerservlet.java

📁 Java/J2EE框架Jdon-Framework系统的Sample
💻 JAVA
字号:
/**
 * Copyright 2005 Jdon.com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */

package com.jdon.bussinessproxy.remote.http;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.*;

import com.jdon.bussinessproxy.meta.EJBTargetMetaDef;

import com.jdon.util.Debug;

import com.jdon.bussinessproxy.*;

import com.jdon.bussinessproxy.meta.MethodMetaArgs;
import com.jdon.util.StringUtil;
import java.util.Collection;
import java.util.ArrayList;
import javax.servlet.ServletContext;
import com.jdon.controller.ContainerSetupScript;
import com.jdon.controller.WebAppUtil;
import com.jdon.controller.ContainerClient;

/**
 * <p>Title: </p>
 * <p>Description:
 * 本类是服务器端处理远程客户端发送的调用EJB请求。
 *
 * 本类功能也可以使用Struts的Action实现。
 * 权限验证是使用J2EE的基于Http的Basic Auth.
 *
 * 使用本类可以作为一个单独的EJB网关服务器。
 *
 * </p>
 * <p>Copyright: Jdon.com Copyright (c) 2003</p>
 * <p></p>
 * @author banq
 * @version 1.0
 */
public class InvokerServlet extends HttpServlet {

  public final static String module = InvokerServlet.class.getName();
  private ContainerSetupScript css = new ContainerSetupScript();
  private ContainerClient containerClient = new ContainerClient();
  /**
   * 使用Picocontainer
   * 将jdonframework.xml配置在web.xml中即可
   *
   * @throws ServletException
   */
  public void init() throws ServletException {
     String configList = this.getServletConfig().getInitParameter("configList");
     String[] configs = StringUtil.split(configList, ",");
     Collection listConfigs = new ArrayList(configs.length);
     for(int i = 0 ; i < configs.length; i ++){
       Debug.logVerbose(" found config in web.xml :" + configs[i], module);
       listConfigs.add(configs[i]);
     }
     if (listConfigs.isEmpty()){
       String initError = "please init jdonframework in InvokerServlet in web.xml";
       Debug.logError(initError, module);
       throw new ServletException();
     }

     ServletContext sc = this.getServletContext();

     Debug.logInfo(" JdonSD framework begin to init..", module);
     css.prepare(listConfigs, sc);
     Debug.logInfo(" JdonSD  Framework init successfully!", module);
  }

  public void destroy() {
    css.destroyed(this.getServletContext());
  }

  protected void doGet(HttpServletRequest request,
                       HttpServletResponse response) throws ServletException,
      IOException {
    response.setContentType("text/html");
    PrintWriter out = new PrintWriter(response.getOutputStream());
    out.println("<html>");
    out.println("<head><title>InvokerServlet</title></head>");
    out.println("<body> ");
    out.println(request.getRemoteUser());
    out.println(" Welcome to login in !!</body></html>");
    out.close();
  }

  protected void doPost(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException,
      IOException {
    if (request.getParameter("login") != null) {
      //如果是login 显示欢迎信息或其它更新通知等信息
      doGet(request, response);
      return;
    }
    //从HttpServletRequest中获得传送的对象
    HttpRequest httpRequest = getHttpServiceRequest(request);
    HttpResponse httpResponse = null;
    try {
      EJBTargetMetaDef eJBMetaDef = httpRequest.getEJBMetaDef();
      String p_methodName = httpRequest.getMethodName();
      Class[] paramTypes = httpRequest.getParamTypes();
      Object[] p_args = httpRequest.getArgs();

      //将上述参数打包到eJBMetaDef中
      MethodMetaArgs methodMetaArgs = new MethodMetaArgs(p_methodName,
          paramTypes, p_args);
      Object object = containerClient.visitEJBService(eJBMetaDef,
                                                 methodMetaArgs,
                                                 request);
      httpResponse = new HttpResponse(object);
    } catch (Exception e) {
      Debug.logError(e, module);
      httpResponse = new HttpResponse(new Throwable(e));
    } catch (Throwable te) {
      Debug.logError(te, module);
      httpResponse = new HttpResponse(te);
    }

    //Write the result in the http stream
    HttpSession session = request.getSession(true);
    response.addHeader("jsessionid", session.getId());
    writeHttpServiceResponse(response, httpResponse);
  }

  /**
   * 序列化处理结果
   */
  private void writeHttpServiceResponse(HttpServletResponse response,
                                        HttpResponse httpResponse) throws
      IOException {
    OutputStream outputStream = response.getOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(outputStream);
    oos.writeObject(httpResponse);
    oos.close();
  }

  /**
   * 从请求信息中反序列化
   */
  private HttpRequest getHttpServiceRequest(HttpServletRequest request) throws
      IOException {
    ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
    HttpRequest httpServiceRequest = null;
    try {
      httpServiceRequest = (HttpRequest) ois.readObject();
    } catch (ClassNotFoundException e) {
      Debug.logError(e, module);
    }
    ois.close();
    return httpServiceRequest;
  }

}

⌨️ 快捷键说明

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