View Javadoc
1   /*
2    * $Id$
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.struts.tiles2.util;
23  
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.apache.struts.config.PlugInConfig;
29  import org.apache.tiles.request.jakarta.servlet.ServletApplicationContext;
30  
31  import jakarta.servlet.ServletContext;
32  
33  
34  /**
35   * Adapts a {@link PlugInConfig} object to become a ServletContext object,
36   * exposing init parameters methods.
37   */
38  public class PlugInConfigContextAdapter extends ServletApplicationContext {
39  
40      /**
41       * The internal plugin config object.
42       */
43      private final PlugInConfig plugInConfigObject;
44  
45      /**
46       * The <code>Map</code> of context initialization parameters.
47       */
48      private final Map<String, String> initParam;
49  
50      /**
51       * Constructor.
52       *
53       * @param plugInConfigObject The plugin config object to use.
54       * @param servletContext The servlet context to use.
55       */
56      public PlugInConfigContextAdapter(PlugInConfig plugInConfigObject,
57              ServletContext servletContext) {
58          super(servletContext);
59          this.plugInConfigObject = plugInConfigObject;
60  
61          HashMap<String, String> initParam_ = new HashMap<>(super.getInitParams());
62          for (Map.Entry<String, Object> entry : plugInConfigObject.getProperties().entrySet()) {
63              initParam_.put(entry.getKey(), entry.getValue().toString());
64          }
65          initParam = Collections.unmodifiableMap(initParam_);
66      }
67  
68      @Override
69      public Map<String, String> getInitParams() {
70          return initParam;
71      }
72  
73      /**
74       * Returns the internal plugin config object.
75       *
76       * @return the internal plugin config object
77       */
78      public PlugInConfig getPlugInConfigObject() {
79          return plugInConfigObject;
80      }
81  }