001/* 002 PtolemyModule loads interface to implementation mappings from the provided 003 ResourceBundle and configures Guice AbstractModule to use those mappings. 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 031import java.util.Enumeration; 032import java.util.HashMap; 033import java.util.ResourceBundle; 034 035/////////////////////////////////////////////////////////////////// 036//// PtolemyModule 037/** 038 * PtolemyModule loads interface to implementation mappings from the provided 039 * ResourceBundle. 040 * 041 * The PtolemyModule is used for creating a PtolemyInjector that is responsible for 042 * dependency injection. The rationale for this class is to promote portability of 043 * the Ptolemy by providing different interface to implementation mappings for different 044 * platforms such as Android and Java SE. 045 * 046 * @author Anar Huseynov, Erwini de Ley 047 * @version $Id$ 048 * @since Ptolemy II 10.0 049 * @Pt.ProposedRating Red (ahuseyno) 050 * @Pt.AcceptedRating Red (ahuseyno) 051 */ 052public class PtolemyModule { 053 054 /** 055 * Create a new instance of the PtolemyModule based on the 056 * provided moduleBundle, and specifying a specific class loader 057 * that should be used to load the implementation classes. 058 * 059 * @param classLoader The ClassLoader 060 * @param moduleBundle The moduleBundle contains mappings from 061 * platform independent interfaces to platform dependent 062 * implementations. The bundle must have key value mappings from 063 * the fully specified interface name to the fully specified class 064 * name. 065 */ 066 public PtolemyModule(ClassLoader classLoader, ResourceBundle moduleBundle) { 067 this(moduleBundle); 068 this._classLoader = classLoader; 069 } 070 071 /** 072 * Create a new instance of the PtolemyModule based on the 073 * provided moduleBundle. 074 * @param moduleBundle The moduleBundle contains mappings from platform independent 075 * interfaces to platform dependent implementations. The bundle must have key value mappings 076 * from the fully specified interface name to the fully specified class name. 077 */ 078 public PtolemyModule(ResourceBundle moduleBundle) { 079 // Key is the interface class name. 080 // Value is the interface implementation class name. 081 // We have to use ResourceBundle.getKeys() method because Android does not support .keySet() method. 082 Enumeration<String> keys = moduleBundle.getKeys(); 083 while (keys.hasMoreElements()) { 084 String key = keys.nextElement(); 085 String value = moduleBundle.getString(key); 086 getBindings().put(key, value); 087 } 088 } 089 090 /** 091 * Return the bindings from interfaces to their implementations. 092 * @return the interface to implementations mappings. 093 */ 094 public HashMap<String, String> getBindings() { 095 return _interfaceToImplementationMap; 096 } 097 098 /** 099 * Return the (optional) specific class loader for the 100 * implementation classes. 101 * @return the (optional) specific class loader for the implementation classes. 102 */ 103 public ClassLoader getClassLoader() { 104 return _classLoader; 105 } 106 107 /////////////////////////////////////////////////////////////////// 108 //// private variables //// 109 110 /** 111 * An optional custom class loader that should be used to load the 112 * mapped implementation classes. 113 */ 114 private ClassLoader _classLoader; 115 116 /** 117 * The mapping from interface to implementation. 118 */ 119 private final HashMap<String, String> _interfaceToImplementationMap = new HashMap<String, String>(); 120}