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

atom.content.pm.ApplicationContext Maven / Gradle / Ivy

/*
 * Copyright © 2015 Geeoz, and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * The Research Projects is dual-licensed under the GNU General Public
 * License, version 2.0 (GPLv2) and the Geeoz Commercial License.
 *
 * Solely for non-commercial purposes. A purpose is non-commercial only if
 * it is in no manner primarily intended for or directed toward commercial
 * advantage or private monetary compensation.
 *
 * This Geeoz Software is supplied to you by Geeoz in consideration of your
 * agreement to the following terms, and your use, installation, modification
 * or redistribution of this Geeoz Software constitutes acceptance of these
 * terms. If you do not agree with these terms, please do not use, install,
 * modify or redistribute this Geeoz Software.
 *
 * Neither the name, trademarks, service marks or logos of Geeoz may be used
 * to endorse or promote products derived from the Geeoz Software without
 * specific prior written permission from Geeoz.
 *
 * The Geeoz Software is provided by Geeoz on an "AS IS" basis. GEEOZ MAKES NO
 * WARRANTIES, EXPRESS  OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE, REGARDING THE GEEOZ SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
 * COMBINATION WITH YOUR PRODUCTS.
 *
 * IN NO EVENT SHALL GEEOZ BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION
 * AND/OR DISTRIBUTION OF THE GEEOZ SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER
 * THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
 * OTHERWISE, EVEN IF GEEOZ HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * A copy of the GNU General Public License is included in the distribution in
 * the file LICENSE and at
 *
 *     http://www.gnu.org/licenses/gpl-2.0.html
 *
 * If you are using the Research Projects for commercial purposes, we
 * encourage you to visit
 *
 *     http://products.geeoz.com/license
 *
 * for more details.
 *
 * This software or hardware and documentation may provide access to
 * or information on content, products, and services from third parties.
 * Geeoz and its affiliates are not responsible for and expressly disclaim
 * all warranties of any kind with respect to third-party content, products,
 * and services. Geeoz and its affiliates will not be responsible for any loss,
 * costs, or damages incurred due to your access to or use of third-party
 * content, products, or services. If a third-party content exists, the
 * additional copyright notices and license terms applicable to portions of the
 * software are set forth in the THIRD_PARTY_LICENSE_README file.
 *
 * Please contact Geeoz or visit www.geeoz.com if you need additional
 * information or have any questions.
 */

package atom.content.pm;

import atom.app.AuditEvent;
import atom.app.AuditManager;
import atom.content.Context;
import atom.security.CompositePolicy;

import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import javax.security.auth.spi.LoginModule;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.security.Policy;
import java.security.URIParameter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.ServiceLoader;

/**
 * Special initialization class for the Atom Framework.
 *
 * @author Alex Voloshyn
 * @version 1.9 2/22/15
 */
public final class ApplicationContext {
    /**
     * Audit manager service.
     */
    private static final AuditManager AUDIT =
            Context.getSystemService(AuditManager.class);
    /**
     * Boolean flag for system initialization status.
     */
    private static volatile boolean initialized = false;
    /**
     * Atom application manifest XML file path.
     */
    private static final String MANIFEST = "META-INF/manifest.atom.xml";

    /**
     * Private constructor for non-instantiability.
     */
    private ApplicationContext() {
        super();
    }

    /**
     * Retrieve initialize status of the system.
     *
     * @return initialize status of the system
     */
    public static boolean isInitialized() {
        return initialized;
    }

    /**
     * Initialize this system. After this method call, system stars deploy all
     * available applications.
     */
    public static void initialize() {
        if (!isInitialized()) {

            // REGISTER LOGIN MODULES
            final List configs = new ArrayList<>();
            final ServiceLoader services =
                    ServiceLoader.load(LoginModule.class);
            if (services.iterator().hasNext()) {
                final LoginModule module = services.iterator().next();
                final String moduleName = module.getClass().getName();
                AUDIT.log(AuditEvent.create(module,
                        AuditEvent.SYSTEM, AuditEvent.CONFIG,
                        "Register login module: {0}", moduleName));
                final AppConfigurationEntry entry = new AppConfigurationEntry(
                        moduleName,
                        AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
                        Collections.emptyMap());
                configs.add(entry);
            }
            final AppConfigurationEntry[] array =
                    configs.toArray(new AppConfigurationEntry[configs.size()]);
            Configuration.setConfiguration(new Configuration() {
                @Override
                public AppConfigurationEntry[] getAppConfigurationEntry(
                        final String name) {
                    return array;
                }
            });

            // REGISTER POLICY
            final Policy defaultPolicy = Policy.getPolicy();
            final List policies = new ArrayList<>();
            policies.add(defaultPolicy);

            Enumeration urls;
            try {
                urls = Thread.currentThread().getContextClassLoader()
                        .getResources("jaas.policy");
            } catch (IOException e) {
                e.printStackTrace();
                urls = Collections.emptyEnumeration();
            }

            while (urls.hasMoreElements()) {
                final URL url = urls.nextElement();
                try {
                    final URIParameter uri = new URIParameter(url.toURI());
                    final Policy policy = Policy.getInstance("JavaPolicy", uri);
                    AUDIT.log(AuditEvent.create(policy,
                            AuditEvent.SYSTEM, AuditEvent.CONFIG,
                            "Register policy file: {0}", url.getFile()));
                    policies.add(policy);
                } catch (URISyntaxException | NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
            }

            final CompositePolicy p = new CompositePolicy(policies);
            Policy.setPolicy(p);

            if (System.getSecurityManager() == null) {
                System.setSecurityManager(new SecurityManager());
            }

            // REGISTER APPLICATIONS
            final PackageManager manager =
                    Context.getSystemService(PackageManager.class);

            // REGISTER SPI APPLICATIONS
            final ServiceLoader apps =
                    ServiceLoader.load(ApplicationInfo.class);
            for (ApplicationInfo app : apps) {
                manager.register(app.getPackageInfo());
                app.onRegister();
            }

            // REGISTER XML APPLICATIONS
            try {
                final JAXBContext context =
                        JAXBContext.newInstance(ApplicationInfo.class);
                final Unmarshaller unmarshaller = context.createUnmarshaller();
                final Enumeration xmls = Thread.currentThread()
                        .getContextClassLoader().getResources(MANIFEST);
                while (xmls.hasMoreElements()) {
                    final URL xml = xmls.nextElement();
                    final ApplicationInfo app =
                            (ApplicationInfo) unmarshaller.unmarshal(xml);
                    manager.register(app.getPackageInfo());
                    app.onRegister();
                }
            } catch (IOException | JAXBException e) {
                e.printStackTrace();
            }

            // CALL OnRegisterListener SPI
            final ServiceLoader listeners =
                    ServiceLoader.load(ApplicationInfo.OnInitListener.class);
            listeners.forEach(ApplicationInfo.OnInitListener::onInit);

            initialized = true;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy