001/*
002 PtolemyInjector contains a static reference to the Injector loaded with Ptolemy Modules
003 for the given target platform.
004
005 Copyright (c) 2011-2016 The Regents of the University of California.
006 All rights reserved.
007 Permission is hereby granted, without written agreement and without
008 license or royalty fees, to use, copy, modify, and distribute this
009 software and its documentation for any purpose, provided that the above
010 copyright notice and the following two paragraphs appear in all copies
011 of this software.
012
013 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
014 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
015 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
016 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
017 SUCH DAMAGE.
018
019 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
020 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
021 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
022 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
023 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
024 ENHANCEMENTS, OR MODIFICATIONS.
025
026 PT_COPYRIGHT_VERSION_2
027 COPYRIGHTENDKEY
028 */
029package ptolemy.actor.injection;
030
031///////////////////////////////////////////////////////////////////
032//// PtolemyInjector
033/**
034 * PtolemyInjector contains a static reference to the Injector loaded with Ptolemy Modules
035 * for the given target platform.
036 * The rationale for having a static reference is to avoid hurdle of passing
037 * the injector to all needed parties.
038 *
039 * @author Anar Huseynov, Erwin de Ley
040 * @version $Id$
041 * @since Ptolemy II 10.0
042 * @Pt.ProposedRating Red (ahuseyno)
043 * @Pt.AcceptedRating Red (ahuseyno)
044 */
045public class PtolemyInjector {
046
047    ///////////////////////////////////////////////////////////////////
048    ////                         public methods                    ////
049
050    /**
051     * Create an injector for the given set of modules.
052     * @param modules the array of modules that contain interface to implementation bindings
053     * used to implement dependency injection.
054     */
055    public static synchronized void createInjector(PtolemyModule... modules) {
056        _instance = new Injector();
057        for (PtolemyModule ptolemyModule : modules) {
058            _instance.loadMappings(ptolemyModule);
059        }
060    }
061
062    /**
063     * Create an injector for the given set of modules.
064     * @param modules the array of modules that contain interface to implementation bindings
065     * used to implement dependency injection.
066     */
067    public static synchronized void createInjector(
068            Iterable<? extends PtolemyModule> modules) {
069        _instance = new Injector();
070        for (PtolemyModule ptolemyModule : modules) {
071            _instance.loadMappings(ptolemyModule);
072        }
073    }
074
075    /**
076     * Return the PtolemyInjector.  Note that {@link #createInjector(PtolemyModule...)}
077     * must be called prior to using this method.
078     * @return the PtolemyInjector that was created with the supplied modules.
079     */
080    public static Injector getInjector() {
081        if (_instance == null) {
082            ActorModuleInitializer.initializeInjector();
083        }
084        return _instance;
085    }
086
087    ///////////////////////////////////////////////////////////////////
088    ////                         private variables                 ////
089
090    /**
091     * The Injector instance that is shared with all users of the PtolemyInjector class.
092     */
093    private static volatile Injector _instance;
094}