001    /**
002     * Copyright 2010-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.license.model;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    import org.apache.maven.plugin.MojoExecutionException;
022    
023    import java.io.IOException;
024    import java.net.URL;
025    import java.util.ArrayList;
026    import java.util.Iterator;
027    import java.util.List;
028    
029    /**
030     * The {@code LicenseStore} offers {@link License} coming from different {@link
031     * LicenseRepository}.
032     *
033     * @author tchemit <chemit@codelutin.com>
034     * @since 1.0
035     */
036    public class LicenseStore
037        implements Iterable<LicenseRepository>
038    {
039    
040        /**
041         * Logger
042         */
043        private static final Log log = LogFactory.getLog( LicenseStore.class );
044    
045        /**
046         * class-path directory where is the licenses repository
047         */
048        public static final String JAR_LICENSE_REPOSITORY = "/META-INF/licenses";
049    
050        /**
051         * list of available license repositories
052         */
053        protected List<LicenseRepository> repositories;
054    
055        /**
056         * flag to know if store was init
057         */
058        protected boolean init;
059    
060        public static LicenseStore createLicenseStore( org.apache.maven.plugin.logging.Log log, String... extraResolver )
061            throws MojoExecutionException
062        {
063            LicenseStore store;
064            try
065            {
066                store = new LicenseStore();
067                store.addJarRepository();
068                if ( extraResolver != null )
069                {
070                    for ( String s : extraResolver )
071                    {
072                        if ( StringUtils.isNotEmpty( s ) )
073                        {
074                            log.info( "adding extra resolver " + s );
075                            store.addRepository( s );
076                        }
077                    }
078                }
079                store.init();
080            }
081            catch ( IllegalArgumentException ex )
082            {
083                throw new MojoExecutionException( "could not obtain the license repository", ex );
084            }
085            catch ( IOException ex )
086            {
087                throw new MojoExecutionException( "could not obtain the license repository", ex );
088            }
089            return store;
090        }
091    
092        public void init()
093            throws IOException
094        {
095            checkNotInit( "init" );
096            try
097            {
098                if ( repositories == null )
099                {
100                    // adding the default class-path repository
101                    addJarRepository();
102                }
103                for ( LicenseRepository r : this )
104                {
105                    r.load();
106                }
107            }
108            finally
109            {
110                init = true;
111            }
112        }
113    
114        public List<LicenseRepository> getRepositories()
115        {
116            return repositories;
117        }
118    
119        public String[] getLicenseNames()
120        {
121            checkInit( "getLicenseNames" );
122            List<String> result = new ArrayList<String>();
123            for ( LicenseRepository repository : this )
124            {
125                for ( License license : repository )
126                {
127                    result.add( license.getName() );
128                }
129            }
130            return result.toArray( new String[result.size()] );
131        }
132    
133        public License[] getLicenses()
134        {
135            checkInit( "getLicenses" );
136            List<License> result = new ArrayList<License>();
137            if ( repositories != null )
138            {
139                for ( LicenseRepository repository : this )
140                {
141                    for ( License license : repository )
142                    {
143                        result.add( license );
144                    }
145                }
146            }
147            return result.toArray( new License[result.size()] );
148        }
149    
150        public License getLicense( String licenseName )
151        {
152            checkInit( "getLicense" );
153            Iterator<LicenseRepository> itr = iterator();
154            License result = null;
155            while ( itr.hasNext() )
156            {
157                LicenseRepository licenseRepository = itr.next();
158                License license = licenseRepository.getLicense( licenseName );
159                if ( license != null )
160                {
161                    result = license;
162                    break;
163                }
164            }
165            if ( result == null && log.isDebugEnabled() )
166            {
167                log.debug( "could not find license named '" + licenseName + "'" );
168            }
169            return result;
170        }
171    
172        public void addRepository( String extraResolver )
173            throws IOException
174        {
175            addRepository( new URL( extraResolver ) );
176        }
177    
178        public void addRepository( URL baseURL )
179            throws IOException
180        {
181            checkNotInit( "addRepository" );
182            LicenseRepository repository = new LicenseRepository();
183            repository.setBaseURL( baseURL );
184            if ( log.isDebugEnabled() )
185            {
186                log.debug( "Adding a license repository " + repository );
187            }
188            addRepository( repository );
189        }
190    
191        public void addJarRepository()
192            throws IOException
193        {
194            checkNotInit( "addJarRepository" );
195            URL baseURL = getClass().getResource( JAR_LICENSE_REPOSITORY );
196            LicenseRepository repository = new LicenseRepository();
197            repository.setBaseURL( baseURL );
198            if ( log.isDebugEnabled() )
199            {
200                log.debug( "Adding a jar license repository " + repository );
201            }
202            addRepository( repository );
203        }
204    
205        public Iterator<LicenseRepository> iterator()
206        {
207            return getRepositories().iterator();
208        }
209    
210        protected void addRepository( LicenseRepository repository )
211        {
212            checkNotInit( "addRepository" );
213            if ( repositories == null )
214            {
215                repositories = new ArrayList<LicenseRepository>();
216    
217            }
218            if ( log.isInfoEnabled() )
219            {
220                log.info( "Adding a license repository " + repository.getBaseURL() );
221            }
222            repositories.add( repository );
223        }
224    
225        protected void checkInit( String operation )
226            throws IllegalStateException
227        {
228            if ( !init )
229            {
230                throw new IllegalStateException( "store was not init, operation [" + operation + "] not possible." );
231            }
232        }
233    
234        protected void checkNotInit( String operation )
235            throws IllegalStateException
236        {
237            if ( init )
238            {
239                throw new IllegalStateException( "store was init, operation [" + operation + "+] not possible." );
240            }
241        }
242    }