001/* A simple provider that gets a list of classes to be provided in its constructor. 002 003Copyright (c) 2015-2016 The Regents of the University of California; iSencia Belgium NV. 004All rights reserved. 005 006Permission is hereby granted, without written agreement and without 007license or royalty fees, to use, copy, modify, and distribute this 008software and its documentation for any purpose, provided that the above 009copyright notice and the following two paragraphs appear in all copies 010of this software. 011 012IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA LIABLE TO ANY PARTY 013FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016SUCH DAMAGE. 017 018THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023ENHANCEMENTS, OR MODIFICATIONS. 024 025PT_COPYRIGHT_VERSION_2 026COPYRIGHTENDKEY 027*/ 028package org.ptolemy.classloading.osgi; 029 030import org.ptolemy.classloading.ModelElementClassProvider; 031import org.ptolemy.commons.VersionSpecification; 032 033import ptolemy.kernel.util.NamedObj; 034 035/////////////////////////////////////////////////////////////////// 036//// DefaultModelElementClassProvider 037 038/** 039 * A simple provider that gets a list of classes to be provided in its constructor. 040 * 041 * @author erwinDL 042 * @version $Id$ 043 * @since Ptolemy II 11.0 044 * @Pt.ProposedRating Red (erwinDL) 045 * @Pt.AcceptedRating Red (reviewmoderator) 046 */ 047public class DefaultModelElementClassProvider 048 implements ModelElementClassProvider { 049 050 /** 051 * Create a provider that does not care about class versions, 052 * i.e. it will only check on class names to check if it can provide 053 * a requested class. 054 * 055 * @param knownClasses The known classes 056 */ 057 @SafeVarargs 058 public DefaultModelElementClassProvider( 059 Class<? extends NamedObj>... knownClasses) { 060 this(null, knownClasses); 061 } 062 063 /** 064 * Create a provider that cares about class versions, i.e. it will 065 * check on class names and on the requested version to check if it 066 * can provide a requested class. 067 * 068 * @param version if null, the provider will not care about versions 069 * @param knownClasses The known classes 070 */ 071 @SafeVarargs 072 public DefaultModelElementClassProvider(VersionSpecification version, 073 Class<? extends NamedObj>... knownClasses) { 074 _knownClasses = knownClasses; 075 _versionSpec = version; 076 } 077 078 /////////////////////////////////////////////////////////////////// 079 //// public methods //// 080 /** Get a class by name and version. 081 * @param className The class name 082 * @param versionSpec The version 083 * @return The class 084 * @exception ClassNotFoundException If the class is not found. 085 */ 086 @Override 087 public Class<? extends NamedObj> getClass(String className, 088 VersionSpecification versionSpec) throws ClassNotFoundException { 089 if (_versionSpec != null && _versionSpec.compareTo(versionSpec) < 0) { 090 throw new ClassNotFoundException(className + " " + versionSpec); 091 } else { 092 for (Class<? extends NamedObj> knownClass : _knownClasses) { 093 if (knownClass.getName().equals(className)) { 094 return knownClass; 095 } 096 } 097 throw new ClassNotFoundException(className); 098 } 099 } 100 101 /////////////////////////////////////////////////////////////////// 102 //// private variables //// 103 104 private VersionSpecification _versionSpec; 105 private Class<? extends NamedObj>[] _knownClasses; 106}