Commit 2e4a61ed authored by Ard Schrijvers's avatar Ard Schrijvers

HSTTWO-3282 add extra parameter WebAppType to indicate which kind of webapp...

HSTTWO-3282 add extra parameter WebAppType to indicate which kind of webapp the servlet context belongs to
parent 1f13c7a7
......@@ -10,3 +10,4 @@ src/main/java/org/onehippo/cms7/services/googleanalytics/GoogleAnalyticsService.
src/main/java/org/onehippo/cms7/services/webfiles/WebFile.java -text
src/main/java/org/onehippo/cms7/services/webfiles/WebFileEvent.java -text
src/main/java/org/onehippo/cms7/services/webfiles/WebFilesService.java -text
src/test/java/org/onehippo/cms7/services/ServletContextRegistryTest.java -text
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2012-2014 Hippo B.V. (http://www.onehippo.com)
Copyright 2012-2015 Hippo B.V. (http://www.onehippo.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
......@@ -35,6 +35,7 @@
<notice.project.name>Hippo CMS7 Services</notice.project.name>
<hippo.commons.version>1.05.00-SNAPSHOT</hippo.commons.version>
<easymock.version>3.0</easymock.version>
</properties>
<scm>
......@@ -70,6 +71,12 @@
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>${easymock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
......
......@@ -24,7 +24,9 @@ import javax.servlet.ServletContext;
/**
* The ServletContextRegistry provides a central registration of web applications their {@link ServletContext} using
* their contextPath as key.
* their contextPath as key and optionally a {@link WebAppType} which gets set as {@link ServletContext} attribute as follows:
* {@link javax.servlet.ServletContext#setAttribute(String, Object)
* ServletContext#setAttribute(org.onehippo.cms7.services.ServletContextRegistry.WebAppType, WebAppType)}.
* <p>
* ServletContexts can/should be registered/unregistered via init/destroy methods of a {@link javax.servlet.Servlet}
* or {@link javax.servlet.Filter}, or a {@link javax.servlet.ServletContextListener} its
......@@ -37,6 +39,10 @@ import javax.servlet.ServletContext;
*/
public final class ServletContextRegistry {
public enum WebAppType {
HST, REPO, OTHER
}
private static Map<String, ServletContext> registry = new HashMap<>();
/**
......@@ -44,12 +50,15 @@ public final class ServletContextRegistry {
* @throws IllegalStateException if the ServletContext already has been registered by its contextPath
* @param ctx the servletContext to register
*/
public synchronized static void register(ServletContext ctx) {
public synchronized static void register(final ServletContext ctx, final WebAppType type) {
if (registry.containsKey(ctx.getContextPath())) {
throw new IllegalStateException("ServletContext "+ctx.getContextPath()+" already registered");
}
Map<String, ServletContext> newMap = new HashMap<>(registry);
newMap.put(ctx.getContextPath(), ctx);
if (type != null) {
ctx.setAttribute(WebAppType.class.getName(), type);
}
registry = Collections.unmodifiableMap(newMap);
}
......@@ -58,7 +67,7 @@ public final class ServletContextRegistry {
* @throws IllegalStateException if the ServletContext has not been registered by its contextPath
* @param ctx the servletContext to unregister
*/
public synchronized static void unregister(ServletContext ctx) {
public synchronized static void unregister(final ServletContext ctx) {
if (!registry.containsKey(ctx.getContextPath())) {
throw new IllegalStateException("ServletContext "+ctx.getContextPath()+" not registered");
}
......@@ -71,14 +80,31 @@ public final class ServletContextRegistry {
* @param contextPath The contextPath for which to lookup the ServletContext
* @return the ServletContext registered under the parameter contextPath
*/
public static ServletContext getContext(String contextPath) {
public static ServletContext getContext(final String contextPath) {
return registry.get(contextPath);
}
/**
* @return all currently registered ServletContexts mapped by their contextPath
* @return unmodifiable map of all currently registered ServletContexts mapped by their contextPath
*/
public static Map<String, ServletContext> getContexts() {
return registry;
}
/**
* @return unmodifiable map of all currently registered ServletContexts of WebAppType <code>type</code> mapped by their contextPath
*/
public static Map<String, ServletContext> getContexts(final WebAppType type) {
if (type == null) {
throw new IllegalArgumentException("WebAppType argument is not allowed to be null.");
}
Map<String, ServletContext> newMap = new HashMap<>();
for (Map.Entry<String, ServletContext> entry : registry.entrySet()) {
final Object attribute = entry.getValue().getAttribute(WebAppType.class.getName());
if (attribute == type) {
newMap.put(entry.getKey(), entry.getValue());
}
}
return Collections.unmodifiableMap(newMap);
}
}
/*
* Copyright 2015 Hippo B.V. (http://www.onehippo.com)
*
* Licensed 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.onehippo.cms7.services;
import javax.servlet.ServletContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class ServletContextRegistryTest {
private ServletContext servletContext;
@Before
public void setUp() {
servletContext = createNiceMock(ServletContext.class);
expect(servletContext.getContextPath()).andReturn("/site").anyTimes();
expect(servletContext.getAttribute(eq(ServletContextRegistry.WebAppType.class.getName()))).andReturn(ServletContextRegistry.WebAppType.HST);
servletContext.setAttribute(eq(ServletContextRegistry.WebAppType.class.getName()), eq(ServletContextRegistry.WebAppType.HST));
expectLastCall();
replay(servletContext);
}
@After
public void tearDown() {
ServletContextRegistry.unregister(servletContext);
}
@Test
public void register_servlet_context() {
ServletContextRegistry.register(servletContext, ServletContextRegistry.WebAppType.HST);
assertEquals(1, ServletContextRegistry.getContexts().size());
try {
ServletContextRegistry.getContexts().put("foo", servletContext);
fail("#getContexts should return unmudifiable map");
} catch (UnsupportedOperationException e) {
// expected
}
try {
ServletContextRegistry.register(servletContext, ServletContextRegistry.WebAppType.HST);
fail("#registering same servlet context twice is not allowed");
} catch (IllegalStateException e) {
// expected
}
try {
ServletContextRegistry.register(servletContext, ServletContextRegistry.WebAppType.REPO);
fail("#registering same servlet context twice is not allowed, even not with different type");
} catch (IllegalStateException e) {
// expected
}
assertEquals(1, ServletContextRegistry.getContexts(ServletContextRegistry.WebAppType.HST).size());
assertEquals(0, ServletContextRegistry.getContexts(ServletContextRegistry.WebAppType.REPO).size());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment