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.codehaus.mojo.license.MojoHelper;
32
33 import java.io.IOException;
34 import java.net.URL;
35 import java.net.URLConnection;
36 import java.util.*;
37 import java.util.Map.Entry;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
40
41
42
43
44
45 public class LicenseRepository
46 implements Iterable<License>
47 {
48
49
50
51
52 private static final Log log = LogFactory.getLog( LicenseRepository.class );
53
54 public static final String REPOSITORY_DEFINITION_FILE = "licenses.properties";
55
56 public static final Pattern LICENSE_DESCRIPTION_PATTERN =
57 Pattern.compile( "(.*)\\s*~~\\s*license\\s*:\\s*(.*)\\s*~~\\s*header\\s*:\\s*(.*)\\s*" );
58
59
60
61
62 protected URL baseURL;
63
64
65
66
67 protected List<License> licenses;
68
69
70
71
72
73 protected boolean init;
74
75 public LicenseRepository()
76 {
77 }
78
79 public URL getBaseURL()
80 {
81 return baseURL;
82 }
83
84 public void setBaseURL( URL baseURL )
85 {
86 checkNotInit( "setBaseURL" );
87 this.baseURL = baseURL;
88 }
89
90 public void load()
91 throws IOException
92 {
93 checkNotInit( "load" );
94 try
95 {
96 if ( baseURL == null || StringUtils.isEmpty( baseURL.toString() ) )
97 {
98 throw new IllegalStateException( "no baseURL defined in " + this );
99 }
100
101 URL definitionURL = MojoHelper.getUrl( getBaseURL(), REPOSITORY_DEFINITION_FILE );
102 if ( licenses != null )
103 {
104 licenses.clear();
105 }
106 else
107 {
108 licenses = new ArrayList<License>();
109 }
110
111 if ( !checkExists( definitionURL ) )
112 {
113 throw new IllegalArgumentException(
114 "no licenses.properties found with url [" + definitionURL + "] for resolver " + this );
115 }
116 Properties p = new Properties();
117 p.load( definitionURL.openStream() );
118
119 for ( Entry<Object, Object> entry : p.entrySet() )
120 {
121 String licenseName = (String) entry.getKey();
122 licenseName = licenseName.trim().toLowerCase();
123 URL licenseBaseURL = MojoHelper.getUrl( baseURL, licenseName );
124
125 License license = new License();
126 license.setName( licenseName );
127 license.setBaseURL( licenseBaseURL );
128
129 String licenseDescription = (String) entry.getValue();
130 Matcher matcher = LICENSE_DESCRIPTION_PATTERN.matcher( licenseDescription );
131 String licenseFile;
132 String headerFile;
133
134 if ( matcher.matches() )
135 {
136 licenseDescription = matcher.group( 1 );
137 licenseFile = matcher.group( 2 );
138 headerFile = matcher.group( 3 );
139 }
140 else
141 {
142 licenseFile = License.LICENSE_CONTENT_FILE;
143 headerFile = License.LICENSE_HEADER_FILE;
144 }
145
146 URL licenseURL = MojoHelper.getUrl( licenseBaseURL, licenseFile );
147 if ( !checkExists( licenseURL ) )
148 {
149 throw new IllegalArgumentException(
150 "Could not find license (" + license + ") content file at [" + licenseURL + "] for resolver " +
151 this );
152 }
153 license.setLicenseURL( licenseURL );
154
155 URL headerURL = MojoHelper.getUrl( licenseBaseURL, headerFile );
156 if ( !checkExists( headerURL ) )
157 {
158 throw new IllegalArgumentException(
159 "Could not find license (" + license + ") header file at [" + headerURL + "] for resolver " +
160 this );
161 }
162 license.setHeaderURL( headerURL );
163
164 license.setDescription( licenseDescription );
165
166 if ( log.isInfoEnabled() )
167 {
168 log.info( "register " + license.getDescription() );
169 }
170 if ( log.isDebugEnabled() )
171 {
172 log.debug( license );
173 }
174 licenses.add( license );
175 }
176 licenses = Collections.unmodifiableList( licenses );
177 }
178 finally
179 {
180
181 init = true;
182 }
183 }
184
185 public String[] getLicenseNames()
186 {
187 checkInit( "getLicenseNames" );
188 List<String> result = new ArrayList<String>( licenses.size() );
189 for ( License license : this )
190 {
191 result.add( license.getName() );
192 }
193 return result.toArray( new String[result.size()] );
194 }
195
196 public License[] getLicenses()
197 {
198 checkInit( "getLicenses" );
199 return licenses.toArray( new License[licenses.size()] );
200 }
201
202 public License getLicense( String licenseName )
203 {
204 checkInit( "getLicense" );
205 if ( StringUtils.isEmpty( licenseName ) )
206 {
207 throw new IllegalArgumentException( "licenceName can not be null, nor empty" );
208 }
209
210 License license = null;
211 for ( License l : this )
212 {
213 if ( licenseName.equals( l.getName() ) )
214 {
215
216 license = l;
217 break;
218 }
219 }
220 return license;
221 }
222
223 public Iterator<License> iterator()
224 {
225 checkInit( "iterator" );
226 return licenses.iterator();
227 }
228
229 protected boolean checkExists( URL url )
230 throws IOException
231 {
232 URLConnection openConnection = url.openConnection();
233 return openConnection.getContentLength() > 0;
234 }
235
236 protected void checkInit( String operation )
237 throws IllegalStateException
238 {
239 if ( !init )
240 {
241 throw new IllegalStateException(
242 "repository " + this + " was not init, operation [" + operation + "] not possible." );
243 }
244 }
245
246 protected void checkNotInit( String operation )
247 throws IllegalStateException
248 {
249 if ( init )
250 {
251 throw new IllegalStateException(
252 "repository " + this + "was init, operation [" + operation + "+] not possible." );
253 }
254 }
255
256 }