RedeployableActionServlet xref

View Javadoc

    1   /*
    2    * $Id: RedeployableActionServlet.java 481833 2006-12-03 17:32:52Z niallp $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts.tiles;
    22
    23  import javax.servlet.ServletException;
    24
    25  import org.apache.struts.Globals;
    26  import org.apache.struts.action.ActionServlet;
    27  import org.apache.struts.action.RequestProcessor;
    28  import org.apache.struts.config.ModuleConfig;
    29  import org.apache.struts.tiles.DefinitionsFactory;
    30  import org.apache.struts.tiles.DefinitionsFactoryException;
    31  import org.apache.struts.tiles.TilesRequestProcessor;
    32
    33
    34  /**
    35   * <p>
    36   * WebLogic (at least v6 and v7) attempts to serialize the TilesRequestProcessor
    37   * when re-deploying the Webapp in development mode. The TilesRequestProcessor
    38   * is not serializable, and loses the Tiles definitions. This results in
    39   * NullPointerException and/or NotSerializableException when using the app after
    40   * automatic redeploy.
    41   * </p>
    42   * <p>
    43   * This bug report proposes a workaround for this problem, in the hope it will
    44   * help others and maybe motivate an actual fix.
    45   * </p>
    46   * <p>
    47   * The attached class extends the Struts Action servlet and fixes the problem by
    48   * reloading the Tiles definitions when they have disappeared.
    49   * </p>
    50   * <p>
    51   * For background discussion see
    52   * http://issues.apache.org/bugzilla/show_bug.cgi?id=26322
    53   * </p>
    54   * @version $Rev: 481833 $ $Date: 2006-12-03 11:32:52 -0600 (Sun, 03 Dec 2006) $
    55   * @since 1.2.1
    56   */
    57  public class RedeployableActionServlet extends ActionServlet {
    58      private TilesRequestProcessor tileProcessor;
    59
    60      protected synchronized RequestProcessor
    61              getRequestProcessor(ModuleConfig config) throws ServletException {
    62
    63          if (tileProcessor != null) {
    64              TilesRequestProcessor processor = (TilesRequestProcessor) super.getRequestProcessor(config);
    65              return processor;
    66          }
    67
    68          // reset the request processor
    69          String requestProcessorKey = Globals.REQUEST_PROCESSOR_KEY +
    70                  config.getPrefix();
    71          getServletContext().removeAttribute(requestProcessorKey);
    72
    73          // create a new request processor instance
    74          TilesRequestProcessor processor = (TilesRequestProcessor) super.getRequestProcessor(config);
    75
    76          tileProcessor = processor;
    77
    78          try {
    79              // reload Tiles defs
    80              DefinitionsFactory factory = processor.getDefinitionsFactory();
    81              factory.init(factory.getConfig(), getServletContext());
    82              // System.out.println("reloaded tiles-definitions");
    83          } catch (DefinitionsFactoryException e) {
    84              e.printStackTrace();
    85          }
    86
    87          return processor;
    88      }
    89  }