Author: tchemit Date: 2008-10-12 20:43:11 +0000 (Sun, 12 Oct 2008) New Revision: 1171 Added: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseFactory.java maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/plugin/ maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseFactoryTest.java maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java Removed: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseTypeTest.java Modified: maven-license-switcher-plugin/trunk/pom.xml maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/JarLicenseResolver.java maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java Log: using LicenseResolver and getLicenseNames Modified: maven-license-switcher-plugin/trunk/pom.xml =================================================================== --- maven-license-switcher-plugin/trunk/pom.xml 2008-10-12 20:42:31 UTC (rev 1170) +++ maven-license-switcher-plugin/trunk/pom.xml 2008-10-12 20:43:11 UTC (rev 1171) @@ -44,7 +44,7 @@ <!-- *** Project Information ************************************* --> <!-- ************************************************************* --> <name>Plugin maven pour switcher les licenses</name> - <version>0.2</version> + <version>0.3-SNAPSHOT</version> <description>Plugin pour switcher de license dans un module maven 2</description> <inceptionYear>2008</inceptionYear> @@ -61,11 +61,11 @@ <plugin> <groupId>org.codelutin</groupId> <artifactId>maven-jrst-plugin</artifactId> - <version>0.8.2</version> + <!--version>0.8.2</version--> </plugin> <!-- plugin license switcher --> - <plugin> + <!--plugin> <groupId>org.codelutin</groupId> <artifactId>maven-license-switcher-plugin</artifactId> <version>0.2</version> @@ -77,7 +77,7 @@ </goals> </execution> </executions> - </plugin> + </plugin--> <plugin> <artifactId>maven-antrun-plugin</artifactId> @@ -127,7 +127,7 @@ </scm> <!-- Maven Environment : profiles --> - <profiles> + <!--profiles> <profile> <id>license</id> <activation> @@ -150,6 +150,6 @@ </plugins> </build> </profile> - </profiles> + </profiles--> </project> Modified: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/JarLicenseResolver.java =================================================================== --- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/JarLicenseResolver.java 2008-10-12 20:42:31 UTC (rev 1170) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/JarLicenseResolver.java 2008-10-12 20:43:11 UTC (rev 1171) @@ -17,23 +17,17 @@ * * @author chemit */ -public class JarLicenseResolver implements LicenseResolver { +public class JarLicenseResolver extends LicenseResolver { protected static final Log log = LogFactory.getLog(JarLicenseResolver.class); - public License resolv(String licenseName) throws IllegalArgumentException { - if (licenseName == null) { - throw new IllegalArgumentException("licenceName can not be null"); - } - String prefix = File.separator + "META-INF" + File.separator + "licenses" + File.separator + licenseName.toLowerCase() + File.separator; - URL licenseURL = getClass().getResource(prefix + "license.txt"); - URL headerURL = getClass().getResource(prefix + "header.txt"); - - if (licenseURL != null && headerURL != null) { - return new License(licenseName, licenseURL, headerURL); - } - - return null; + public JarLicenseResolver() { + super(acquireBaseURL()); } + protected static String acquireBaseURL() { + String prefix = File.separator + "META-INF" + File.separator + "licenses"; + URL licenseURL = JarLicenseResolver.class.getResource(prefix); + return licenseURL.toString(); + } } Added: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseFactory.java =================================================================== --- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseFactory.java (rev 0) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseFactory.java 2008-10-12 20:43:11 UTC (rev 1171) @@ -0,0 +1,63 @@ +package org.codelutin.license; + +import java.util.Arrays; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +/** + * Class responsible of instanciate {@link License}. + * + * @author chemit + */ +public class LicenseFactory { + + protected LicenseResolver[] resolvers; + + public static LicenseFactory newInstance(String extraResolver) throws IllegalArgumentException { + if (extraResolver == null || extraResolver.trim().isEmpty()) { + return new LicenseFactory(new LicenseResolver[]{ + new JarLicenseResolver(), + }); + } + return new LicenseFactory(new LicenseResolver[]{ + new JarLicenseResolver(), + new LicenseResolver(extraResolver) + }); + } + + public Map<String, String> getLicenseNames() throws IllegalArgumentException { + Map<String, String> result = new TreeMap<String, String>(); + for (LicenseResolver resolver : resolvers) { + Map<String, String> tmp = resolver.getLicenseNames(); + for (Entry<String, String> s : tmp.entrySet()) { + if (!result.containsKey(s.getKey())) { + result.put(s.getKey(), s.getValue()); + } + } + } + return result; + } + + + public License revolv(String licenseName) throws IllegalArgumentException { + if (licenseName == null) { + throw new IllegalArgumentException("licenseName can not be null"); + } + for (LicenseResolver resolver : resolvers) { + License result = resolver.resolv(licenseName); + if (result != null) { + return result; + } + } + throw new IllegalArgumentException("could not find the license " + licenseName + " with resolvers" + Arrays.toString(resolvers)); + } + + protected LicenseFactory(LicenseResolver[] resolvers) throws IllegalArgumentException { + if (resolvers == null || resolvers.length == 0) { + throw new IllegalArgumentException("no resolver defined"); + } + this.resolvers = resolvers; + } + +} Modified: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java =================================================================== --- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java 2008-10-12 20:42:31 UTC (rev 1170) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseMojo.java 2008-10-12 20:43:11 UTC (rev 1171) @@ -52,16 +52,19 @@ /** * Le type de license a appliquer. * - * @parameter expression="${license-switcher.licenseType}" default-value="LGPL_V3" + * @parameter expression="${license-switcher.licenseName}" * @required */ - protected String licenseType; + protected String licenseName; - /** le header a ajouter dans chaque fichier java */ - protected String licenseHeaderContent; + /** + * La baseURL d'un resolver de license supplementaire + * + * @parameter expression="${license-switcher.extraResolver}" + */ + protected String extraResolver; - /** le texte de la licence a placer dans le fichier ${basedir}/LICENSE.txt */ - protected String licenseFileContent; + protected License license; protected void init() throws Exception { @@ -70,11 +73,12 @@ if (doGenerate) { - // prepare licenseFileContent + // acquire license - LicenseType type = LicenseType.valueOf(licenseType); + LicenseFactory factory = LicenseFactory.newInstance(extraResolver); - licenseFileContent = FileUtil.readAsString(type.getLicenceFileReader(encoding)); + license = factory.revolv(licenseName); + } } @@ -85,6 +89,12 @@ if (doGenerate) { + getLog().info("using licence ["+licenseName+"]"); + + if (verbose) { + getLog().info("licence : "+license); + } + if (licenseFile.exists() && keepBackup) { if (verbose) { getLog().info("backup " + licenseFile); @@ -94,7 +104,7 @@ licenseFile.renameTo(backup); } try { - FileUtil.writeString(licenseFile, licenseFileContent, encoding); + FileUtil.writeString(licenseFile, license.getLicenseContent(encoding), encoding); } catch (IOException e) { throw new MojoExecutionException("could not write license file " + licenseFile + " for reason : " + e.getMessage(), e); Deleted: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java =================================================================== --- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java 2008-10-12 20:42:31 UTC (rev 1170) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java 2008-10-12 20:43:11 UTC (rev 1171) @@ -1,8 +0,0 @@ -package org.codelutin.license; - -/** @author chemit */ -public interface LicenseResolver { - - License resolv(String licenseName) throws IllegalArgumentException; - -} Added: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java =================================================================== --- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java (rev 0) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/LicenseResolver.java 2008-10-12 20:43:11 UTC (rev 1171) @@ -0,0 +1,133 @@ +package org.codelutin.license; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collections; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.TreeMap; + +/** + * Un resolver de license a partir d'un {@link #baseURL} . + * <p/> + * On doit avoir un fichier + * <pre>baseURL/licenses.txt + * </pre> qui contient la liste des licenses gerees par ce resolver. + * <p/> + * Chaque license est definit par deux fichiers : + * <pre> + * baseURL/licenseName/license.txt + * baseURL/licenseName/header.txt + * </pre> + * + * @author chemit + */ +public class LicenseResolver { + + protected static final Log log = LogFactory.getLog(LicenseResolver.class); + + /** + * BaseURL + * + * @required + */ + protected String baseURL; + + /** list of known licenses with this resolver */ + protected Map<String, String> licenseNames; + + public LicenseResolver() { + } + + public LicenseResolver(String baseURL) { + this.baseURL = baseURL; + } + + public String getBaseURL() { + return baseURL; + } + + public void setBaseURL(String baseURL) { + this.baseURL = baseURL; + // reset licenseNames + licenseNames = null; + } + + public Map<String, String> getLicenseNames() { + if (licenseNames == null) { + if (baseURL == null || baseURL.trim().isEmpty()) { + throw new IllegalArgumentException("no baseURL defined in " + this); + } + + Map<String, String> list = new TreeMap<String, String>(); + try { + URL licenseURL = new URL(baseURL + "/licenses.properties"); + + if (!checkExists(licenseURL)) { + throw new IllegalArgumentException("no licenses.properties found withurl [" + licenseURL + "] for resolver " + this); + } + Properties p = new Properties(); + p.load(licenseURL.openStream()); + for (Entry<Object, Object> entry : p.entrySet()) { + list.put((String) entry.getKey(), entry.getValue() + " [" + baseURL + "]"); + } + } catch (MalformedURLException e) { + throw new IllegalArgumentException(e); + } catch (IOException e) { + return null; + } + + licenseNames = Collections.unmodifiableMap(list); + } + return licenseNames; + } + + public License resolv(String licenseName) throws IllegalArgumentException { + if (baseURL == null || baseURL.trim().isEmpty()) { + throw new IllegalArgumentException("no baseURL defined in " + this); + } + if (licenseName == null || licenseName.trim().isEmpty()) { + throw new IllegalArgumentException("licenceName can not be null"); + } + licenseName = licenseName.trim().toLowerCase(); + + if (!getLicenseNames().containsKey(licenseName)) { + // license not found + return null; + } + + try { + URL licenseURL = new URL(baseURL + "/" + licenseName + "/license.txt"); + URL headerURL = new URL(baseURL + "/" + licenseName + "/header.txt"); + + if (!checkExists(licenseURL) || !checkExists(headerURL)) { + // no content find + return null; + } + + return new License(licenseName, licenseURL, headerURL); + + + } catch (MalformedURLException e) { + throw new IllegalArgumentException(e); + } catch (IOException e) { + return null; + } + + } + + @Override + public String toString() { + return super.toString() + "<baseURL:" + baseURL + ", licenseNames:" + licenseNames + ">"; + } + + protected boolean checkExists(URL url) throws IOException { + return url.openConnection().getContentLength() > 0; + } + +} \ No newline at end of file Modified: maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java =================================================================== --- maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java 2008-10-12 20:42:31 UTC (rev 1170) +++ maven-license-switcher-plugin/trunk/src/main/java/org/codelutin/license/SwitchLicenseMojo.java 2008-10-12 20:43:11 UTC (rev 1171) @@ -75,10 +75,10 @@ /** * Le type de license a appliquer. * - * @parameter expression="${license-switcher.licenseType}" default-value="LGPL_V3" + * @parameter expression="${license-switcher.licenseName}" * @required */ - protected String licenseType; + protected String licenseName; /** * Repertoires des fichiers sources a traiter. @@ -96,6 +96,13 @@ */ protected List<String> testCompileSourceRoots; + /** + * Un resolver externe + * + * @parameter expression="${license-switcher.extraResolver}" + */ + protected String extraResolver; + /** le header a ajouter dans chaque fichier source java */ protected String licenseHeaderContent; @@ -108,8 +115,6 @@ timestamp = System.nanoTime(); - LicenseType type = LicenseType.valueOf(licenseType); - // obtain all java source files to be treated javaFilesToTreate = getFilesToTreate(); @@ -118,10 +123,13 @@ return; } + LicenseFactory factory = LicenseFactory.newInstance(extraResolver); + + License license = factory.revolv(licenseName); + // obtain content of license header - licenseHeaderContent = computeHeader(type); + licenseHeaderContent = computeHeader(license); - if (verbose) { getLog().info("header to write on java source files \n" + licenseHeaderContent); } @@ -190,9 +198,11 @@ return files; } - protected String computeHeader(LicenseType type) throws IOException { - String tmpHeader = FileUtil.readAsString(type.getLicenceHeaderReader(encoding)); + protected String computeHeader(License license) throws IOException { + + String tmpHeader = license.getHeaderContent(encoding); + // defined inceptionYear (if year is older than now suffix with a - thisYear) Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); Modified: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java =================================================================== --- maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java 2008-10-12 20:42:31 UTC (rev 1170) +++ maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java 2008-10-12 20:43:11 UTC (rev 1171) @@ -3,6 +3,9 @@ import junit.framework.TestCase; import java.io.IOException; +import java.util.Arrays; +import java.util.Map; +import java.util.ArrayList; /** @author chemit */ public class JarLicenseResolverTest extends TestCase { @@ -19,6 +22,20 @@ assertTrue(true); } + try { + resolver.resolv(""); + fail(); + } catch (IllegalArgumentException e) { + assertTrue(true); + } + + try { + resolver.resolv(" "); + fail(); + } catch (IllegalArgumentException e) { + assertTrue(true); + } + { String name = "dummy_" + System.currentTimeMillis(); License license = resolver.resolv(name); @@ -37,6 +54,17 @@ } + public void testGetLicenseNames() throws Exception { + JarLicenseResolver resolver = new JarLicenseResolver(); + Map<String,String> licenseNames = resolver.getLicenseNames(); + + assertEquals(Arrays.asList("gpl_v1", + "gpl_v2", + "gpl_v3", + "lgpl_v2_1", + "lgpl_v3"), new ArrayList<String>(licenseNames.keySet())); + } + protected void assertLicenseFound(String name, License license) throws IOException { System.out.println(license); assertNotNull(license); Added: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseFactoryTest.java =================================================================== --- maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseFactoryTest.java (rev 0) +++ maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseFactoryTest.java 2008-10-12 20:43:11 UTC (rev 1171) @@ -0,0 +1,43 @@ +package org.codelutin.license; + +import junit.framework.TestCase; + +import java.util.Map; + +/** @author chemit */ +public class LicenseFactoryTest extends TestCase { + + public void testNewInstance() throws Exception { + + LicenseFactory factory = LicenseFactory.newInstance(null); + assertNotNull(factory); + assertEquals(1, factory.resolvers.length); + assertNotNull(factory.resolvers[0]); + assertEquals(JarLicenseResolver.class, factory.resolvers[0].getClass()); + + factory = LicenseFactory.newInstance(""); + assertNotNull(factory); + assertEquals(1, factory.resolvers.length); + assertNotNull(factory.resolvers[0]); + assertEquals(JarLicenseResolver.class, factory.resolvers[0].getClass()); + + factory = LicenseFactory.newInstance("yo"); + assertNotNull(factory); + assertEquals(2, factory.resolvers.length); + assertNotNull(factory.resolvers[0]); + assertEquals(JarLicenseResolver.class, factory.resolvers[0].getClass()); + assertNotNull(factory.resolvers[1]); + assertEquals(LicenseResolver.class, factory.resolvers[1].getClass()); + assertEquals("yo", factory.resolvers[1].getBaseURL()); + } + + public void testGetLicenseNames() throws Exception { + LicenseFactory factory = LicenseFactory.newInstance(null); + + Map<String, String> licenseNames = factory.getLicenseNames(); + //todo make dynamic that test + assertEquals(5, licenseNames.size()); + + } + +} Copied: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java (from rev 1165, maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/JarLicenseResolverTest.java) =================================================================== --- maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java (rev 0) +++ maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java 2008-10-12 20:43:11 UTC (rev 1171) @@ -0,0 +1,122 @@ +package org.codelutin.license; + +import junit.framework.TestCase; +import org.codelutin.util.FileUtil; + +import java.io.File; +import java.util.Map; + +/** @author chemit */ +public class LicenseResolverTest extends TestCase { + + protected String encoding = "utf-8"; + + public void testResolvIllegalArgumentException() throws Exception { + + LicenseResolver resolver = new LicenseResolver(); + + try { + // no baseURL + // no licenseName + // no licenses.txt + resolver.resolv(null); + fail(); + } catch (IllegalArgumentException e) { + assertTrue(true); + } + + try { + // no baseURL + // no licenseName + // no licenses.txt + resolver.resolv(""); + fail(); + } catch (IllegalArgumentException e) { + assertTrue(true); + } + + try { + // no baseURL + // no licenseName + // no licenses.txt + resolver.resolv(" "); + fail(); + } catch (IllegalArgumentException e) { + assertTrue(true); + } + + try { + // no baseURL + // no licenses.txt + resolver.resolv("dummy"); + fail(); + } catch (IllegalArgumentException e) { + assertTrue(true); + } + + resolver.setBaseURL("dummy"); + try { + // no licenses.txt + resolver.resolv("dummy"); + fail(); + } catch (IllegalArgumentException e) { + assertTrue(true); + } + + } + + public void testResolv() throws Exception { + + LicenseResolver resolver = new LicenseResolver(); + + long timestamp = System.currentTimeMillis(); + String licenseName = "dummy_" + timestamp; + + File file = new File(getBaseDir(), "target" + File.separator + "licenses_" + timestamp); + + resolver.setBaseURL(file.toURI().toURL().toString()); + + License license; + + try { + // no licenses.txt + resolver.resolv(licenseName); + fail(); + } catch (IllegalArgumentException e) { + assertTrue(true); + } + + FileUtil.writeString(new File(file, "licenses.properties"), licenseName + "=My dummy license\n", encoding); + license = resolver.resolv(licenseName); + assertNull(license); + + Map<String, String> licenseNames = resolver.getLicenseNames(); + assertNotNull(licenseNames); + assertEquals(1, licenseNames.size()); + assertTrue(licenseNames.containsKey(licenseName)); + + // create dummy licenses + file = new File(file, licenseName); + file.mkdirs(); + FileUtil.writeString(new File(file, "license.txt"), "license", encoding); + FileUtil.writeString(new File(file, "header.txt"), "header", encoding); + license = resolver.resolv(licenseName); + + assertNotNull(license); + assertEquals(licenseName, license.getName()); + assertNotNull(license.getLicenseContent(encoding)); + assertNotNull(license.getHeaderContent(encoding)); + assertEquals("license", license.getLicenseContent(encoding)); + assertEquals("header", license.getHeaderContent(encoding)); + + } + + protected File getBaseDir() { + String path = System.getenv("basedir"); + if (path == null) { + path = new File("").getAbsolutePath(); + } + return new File(path); + } + +} \ No newline at end of file Property changes on: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseResolverTest.java ___________________________________________________________________ Name: svn:mergeinfo + Deleted: maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseTypeTest.java =================================================================== --- maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseTypeTest.java 2008-10-12 20:42:31 UTC (rev 1170) +++ maven-license-switcher-plugin/trunk/src/test/java/org/codelutin/license/LicenseTypeTest.java 2008-10-12 20:43:11 UTC (rev 1171) @@ -1,38 +0,0 @@ -/** - * *##% Plugin maven pour switcher les licenses - * Copyright (C) 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* - */ - -package org.codelutin.license; - -/** - * A class to test loading of license via {@link LicenseType} - * - * @author chemit - */ -public class LicenseTypeTest extends junit.framework.TestCase { - - private static final String ENCODING = "UTF-8"; - - public void testAcquireLicenceReaders() throws Exception { - for (LicenseType license : LicenseType.values()) { - license.getLicenceFileReader(ENCODING); - license.getLicenceHeaderReader(ENCODING); - } - } - -}