package org.minimalj.application;

import java.util.List;

import org.minimalj.frontend.action.Action;
import org.minimalj.frontend.page.Page;

/**
 * <p>This class is only for cases where you really need several applications in one JVM.
 * Normally there is exactly one Application configured at startup and not further
 * change is made.</p>
 * 
 * <p>For tests or demo deployments it may be needed that the application can be replaced.
 * This is forbidden by the Application class itself. This extends can switch the current
 * Application.</p>
 *
 */
public class ThreadLocalApplication extends Application {

	private InheritableThreadLocal<Application> current = new InheritableThreadLocal<>();
	
	public ThreadLocalApplication() {
	}

	public void setCurrentApplication(Application application) {
		current.set(application);
	}
	
	private Application getCurrentApplication() {
		return current.get();
	}
	
	@Override
	public Class<?>[] getEntityClasses() {
		return getCurrentApplication().getEntityClasses();
	}
	
	@Override
	public String getName() {
		return getCurrentApplication().getName();
	}
	
	@Override
	public Page createSearchPage(String query) {
		return getCurrentApplication().createSearchPage(query);
	}
	
	@Override
	public Page createDefaultPage() {
		return getCurrentApplication().createDefaultPage();
	}
	
	@Override
	public List<Action> getNavigation() {
		return getCurrentApplication().getNavigation();
	}
}

@@ -51,15 +51,145 @@
 til.
-List;%0D%0A
+HashMap;%0D%0Aimport java.util.List;%0D%0Aimport java.util.Map;%0D%0A%0D%0Aimport org.minimalj.backend.Backend;%0D%0Aimport org.minimalj.backend.Persistence;
 %0D%0Aim
@@ -285,16 +285,25 @@
 %0D%0A * %3Cp%3E
+Warning: 
 This cla
@@ -317,16 +317,21 @@
 nly for 
+rare 
 cases wh
@@ -457,17 +457,16 @@
 p and no
-t
  further
@@ -485,16 +485,50 @@
 is made.
+ This class is not for production!
 %3C/p%3E%0D%0A *
@@ -686,18 +686,20 @@
 is exten
-d
 s
+ion
  can swi
@@ -729,17 +729,148 @@
 lication
-.
+ and the persistence.%3C/p%3E%0D%0A * %0D%0A * %3Cp%3EAlso note that this trick only works if Frontend and Backend are deployed in%0D%0A * the same JVM!
 %3C/p%3E%0D%0A *
@@ -946,16 +946,22 @@
 private 
+final 
 Inherita
@@ -1070,16 +1070,66 @@
 on() %7B%0D%0A
+%09%09Backend.setInstance(new ThreadLocalBackend());%0D%0A
 %09%7D%0D%0A%0D%0A%09p
@@ -1852,12 +1852,661 @@
 ();%0D%0A%09%7D%0D
+%0A%0D%0A%09public class ThreadLocalBackend extends Backend %7B%0D%0A%09%09private final Map%3CApplication, Persistence%3E persistences = new HashMap%3C%3E(); %0D%0A%0D%0A%09%09@Override%0D%0A%09%09public Persistence getPersistence() %7B%0D%0A%09%09%09Application application = getCurrentApplication();%0D%0A%09%09%09if (!persistences.containsKey(application)) %7B%0D%0A%09%09%09%09super.setPersistence(null); // this forces super.getPersistence to create a new instance%0D%0A%09%09%09%09persistences.put(application, super.getPersistence());%0D%0A%09%09%09%7D%0D%0A%09%09%09return persistences.get(application);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09@Override%0D%0A%09%09public void setPersistence(Persistence persistence) %7B%0D%0A%09%09%09persistences.put(getCurrentApplication(), persistence);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%7D%0D
 %0A%7D%0D%0A

make Backend work