View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  package org.apache.myfaces.orchestra.frameworkAdapter.local;
    20
    21  import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
    22  import org.springframework.context.ApplicationContextAware;
    23  import org.springframework.context.ConfigurableApplicationContext;
    24  import org.springframework.context.ApplicationContext;
    25  import org.springframework.beans.BeansException;
    26
    27  import java.io.IOException;
    28  import java.util.HashMap;
    29  import java.util.Map;
    30
    31  /**
    32   * A FrameworkAdapter which uses local maps to simulate a servlet environment.
    33   * <p>
    34   * This is intended when Orchestra functionality is desired outside of any real
    35   * request/response type system. One example is where a job scheduler (eg quartz)
    36   * is used to execute background threads that want to execute the same beans that
    37   * can also be used. In this case:
    38   * <ol>
    39   * <li>Create a new Thread object
    40   * <li>Create a new LocalFrameworkAdapter instance
    41   * <li>Within the new thread, call FrameworkAdapter.setInstance.
    42   * </ol>
    43   * When Orchestra methods are invoked from within the new thread, calls made to the
    44   * framework adapter will be handled by the created LocalFrameworkAdapter instance,
    45   * and will not interact with the user session, the original request or response.
    46   * Because the session is new, no conversations will be inherited from the environment
    47   * in which that new thread was created.
    48   * <p>
    49   * This class is not expected to be used very often.
    50   * <p>
    51   * Note that this adapter relies on Spring and thus you have to ensure
    52   * {@link #setApplicationContext} is called.
    53   * <p>
    54   * Note also that because this is intended for use only without a real request
    55   * and response, there is no corresponding Filter class.
    56   * <p>
    57   * This class does not override the inherited createConversationMessager method, so
    58   * it is mandatory for anyone using this class to explicitly call method
    59   * setConversationMessager before using an instance of this adapter.
    60   */
    61  public class LocalFrameworkAdapter extends FrameworkAdapter implements ApplicationContextAware
    62  {
    63      private ConfigurableApplicationContext configurableApplicationContext;
    64
    65      private final Map sessionMap = new HashMap();
    66      private final Map requestMap = new HashMap();
    67      private final Map requestParameterMap = new HashMap();
    68      private final Map initMap = new HashMap();
    69
    70      public String getInitParameter(String key)
    71      {
    72          return (String) initMap.get(key);
    73      }
    74
    75      public Object getRequestParameterAttribute(String key)
    76      {
    77          return requestParameterMap.get(key);
    78      }
    79
    80      public boolean containsRequestParameterAttribute(String key)
    81      {
    82          return requestParameterMap.containsKey(key);
    83      }
    84
    85      public void setRequestParameterAttribute(String key , Object value)
    86      {
    87          requestParameterMap.put(key, value);
    88      }
    89
    90      public Object getRequestAttribute(String key)
    91      {
    92          return requestMap.get(key);
    93      }
    94
    95      public void setRequestAttribute(String key, Object value)
    96      {
    97          requestMap.put(key, value);
    98      }
    99
    100     public void clearRequestMap()
    101     {
    102         requestMap.clear();
    103     }
    104
    105     public boolean containsRequestAttribute(String key)
    106     {
    107         return requestMap.containsKey(key);
    108     }
    109
    110     public Object getSessionAttribute(String key)
    111     {
    112         return sessionMap.get(key);
    113     }
    114
    115     public void setSessionAttribute(String key, Object value)
    116     {
    117         sessionMap.put(key, value);
    118     }
    119
    120     public boolean containsSessionAttribute(String key)
    121     {
    122         return sessionMap.containsKey(key);
    123     }
    124
    125     protected ConfigurableApplicationContext getApplicationContext()
    126     {
    127         return configurableApplicationContext;
    128     }
    129
    130     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    131     {
    132         setApplicationContext((ConfigurableApplicationContext) applicationContext);
    133     }
    134
    135     public void setApplicationContext(ConfigurableApplicationContext configurableApplicationContext)
    136     {
    137         this.configurableApplicationContext = configurableApplicationContext;
    138     }
    139
    140     public void redirect(String url) throws IOException
    141     {
    142     }
    143
    144     public Object getBean(String name)
    145     {
    146         if (!getApplicationContext().containsBean(name))
    147         {
    148             return null;
    149         }
    150         return getApplicationContext().getBean(name);
    151     }
    152
    153     public void invokeNavigation(String navigationName)
    154     {
    155     }
    156 }