This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/struts-examples.git
The following commit(s) were added to refs/heads/master by this push: new 50013f1 WW-5173 Example app using custom cache implementations 50013f1 is described below commit 50013f1beb1b159d5e8fffc962d1bb1f929fbc0d Author: Lukasz Lenart <lukaszlen...@apache.org> AuthorDate: Mon Aug 8 10:25:34 2022 +0200 WW-5173 Example app using custom cache implementations --- expression-cache/pom.xml | 63 ++++++++++++++++++++++ .../java/org/apache/struts/cache/CustomOEC.java | 34 ++++++++++++ .../org/apache/struts/cache/CustomOECFactory.java | 56 +++++++++++++++++++ .../java/org/apache/struts/cache/CustomOELRUC.java | 37 +++++++++++++ .../java/org/apache/struts/cache/IndexAction.java | 25 +++++++++ expression-cache/src/main/resources/log4j2.xml | 33 ++++++++++++ expression-cache/src/main/resources/struts.xml | 54 +++++++++++++++++++ expression-cache/src/main/webapp/WEB-INF/index.jsp | 32 +++++++++++ expression-cache/src/main/webapp/WEB-INF/web.xml | 46 ++++++++++++++++ pom.xml | 1 + 10 files changed, 381 insertions(+) diff --git a/expression-cache/pom.xml b/expression-cache/pom.xml new file mode 100644 index 0000000..e7fb4d3 --- /dev/null +++ b/expression-cache/pom.xml @@ -0,0 +1,63 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +/* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ +--> +<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"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.struts</groupId> + <artifactId>struts-examples</artifactId> + <version>1.1.0</version> + </parent> + + <artifactId>expression-cache</artifactId> + <packaging>war</packaging> + <name>Expression Cache</name> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <struts2.version>6.1.0-SNAPSHOT</struts2.version> + </properties> + + + <build> + <plugins> + <plugin> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-maven-plugin</artifactId> + <version>${jetty-plugin.version}</version> + <configuration> + <webApp> + <contextPath>/${project.artifactId}</contextPath> + </webApp> + <stopKey>CTRL+C</stopKey> + <stopPort>8999</stopPort> + <scanIntervalSeconds>10</scanIntervalSeconds> + <scanTargets> + <scanTarget>src/main/webapp/WEB-INF/web.xml</scanTarget> + </scanTargets> + </configuration> + </plugin> + </plugins> + </build> + +</project> diff --git a/expression-cache/src/main/java/org/apache/struts/cache/CustomOEC.java b/expression-cache/src/main/java/org/apache/struts/cache/CustomOEC.java new file mode 100644 index 0000000..8aab971 --- /dev/null +++ b/expression-cache/src/main/java/org/apache/struts/cache/CustomOEC.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts.cache; + +import com.opensymphony.xwork2.ognl.OgnlDefaultCache; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class CustomOEC extends OgnlDefaultCache<String, Object> { + + private static final Logger LOG = LogManager.getLogger(CustomOEC.class); + + public CustomOEC(int evictionLimit, int initialCapacity, float loadFactor) { + super(evictionLimit, initialCapacity, loadFactor); + LOG.info("Custom cache has been created"); + } + +} diff --git a/expression-cache/src/main/java/org/apache/struts/cache/CustomOECFactory.java b/expression-cache/src/main/java/org/apache/struts/cache/CustomOECFactory.java new file mode 100644 index 0000000..4a0ef75 --- /dev/null +++ b/expression-cache/src/main/java/org/apache/struts/cache/CustomOECFactory.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts.cache; + +import com.opensymphony.xwork2.ognl.DefaultOgnlExpressionCacheFactory; +import com.opensymphony.xwork2.ognl.OgnlCache; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * Placeholder custom OGNL expression cache factory + * + * Breakpoints: + * 1) OgnlUtil (Struts Core) - 1st line of constructor (to see what factory references are passed in). + * 2) HelloWorldAction, IndexAction (S2_StarterApp_1) - 1st line of execute() methods (to see what is returned from the container). + * 3) CustomOECFactory (S2_StarterApp_1) - every method to see if any get called. + */ +public class CustomOECFactory extends DefaultOgnlExpressionCacheFactory<String, Object> { + + private static final Logger LOG = LogManager.getLogger(CustomOECFactory.class); + + public CustomOECFactory() { + LOG.info("Custom expression cache factory has been created"); + } + + @Override + public OgnlCache<String, Object> buildOgnlCache() { + return buildOgnlCache(getCacheMaxSize(), 16, 0.75f, getUseLRUCache()); + } + + @Override + public OgnlCache<String, Object> buildOgnlCache(int evictionLimit, int initialCapacity, float loadFactor, boolean lruCache) { + if (lruCache) { + return new CustomOELRUC(evictionLimit, initialCapacity, loadFactor); + } else { + return new CustomOEC(evictionLimit, initialCapacity, loadFactor); + } + } + +} diff --git a/expression-cache/src/main/java/org/apache/struts/cache/CustomOELRUC.java b/expression-cache/src/main/java/org/apache/struts/cache/CustomOELRUC.java new file mode 100644 index 0000000..342d64d --- /dev/null +++ b/expression-cache/src/main/java/org/apache/struts/cache/CustomOELRUC.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts.cache; + +import com.opensymphony.xwork2.ognl.OgnlLRUCache; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * Placeholder custom OGNL LRU cache + */ +public class CustomOELRUC extends OgnlLRUCache<String, Object> { + + private static final Logger LOG = LogManager.getLogger(CustomOELRUC.class); + + public CustomOELRUC(int evictionLimit, int initialCapacity, float loadFactor) { + super(evictionLimit, initialCapacity, loadFactor); + LOG.info("Custom LRU cache has been created"); + } + +} diff --git a/expression-cache/src/main/java/org/apache/struts/cache/IndexAction.java b/expression-cache/src/main/java/org/apache/struts/cache/IndexAction.java new file mode 100644 index 0000000..c033832 --- /dev/null +++ b/expression-cache/src/main/java/org/apache/struts/cache/IndexAction.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts.cache; + +import com.opensymphony.xwork2.ActionSupport; + +public class IndexAction extends ActionSupport { + +} diff --git a/expression-cache/src/main/resources/log4j2.xml b/expression-cache/src/main/resources/log4j2.xml new file mode 100644 index 0000000..2aab5ee --- /dev/null +++ b/expression-cache/src/main/resources/log4j2.xml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +/* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ +--> +<Configuration> + <Appenders> + <Console name="STDOUT" target="SYSTEM_OUT"> + <PatternLayout pattern="[%p] %C{2} (%F:%L) - %m%n"/> + </Console> + </Appenders> + <Loggers> + <Root level="info"> + <AppenderRef ref="STDOUT"/> + </Root> + </Loggers> +</Configuration> diff --git a/expression-cache/src/main/resources/struts.xml b/expression-cache/src/main/resources/struts.xml new file mode 100644 index 0000000..f531ce5 --- /dev/null +++ b/expression-cache/src/main/resources/struts.xml @@ -0,0 +1,54 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!-- +/* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ +--> +<!DOCTYPE struts PUBLIC + "-//Apache Software Foundation//DTD Struts Configuration 6.0//EN" + "http://struts.apache.org/dtds/struts-6.0.dtd"> + +<struts> + + <constant name="struts.devMode" value="true"/> + <constant name="struts.enable.DynamicMethodInvocation" value="false"/> + + <!-- Set expression and BeanInfo caches to size 1000 with LRU cache mode --> + <constant name="struts.ognl.expressionCacheMaxSize" value="1000"/> + <constant name="struts.ognl.expressionCacheLRUMode" value="true"/> + <constant name="struts.ognl.beanInfoCacheMaxSize" value="1000"/> + <constant name="struts.ognl.beanInfoCacheLRUMode" value="true"/> + + <bean type="com.opensymphony.xwork2.ognl.ExpressionCacheFactory" + class="org.apache.struts.cache.CustomOECFactory" + name="customOgnlExpressionCacheFactory" + scope="singleton"/> + + <constant name="struts.ognl.expressionCacheFactory" value="customOgnlExpressionCacheFactory"/> + + <package name="default" extends="struts-default"> + + <default-action-ref name="index"/> + + <action name="index"> + <result>/WEB-INF/index.jsp</result> + </action> + + </package> + +</struts> diff --git a/expression-cache/src/main/webapp/WEB-INF/index.jsp b/expression-cache/src/main/webapp/WEB-INF/index.jsp new file mode 100644 index 0000000..3051fff --- /dev/null +++ b/expression-cache/src/main/webapp/WEB-INF/index.jsp @@ -0,0 +1,32 @@ +<%-- +/* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ +--%> + +<%@ page contentType="text/html; charset=UTF-8" %> +<%@ taglib prefix="s" uri="/struts-tags" %> +<html> +<head> + <title>Cache Expression</title> +</head> + +<body> +<h1>It works!</h1> +</body> +</html> diff --git a/expression-cache/src/main/webapp/WEB-INF/web.xml b/expression-cache/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..d830047 --- /dev/null +++ b/expression-cache/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,46 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +/* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ +--> +<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee + http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" + version="3.1"> + + <display-name>Struts 2 - Expression Cache</display-name> + + <filter> + <filter-name>struts2</filter-name> + <filter-class> + org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter + </filter-class> + </filter> + + <filter-mapping> + <filter-name>struts2</filter-name> + <url-pattern>/*</url-pattern> + </filter-mapping> + + <welcome-file-list> + <welcome-file>index.jsp</welcome-file> + </welcome-file-list> + +</web-app> diff --git a/pom.xml b/pom.xml index 7b57590..761c563 100644 --- a/pom.xml +++ b/pom.xml @@ -82,6 +82,7 @@ <module>dynamic-href</module> <module>exception-handling</module> <module>exclude-parameters</module> + <module>expression-cache</module> <module>file-upload</module> <module>form-processing</module> <module>form-tags</module>