001 /*
002 * Copyright 2004-2005 Stephen J. McConnell.
003 * Copyright 2004 Niclas Hedhman.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
014 * implied.
015 *
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020 package net.dpml.transit.layout;
021
022 import net.dpml.transit.Artifact;
023 import net.dpml.transit.Layout;
024 import net.dpml.transit.TransitRuntimeException;
025
026 /**
027 * The ClassicLayout decodes artifacts into the Classic/Maven layout
028 * of artifacts on a file system or http server.
029 * This format says that for an artifact <code>artifact:[type]:[group]/[name]#[version]</code>
030 * the location of such artifact would be;
031 * <code>[group]/[type]s/[name]-[version].[type]</code>.
032 * Example; <code>artifact:jar:metro/cache/dpml-cache-main#1.0.0</code>
033 * would return the path <code>metro/cache/jars/dpml-cache-main-1.0.0.jar</code>.
034 *
035 * @author <a href="http://www.dpml.net">Digital Product Management Library</a>
036 * @version 2.1.1
037 */
038 public class ClassicLayout extends Layout
039 {
040 private static final String CLASSIC_IDENTIFIER = "classic";
041
042 /**
043 * Return the layout identifier. The id value is used
044 * to identify layout instances assigned to cache handlers and
045 * resource host handlers.
046 *
047 * @return the layout id
048 */
049 public String getID()
050 {
051 return CLASSIC_IDENTIFIER;
052 }
053
054 /**
055 * Return the base path for an artifact. The base path is derived from
056 * the artifact group and type. For an artifact group of "metro/cache" and a
057 * type equal to "jar", the base value will be translated using the pattern
058 * "[group]/[type]s" to form "metro/cache/jars". The base path value represents
059 * the directory path relative to a repository root of the directory containing
060 * this artifact.
061 *
062 * @param artifact the resource artifact
063 * @return the base path
064 */
065 public final String resolveBase( Artifact artifact )
066 {
067 if( null == artifact.getGroup() )
068 {
069 return artifact.getType() + "s";
070 }
071 else
072 {
073 return artifact.getGroup() + "/" + artifact.getType() + "s";
074 }
075 }
076
077 /**
078 * Returns the full path of the artifact relative to a logical root directory.
079 * The full path is equivalent to the base path and artifact filename using the
080 * pattern "[base]/[filename]". Path values may be used to resolve an artifact
081 * from a remote repository or local cache relative to the repository or cache
082 * root. An artifact such as <code>artifact:jar:metro/cache/dpml-cache-main#1.0.0</code>
083 * would return the path <code>metro/cache/jars/dpml-cache-main-1.0.0.jar</code>.
084 *
085 * @param artifact the resource artifact
086 * @see #resolveBase
087 * @see #resolveFilename
088 * @return the logical artifact path
089 */
090 public final String resolvePath( Artifact artifact )
091 {
092 return resolveBase( artifact ) + "/" + resolveFilename( artifact );
093 }
094
095 /**
096 * Return the expanded filename of the artifact. The filename is expressed
097 * as [name]-[version].[type] or in case of a null version simply [name].[type].
098 *
099 * @param artifact the resource artifact
100 * @return the artifact expanded filename
101 */
102 public String resolveFilename( Artifact artifact )
103 {
104 String scheme = artifact.getScheme();
105 String filename = resolveBaseFilename( artifact );
106 if( "artifact".equals( scheme ) )
107 {
108 return filename;
109 }
110 else if( "link".equals( scheme ) )
111 {
112 return filename + ".link";
113 }
114 else
115 {
116 final String error =
117 "Protocol not recognized: " + scheme;
118 throw new TransitRuntimeException( error );
119 }
120 }
121
122 private String resolveBaseFilename( Artifact artifact )
123 {
124 String ver = artifact.getVersion();
125 if( null == ver )
126 {
127 return artifact.getName() + "." + artifact.getType();
128 }
129 else
130 {
131 return artifact.getName() + "-" + ver + "." + artifact.getType();
132 }
133 }
134 }