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