001/* A package-based provider that gets a list of classes to be provided in its constructor.
002
003   Copyright (c) 2015-2016 The Regents of the University of California; iSencia Belgium NV.
004   All rights reserved.
005
006   Permission is hereby granted, without written agreement and without
007   license or royalty fees, to use, copy, modify, and distribute this
008   software and its documentation for any purpose, provided that the above
009   copyright notice and the following two paragraphs appear in all copies
010   of this software.
011
012   IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA LIABLE TO ANY PARTY
013   FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014   ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015   THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016   SUCH DAMAGE.
017
018   THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021   PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022   CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023   ENHANCEMENTS, OR MODIFICATIONS.
024
025   PT_COPYRIGHT_VERSION_2
026   COPYRIGHTENDKEY
027*/
028
029package org.ptolemy.classloading.osgi;
030
031import org.ptolemy.classloading.ModelElementClassProvider;
032import org.ptolemy.commons.VersionSpecification;
033
034import ptolemy.kernel.util.NamedObj;
035
036///////////////////////////////////////////////////////////////////
037//// PackageBasedModelElementClassProvider
038
039/**
040 *  A package-based provider that gets a list of classes to be provided in its constructor.
041 *
042 *  @author erwinDL
043 *  @version $Id$
044 *  @since Ptolemy II 11.0
045 *  @Pt.ProposedRating Red (erwinDL)
046 *  @Pt.AcceptedRating Red (reviewmoderator)
047 */
048public class PackageBasedModelElementClassProvider
049        implements ModelElementClassProvider {
050
051    /** Create a package-based model element class provider.
052     *  @param classLoader The class loader.
053     *  @param packageNames The package names.
054     */
055    public PackageBasedModelElementClassProvider(ClassLoader classLoader,
056            String... packageNames) {
057        _classLoader = classLoader;
058        _packageNames = packageNames;
059    }
060
061    ///////////////////////////////////////////////////////////////////
062    ////                         public methods                    ////
063
064    /**
065     * Return the requested class for the requested version (if specified).
066     *
067     * <p>If this provider doesn't have this class available, it should
068     * throw a <code>ClassNotFoundException</code>.  (Optionally, it
069     * could also just return null, for those dvp-ers who don't like
070     * exceptions. ;-) )</p>
071     *
072     * @param className typically a fully qualified Java class name. Mandatory non-null.
073     * @param versionSpec optional constraint on desired version for
074     * the class that must be provided. If null, no version constraint
075     * is imposed.
076     * @return the concrete class of the <code>NamedObj</code> matching the given className.
077     * @exception ClassNotFoundException if this provider can not provide
078     * the requested class for the requested version (if specified).
079     *
080     */
081    @Override
082    public Class<? extends NamedObj> getClass(String className,
083            VersionSpecification versionSpec) throws ClassNotFoundException {
084        if (_packageNames != null) {
085            boolean packageNameMatch = false;
086            for (String packageName : _packageNames) {
087                packageNameMatch = className.startsWith(packageName);
088                if (packageNameMatch) {
089                    break;
090                }
091            }
092            if (!packageNameMatch) {
093                throw new ClassNotFoundException();
094            }
095        }
096        return (Class<? extends NamedObj>) _classLoader.loadClass(className);
097    }
098
099    ///////////////////////////////////////////////////////////////////
100    ////                         private variables                 ////
101
102    private String[] _packageNames;
103    private ClassLoader _classLoader;
104}