Skip to content

Commit

Permalink
#1234 - Implement ability to access AppContext from any ActiveWeb loc…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
ipolevoy committed Sep 27, 2022
1 parent a460cb4 commit a290086
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
14 changes: 7 additions & 7 deletions activeweb/src/main/java/org/javalite/activeweb/AppContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
* Instance of this class can hold application-specific objects for duration of application life span.
Expand All @@ -26,8 +27,7 @@
*/
public class AppContext {

private Map context = new HashMap();

private static final AtomicReference<Map<String, Object>> contextReference = new AtomicReference<>(new HashMap<>());

/**
* Retrieves object by name
Expand All @@ -36,7 +36,7 @@ public class AppContext {
* @return object by name;
*/
public Object get(String name){
return context.get(name);
return contextReference.get().get(name);
}

/**
Expand All @@ -46,8 +46,8 @@ public Object get(String name){
* @param type type requested.
* @return object by name
*/
public <T> T get(String name, Class<T> type){
Object o = context.get(name);
public static <T> T get(String name, Class<T> type){
Object o = contextReference.get().get(name);
return o == null? null : (T) o;
}

Expand All @@ -57,7 +57,7 @@ public <T> T get(String name, Class<T> type){
* @param name name of object
* @param object - instance.
*/
public void set(String name, Object object){
context.put(name, object);
public static void set(String name, Object object){
contextReference.get().put(name, object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public abstract class Bootstrap implements InitConfig {


/**
* Called when application is bootstraps.
* Called when application bootstraps.
*
* @param context app context instance
*/
Expand Down
2 changes: 0 additions & 2 deletions activeweb/src/test/java/app/config/AppControllerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import app.controllers.DbExceptionController;
import app.controllers.DoFiltersController;
import app.controllers.RequestArgumentController;
import app.controllers.filters.*;
import org.javalite.activeweb.AbstractControllerConfig;
import org.javalite.activeweb.AppContext;
import org.javalite.activeweb.controller_filters.DBConnectionFilter;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

import static org.javalite.test.SystemStreamUtil.replaceError;
import static org.javalite.test.SystemStreamUtil.restoreSystemErr;
Expand All @@ -38,11 +37,7 @@
*/
public abstract class RequestSpec implements JSpecSupport {

protected FilterChain filterChain = new FilterChain() {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {

}
};
protected FilterChain filterChain = (servletRequest, servletResponse) -> {};
protected RequestDispatcher dispatcher;
protected MockHttpServletRequest request;
protected MockHttpServletResponse response;
Expand Down

0 comments on commit a290086

Please sign in to comment.