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