package org.minimalj.frontend.impl.util;

import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

import org.minimalj.frontend.Frontend;
import org.minimalj.frontend.Frontend.IContent;
import org.minimalj.frontend.action.Action;
import org.minimalj.frontend.page.Page;
import org.minimalj.util.ExceptionUtils;

/**
 * Framework internal. The framework should be protected against exception
 * thrown by the application pages. The PageManagers could get into a invalid
 * state if for example getActions throws an Exception and the page is already
 * added to the list of displayed pages.
 *
 */
public class PageAccess {

	private static final Logger logger = Logger.getLogger(PageAccess.class.getName());

	public static IContent getContent(Page page) {
		try {
			return page.getContent();
		} catch (Exception exception) {
			ExceptionUtils.logReducedStackTrace(logger, exception);
			String text = exception.getClass().getSimpleName() + ": " + exception.getMessage();
			return Frontend.getInstance().createHtmlContent("<html>" + text + "</html>");
		}
	}

	public static List<Action> getActions(Page page) {
		try {
			return page.getActions();
		} catch (Exception exception) {
			ExceptionUtils.logReducedStackTrace(logger, exception);
			return Collections.emptyList();
		}
	}

	public static String getRoute(Page page) {
		try {
			return page.getRoute();
		} catch (Exception exception) {
			ExceptionUtils.logReducedStackTrace(logger, exception);
			return null;
		}
	}
	
}

@@ -33,24 +33,83 @@
 pl.util;%0D%0A%0D%0A
+import java.io.PrintWriter;%0D%0Aimport java.io.StringWriter;%0D%0A
 import java.
@@ -183,24 +183,72 @@
 .Logger;%0D%0A%0D%0A
+import org.minimalj.application.Configuration;%0D%0A
 import org.m
@@ -1062,167 +1062,437 @@
 text
- = exception.getClass().getSimpleName() + %22: %22 + exception.getMessage();%0D%0A%09%09%09return Frontend.getInstance().createHtmlContent(%22%3Chtml%3E%22 + text + %22%3C/html%3E%22);%0D%0A%09%09%7D
+;%0D%0A%09%09%09if (Configuration.isDevModeActive()) %7B%0D%0A%09%09%09%09text = getStackTrace(exception);%0D%0A%09%09%09%09text = text.replace(%22%5Cn%22, %22%3Cbr%3E%22);%0D%0A%09%09%09%7D else %7B%0D%0A%09%09%09%09text = exception.toString();%0D%0A%09%09%09%7D%0D%0A%09%09%09return Frontend.getInstance().createHtmlContent(%22%3Chtml%3E%22 + text + %22%3C/html%3E%22);%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09public static String getStackTrace(Throwable t) %7B%0D%0A%09%09StringWriter sw = new StringWriter();%0D%0A%09%09t.printStackTrace(new PrintWriter(sw));%0D%0A%09%09return sw.toString();
 %0D%0A%09%7D

print stacktrace in dev mode