View Javadoc

1   /*
2    * #%L
3    * License Maven Plugin
4    * 
5    * $Id: LicenseStore.java 13616 2011-02-14 11:38:39Z tchemit $
6    * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/model/LicenseStore.java $
7    * %%
8    * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23   * #L%
24   */
25  
26  package org.codehaus.mojo.license.model;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.maven.plugin.MojoExecutionException;
32  
33  import java.io.IOException;
34  import java.net.URL;
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * The {@code LicenseStore} offers {@link License} coming from different {@link
41   * LicenseRepository}.
42   *
43   * @author tchemit <chemit@codelutin.com>
44   * @since 1.0
45   */
46  public class LicenseStore
47      implements Iterable<LicenseRepository>
48  {
49  
50      /**
51       * Logger
52       */
53      private static final Log log = LogFactory.getLog( LicenseStore.class );
54  
55      /**
56       * class-path directory where is the licenses repository
57       */
58      public static final String JAR_LICENSE_REPOSITORY = "/META-INF/licenses";
59  
60      /**
61       * list of available license repositories
62       */
63      protected List<LicenseRepository> repositories;
64  
65      /**
66       * flag to know if store was init
67       */
68      protected boolean init;
69  
70      public static LicenseStore createLicenseStore( org.apache.maven.plugin.logging.Log log, String... extraResolver )
71          throws MojoExecutionException
72      {
73          LicenseStore store;
74          try
75          {
76              store = new LicenseStore();
77              store.addJarRepository();
78              if ( extraResolver != null )
79              {
80                  for ( String s : extraResolver )
81                  {
82                      if ( StringUtils.isNotEmpty( s ) )
83                      {
84                          log.info( "adding extra resolver " + s );
85                          store.addRepository( s );
86                      }
87                  }
88              }
89              store.init();
90          }
91          catch ( IllegalArgumentException ex )
92          {
93              throw new MojoExecutionException( "could not obtain the license repository", ex );
94          }
95          catch ( IOException ex )
96          {
97              throw new MojoExecutionException( "could not obtain the license repository", ex );
98          }
99          return store;
100     }
101 
102     public void init()
103         throws IOException
104     {
105         checkNotInit( "init" );
106         try
107         {
108             if ( repositories == null )
109             {
110                 // adding the default class-path repository
111                 addJarRepository();
112             }
113             for ( LicenseRepository r : this )
114             {
115                 r.load();
116             }
117         }
118         finally
119         {
120             init = true;
121         }
122     }
123 
124     public List<LicenseRepository> getRepositories()
125     {
126         return repositories;
127     }
128 
129     public String[] getLicenseNames()
130     {
131         checkInit( "getLicenseNames" );
132         List<String> result = new ArrayList<String>();
133         for ( LicenseRepository repository : this )
134         {
135             for ( License license : repository )
136             {
137                 result.add( license.getName() );
138             }
139         }
140         return result.toArray( new String[result.size()] );
141     }
142 
143     public License[] getLicenses()
144     {
145         checkInit( "getLicenses" );
146         List<License> result = new ArrayList<License>();
147         if ( repositories != null )
148         {
149             for ( LicenseRepository repository : this )
150             {
151                 for ( License license : repository )
152                 {
153                     result.add( license );
154                 }
155             }
156         }
157         return result.toArray( new License[result.size()] );
158     }
159 
160     public License getLicense( String licenseName )
161     {
162         checkInit( "getLicense" );
163         Iterator<LicenseRepository> itr = iterator();
164         License result = null;
165         while ( itr.hasNext() )
166         {
167             LicenseRepository licenseRepository = itr.next();
168             License license = licenseRepository.getLicense( licenseName );
169             if ( license != null )
170             {
171                 result = license;
172                 break;
173             }
174         }
175         if ( result == null && log.isDebugEnabled() )
176         {
177             log.debug( "could not find license named '" + licenseName + "'" );
178         }
179         return result;
180     }
181 
182     public void addRepository( String extraResolver )
183         throws IOException
184     {
185         addRepository( new URL( extraResolver ) );
186     }
187 
188     public void addRepository( URL baseURL )
189         throws IOException
190     {
191         checkNotInit( "addRepository" );
192         LicenseRepository repository = new LicenseRepository();
193         repository.setBaseURL( baseURL );
194         if ( log.isDebugEnabled() )
195         {
196             log.debug( "Adding a license repository " + repository );
197         }
198         addRepository( repository );
199     }
200 
201     public void addJarRepository()
202         throws IOException
203     {
204         checkNotInit( "addJarRepository" );
205         URL baseURL = getClass().getResource( JAR_LICENSE_REPOSITORY );
206         LicenseRepository repository = new LicenseRepository();
207         repository.setBaseURL( baseURL );
208         if ( log.isDebugEnabled() )
209         {
210             log.debug( "Adding a jar license repository " + repository );
211         }
212         addRepository( repository );
213     }
214 
215     public Iterator<LicenseRepository> iterator()
216     {
217         return getRepositories().iterator();
218     }
219 
220     protected void addRepository( LicenseRepository repository )
221     {
222         checkNotInit( "addRepository" );
223         if ( repositories == null )
224         {
225             repositories = new ArrayList<LicenseRepository>();
226 
227         }
228         if ( log.isInfoEnabled() )
229         {
230             log.info( "Adding a license repository " + repository.getBaseURL() );
231         }
232         repositories.add( repository );
233     }
234 
235     protected void checkInit( String operation )
236         throws IllegalStateException
237     {
238         if ( !init )
239         {
240             throw new IllegalStateException( "store was not init, operation [" + operation + "] not possible." );
241         }
242     }
243 
244     protected void checkNotInit( String operation )
245         throws IllegalStateException
246     {
247         if ( init )
248         {
249             throw new IllegalStateException( "store was init, operation [" + operation + "+] not possible." );
250         }
251     }
252 }