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 EclipseLayout decodes artifacts into the Eclipse specified 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]-[version]/[name].[type]</code>.
032     * Example; <code>artifact:jar:eclipse/plugins/eclipse-osgi-runtime/core#3.1.0</code>
033     * would return the path <code>eclipse/plugins/eclipse-osgi-runtime-3.1.0/core.jar</code>.
034     *
035     * @author <a href="http://www.dpml.net">Digital Product Management Laboratory</a>
036     * @version 2.0.1
037     */
038    public class EclipseLayout extends Layout
039    {
040        private static final String LAYOUT_IDENTIFIER = "eclipse";
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 LAYOUT_IDENTIFIER;
052        }
053        
054        /**
055         * Return the base path for an artifact.  The base path is derived from
056         * the artifact group and version.  For an artifact group of "metro/cache" and a
057         * version equal to "1.3", the base value will be translated using the pattern
058         * "[group]-[version]" to form "metro/cache-1.3".  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 artifact to resolve the base path from
063         * @return the base path
064         */
065        public final String resolveBase( Artifact artifact )
066        {
067            if( null == artifact.getGroup() )
068            {
069                return artifact.getVersion();
070            }
071            else
072            {
073                return artifact.getGroup() + "-" + artifact.getVersion();
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
083         * <code>artifact:jar:eclipse/plugins/eclipse-osgi-runtime/core#3.1.0</code>
084         * would return the path
085         * <code>eclipse/plugins/eclipse-osgi-runtime-3.1.0/core.jar</code>.
086         *
087         * @param artifact the artifact to resolve the path from
088         * @see #resolveBase
089         * @see #resolveFilename
090         * @return the logical artifact path
091         */
092        public final String resolvePath( Artifact artifact )
093        {
094            return resolveBase( artifact ) + "/" + resolveFilename( artifact );
095        }
096    
097        /**
098         * Return the expanded filename of the artifact.
099         * The filename is expressed as <code>[name].[type]</code>.
100         *
101         * @param artifact the artifact to resolve
102         * @return the artifact expanded filename
103         */
104        public String resolveFilename( Artifact artifact )
105        {
106            String scheme = artifact.getScheme();
107            String filename = resolveBaseFilename( artifact );
108            if( "artifact".equals( scheme ) )
109            {
110                return filename;
111            }
112            else if( "link".equals( scheme ) )
113            {
114                return filename + ".link";
115            }
116            else
117            {
118                final String error = 
119                  "Protocol not recognized: " + scheme;
120                throw new TransitRuntimeException( error );
121            }
122        }
123    
124        /**
125         * Return the expanded filename of the artifact.
126         * The filename is expressed as <code>[name].[type]</code>.
127         *
128         * @param artifact the artifact to resolve
129         * @return the artifact expanded filename
130         */
131        public String resolveBaseFilename( Artifact artifact )
132        {
133            return artifact.getName() + "." + artifact.getType();
134        }
135    
136       /**
137        * Compare this object with another for equality.
138        * @param other the object to compare with this object
139        * @return true if the objects are equal
140        */
141        public boolean equals( Object other )
142        {
143            if( null == other )
144            {
145                return false;
146            }
147            else
148            {
149                return getClass().equals( other.getClass() );
150            }
151        }
152    
153       /**
154        * Compare the object hash code.
155        * @return the hash value
156        */
157        public int hashCode()
158        {
159            return getClass().hashCode();
160        }
161    }