1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
41
42
43
44
45
46 public class LicenseStore
47 implements Iterable<LicenseRepository>
48 {
49
50
51
52
53 private static final Log log = LogFactory.getLog( LicenseStore.class );
54
55
56
57
58 public static final String JAR_LICENSE_REPOSITORY = "/META-INF/licenses";
59
60
61
62
63 protected List<LicenseRepository> repositories;
64
65
66
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
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 }