001// Copyright 2006, 2010, 2011, 2012, 2014 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.
014package org.apache.tapestry5.services;
015
016import org.apache.tapestry5.ioc.internal.util.InternalUtils;
017
018/**
019 * Used to configure the {@link ComponentClassResolver}, to allow it to map library names to library root packages (the
020 * application namespace is a special case of this). In each case, a prefix on the path is mapped to a package.
021 * <p/>
022 * The root package name should have a number of sub-packages:
023 * <dl>
024 * <dt>pages</dt>
025 * <dd>contains named pages</dd>
026 * <dt>components</dt>
027 * <dd>contains components</dd>
028 * <dt>mixins</dt>
029 * <dd>contains component mixins</dd>
030 * <dt>base</dt>
031 * <dd>contains base classes</dd>
032 * </dl>
033 * <p>
034 * @see ComponentLibraryInfo 
035 */
036public final class LibraryMapping
037{
038    public final String libraryName, rootPackage;
039    
040    /**
041     * Identifies the root package of a library. The application has uses the library name "" (the empty string).
042     * The special library "core" is all the built-in components.
043     * <p/>
044     * The library name is sometimes referred to as the "path prefix" or the "virtual folder name". This is for historical
045     * reasons, as the concept of a library and how it was defined and managed evolved from release to release.
046     * <p/>
047     * The library name should be alpha numeric, and directly encodable into a URL. It may contain slashes (though this is not
048     * used often), but may not start or end with one.
049     * <p/>
050     * Note that it <em>is</em> allowed to contribute multiple LibraryMappings with the library name to the
051     * {@link ComponentClassResolver}, and the results are merged: the single library will have multiple root packages.
052     * Be careful that <em>none</em> of the root packages overlap!
053     *
054     * @param libraryName
055     *         the unique identifier for the library.
056     * @param rootPackage
057     *         the root package to search for classes; sub-packages will include ".pages", ".components", etc.
058     */
059    public LibraryMapping(String libraryName, String rootPackage)
060    {
061        assert libraryName != null;
062        assert InternalUtils.isNonBlank(rootPackage);
063
064        if (libraryName.startsWith("/") || libraryName.endsWith("/"))
065        {
066            throw new IllegalArgumentException(
067                    "Library names may not start with or end with a slash.");
068        }
069
070        this.libraryName = libraryName;
071        this.rootPackage = rootPackage;
072    }
073
074    /**
075     * Returns the library name; the method is oddly named for historical reasons. The library name is sometimes
076     * referred to as the virtual folder name.
077     *
078     * @deprecated In 5.4, use {@link #getLibraryName()} instead.
079     */
080    public String getPathPrefix()
081    {
082        return libraryName;
083    }
084
085    public String getRootPackage()
086    {
087        return rootPackage;
088    }
089    
090    @Override
091    public String toString()
092    {
093        return String.format("LibraryMapping[%s, %s]", libraryName, rootPackage);
094    }
095    
096}