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