<commit_before>// Copyright (c) 2003-2012, Jodd Team (jodd.org). All Rights Reserved.

package jodd.joy.core;

import jodd.db.DbDefault;
import jodd.db.DbSessionProvider;
import jodd.db.connection.ConnectionProvider;
import jodd.db.oom.DbOomManager;
import jodd.db.oom.config.AutomagicDbOomConfigurator;
import jodd.db.pool.CoreConnectionPool;
import jodd.joy.exception.AppException;
import jodd.joy.jtx.meta.ReadWriteTransaction;
import jodd.jtx.JtxTransactionManager;
import jodd.jtx.db.DbJtxSessionProvider;
import jodd.jtx.db.DbJtxTransactionManager;
import jodd.jtx.meta.Transaction;
import jodd.jtx.proxy.AnnotationTxAdvice;
import jodd.jtx.proxy.AnnotationTxAdviceManager;
import jodd.jtx.proxy.AnnotationTxAdviceSupport;
import jodd.log.Log;
import jodd.petite.PetiteContainer;
import jodd.petite.config.AutomagicPetiteConfigurator;
import jodd.petite.proxetta.ProxettaAwarePetiteContainer;
import jodd.petite.scope.SessionScope;
import jodd.petite.scope.SingletonScope;
import jodd.props.Props;
import jodd.props.PropsUtil;
import jodd.proxetta.MethodInfo;
import jodd.proxetta.Proxetta;
import jodd.proxetta.ProxyAspect;
import jodd.proxetta.pointcuts.MethodAnnotationPointcut;
import jodd.util.ClassLoaderUtil;
import jodd.util.SystemUtil;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * Default application core frame.
 */
public abstract class DefaultAppCore {

	/**
	 * Properties.
	 */
	public static final String APP_DIR = "app.dir";
	public static final String APP_WEB = "app.web";

	/**
	 * Petite bean names.
	 */
	public static final String PETITE_APPCORE = "app";		// AppCore
	public static final String PETITE_DBPOOL = "dbpool";	// database pool
	public static final String PETITE_DBOOM = "dboom";		// DbOom instance
	public static final String PETITE_APPINIT = "appInit";	// init bean

	/**
	 * Logger. Resolved during initialization.
	 */
	protected static Log log;

	/**
	 * App dir. Resolved during initialization.
	 */
	protected String appDir;

	/**
	 * Is web application. Resolved during initialization.
	 */
	protected boolean isWebApplication;

	/**
	 * Default scanning path that will be examined by various
	 * Jodd auto-magic tools.
	 */
	protected final String[] scanningPath;

	/**
	 * Default constructor.
	 */
	protected DefaultAppCore() {
		scanningPath = resolveScanningPath();
	}

	/**
	 * Defines the scanning path for Jodd frameworks.
	 * Scanning path will contain all classes inside and bellow
	 * the app core packages and 'jodd' packages.
	 */
	protected String[] resolveScanningPath() {
		return new String[] {this.getClass().getPackage().getName() + ".*", "jodd.*"};
	}

	// ---------------------------------------------------------------- init
	
	protected boolean initialized;

	/**
	 * Returns <code>true</code> if application is started as a part of web application.
	 */
	public boolean isWebApplication() {
		return isWebApplication;
	}

	/**
	 * Returns application directory.
	 */
	public String getAppDir() {
		return appDir;
	}

	/**
	 * Initializes system. Invoked *before* anything else!
	 * Override to register types for BeanUtil, DbOom conversions etc.
	 * <p>
	 * May also set the value of <code>appDir</code>.
	 */
	public void init() {
		if (initialized == true) {
			return;
		}
		initialized = true;
		
		if (appDir == null) {
			resolveAppDir("app.props");			// app directory is resolved from location of 'app.props'.
		}

		System.setProperty(APP_DIR, appDir);
		System.setProperty(APP_WEB, Boolean.toString(isWebApplication));

		initLogger();							// logger becomes available after this point

		log.info("app dir: " + appDir);
	}

