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;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.struts.config.ModuleConfig;
28 import org.apache.struts.util.ModuleUtils;
29 import org.apache.tiles.Definition;
30 import org.apache.tiles.definition.DefinitionsFactory;
31 import org.apache.tiles.impl.BasicTilesContainer;
32 import org.apache.tiles.locale.LocaleResolver;
33 import org.apache.tiles.request.Request;
34 import org.apache.tiles.request.jakarta.servlet.ServletRequest;
35
36 import jakarta.servlet.http.HttpServletRequest;
37
38 public class TilesPluginContainer extends BasicTilesContainer {
39
40 /**
41 * Maps definition factories to their keys.
42 */
43 private final Map<String, DefinitionsFactory> key2definitionsFactory =
44 new HashMap<>();
45
46 /**
47 * The locale resolver.
48 */
49 private LocaleResolver localeResolver;
50
51 /**
52 * Sets the locale resolver.
53 *
54 * @param localeResolver the locale resolver for this container.
55 *
56 * @since Struts 1.4.1
57 */
58 void setLocaleResolver(LocaleResolver localeResolver) {
59 this.localeResolver = localeResolver;
60 }
61
62 /**
63 * Returns the locale resolver.
64 *
65 * @return The locale resolve.
66 *
67 * @since Struts 1.4.1
68 */
69 LocaleResolver getLocaleResolverIntern() {
70 return localeResolver;
71 }
72
73 /**
74 * Returns the proper definition factory for the given key, i.e. if the key
75 * is not present, <code>null</code> will be returned.
76 *
77 * @return the definitions factory used by this container. If the key is not
78 * valid, <code>null</code> will be returned.
79 * @param key The key of the needed definitions factory.
80 */
81 public DefinitionsFactory getProperDefinitionsFactory(String key) {
82 DefinitionsFactory retValue = null;
83
84 if (key != null) {
85 retValue = key2definitionsFactory.get(key);
86 }
87
88 return retValue;
89 }
90
91 /**
92 * Set the definitions factory. This method first ensures that the container
93 * has not yet been initialized.
94 *
95 * @param key The key under which the definitions factory is cataloged.
96 * @param definitionsFactory the definitions factory for this instance.
97 * @since 2.1.0
98 */
99 public void setDefinitionsFactory(String key,
100 DefinitionsFactory definitionsFactory) {
101 if (key != null) {
102 key2definitionsFactory.put(key, definitionsFactory);
103 } else {
104 setDefinitionsFactory(definitionsFactory);
105 }
106 }
107
108 @Override
109 public Definition getDefinition(String definitionName,
110 Request request) {
111 Definition retValue = null;
112 String key = getDefinitionsFactoryKey(request);
113 if (key != null) {
114 DefinitionsFactory definitionsFactory =
115 key2definitionsFactory.get(key);
116 if (definitionsFactory != null) {
117 retValue = definitionsFactory.getDefinition(definitionName,
118 request);
119 }
120 }
121 if (retValue == null) {
122 retValue = super.getDefinition(definitionName, request);
123 }
124 return retValue;
125 }
126
127 /**
128 * Returns the definitions factory key.
129 *
130 * @param request The request object.
131 * @return The needed factory key.
132 */
133 protected String getDefinitionsFactoryKey(Request request) {
134 String retValue = null;
135
136 if (request instanceof ServletRequest) {
137 HttpServletRequest servletRequest =
138 ((ServletRequest) request).getRequest();
139 ModuleConfig config = ModuleUtils.getInstance().getModuleConfig(
140 servletRequest);
141
142 if (config == null) {
143 // ModuleConfig not found in current request. Select it.
144 ModuleUtils.getInstance().selectModule(servletRequest,
145 servletRequest.getSession().getServletContext());
146 config = ModuleUtils.getInstance().getModuleConfig(servletRequest);
147 }
148
149 if (config != null) {
150 retValue = config.getPrefix();
151 }
152 }
153 return retValue;
154 }
155 }