All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.mhus.lib.core.MSingleton Maven / Gradle / Ivy

There is a newer version: 6.3.1
Show newest version
package de.mhus.lib.core;

import java.util.UUID;
import java.util.WeakHashMap;

import de.mhus.lib.core.config.HashConfig;
import de.mhus.lib.core.configupdater.ConfigUpdater;
import de.mhus.lib.core.directory.ResourceNode;
import de.mhus.lib.core.logging.LevelMapper;
import de.mhus.lib.core.logging.Log;
import de.mhus.lib.core.logging.TrailLevelMapper;
import de.mhus.lib.core.system.DefaultSingleton;
import de.mhus.lib.core.system.DummyClass;
import de.mhus.lib.core.system.ISingleton;
import de.mhus.lib.core.system.ISingletonFactory;
import de.mhus.lib.core.system.SingletonInitialize;

/**
 * 

MSingleton class.

* * @author mikehummel * @version $Id: $Id */ public class MSingleton { private static ISingleton singleton; /** Constant trace */ protected static Boolean trace; private static WeakHashMap loggers = new WeakHashMap<>(); private static ResourceNode emptyConfig = null; private static ConfigUpdater configUpdater; // private static DummyClass dummy = new DummyClass(); // the class is inside this bundle and has the correct class loader private MSingleton() {} /** *

get.

* * @return a {@link de.mhus.lib.core.system.ISingleton} object. */ public static synchronized ISingleton get() { if (singleton == null) { try { ISingleton obj = null; String path = "de.mhus.lib.mutable.SingletonFactory"; if (System.getProperty("mhu.lib.singleton.factory") != null) path = System.getProperty(MConstants.PROP_SINGLETON_FACTORY_CLASS); if (isDirtyTrace()) System.out.println("--- MSingletonFactory:" + path); ISingletonFactory factory = (ISingletonFactory)Class.forName(path).newInstance(); if (factory != null) { obj = factory.createSingleton(); } singleton = obj; } catch (Throwable t) { if (isDirtyTrace()) t.printStackTrace(); } if (singleton == null) singleton = new DefaultSingleton(); if (isDirtyTrace()) System.out.println("--- MSingleton: " + singleton.getClass().getCanonicalName()); if (singleton instanceof SingletonInitialize) ((SingletonInitialize)singleton).doInitialize(DummyClass.class.getClassLoader()); } return singleton; } /** *

isDirtyTrace.

* * @return a boolean. */ public static boolean isDirtyTrace() { if (trace == null) trace = "true".equals(System.getProperty(MConstants.PROP_DIRTY_TRACE)); return trace; } /** *

setDirtyTrace.

* * @param dt a boolean. */ public static void setDirtyTrace(boolean dt) { trace = dt; } /** *

isTrace.

* * @param name a {@link java.lang.String} object. * @return a boolean. */ public static boolean isTrace(String name) { if (isDirtyTrace()) System.out.println("--- Ask for trace: " + name); // String value = System.getProperty(name+".trace"); // if (value != null) return "true".equals(value); return get().isTrace(name); } /** *

doStartTrailLog.

* * @since 3.2.9 */ public static void doStartTrailLog() { LevelMapper mapper = get().getLogFactory().getLevelMapper(); if (mapper != null && mapper instanceof TrailLevelMapper) ((TrailLevelMapper)mapper).doConfigureTrail("MAP"); } /** *

doStopTrailLog.

* * @since 3.2.9 */ public static void doStopTrailLog() { LevelMapper mapper = get().getLogFactory().getLevelMapper(); if (mapper != null && mapper instanceof TrailLevelMapper) ((TrailLevelMapper)mapper).doResetTrail(); } /** *

registerLogger.

* * @param log a {@link de.mhus.lib.core.logging.Log} object. * @since 3.2.9 */ public static void registerLogger(Log log) { synchronized (loggers) { loggers.put(log.getId(), log); } } /** *

unregisterLogger.

* * @param log a {@link de.mhus.lib.core.logging.Log} object. * @since 3.2.9 */ public static void unregisterLogger(Log log) { synchronized (loggers) { loggers.remove(log.getId()); } } /** *

updateLoggers.

* * @since 3.2.9 */ public static void updateLoggers() { try { synchronized (loggers) { for (UUID logId : loggers.keySet().toArray(new UUID[loggers.size()])) loggers.get(logId).update(); } } catch(Throwable t) { if (MSingleton.isDirtyTrace()) t.printStackTrace(); } } /** *

getConfig.

* * @param owner a {@link java.lang.Object} object. * @return a {@link de.mhus.lib.core.directory.ResourceNode} object. * @since 3.2.9 */ public static ResourceNode getConfig(Object owner) { if (emptyConfig == null) emptyConfig = new HashConfig(); return get().getConfigProvider().getConfig(owner, emptyConfig); } /** *

Getter for the field configUpdater.

* * @return a {@link de.mhus.lib.core.configupdater.ConfigUpdater} object. * @since 3.2.9 */ public static synchronized ConfigUpdater getConfigUpdater() { if (configUpdater == null) configUpdater = new ConfigUpdater(); return configUpdater; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy