001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019
020package icy.plugin.classloader;
021
022/**
023 * General configuration using System properties
024 * 
025 * @author Kamran Zafar
026 * 
027 */
028public class Configuration {
029
030    private static final String JCL_SUPPRESS_COLLISION_EXCEPTION = "jcl.suppressCollisionException";
031    private static final String JCL_SUPPRESS_MISSING_RESOURCE_EXCEPTION = "jcl.suppressMissingResourceException";
032    private static final String AUTO_PROXY = "jcl.autoProxy";
033
034    /**
035     * OSGi boot delegation
036     */
037    private static final String OSGI_BOOT_DELEGATION = "osgi.bootdelegation";
038    private static final String OSGI_BOOT_DELEGATION_STRICT = "osgi.bootdelegation.strict";
039    private static final String OSGI_BOOT_DELEGATION_CLASSES = "org.osgi.framework.bootdelegation";
040
041    public static boolean suppressCollisionException() {
042        if (System.getProperty( JCL_SUPPRESS_COLLISION_EXCEPTION ) == null)
043            return true;
044
045        return Boolean.parseBoolean( System.getProperty( JCL_SUPPRESS_COLLISION_EXCEPTION ) );
046    }
047
048    public static boolean suppressMissingResourceException() {
049        if (System.getProperty( JCL_SUPPRESS_MISSING_RESOURCE_EXCEPTION ) == null)
050            return true;
051
052        return Boolean.parseBoolean( System.getProperty( JCL_SUPPRESS_MISSING_RESOURCE_EXCEPTION ) );
053    }
054
055    public static boolean autoProxy() {
056        if (System.getProperty( AUTO_PROXY ) == null) {
057            return false;
058        }
059
060        return Boolean.parseBoolean( System.getProperty( AUTO_PROXY ) );
061    }
062
063    @SuppressWarnings("unchecked")
064    public static boolean isLoaderEnabled(Class cls) {
065        if (System.getProperty( cls.getName() ) == null)
066            return true;
067
068        return Boolean.parseBoolean( System.getProperty( cls.getName() ) );
069    }
070
071    public static boolean isSystemLoaderEnabled() {
072        return isLoaderEnabled( AbstractClassLoader.SystemLoader.class );
073    }
074
075    public static boolean isParentLoaderEnabled() {
076        return isLoaderEnabled( AbstractClassLoader.ParentLoader.class );
077    }
078
079    public static boolean isCurrentLoaderEnabled() {
080        return isLoaderEnabled( AbstractClassLoader.CurrentLoader.class );
081    }
082
083    public static boolean isLocalLoaderEnabled() {
084        return isLoaderEnabled( JarClassLoader.LocalLoader.class );
085    }
086
087    public static boolean isThreadContextLoaderEnabled() {
088        if (System.getProperty( AbstractClassLoader.ThreadContextLoader.class.getName() ) == null)
089            return false;
090
091        return isLoaderEnabled( AbstractClassLoader.ThreadContextLoader.class );
092    }
093
094    public static boolean isOsgiBootDelegationEnabled() {
095        if (System.getProperty( OSGI_BOOT_DELEGATION ) == null)
096            return false;
097
098        return Boolean.parseBoolean( System.getProperty( OSGI_BOOT_DELEGATION ) );
099    }
100
101    public static boolean isOsgiBootDelegationStrict() {
102        if (System.getProperty( OSGI_BOOT_DELEGATION_STRICT ) == null)
103            return true;
104
105        return Boolean.parseBoolean( System.getProperty( OSGI_BOOT_DELEGATION_STRICT ) );
106    }
107
108    public static String[] getOsgiBootDelegation() {
109        if (System.getProperty( OSGI_BOOT_DELEGATION_CLASSES ) == null)
110            return null;
111
112        return System.getProperty( OSGI_BOOT_DELEGATION_CLASSES ).split( "," );
113    }
114}