001    /*
002     * #%L
003     * License Maven Plugin
004     * 
005     * $Id: AbstractLicenseNameMojo.java 13616 2011-02-14 11:38:39Z tchemit $
006     * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/AbstractLicenseNameMojo.java $
007     * %%
008     * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
009     * %%
010     * This program is free software: you can redistribute it and/or modify
011     * it under the terms of the GNU Lesser General Public License as 
012     * published by the Free Software Foundation, either version 3 of the 
013     * License, or (at your option) any later version.
014     * 
015     * This program is distributed in the hope that it will be useful,
016     * but WITHOUT ANY WARRANTY; without even the implied warranty of
017     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
018     * GNU General Lesser Public License for more details.
019     * 
020     * You should have received a copy of the GNU General Lesser Public 
021     * License along with this program.  If not, see
022     * <http://www.gnu.org/licenses/lgpl-3.0.html>.
023     * #L%
024     */
025    
026    package org.codehaus.mojo.license;
027    
028    import org.apache.commons.lang.StringUtils;
029    import org.codehaus.mojo.license.model.License;
030    import org.codehaus.mojo.license.model.LicenseStore;
031    
032    import java.util.Arrays;
033    
034    /**
035     * Abstract mojo which using a {@link #licenseName} and owns a
036     * {@link #licenseStore}.
037     *
038     * @author tchemit <chemit@codelutin.com>
039     * @since 1.0
040     */
041    public abstract class AbstractLicenseNameMojo
042        extends AbstractLicenseMojo
043    {
044    
045        /**
046         * To specify an external extra licenses repository resolver (says the base
047         * url of the repository where the {@code license.properties} is present).
048         *
049         * @parameter expression="${license.licenseResolver}"
050         * @since 1.0
051         */
052        private String licenseResolver;
053    
054        /**
055         * A flag to keep a backup of every modified file.
056         *
057         * @parameter expression="${license.keepBackup}"  default-value="false"
058         * @since 1.0
059         */
060        private boolean keepBackup;
061    
062        /**
063         * Name of the license to use in the project.
064         *
065         * @parameter expression="${license.licenseName}"
066         * @since 1.0
067         */
068        private String licenseName;
069    
070        /**
071         * License loaded from the {@link #licenseName}.
072         */
073        private License license;
074    
075        /**
076         * Store of available licenses.
077         */
078        private LicenseStore licenseStore;
079    
080        /**
081         * When is sets to {@code true}, will skip execution.
082         * <p/>
083         * This will take effects in method {@link #checkSkip()}.
084         * So the method {@link #doAction()} will never be invoked.
085         *
086         * @return {@code true} if goal will not be executed
087         */
088        public abstract boolean isSkip();
089    
090        /**
091         * Changes internal state {@code skip} to execute (or not) goal.
092         *
093         * @param skip new state value
094         */
095        public abstract void setSkip( boolean skip );
096    
097        @Override
098        protected boolean checkSkip()
099        {
100            if ( isSkip() )
101            {
102                getLog().info( "skip flag is on, will skip goal." );
103                return false;
104            }
105            return super.checkSkip();
106        }
107    
108        @Override
109        protected void init()
110            throws Exception
111        {
112    
113            if ( isSkip() )
114            {
115                return;
116            }
117    
118            // init licenses store
119            licenseStore = LicenseStore.createLicenseStore( getLog(), getLicenseResolver() );
120    
121            // check licenseName exists
122            license = getLicense( licenseName, true );
123        }
124    
125        public License getLicense( String licenseName, boolean checkIfExists )
126            throws IllegalArgumentException, IllegalStateException
127        {
128            if ( StringUtils.isEmpty( licenseName ) )
129            {
130                throw new IllegalArgumentException( "licenseName can not be null, nor empty" );
131            }
132            LicenseStore licenseStore = getLicenseStore();
133            if ( licenseStore == null )
134            {
135                throw new IllegalStateException( "No license store initialized!" );
136            }
137            License license = licenseStore.getLicense( licenseName );
138            if ( checkIfExists && license == null )
139            {
140                throw new IllegalArgumentException( "License named '" + licenseName + "' is unknown, use one of " +
141                                                        Arrays.toString( licenseStore.getLicenseNames() ) );
142            }
143            return license;
144        }
145    
146        public boolean isKeepBackup()
147        {
148            return keepBackup;
149        }
150    
151        public String getLicenseName()
152        {
153            return licenseName;
154        }
155    
156        public String getLicenseResolver()
157        {
158            return licenseResolver;
159        }
160    
161        public LicenseStore getLicenseStore()
162        {
163            return licenseStore;
164        }
165    
166        public License getLicense()
167        {
168            return license;
169        }
170    
171        public void setKeepBackup( boolean keepBackup )
172        {
173            this.keepBackup = keepBackup;
174        }
175    
176        public void setLicenseResolver( String licenseResolver )
177        {
178            this.licenseResolver = licenseResolver;
179        }
180    
181        public void setLicenseName( String licenseName )
182        {
183            this.licenseName = licenseName;
184        }
185    
186        public void setLicenseStore( LicenseStore licenseStore )
187        {
188            this.licenseStore = licenseStore;
189        }
190    
191    }