1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.license.model;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.maven.plugin.MojoExecutionException;
22
23 import java.io.IOException;
24 import java.net.URL;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
28
29
30
31
32
33
34
35
36 public class LicenseStore
37 implements Iterable<LicenseRepository>
38 {
39
40
41
42
43 private static final Log log = LogFactory.getLog( LicenseStore.class );
44
45
46
47
48 public static final String JAR_LICENSE_REPOSITORY = "/META-INF/licenses";
49
50
51
52
53 protected List<LicenseRepository> repositories;
54
55
56
57
58 protected boolean init;
59
60 public static LicenseStore createLicenseStore( org.apache.maven.plugin.logging.Log log, String... extraResolver )
61 throws MojoExecutionException
62 {
63 LicenseStore store;
64 try
65 {
66 store = new LicenseStore();
67 store.addJarRepository();
68 if ( extraResolver != null )
69 {
70 for ( String s : extraResolver )
71 {
72 if ( StringUtils.isNotEmpty( s ) )
73 {
74 log.info( "adding extra resolver " + s );
75 store.addRepository( s );
76 }
77 }
78 }
79 store.init();
80 }
81 catch ( IllegalArgumentException ex )
82 {
83 throw new MojoExecutionException( "could not obtain the license repository", ex );
84 }
85 catch ( IOException ex )
86 {
87 throw new MojoExecutionException( "could not obtain the license repository", ex );
88 }
89 return store;
90 }
91
92 public void init()
93 throws IOException
94 {
95 checkNotInit( "init" );
96 try
97 {
98 if ( repositories == null )
99 {
100
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 }