Skip to content
This repository was archived by the owner on Jul 9, 2022. It is now read-only.

Spring MVC Async support #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<name>Spring Multi-Tenancy Framework</name>

<properties>
<spring.framework.version>3.0.5.RELEASE</spring.framework.version>
<spring.framework.version>3.2.0.RELEASE</spring.framework.version>
</properties>

<profiles>
Expand Down Expand Up @@ -128,6 +128,12 @@
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>

<url>http://r.tasktop.com/#projects/spring-tenancy</url>
Expand All @@ -146,8 +152,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.util.logging.Logger;

import javax.sql.DataSource;

Expand Down Expand Up @@ -78,6 +80,11 @@ public void setLogWriter(PrintWriter arg0) throws SQLException {
wrappedDataSource.setLogWriter(arg0);
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return wrappedDataSource.getParentLogger();
}

@Override
public void setLoginTimeout(int arg0) throws SQLException {
wrappedDataSource.setLoginTimeout(arg0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*******************************************************************************
* Copyright (c) 2010, 2011 SpringSource, a division of VMware
*
* 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.
*
* Contributors:
* Tasktop Technologies Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.tenancy.web;

import java.util.concurrent.Callable;

import org.springframework.tenancy.context.TenancyContext;
import org.springframework.tenancy.context.TenancyContextHolder;
import org.springframework.util.Assert;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter;

public class TenancyContextCallableProcessingInterceptor extends CallableProcessingInterceptorAdapter {

private TenancyContext tenancyContext;

/**
* Create a new {@link TenancyContextCallableProcessingInterceptor} that uses the
* {@link TenancyContext} from the {@link TenancyContextHolder} at the time
* {@link #beforeConcurrentHandling(NativeWebRequest, Callable)} is invoked.
*/
public TenancyContextCallableProcessingInterceptor() {
}

/**
* Creates a new {@link TenancyContextCallableProcessingInterceptor} with the
* specified {@link TenancyContext}.
*
* @param tenancyContext the {@link TenancyContext} to set on the
* {@link org.springframework.tenancy.context.TenancyContextHolder} in {@link #preProcess(NativeWebRequest, Callable)}.
* Cannot be null.
* @throws IllegalArgumentException if tenancyContext is null.
*/
public TenancyContextCallableProcessingInterceptor(TenancyContext tenancyContext) {
Assert.notNull(tenancyContext, "tenancyContext cannot be null");
setTenancyContext(tenancyContext);
}

@Override
public <T> void beforeConcurrentHandling(NativeWebRequest request, Callable<T> task) throws Exception {
if (tenancyContext == null) {
setTenancyContext(TenancyContextHolder.getContext());
}
}

@Override
public <T> void preProcess(NativeWebRequest request, Callable<T> task) throws Exception {
TenancyContextHolder.setContext(tenancyContext);
}

@Override
public <T> void postProcess(NativeWebRequest request, Callable<T> task, Object concurrentResult) throws Exception {
TenancyContextHolder.clearContext();
}

private void setTenancyContext(TenancyContext tenancyContext) {
this.tenancyContext = tenancyContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*******************************************************************************
* Copyright (c) 2010, 2011 SpringSource, a division of VMware
*
* 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.
*
* Contributors:
* Tasktop Technologies Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.tenancy.web;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.async.WebAsyncManager;
import org.springframework.web.context.request.async.WebAsyncUtils;
import org.springframework.web.filter.OncePerRequestFilter;

/**
* This filter provides Asynchronous Request Processing for multi-tenant
* application.
*
* You need to add this filter to your application to support
* {@link org.springframework.tenancy.context.TenancyContextHolder} usage
* in Spring Web MVC Async applications.
*
* i.e.
* <pre>
* <code>
@Bean
@Conditional(TenantCondition.class)
public FilterRegistrationBean tenancyWebAsyncFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
TenancyWebAsyncFilter filter = new TenancyWebAsyncFilter();
registration.setFilter(filter);
registration.setOrder(5); // whatever order
return registration;
}
* </code>
* </pre>
*/
public class TenancyWebAsyncFilter extends OncePerRequestFilter {

private static final Object CALLABLE_INTERCEPTOR_KEY = new Object();

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

TenancyWebAsyncFilter tenancyProcessingInterceptor =
(TenancyWebAsyncFilter) asyncManager.getCallableInterceptor(CALLABLE_INTERCEPTOR_KEY);
if (tenancyProcessingInterceptor == null) {
asyncManager.registerCallableInterceptor(CALLABLE_INTERCEPTOR_KEY, new TenancyContextCallableProcessingInterceptor());
}

filterChain.doFilter(request, response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*******************************************************************************
* Copyright (c) 2010, 2011 SpringSource, a division of VMware
*
* 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.
*
* Contributors:
* Tasktop Technologies Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.tenancy.web;

import java.util.concurrent.Callable;

import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.tenancy.context.TenancyContext;
import org.springframework.tenancy.context.TenancyContextHolder;
import org.springframework.web.context.request.NativeWebRequest;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

@RunWith(MockitoJUnitRunner.class)
public class TenancyContextCallableProcessingInterceptorTest {

@Mock
private TenancyContext tenancyContext;

@Mock
private Callable<?> callable;

@Mock
private NativeWebRequest webRequest;

@After
public void clearTenancyContext() {
TenancyContextHolder.clearContext();
}

@Test(expected = IllegalArgumentException.class)
public void constructorNull() {
new TenancyContextCallableProcessingInterceptor(null);
}

@Test
public void currentTenancyContext() throws Exception {
TenancyContextCallableProcessingInterceptor interceptor = new TenancyContextCallableProcessingInterceptor();
TenancyContextHolder.setContext(tenancyContext);
interceptor.beforeConcurrentHandling(webRequest, callable);
TenancyContextHolder.clearContext();

interceptor.preProcess(webRequest, callable);
assertThat(TenancyContextHolder.getContext(), is(tenancyContext));

interceptor.postProcess(webRequest, callable, null);
assertThat(TenancyContextHolder.getContext(), is(not(tenancyContext)));
}

@Test
public void specificTenancyContext() throws Exception {
TenancyContextCallableProcessingInterceptor interceptor = new TenancyContextCallableProcessingInterceptor(tenancyContext);

interceptor.preProcess(webRequest, callable);
assertThat(TenancyContextHolder.getContext(), is(tenancyContext));

interceptor.postProcess(webRequest, callable, null);
assertThat(TenancyContextHolder.getContext(), is(not(tenancyContext)));
}
}
Loading