[jboss-cvs] jboss-seam/src/main/org/jboss/seam/remoting ...

Shane Bryzak Shane_Bryzak at symantec.com
Sun Aug 6 06:11:48 EDT 2006


  User: sbryzak2
  Date: 06/08/06 06:11:48

  Modified:    src/main/org/jboss/seam/remoting   SeamRemotingServlet.java
  Added:       src/main/org/jboss/seam/remoting   RemotingConfig.java
  Log:
  JBSEAM-281
  
  Revision  Changes    Path
  1.10      +86 -13    jboss-seam/src/main/org/jboss/seam/remoting/SeamRemotingServlet.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: SeamRemotingServlet.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/remoting/SeamRemotingServlet.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -b -r1.9 -r1.10
  --- SeamRemotingServlet.java	7 Jun 2006 22:10:27 -0000	1.9
  +++ SeamRemotingServlet.java	6 Aug 2006 10:11:48 -0000	1.10
  @@ -3,6 +3,8 @@
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.OutputStream;
  +import java.util.HashMap;
  +import java.util.Map;
   import java.util.regex.Matcher;
   import java.util.regex.Pattern;
   import javax.servlet.ServletConfig;
  @@ -14,7 +16,10 @@
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  +import org.jboss.seam.contexts.Lifecycle;
   import org.jboss.seam.servlet.SeamServletFilter;
  +import javax.servlet.http.HttpSession;
  +import javax.servlet.ServletRequest;
   
   /**
    * Provides remoting capabilities for Seam.
  @@ -33,7 +38,20 @@
   
     private ServletContext servletContext;
   
  -  public void init(ServletConfig config) throws ServletException
  +  /**
  +   *  We use a Map for this because a Servlet can serve
  +   *  requests for more than one context path.
  +   */
  +  private Map<String,byte[]> cachedConfig = new HashMap<String,byte[]>();
  +
  +  /**
  +   * Initialise the Remoting servlet
  +   *
  +   * @param config ServletConfig
  +   * @throws ServletException
  +   */
  +  public void init(ServletConfig config)
  +      throws ServletException
     {
       servletContext = config.getServletContext();
     }
  @@ -44,12 +62,14 @@
       doPost(request, response);
     }
   
  -  protected void doPost(HttpServletRequest request, HttpServletResponse response)
  +  protected void doPost(HttpServletRequest request,
  +                        HttpServletResponse response)
         throws ServletException, IOException
     {
       try
       {
  -      RequestHandler handler = RequestHandlerFactory.getInstance().getRequestHandler(request.getPathInfo());
  +      RequestHandler handler = RequestHandlerFactory.getInstance().
  +          getRequestHandler(request.getPathInfo());
         if (handler != null)
         {
           handler.setServletContext(servletContext);
  @@ -58,19 +78,18 @@
         else
         {
           Matcher m = pathPattern.matcher(request.getPathInfo());
  -        if (m.matches()) {
  +        if (m.matches())
  +        {
             String path = m.group(1);
             String resource = m.group(2);
   
  -          if (RESOURCE_PATH.equals(path)) {
  +          if (RESOURCE_PATH.equals(path))
  +          {
               writeResource(resource, response.getOutputStream());
  -            if ("remote.js".equals(resource)) {
  -              response.getOutputStream().write("\nSeam.Remoting.contextPath = \"".
  -                                               getBytes());
  -              response.getOutputStream().write(request.getContextPath().
  -                                               getBytes());
  -              response.getOutputStream().write("\";".getBytes());
  -              response.getOutputStream().flush();
  +            if ("remote.js".equals(resource))
  +            {
  +              appendConfig(response.getOutputStream(), request.getContextPath(),
  +                  request.getSession(), request);
               }
             }
           }
  @@ -83,6 +102,59 @@
     }
   
     /**
  +   * Appends various configuration options to the remoting javascript client api.
  +   *
  +   * @param out OutputStream
  +   */
  +  private void appendConfig(OutputStream out, String contextPath,
  +                            HttpSession session, ServletRequest request)
  +      throws IOException
  +  {
  +    if (!cachedConfig.containsKey(contextPath))
  +      initConfig(contextPath, session, request);
  +
  +    out.write(cachedConfig.get(contextPath));
  +    out.flush();
  +  }
  +
  +  /**
  +   * Initialise the configuration stuff for the specified context path.
  +   *
  +   * @param contextPath String
  +   */
  +  private synchronized void initConfig(String contextPath, HttpSession session,
  +                                       ServletRequest request)
  +  {
  +    if (!cachedConfig.containsKey(contextPath))
  +    {
  +      try
  +      {
  +        Lifecycle.beginRequest(servletContext, session, request);
  +
  +        StringBuilder sb = new StringBuilder();
  +        sb.append("\nSeam.Remoting.contextPath = \"");
  +        sb.append(contextPath);
  +        sb.append("\";");
  +        sb.append("\nSeam.Remoting.debug = ");
  +        sb.append(RemotingConfig.instance().getDebug() ? "true" : "false");
  +        sb.append(";");
  +        sb.append("\nSeam.Remoting.pollInterval = ");
  +        sb.append(RemotingConfig.instance().getPollInterval());
  +        sb.append(";");
  +        sb.append("\nSeam.Remoting.pollTimeout = ");
  +        sb.append(RemotingConfig.instance().getPollTimeout());
  +        sb.append(";");
  +
  +        cachedConfig.put(contextPath, sb.toString().getBytes());
  +      }
  +      finally
  +      {
  +        Lifecycle.endRequest(session);
  +      }
  +    }
  +  }
  +
  +  /**
      *
      * @param resourceName String
      * @param out OutputStream
  @@ -100,7 +172,8 @@
         {
           byte[] buffer = new byte[1024];
           int read = in.read(buffer);
  -        while (read != -1) {
  +        while (read != -1)
  +        {
             out.write(buffer, 0, read);
             read = in.read(buffer);
             out.flush();
  
  
  
  1.1      date: 2006/08/06 10:11:48;  author: sbryzak2;  state: Exp;jboss-seam/src/main/org/jboss/seam/remoting/RemotingConfig.java
  
  Index: RemotingConfig.java
  ===================================================================
  package org.jboss.seam.remoting;
  
  import static org.jboss.seam.ScopeType.APPLICATION;
  import org.jboss.seam.Component;
  import org.jboss.seam.ScopeType;
  import org.jboss.seam.annotations.Name;
  import org.jboss.seam.annotations.Scope;
  import org.jboss.seam.contexts.Contexts;
  
  /**
   * Various configuration options for Seam Remoting
   *
   * @author Shane Bryzak
   */
  @Name("org.jboss.seam.remoting.remotingConfig")
  @Scope(APPLICATION)
  public class RemotingConfig
  {
    public static final int DEFAULT_POLL_TIMEOUT = 10; // 10 seconds
    public static final int DEFAULT_POLL_INTERVAL = 1; // 1 second
  
    private int pollTimeout;
    private int pollInterval;
    private boolean debug;
  
    public static RemotingConfig instance()
    {
      if (!Contexts.isApplicationContextActive())
         throw new IllegalStateException("No active application context");
  
      RemotingConfig instance = (RemotingConfig) Component.getInstance(
          RemotingConfig.class, ScopeType.APPLICATION, true);
  
      if (instance==null)
      {
        throw new IllegalStateException(
            "No RemotingConfig could be created, make sure the Component exists in application scope");
      }
  
      return instance;
    }
  
    public RemotingConfig()
    {
      pollTimeout = DEFAULT_POLL_TIMEOUT;
      pollInterval = DEFAULT_POLL_INTERVAL;
      debug = false;
    }
  
    public int getPollTimeout()
    {
      return pollTimeout;
    }
  
    public void setPollTimeout(int pollTimeout)
    {
      this.pollTimeout = pollTimeout;
    }
  
    public int getPollInterval()
    {
      return pollInterval;
    }
  
    public void setPollInterval(int pollInterval)
    {
      this.pollInterval = pollInterval;
    }
  
    public boolean getDebug()
    {
      return debug;
    }
  
    public void setDebug(boolean debug)
    {
      this.debug = debug;
    }
  }
  
  
  



More information about the jboss-cvs-commits mailing list