001    /*
002     * Copyright 2005 Stephen McConnell
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.transit.tools;
020    
021    import java.io.File;
022    import java.net.URI;
023    import java.net.URL;
024    import java.net.URLConnection;
025    
026    import net.dpml.transit.Artifact;
027    
028    import org.apache.tools.ant.BuildException;
029    import org.apache.tools.ant.Project;
030    
031    /**
032     * The get task handles the retrival of a rresource and the binding of the resource filename
033     * to a project property.
034     *
035     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
036     * @version 1.0.3
037     */
038    public class GetTask extends TransitTask
039    {
040       /**
041        * The uri of the artifact to load.
042        */
043        private String m_uri;
044    
045       /**
046        * The name of a property under which the locally cached artifact filename will be bound.
047        */
048        private String m_name;
049    
050       /**
051        * Set the project.
052        * @param project the current project
053        */
054        public void setProject( Project project )
055        {
056            setTaskName( "get" );
057            super.setProject( project );
058        }
059    
060       /**
061        * Set the artifact uri of the plugin from which the task is to be loaded.
062        * @param uri an artifact plugin uri
063        */
064        public void setUri( String uri )
065        {
066            m_uri = uri;
067        }
068    
069       /**
070        * Set the name of a property into which the local file path will be assigned.
071        * @param name the ant property name
072        */
073        public void setProperty( String name )
074        {
075            m_name = name;
076        }
077    
078       /**
079        * Return the artifact uri of the plugin.
080        * @return the artifact uri
081        */
082        private URI getURI()
083        {
084            try
085            {
086                return new URI( m_uri.toString() );
087            }
088            catch( Throwable e )
089            {
090                final String error =
091                  "Cound not convert the supplied uri spec ["
092                  + m_uri
093                  + "] to a formal URI.";
094                throw new BuildException( error, e, getLocation() );
095            }
096        }
097    
098       /**
099        * Return the ant property name.
100        * @return the property name
101        */
102        private String getPropertyName()
103        {
104            if( null == m_name )
105            {
106                final String error =
107                  "The required 'property' attribute is not declared.";
108                throw new BuildException( error, getLocation() );
109            }
110            else
111            {
112                return m_name;
113            }
114        }
115    
116       /**
117        * Load the resource and assign the locally cached file path to the supplied property name.
118        * @exception BuildException if an error occurs during resource resolution
119        */
120        public void execute() throws BuildException
121        {
122            if( null == m_uri )
123            {
124                final String error =
125                  "Missing uri attribute.";
126                throw new BuildException( error, getLocation() );
127            }
128    
129            String name = getPropertyName();
130    
131            try
132            {
133                URI uri = getURI();
134                String spec = getURI().toString();
135                log( "artifact: " + spec );
136                URL url = Artifact.toURL( uri );
137                URLConnection connection = url.openConnection();
138                connection.connect();
139                File file = (File) connection.getContent( new Class[]{File.class} );
140                String path = file.getCanonicalPath();
141                getProject().setNewProperty( name, path );
142            }
143            catch( Throwable e )
144            {
145                final String error =
146                  "Unexpected error while attempting to resolve artifact uri ["
147                  + m_uri
148                  + "]";
149               throw new BuildException( error, e, getLocation() );
150            }
151        }
152    }