001    package org.codehaus.mojo.wagon;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
005     * agreements. See the NOTICE file distributed with this work for additional information regarding
006     * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance with the License. You may obtain a
008     * copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software distributed under the License
013     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
014     * or implied. See the License for the specific language governing permissions and limitations under
015     * the License.
016     */
017    
018    import java.io.IOException;
019    
020    import org.apache.maven.plugin.MojoExecutionException;
021    import org.apache.maven.wagon.ConnectionException;
022    import org.apache.maven.wagon.Wagon;
023    import org.apache.maven.wagon.WagonException;
024    
025    /**
026     * Provides base functionality for dealing with I/O using single wagon.
027     *
028     */
029    public abstract class AbstractSingleWagonMojo extends AbstractWagonMojo {
030    
031        /**
032         * URL to upload to or download from or list. Must exist and point to a directory.
033         *
034         * @parameter expression="${wagon.url}"
035         * @required
036         */
037        private String url;
038    
039        /**
040         * settings.xml's server id for the URL. This is used when wagon needs extra authentication information.
041         *
042         * @parameter expression="${wagon.serverId}" default-value="serverId";
043         */
044        private String serverId;
045    
046        @Override
047        public void execute() throws MojoExecutionException {
048            if (this.skip) {
049                this.getLog().info("Skip execution.");
050                return;
051            }
052    
053            Wagon wagon = null;
054            try {
055                wagon = createWagon(serverId, url);
056                execute(wagon);
057            } catch (WagonException e) {
058                throw new MojoExecutionException("Error handling resource", e);
059            } catch (IOException e) {
060                throw new MojoExecutionException("Error handling resource", e);
061            } finally {
062                disconnectQuietly(wagon);
063            }
064        }
065    
066        protected void disconnectQuietly(Wagon wagon) {
067            if (wagon == null) {
068                return;
069            }
070            try {
071                wagon.disconnect();
072            } catch (ConnectionException e) {
073                getLog().debug("Error disconnecting wagon - ignored", e);
074            }
075        }
076    
077        /**
078         * Perform the necessary action. To be implemented in the child mojo.
079         *
080         * @param wagon
081         * @throws MojoExecutionException
082         * @throws WagonException
083         */
084        protected abstract void execute(Wagon wagon) throws MojoExecutionException, WagonException, IOException;
085    
086    }