001/*
002 This class initialized the PtolemyInjector with Java SE specific interface to
003 implementation mappings that are within Ptolemy package.
004
005 Copyright (c) 2011-2014 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 */
029
030package ptolemy.actor.injection;
031
032import java.util.ArrayList;
033import java.util.ResourceBundle;
034
035///////////////////////////////////////////////////////////////////
036//// ActorModuleInitializer
037/**
038 * Initializer of the PtolemyInjector with Java SE specific actor interface to
039 * implementation mappings.  This class reads the ptolemy.actor.ActorModule.properties
040 * file to initialize the PtolemyModule.
041 * @author Anar Huseynov
042 * @version $Id$
043 * @since Ptolemy II 10.0
044 * @Pt.ProposedRating Red (ahuseyno)
045 * @Pt.AcceptedRating Red (ahuseyno)
046 */
047public class ActorModuleInitializer {
048
049    /**
050     * Create instance that initializes the PtolemyInjector.
051     */
052    public ActorModuleInitializer() {
053        initializeInjector();
054    }
055
056    ///////////////////////////////////////////////////////////////////
057    ////                         public methods                    ////
058    /**
059     * Get Modules used by the initializer.
060     * @return get Modules used by the initializer.
061     */
062    public static ArrayList<PtolemyModule> getModules() {
063        return _PTOLEMY_MODULES;
064    }
065
066    /**
067     * Initialize the PtolemyInjector using module definitions from
068     * the ptolemy.actor.ActorModule.properties file.
069     */
070    public synchronized static void initializeInjector() {
071        if (!_isInitialized) {
072            _initializer.initialize();
073            _isInitialized = true;
074        }
075    }
076
077    /** Set the initializer.
078     *  @param initializer The initializer.
079     */
080    public static void setInitializer(Initializer initializer) {
081        if (initializer == null) {
082            throw new NullPointerException("Initializer must be non-null");
083        }
084        _initializer = initializer;
085    }
086
087    ///////////////////////////////////////////////////////////////////
088    ////                         private variables                 ////
089    /**
090     * Flag indicating whether the injector is initialized.
091     */
092    private static volatile boolean _isInitialized;
093
094    /**
095     * Modules used by the initializer
096     */
097    private static final ArrayList<PtolemyModule> _PTOLEMY_MODULES = new ArrayList<PtolemyModule>();
098    static {
099        _PTOLEMY_MODULES.add(new PtolemyModule(
100                ResourceBundle.getBundle("ptolemy.actor.ActorModule")));
101    }
102
103    /**
104     * The default initializer used by the PtolemyInjector if one is not provided to
105     * it.  The default initializer would initialize Java SE specific classes.
106     */
107    protected static Initializer _defaultInitializer = new Initializer() {
108        @Override
109        public void initialize() {
110            PtolemyInjector.createInjector(_PTOLEMY_MODULES);
111        }
112    };
113
114    private static Initializer _initializer = _defaultInitializer;
115
116    /**
117     * Initializer is responsible for initializing the PtolemyInjector with
118     * modules specific to the platform it was developed for.
119     *
120     */
121    public interface Initializer {
122        /**
123         * Initialize the PtolemyInjector with modules specific to its platform.
124         */
125        public void initialize();
126    }
127}