Skip to content

Commit 5ec34b1

Browse files
unknownunknown
authored andcommitted
el-ognl example first commit
1 parent c294ecf commit 5ec34b1

File tree

10 files changed

+797
-0
lines changed

10 files changed

+797
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<module>tiles-ognl</module>
6060
<module>tiles-compat</module>
6161
<module>tiles-extras</module>
62+
<module>tiles-examples</module>
6263
<module>assembly</module>
6364
<module>tiles-test-pom</module>
6465
</modules>

tiles-examples/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/*
4+
* $Id$
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one
7+
* or more contributor license agreements. See the NOTICE file
8+
* distributed with this work for additional information
9+
* regarding copyright ownership. The ASF licenses this file
10+
* to you under the Apache License, Version 2.0 (the
11+
* "License"); you may not use this file except in compliance
12+
* with the License. You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing,
17+
* software distributed under the License is distributed on an
18+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19+
* KIND, either express or implied. See the License for the
20+
* specific language governing permissions and limitations
21+
* under the License.
22+
*/
23+
-->
24+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
25+
26+
<parent>
27+
<groupId>org.apache.tiles</groupId>
28+
<artifactId>tiles-parent</artifactId>
29+
<version>3.0.7-SNAPSHOT</version>
30+
</parent>
31+
32+
<modelVersion>4.0.0</modelVersion>
33+
<artifactId>tiles-examples</artifactId>
34+
<packaging>pom</packaging>
35+
<name>Tiles - Examples</name>
36+
<description>Extras for Tiles, including non-standard, non-generic, non-identifiable components that may be useful for Tiles users.
37+
</description>
38+
<modules>
39+
<module>tiles-elognl</module>
40+
</modules>
41+
<build>
42+
</build>
43+
</project>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/*
4+
* $Id$
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one
7+
* or more contributor license agreements. See the NOTICE file
8+
* distributed with this work for additional information
9+
* regarding copyright ownership. The ASF licenses this file
10+
* to you under the Apache License, Version 2.0 (the
11+
* "License"); you may not use this file except in compliance
12+
* with the License. You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing,
17+
* software distributed under the License is distributed on an
18+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19+
* KIND, either express or implied. See the License for the
20+
* specific language governing permissions and limitations
21+
* under the License.
22+
*/
23+
-->
24+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
25+
26+
<parent>
27+
<groupId>org.apache.tiles</groupId>
28+
<artifactId>tiles-examples</artifactId>
29+
<version>3.0.7-SNAPSHOT</version>
30+
</parent>
31+
32+
<modelVersion>4.0.0</modelVersion>
33+
<artifactId>tiles-elognl</artifactId>
34+
<packaging>jar</packaging>
35+
<name>Tiles - EL OGNL Freemarker support</name>
36+
<description>Extras for Tiles, including non-standard, non-generic, non-identifiable components that may be useful for Tiles users.
37+
</description>
38+
39+
<properties>
40+
<tiles.osgi.symbolicName>org.apache.tiles.extras</tiles.osgi.symbolicName>
41+
</properties>
42+
43+
<build>
44+
<resources>
45+
<resource>
46+
<directory>src/main/resources</directory>
47+
<excludes>
48+
<exclude>LICENSE.txt</exclude>
49+
<exclude>NOTICE.txt</exclude>
50+
</excludes>
51+
</resource>
52+
<resource>
53+
<directory>src/main/resources</directory>
54+
<includes>
55+
<include>LICENSE.txt</include>
56+
<include>NOTICE.txt</include>
57+
</includes>
58+
<targetPath>META-INF</targetPath>
59+
</resource>
60+
</resources>
61+
</build>
62+
63+
<dependencies>
64+
<dependency>
65+
<groupId>org.apache.tiles</groupId>
66+
<artifactId>tiles-core</artifactId>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.apache.tiles</groupId>
70+
<artifactId>tiles-freemarker</artifactId>
71+
<version>${project.version}</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.apache.tiles</groupId>
75+
<artifactId>tiles-el</artifactId>
76+
<version>${project.version}</version>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.apache.tiles</groupId>
80+
<artifactId>tiles-ognl</artifactId>
81+
<version>${project.version}</version>
82+
</dependency>
83+
84+
<dependency>
85+
<groupId>javax.servlet</groupId>
86+
<artifactId>servlet-api</artifactId>
87+
<scope>provided</scope>
88+
</dependency>
89+
90+
<dependency>
91+
<groupId>javax.servlet.jsp</groupId>
92+
<artifactId>jsp-api</artifactId>
93+
<scope>provided</scope>
94+
</dependency>
95+
96+
<dependency>
97+
<groupId>org.slf4j</groupId>
98+
<artifactId>slf4j-api</artifactId>
99+
</dependency>
100+
101+
<dependency>
102+
<groupId>junit</groupId>
103+
<artifactId>junit</artifactId>
104+
<scope>test</scope>
105+
</dependency>
106+
107+
<dependency>
108+
<groupId>org.easymock</groupId>
109+
<artifactId>easymock</artifactId>
110+
<scope>test</scope>
111+
</dependency>
112+
113+
</dependencies>
114+
115+
</project>
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package org.apache.tiles.elognl;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.List;
6+
import java.util.Locale;
7+
8+
import javax.el.ArrayELResolver;
9+
import javax.el.BeanELResolver;
10+
import javax.el.CompositeELResolver;
11+
import javax.el.ELResolver;
12+
import javax.el.ListELResolver;
13+
import javax.el.MapELResolver;
14+
import javax.el.ResourceBundleELResolver;
15+
16+
import org.apache.tiles.TilesContainer;
17+
import org.apache.tiles.el.ELAttributeEvaluator;
18+
import org.apache.tiles.el.JspExpressionFactoryFactory;
19+
import org.apache.tiles.el.ScopeELResolver;
20+
import org.apache.tiles.el.TilesContextBeanELResolver;
21+
import org.apache.tiles.el.TilesContextELResolver;
22+
import org.apache.tiles.evaluator.AttributeEvaluatorFactory;
23+
import org.apache.tiles.evaluator.BasicAttributeEvaluatorFactory;
24+
import org.apache.tiles.factory.BasicTilesContainerFactory;
25+
import org.apache.tiles.factory.TilesContainerFactoryException;
26+
import org.apache.tiles.freemarker.TilesSharedVariableFactory;
27+
import org.apache.tiles.locale.LocaleResolver;
28+
import org.apache.tiles.ognl.AnyScopePropertyAccessor;
29+
import org.apache.tiles.ognl.DelegatePropertyAccessor;
30+
import org.apache.tiles.ognl.NestedObjectDelegatePropertyAccessor;
31+
import org.apache.tiles.ognl.OGNLAttributeEvaluator;
32+
import org.apache.tiles.ognl.PropertyAccessorDelegateFactory;
33+
import org.apache.tiles.ognl.ScopePropertyAccessor;
34+
import org.apache.tiles.ognl.TilesApplicationContextNestedObjectExtractor;
35+
import org.apache.tiles.ognl.TilesContextPropertyAccessorDelegateFactory;
36+
import org.apache.tiles.request.ApplicationContext;
37+
import org.apache.tiles.request.ApplicationResource;
38+
import org.apache.tiles.request.Request;
39+
import org.apache.tiles.request.freemarker.render.FreemarkerRenderer;
40+
import org.apache.tiles.request.freemarker.render.FreemarkerRendererBuilder;
41+
import org.apache.tiles.request.freemarker.servlet.SharedVariableLoaderFreemarkerServlet;
42+
import org.apache.tiles.request.render.BasicRendererFactory;
43+
import org.apache.tiles.request.render.ChainedDelegateRenderer;
44+
import org.apache.tiles.request.render.Renderer;
45+
46+
import ognl.OgnlException;
47+
import ognl.OgnlRuntime;
48+
import ognl.PropertyAccessor;
49+
50+
public class ElOgnlTilesContainerFactory extends BasicTilesContainerFactory {
51+
52+
/**
53+
* The freemarker renderer name.
54+
*/
55+
private static final String FREEMARKER_RENDERER_NAME = "freemarker";
56+
57+
/** {@inheritDoc} */
58+
@Override
59+
protected List<ApplicationResource> getSources(ApplicationContext applicationContext) {
60+
Collection<ApplicationResource> webINFSet = applicationContext.getResources("/WEB-INF/tiles.xml");
61+
//applicationContext.getResources("/WEB-INF/**/tiles*.xml");
62+
63+
List<ApplicationResource> filteredResources = new ArrayList<ApplicationResource>();
64+
if (webINFSet != null) {
65+
for (ApplicationResource resource : webINFSet) {
66+
if (Locale.ROOT.equals(resource.getLocale())) {
67+
filteredResources.add(resource);
68+
}
69+
}
70+
}
71+
return filteredResources;
72+
}
73+
74+
75+
/**
76+
* Creates the EL evaluator.
77+
*
78+
* @param applicationContext The Tiles application context.
79+
* @return The EL evaluator.
80+
*/
81+
private ELAttributeEvaluator createELEvaluator(ApplicationContext applicationContext) {
82+
ELAttributeEvaluator evaluator = new ELAttributeEvaluator();
83+
JspExpressionFactoryFactory efFactory = new JspExpressionFactoryFactory();
84+
efFactory.setApplicationContext(applicationContext);
85+
evaluator.setExpressionFactory(efFactory.getExpressionFactory());
86+
ELResolver elResolver = new CompositeELResolver() {
87+
{
88+
BeanELResolver beanElResolver = new BeanELResolver(false);
89+
add(new ScopeELResolver());
90+
add(new TilesContextELResolver(beanElResolver));
91+
add(new TilesContextBeanELResolver());
92+
add(new ArrayELResolver(false));
93+
add(new ListELResolver(false));
94+
add(new MapELResolver(false));
95+
add(new ResourceBundleELResolver());
96+
add(beanElResolver);
97+
}
98+
};
99+
evaluator.setResolver(elResolver);
100+
return evaluator;
101+
}
102+
103+
/**
104+
* Creates the OGNL evaluator.
105+
*
106+
* @return The OGNL evaluator.
107+
*/
108+
private OGNLAttributeEvaluator createOGNLEvaluator() {
109+
try {
110+
PropertyAccessor objectPropertyAccessor = OgnlRuntime.getPropertyAccessor(Object.class);
111+
PropertyAccessor applicationContextPropertyAccessor = new NestedObjectDelegatePropertyAccessor<Request>(
112+
new TilesApplicationContextNestedObjectExtractor(), objectPropertyAccessor);
113+
PropertyAccessor anyScopePropertyAccessor = new AnyScopePropertyAccessor();
114+
PropertyAccessor scopePropertyAccessor = new ScopePropertyAccessor();
115+
PropertyAccessorDelegateFactory<Request> factory = new TilesContextPropertyAccessorDelegateFactory(
116+
objectPropertyAccessor, applicationContextPropertyAccessor, anyScopePropertyAccessor,
117+
scopePropertyAccessor);
118+
PropertyAccessor tilesRequestAccessor = new DelegatePropertyAccessor<Request>(factory);
119+
OgnlRuntime.setPropertyAccessor(Request.class, tilesRequestAccessor);
120+
return new OGNLAttributeEvaluator();
121+
} catch (OgnlException e) {
122+
throw new TilesContainerFactoryException("Cannot initialize OGNL evaluator", e);
123+
}
124+
}
125+
126+
127+
/** {@inheritDoc} */
128+
@Override
129+
protected void registerAttributeRenderers(
130+
BasicRendererFactory rendererFactory,
131+
ApplicationContext applicationContext,
132+
TilesContainer container,
133+
AttributeEvaluatorFactory attributeEvaluatorFactory) {
134+
super.registerAttributeRenderers(rendererFactory, applicationContext, container, attributeEvaluatorFactory);
135+
136+
FreemarkerRenderer freemarkerRenderer = FreemarkerRendererBuilder
137+
.createInstance()
138+
.setApplicationContext(applicationContext)
139+
.setParameter("TemplatePath", "/")
140+
.setParameter("NoCache", "true")
141+
.setParameter("ContentType", "text/html")
142+
.setParameter("template_update_delay", "0")
143+
.setParameter("default_encoding", "ISO-8859-1")
144+
.setParameter("number_format", "0.##########")
145+
.setParameter(SharedVariableLoaderFreemarkerServlet.CUSTOM_SHARED_VARIABLE_FACTORIES_INIT_PARAM,
146+
"tiles," + TilesSharedVariableFactory.class.getName()).build();
147+
rendererFactory.registerRenderer(FREEMARKER_RENDERER_NAME, freemarkerRenderer);
148+
}
149+
150+
/** {@inheritDoc} */
151+
@Override
152+
protected Renderer createDefaultAttributeRenderer(BasicRendererFactory rendererFactory,
153+
ApplicationContext applicationContext, TilesContainer container,
154+
AttributeEvaluatorFactory attributeEvaluatorFactory) {
155+
156+
ChainedDelegateRenderer retValue = new ChainedDelegateRenderer();
157+
retValue.addAttributeRenderer(rendererFactory.getRenderer(DEFINITION_RENDERER_NAME));
158+
retValue.addAttributeRenderer(rendererFactory.getRenderer(FREEMARKER_RENDERER_NAME));
159+
retValue.addAttributeRenderer(rendererFactory.getRenderer(TEMPLATE_RENDERER_NAME));
160+
retValue.addAttributeRenderer(rendererFactory.getRenderer(STRING_RENDERER_NAME));
161+
return retValue;
162+
}
163+
164+
165+
/** {@inheritDoc} */
166+
@Override
167+
protected AttributeEvaluatorFactory createAttributeEvaluatorFactory(ApplicationContext applicationContext,
168+
LocaleResolver resolver) {
169+
BasicAttributeEvaluatorFactory attributeEvaluatorFactory = new BasicAttributeEvaluatorFactory(
170+
createELEvaluator(applicationContext));
171+
attributeEvaluatorFactory.registerAttributeEvaluator("OGNL", createOGNLEvaluator());
172+
173+
return attributeEvaluatorFactory;
174+
}
175+
176+
177+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.apache.tiles.elognl;
2+
3+
import javax.servlet.ServletContext;
4+
5+
import org.apache.tiles.startup.AbstractTilesInitializer;
6+
import org.apache.tiles.factory.AbstractTilesContainerFactory;
7+
import org.apache.tiles.request.ApplicationContext;
8+
import org.apache.tiles.request.servlet.ServletApplicationContext;
9+
10+
public class ElOgnlTilesInitializer extends AbstractTilesInitializer {
11+
12+
/** {@inheritDoc} */
13+
@Override
14+
protected ApplicationContext createTilesApplicationContext(
15+
ApplicationContext preliminaryContext) {
16+
return new ServletApplicationContext(
17+
(ServletContext) preliminaryContext.getContext());
18+
}
19+
20+
21+
/** {@inheritDoc} */
22+
@Override
23+
protected AbstractTilesContainerFactory createContainerFactory(
24+
ApplicationContext context) {
25+
return new ElOgnlTilesContainerFactory();
26+
}
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.apache.tiles.elognl;
2+
3+
import org.apache.tiles.startup.TilesInitializer;
4+
import org.apache.tiles.web.startup.AbstractTilesListener;
5+
6+
public class ElOgnlTilesListener extends AbstractTilesListener {
7+
8+
/** {@inheritDoc} */
9+
@Override
10+
protected TilesInitializer createTilesInitializer() {
11+
return new ElOgnlTilesInitializer();
12+
}
13+
14+
}

0 commit comments

Comments
 (0)