001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005// http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012package org.apache.tapestry5.services;
013
014import org.apache.tapestry5.ioc.internal.util.InternalUtils;
015
016/**
017 * Used to configure the {@link ComponentClassResolver}, to allow it to map library names to library root packages (the
018 * application namespace is a special case of this). In each case, a prefix on the path is mapped to a package.
019 *
020 * The root package name should have a number of sub-packages:
021 * <dl>
022 * <dt>pages</dt>
023 * <dd>contains named pages</dd>
024 * <dt>components</dt>
025 * <dd>contains components</dd>
026 * <dt>mixins</dt>
027 * <dd>contains component mixins</dd>
028 * <dt>base</dt>
029 * <dd>contains base classes</dd>
030 * </dl>
031 *
032 * @see ComponentLibraryInfo 
033 */
034public final class LibraryMapping
035{
036    public final String libraryName, rootPackage;
037    
038    /**
039     * Identifies the root package of a library. The application has uses the library name "" (the empty string).
040     * The special library "core" is all the built-in components.
041     *
042     * The library name is sometimes referred to as the "path prefix" or the "virtual folder name". This is for historical
043     * reasons, as the concept of a library and how it was defined and managed evolved from release to release.
044     *
045     * The library name should be alpha numeric, and directly encodable into a URL. It may contain slashes (though this is not
046     * used often), but may not start or end with one.
047     *
048     * Note that it <em>is</em> allowed to contribute multiple LibraryMappings with the library name to the
049     * {@link ComponentClassResolver}, and the results are merged: the single library will have multiple root packages.
050     * Be careful that <em>none</em> of the root packages overlap!
051     *
052     * @param libraryName
053     *         the unique identifier for the library.
054     * @param rootPackage
055     *         the root package to search for classes; sub-packages will include ".pages", ".components", etc.
056     */
057    public LibraryMapping(String libraryName, String rootPackage)
058    {
059        assert libraryName != null;
060        assert InternalUtils.isNonBlank(rootPackage);
061
062        if (libraryName.startsWith("/") || libraryName.endsWith("/"))
063        {
064            throw new IllegalArgumentException(
065                    "Library names may not start with or end with a slash.");
066        }
067
068        this.libraryName = libraryName;
069        this.rootPackage = rootPackage;
070    }
071
072    /**
073     * Returns the library name; the method is oddly named for historical reasons. The library name is sometimes
074     * referred to as the virtual folder name.
075     *
076     * @deprecated In 5.4, use {@link #libraryName} instead.
077     */
078    public String getPathPrefix()
079    {
080        return libraryName;
081    }
082
083    public String getRootPackage()
084    {
085        return rootPackage;
086    }
087    
088    @Override
089    public String toString()
090    {
091        return String.format("LibraryMapping[%s, %s]", libraryName, rootPackage);
092    }
093    
094}