package edu.teco.dnd.module;

import java.security.Permission;

/**
 * the security manager designed to limit assess rights of functionBlocks.
 * 
 * @author Marvin Marx
 * 
 */
public class ApplicationSecurityManager extends SecurityManager {

	public ApplicationSecurityManager() {
		super();
	}

	/**
	 * check if we are being called from within a functionBlocks doUpdate() or init() methode.
	 * 
	 * @return true if we are inside an application, and not within other context.
	 */
	private boolean isApplication() {
		Thread currentThread = Thread.currentThread();
		if (currentThread == null) {
			throw new SecurityException();
			// Thread group is dead, which is strange in this context.
		}

		for (StackTraceElement ste : currentThread.getStackTrace()) {
			if (ste.getClassName().contains("edu.teco.dnd.module.BlockRunner")) {
				// We are inside a FunctionBlocks doUpdate() or init()
				// == the stack contains BlockRunner somewhere, which marks that we are in user code.
				return true;
			}
		}
		return false;
	}

	@Override
	public void checkPermission(Permission perm) {
		if (!isApplication()) {
			return;
		}
		
		throw new SecurityException();
		// TODO: implement security here.

	}
}

@@ -57,16 +57,101 @@
 ssion;%0A%0A
+import org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger;%0A%0A
 /**%0A * t
@@ -323,16 +323,111 @@
 ager %7B%0A%0A
+%09private static final Logger LOGGER = LogManager.getLogger(ApplicationSecurityManager.class);%0A%0A
 %09public 
@@ -1266,16 +1266,38 @@
 perm) %7B%0A
+%09%09LOGGER.entry(perm);%0A
 %09%09if (!i
@@ -1314,16 +1314,55 @@
 on()) %7B%0A
+%09%09%09LOGGER.exit(%22permission granted.%22);%0A
 %09%09%09retur
@@ -1372,79 +1372,323 @@
 %09%09%7D%0A
+%0A
 %09%09
-%0A%09%09throw new SecurityException();%0A%09%09// TODO: implement security here.
+LOGGER.warn(%22Permission: %7B%7D, denied for application.%22, perm);%0A%09%09LOGGER.exit(%22permission denied.%22);%0A%09%09throw new SecurityException();%0A%09%09// TODO: implement security here.%0A%09%09// file permissions require other methods to be overriden. (it probably is a good idea to handle ALL permissions%0A%09%09// by their other methods.)
 %0A%0A%09%7D

improve logging