001 /*
002 * #%L
003 * License Maven Plugin
004 *
005 * $Id: LicenseStore.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/model/LicenseStore.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.model;
027
028 import org.apache.commons.lang.StringUtils;
029 import org.apache.commons.logging.Log;
030 import org.apache.commons.logging.LogFactory;
031 import org.apache.maven.plugin.MojoExecutionException;
032
033 import java.io.IOException;
034 import java.net.URL;
035 import java.util.ArrayList;
036 import java.util.Iterator;
037 import java.util.List;
038
039 /**
040 * The {@code LicenseStore} offers {@link License} coming from different {@link
041 * LicenseRepository}.
042 *
043 * @author tchemit <chemit@codelutin.com>
044 * @since 1.0
045 */
046 public class LicenseStore
047 implements Iterable<LicenseRepository>
048 {
049
050 /**
051 * Logger
052 */
053 private static final Log log = LogFactory.getLog( LicenseStore.class );
054
055 /**
056 * class-path directory where is the licenses repository
057 */
058 public static final String JAR_LICENSE_REPOSITORY = "/META-INF/licenses";
059
060 /**
061 * list of available license repositories
062 */
063 protected List<LicenseRepository> repositories;
064
065 /**
066 * flag to know if store was init
067 */
068 protected boolean init;
069
070 public static LicenseStore createLicenseStore( org.apache.maven.plugin.logging.Log log, String... extraResolver )
071 throws MojoExecutionException
072 {
073 LicenseStore store;
074 try
075 {
076 store = new LicenseStore();
077 store.addJarRepository();
078 if ( extraResolver != null )
079 {
080 for ( String s : extraResolver )
081 {
082 if ( StringUtils.isNotEmpty( s ) )
083 {
084 log.info( "adding extra resolver " + s );
085 store.addRepository( s );
086 }
087 }
088 }
089 store.init();
090 }
091 catch ( IllegalArgumentException ex )
092 {
093 throw new MojoExecutionException( "could not obtain the license repository", ex );
094 }
095 catch ( IOException ex )
096 {
097 throw new MojoExecutionException( "could not obtain the license repository", ex );
098 }
099 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 }