Skip to content

Commit a383a40

Browse files
committed
Implemented eager loading of configuration on Filter startup to populate caches.
1 parent 15146ae commit a383a40

12 files changed

Lines changed: 194 additions & 57 deletions

File tree

‎api-servlet/src/main/java/org/ocpsoft/rewrite/servlet/RewriteLifecycleContext.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -30,10 +30,10 @@
3030

3131
/**
3232
* Stores implementations of all Rewrite lifecycle services.
33-
*
33+
*
3434
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3535
*/
36-
public interface RewriteLifecycleContext extends Context
36+
public interface RewriteLifecycleContext<C> extends Context
3737
{
3838
public static final String CONTEXT_KEY = "_com.ocpsoft.rewrite.RequestContext";
3939

@@ -50,7 +50,7 @@ public interface RewriteLifecycleContext extends Context
5050
/**
5151
* Get all available {@link org.ocpsoft.rewrite.spi.RewriteProvider} instances.
5252
*/
53-
List<RewriteProvider<Rewrite>> getRewriteProviders();
53+
List<RewriteProvider<C, Rewrite>> getRewriteProviders();
5454

5555
/**
5656
* Get all available {@link org.ocpsoft.rewrite.servlet.spi.InboundRewriteProducer} instances.

‎api-servlet/src/main/java/org/ocpsoft/rewrite/servlet/ServletRewriteProvider.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,6 +15,7 @@
1515
*/
1616
package org.ocpsoft.rewrite.servlet;
1717

18+
import javax.servlet.ServletContext;
1819
import javax.servlet.ServletRequest;
1920
import javax.servlet.ServletResponse;
2021

@@ -24,12 +25,12 @@
2425

2526
/**
2627
* Abstract implementation of {@link org.ocpsoft.rewrite.spi.RewriteProvider} which only handles {@link org.ocpsoft.rewrite.servlet.event.ServletRewrite} events.
27-
*
28+
*
2829
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
29-
*
30+
*
3031
*/
3132
public abstract class ServletRewriteProvider<T extends ServletRewrite<? extends ServletRequest, ? extends ServletResponse>>
32-
implements RewriteProvider<T>
33+
implements RewriteProvider<ServletContext, Rewrite>
3334
{
3435
@Override
3536
public boolean handles(final Rewrite payload)

‎api-servlet/src/main/java/org/ocpsoft/rewrite/servlet/http/HttpRewriteProvider.java‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -23,9 +23,9 @@
2323
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
2424

2525
/**
26-
* A {@link org.ocpsoft.rewrite.spi.RewriteProvider} that only operates on {@link HttpServletRequest} and {@link HttpServletResponse} request
27-
* cycle types.
28-
*
26+
* A {@link org.ocpsoft.rewrite.spi.RewriteProvider} that only operates on {@link HttpServletRequest} and
27+
* {@link HttpServletResponse} request cycle types.
28+
*
2929
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3030
*/
3131
public abstract class HttpRewriteProvider extends
@@ -36,4 +36,13 @@ public boolean handles(final Rewrite event)
3636
{
3737
return event instanceof HttpServletRewrite;
3838
}
39+
40+
@Override
41+
public final void rewrite(Rewrite event)
42+
{
43+
if (event instanceof HttpServletRewrite)
44+
rewriteHttp((HttpServletRewrite) event);
45+
}
46+
47+
public abstract void rewriteHttp(HttpServletRewrite event);
3948
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.config;
17+
18+
import org.apache.http.client.methods.HttpGet;
19+
import org.jboss.arquillian.container.test.api.Deployment;
20+
import org.jboss.shrinkwrap.api.asset.StringAsset;
21+
import org.jboss.shrinkwrap.api.spec.WebArchive;
22+
import org.junit.Assert;
23+
import org.junit.Test;
24+
import org.ocpsoft.rewrite.Root;
25+
import org.ocpsoft.rewrite.test.HttpAction;
26+
import org.ocpsoft.rewrite.test.RewriteTestBase;
27+
28+
/**
29+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
30+
*
31+
*/
32+
public class RewriteFilterInvalidProviderTest extends RewriteTestBase
33+
{
34+
35+
@Deployment(testable = true)
36+
public static WebArchive getDeployment()
37+
{
38+
WebArchive deployment = RewriteTestBase.getDeployment()
39+
.addPackages(true, Root.class.getPackage())
40+
.addAsResource(new StringAsset("org.ocpsoft.rewrite.config.RewriteFilterInvalidRewriteProvider"),
41+
"/META-INF/services/org.ocpsoft.rewrite.spi.RewriteProvider");
42+
return deployment;
43+
}
44+
45+
@Test
46+
public void testInvalidProvidersAreIgnored()
47+
{
48+
HttpAction<HttpGet> action = get("/");
49+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
50+
}
51+
52+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.ocpsoft.rewrite.config;
2+
3+
import org.ocpsoft.rewrite.event.Rewrite;
4+
import org.ocpsoft.rewrite.spi.RewriteProvider;
5+
6+
public class RewriteFilterInvalidRewriteProvider implements RewriteProvider<Integer, RewriteFilterInvalidRewriteType>
7+
{
8+
@Override
9+
public int priority()
10+
{
11+
return 0;
12+
}
13+
14+
@Override
15+
public boolean handles(Rewrite payload)
16+
{
17+
return false;
18+
}
19+
20+
@Override
21+
public void rewrite(RewriteFilterInvalidRewriteType event)
22+
{
23+
throw new IllegalStateException("Fail if called.");
24+
}
25+
26+
@Override
27+
public void init(Integer context)
28+
{
29+
throw new IllegalStateException("Fail if called.");
30+
}
31+
32+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.ocpsoft.rewrite.config;
2+
3+
import org.ocpsoft.rewrite.context.Context;
4+
import org.ocpsoft.rewrite.event.Rewrite;
5+
6+
public class RewriteFilterInvalidRewriteType implements Rewrite
7+
{
8+
@Override
9+
public Context getRewriteContext()
10+
{
11+
throw new IllegalStateException("Fail if called.");
12+
}
13+
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -17,15 +17,16 @@
1717

1818
import org.ocpsoft.common.pattern.Specialized;
1919
import org.ocpsoft.common.pattern.Weighted;
20-
2120
import org.ocpsoft.rewrite.event.Rewrite;
2221

2322
/**
2423
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
25-
*
24+
*
2625
*/
27-
public interface RewriteProvider<T extends Rewrite>
28-
extends Weighted, Specialized<Rewrite>
26+
public interface RewriteProvider<C, T extends Rewrite>
27+
extends Weighted, Specialized<Rewrite>
2928
{
3029
void rewrite(T event);
30+
31+
void init(C context);
3132
}

‎impl-servlet/src/main/java/org/ocpsoft/rewrite/servlet/RewriteFilter.java‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.servlet.Filter;
2323
import javax.servlet.FilterChain;
2424
import javax.servlet.FilterConfig;
25+
import javax.servlet.ServletContext;
2526
import javax.servlet.ServletException;
2627
import javax.servlet.ServletRequest;
2728
import javax.servlet.ServletResponse;
@@ -37,7 +38,7 @@
3738
import org.ocpsoft.rewrite.event.Rewrite;
3839
import org.ocpsoft.rewrite.servlet.event.BaseRewrite;
3940
import org.ocpsoft.rewrite.servlet.event.InboundServletRewrite;
40-
import org.ocpsoft.rewrite.servlet.impl.RewriteContextImpl;
41+
import org.ocpsoft.rewrite.servlet.impl.HttpRewriteContextImpl;
4142
import org.ocpsoft.rewrite.servlet.spi.ContextListener;
4243
import org.ocpsoft.rewrite.servlet.spi.InboundRewriteProducer;
4344
import org.ocpsoft.rewrite.servlet.spi.OutboundRewriteProducer;
@@ -62,7 +63,7 @@ public class RewriteFilter implements Filter
6263

6364
private List<RewriteLifecycleListener<Rewrite>> listeners;
6465
private List<RequestCycleWrapper<ServletRequest, ServletResponse>> wrappers;
65-
private List<RewriteProvider<Rewrite>> providers;
66+
private List<RewriteProvider<ServletContext, Rewrite>> providers;
6667
private List<InboundRewriteProducer<ServletRequest, ServletResponse>> inbound;
6768
private List<OutboundRewriteProducer<ServletRequest, ServletResponse, Object>> outbound;
6869

@@ -122,6 +123,11 @@ public void init(final FilterConfig filterConfig) throws ServletException
122123
.load(ConfigurationProvider.class));
123124
ServiceLogger.logLoadedServices(log, ConfigurationProvider.class, configurations);
124125

126+
for (RewriteProvider<?, ?> provider : providers) {
127+
if (provider instanceof ServletRewriteProvider)
128+
((ServletRewriteProvider<?>) provider).init(filterConfig.getServletContext());
129+
}
130+
125131
if ((configurations == null) || configurations.isEmpty())
126132
{
127133
log.warn("No ConfigurationProviders were registered: " +
@@ -149,7 +155,8 @@ public void doFilter(final ServletRequest request, final ServletResponse respons
149155
{
150156
if (request.getAttribute(RewriteLifecycleContext.CONTEXT_KEY) == null)
151157
{
152-
RewriteLifecycleContext context = new RewriteContextImpl(inbound, outbound, listeners, wrappers, providers);
158+
RewriteLifecycleContext<ServletContext> context = new HttpRewriteContextImpl(inbound, outbound, listeners,
159+
wrappers, providers);
153160
request.setAttribute(RewriteLifecycleContext.CONTEXT_KEY, context);
154161
}
155162

@@ -208,7 +215,7 @@ private void rewrite(final InboundServletRewrite<ServletRequest, ServletResponse
208215
listener.beforeInboundRewrite(event);
209216
}
210217

211-
for (RewriteProvider<Rewrite> provider : providers)
218+
for (RewriteProvider<?, Rewrite> provider : providers)
212219
{
213220
if (provider.handles(event))
214221
{

‎impl-servlet/src/main/java/org/ocpsoft/rewrite/servlet/impl/DefaultHttpRewriteProvider.java‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,25 +18,36 @@
1818
import javax.servlet.ServletContext;
1919

2020
import org.ocpsoft.common.services.NonEnriching;
21-
22-
import org.ocpsoft.rewrite.config.*;
23-
import org.ocpsoft.rewrite.servlet.event.BaseRewrite.Flow;
24-
import org.ocpsoft.rewrite.servlet.http.HttpRewriteProvider;
25-
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
21+
import org.ocpsoft.rewrite.config.Configuration;
2622
import org.ocpsoft.rewrite.config.ConfigurationLoader;
2723
import org.ocpsoft.rewrite.config.Operation;
2824
import org.ocpsoft.rewrite.config.Rule;
25+
import org.ocpsoft.rewrite.servlet.event.BaseRewrite.Flow;
26+
import org.ocpsoft.rewrite.servlet.http.HttpRewriteProvider;
27+
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
2928

3029
/**
3130
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
32-
*
31+
*
3332
*/
3433
public class DefaultHttpRewriteProvider extends HttpRewriteProvider implements NonEnriching
3534
{
3635
private volatile ConfigurationLoader loader;
3736

3837
@Override
39-
public void rewrite(final HttpServletRewrite event)
38+
public void init(ServletContext context)
39+
{
40+
if (loader == null)
41+
synchronized (this) {
42+
if (loader == null)
43+
loader = ConfigurationLoader.create(context);
44+
}
45+
46+
loader.loadConfiguration(context);
47+
}
48+
49+
@Override
50+
public void rewriteHttp(final HttpServletRewrite event)
4051
{
4152
ServletContext servletContext = event.getRequest().getServletContext();
4253
if (loader == null)

0 commit comments

Comments
 (0)