View Javadoc

1   /*
2    * $Id: PortletTilesContextFactory.java 531864 2007-04-24 10:24:30Z apetrelli $
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  
22  package org.apache.tiles.portlet.context;
23  
24  import org.apache.tiles.TilesApplicationContext;
25  import org.apache.tiles.context.TilesContextFactory;
26  import org.apache.tiles.context.TilesRequestContext;
27  
28  import javax.portlet.PortletContext;
29  import javax.portlet.PortletRequest;
30  import javax.portlet.PortletResponse;
31  import java.util.Map;
32  
33  /***
34   * Creates an instance of the appropriate TilesApplicationContext implementation.
35   *
36   * @version $Rev: 531864 $ $Date: 2007-04-24 12:24:30 +0200 (Tue, 24 Apr 2007) $
37   */
38  public class PortletTilesContextFactory implements TilesContextFactory {
39  
40      /*** {@inheritDoc} */
41      public void init(Map<String, String> configParameters) {
42      }
43  
44      /*** {@inheritDoc} */
45      public TilesApplicationContext createApplicationContext(Object context) {
46          if (context instanceof PortletContext) {
47              PortletContext portletContext = (PortletContext) context;
48              return new PortletTilesApplicationContext(portletContext);
49          }
50  
51          return null;
52      }
53  
54      /*** {@inheritDoc} */
55      public TilesRequestContext createRequestContext(TilesApplicationContext context,
56                                                      Object... requestItems) {
57          if (requestItems.length == 2) {
58              PortletContext portletContext = getPortletContext(context);
59              if (portletContext != null) {
60                  PortletTilesApplicationContext app = (PortletTilesApplicationContext) context;
61                  return new PortletTilesRequestContext(app.getPortletContext(),
62                      (PortletRequest) requestItems[0],
63                      (PortletResponse) requestItems[1]);
64              }
65          }
66  
67          return null;
68      }
69  
70      /***
71       * Returns the original portlet context.
72       *
73       * @param context The application context.
74       * @return The original portlet context, if found.
75       */
76      protected PortletContext getPortletContext(TilesApplicationContext context) {
77          if (context instanceof PortletTilesApplicationContext) {
78              PortletTilesApplicationContext app = (PortletTilesApplicationContext) context;
79              return app.getPortletContext();
80          }
81          return null;
82      }
83  }