	/**
	 * Initializes the logger. It must be initialized after the
	 * log path is defined.
	 */
	protected void initLogger() {
		log = Log.getLogger(DefaultAppCore.class);
	}

	/**
	 * Resolves application root folders.
	 * Usually invoked on the very beginning, <b>before</b> application initialization.
	 * <p>
	 * If application is started as web application, app folder is one below the WEB-INF folder.
	 * Otherwise, the root folder is equal to the working folder.
	 */
	protected void resolveAppDir(String classPathFileName) {
		URL url = ClassLoaderUtil.getResourceUrl(classPathFileName);
		if (url == null) {
			throw new AppException("Unable to resolve app dirs, missing: '" + classPathFileName + "'.");
		}
		String protocol = url.getProtocol();


		if (protocol.equals("file") == false) {
			try {
				url = new URL(url.getFile());
			} catch (MalformedURLException ignore) {
			}
		}

		appDir = url.getFile();

		int ndx = appDir.indexOf("WEB-INF");
		isWebApplication = (ndx != -1);

		appDir = isWebApplication ? appDir.substring(0, ndx) : SystemUtil.getWorkingFolder();
	}

	// ---------------------------------------------------------------- start

	/**
	 * Starts the application and performs all initialization.
	 */
	public void start() {

		init();

		try {
			startProxetta();
			startPetite();
			startDb();
			startApp();
			log.info("app started");
		} catch (RuntimeException rex) {
			log.error(rex.toString(), rex);
			try {
				stop();
			} catch (Exception ignore) {
			}
			throw rex;
		}
	}

	/**
	 * Stops the application.
	 */
	public void stop() {
		log.info("shutting down...");
		stopApp();
		stopDb();
		log.info("app stopped");
	}

	// ---------------------------------------------------------------- proxetta

	protected Proxetta proxetta;

	/**
	 * Returns proxetta.
	 */
	public Proxetta getProxetta() {<commit_msg>fix npe when log is not avail.<commit_after>// Copyright (c) 2003-2012, Jodd Team (jodd.org). All Rights Reserved.

package jodd.joy.core;

import jodd.db.DbDefault;
import jodd.db.DbSessionProvider;
import jodd.db.connection.ConnectionProvider;
import jodd.db.oom.DbOomManager;
import jodd.db.oom.config.AutomagicDbOomConfigurator;
import jodd.db.pool.CoreConnectionPool;
import jodd.joy.exception.AppException;
import jodd.joy.jtx.meta.ReadWriteTransaction;
import jodd.jtx.JtxTransactionManager;
import jodd.jtx.db.DbJtxSessionProvider;
import jodd.jtx.db.DbJtxTransactionManager;
import jodd.jtx.meta.Transaction;
import jodd.jtx.proxy.AnnotationTxAdvice;
import jodd.jtx.proxy.AnnotationTxAdviceManager;
import jodd.jtx.proxy.AnnotationTxAdviceSupport;
import jodd.log.Log;
import jodd.petite.PetiteContainer;
import jodd.petite.config.AutomagicPetiteConfigurator;
import jodd.petite.proxetta.ProxettaAwarePetiteContainer;
import jodd.petite.scope.SessionScope;
import jodd.petite.scope.SingletonScope;
import jodd.props.Props;
import jodd.props.PropsUtil;
import jodd.proxetta.MethodInfo;
import jodd.proxetta.Proxetta;
import jodd.proxetta.ProxyAspect;
import jodd.proxetta.pointcuts.MethodAnnotationPointcut;
import jodd.util.ClassLoaderUtil;
import jodd.util.SystemUtil;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * Default application core frame.
 */
public abstract class DefaultAppCore {

	/**
	 * Properties.
	 */
	public static final String APP_DIR = "app.dir";
	public static final String APP_WEB = "app.web";

	/**
	 * Petite bean names.
	 */
	public static final String PETITE_APPCORE = "app";		// AppCore
	public static final String PETITE_DBPOOL = "dbpool";	// database pool
	public static final String PETITE_DBOOM = "dboom";		// DbOom instance
	public static final String PETITE_APPINIT = "appInit";	// init bean

	/**
	 * Logger. Resolved during initialization.
	 */
	protected static Log log;

	/**
	 * App dir. Resolved during initialization.
	 */
	protected String appDir;

	/**
	 * Is web application. Resolved during initialization.
	 */
	protected boolean isWebApplication;

	/**
	 * Default scanning path that will be examined by various
	 * Jodd auto-magic tools.
	 */
	protected final String[] scanningPath;

	/**
	 * Default constructor.
	 */
	protected DefaultAppCore() {
		scanningPath = resolveScanningPath();
	}

	/**
	 * Defines the scanning path for Jodd frameworks.
	 * Scanning path will contain all classes inside and bellow
	 * the app core packages and 'jodd' packages.
	 */
	protected String[] resolveScanningPath() {
		return new String[] {this.getClass().getPackage().getName() + ".*", "jodd.*"};
	}

	// ---------------------------------------------------------------- init
	
	protected boolean initialized;

	/**
	 * Returns <code>true</code> if application is started as a part of web application.
	 */
	public boolean isWebApplication() {
		return isWebApplication;
	}

	/**
	 * Returns application directory.
	 */
	public String getAppDir() {
		return appDir;
	}

	/**
	 * Initializes system. Invoked *before* anything else!
	 * Override to register types for BeanUtil, DbOom conversions etc.
	 * <p>
	 * May also set the value of <code>appDir</code>.
	 */
	public void init() {
		if (initialized == true) {
			return;
		}
		initialized = true;
		
		if (appDir == null) {
			resolveAppDir("app.props");			// app directory is resolved from location of 'app.props'.
		}

		System.setProperty(APP_DIR, appDir);
		System.setProperty(APP_WEB, Boolean.toString(isWebApplication));

		initLogger();							// logger becomes available after this point

		log.info("app dir: " + appDir);
	}

	/**
	 * Initializes the logger. It must be initialized after the
	 * log path is defined.
	 */
	protected void initLogger() {
		log = Log.getLogger(DefaultAppCore.class);
	}

	/**
	 * Resolves application root folders.
	 * Usually invoked on the very beginning, <b>before</b> application initialization.
	 * <p>
	 * If application is started as web application, app folder is one below the WEB-INF folder.
	 * Otherwise, the root folder is equal to the working folder.
	 */
	protected void resolveAppDir(String classPathFileName) {
		URL url = ClassLoaderUtil.getResourceUrl(classPathFileName);
		if (url == null) {
			throw new AppException("Unable to resolve app dirs, missing: '" + classPathFileName + "'.");
		}
		String protocol = url.getProtocol();


		if (protocol.equals("file") == false) {
			try {
				url = new URL(url.getFile());
			} catch (MalformedURLException ignore) {
			}
		}

		appDir = url.getFile();

		int ndx = appDir.indexOf("WEB-INF");
		isWebApplication = (ndx != -1);

		appDir = isWebApplication ? appDir.substring(0, ndx) : SystemUtil.getWorkingFolder();
	}

	// ---------------------------------------------------------------- start

	/**
	 * Starts the application and performs all initialization.
	 */
	public void start() {

		init();

		try {
			startProxetta();
			startPetite();
			startDb();
			startApp();
			log.info("app started");
		} catch (RuntimeException rex) {
			if (log != null) {
				log.error(rex.toString(), rex);
			} else {
				System.out.println(rex.toString());
				rex.printStackTrace();
			}
			try {
				stop();
			} catch (Exception ignore) {
			}
			throw rex;
		}
	}

	/**
	 * Stops the application.
	 */
	public void stop() {
		log.info("shutting down...");
		stopApp();
		stopDb();
		log.info("app stopped");
	}

	// ---------------------------------------------------------------- proxetta

	protected Proxetta proxetta;

	/**
	 * Returns proxetta.
	 */
	public Proxetta getProxetta() {
{'fraction_non_alphanumeric': 0.14990084730484945, 'fraction_numerical': 0.0018027762754642149, 'mean_word_length': 3.2042440318302385, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2268594', 'n_tokens_mistral': 3705, 'n_tokens_neox': 3318, 'n_words': 908}