View Javadoc
1   package org.argeo.slc.repo;
2   
3   import java.io.InputStream;
4   import java.net.URL;
5   
6   import org.apache.commons.io.IOUtils;
7   import org.argeo.slc.SlcException;
8   import org.argeo.slc.build.License;
9   
10  /** A free software license */
11  public abstract class FreeLicense implements License {
12  	final static String RESOURCES = "/org/argeo/slc/repo/license/";
13  
14  	/** GNU */
15  	public final static FreeLicense GPL_v3 = new FreeLicense(
16  			"GNU General Public License, version 3.0",
17  			"http://www.gnu.org/licenses/gpl-3.0.txt",
18  			"http://www.gnu.org/licenses/", RESOURCES + "gpl-3.0.txt") {
19  	};
20  
21  	public final static FreeLicense GPL_v2 = new FreeLicense(
22  			"GNU General Public License, version 2.0",
23  			"http://www.gnu.org/licenses/gpl-2.0.txt",
24  			"http://www.gnu.org/licenses/", RESOURCES + "gpl-2.0.txt") {
25  	};
26  	public final static FreeLicense GPL = GPL_v3;
27  
28  	public final static FreeLicense LGPL_v3 = new FreeLicense(
29  			"GNU Lesser General Public License, version 3.0",
30  			"http://www.gnu.org/licenses/lgpl-3.0.txt",
31  			"http://www.gnu.org/licenses/", RESOURCES + "lgpl-3.0.txt") {
32  	};
33  
34  	public final static FreeLicense LGPL_v2 = new FreeLicense(
35  			"GNU Lesser General Public License, version 2.1",
36  			"http://www.gnu.org/licenses/lgpl-2.1.txt",
37  			"http://www.gnu.org/licenses/", RESOURCES + "lgpl-2.1.txt") {
38  	};
39  	public final static FreeLicense LGPL = LGPL_v3;
40  
41  	/** Apache */
42  	public final static FreeLicense APACHE_v2 = new FreeLicense(
43  			"Apache License, Version 2.0",
44  			"http://www.apache.org/licenses/LICENSE-2.0.txt",
45  			"http://www.apache.org/licenses/", RESOURCES + "apache-2.0.txt") {
46  	};
47  	public final static FreeLicense APACHE = APACHE_v2;
48  
49  	/** Eclipse */
50  	public final static FreeLicense EPL_v1 = new FreeLicense(
51  			"Eclipse Public License, Version 1.0",
52  			"http://www.eclipse.org/legal/epl-v10.html",
53  			"http://www.eclipse.org/legal/eplfaq.php", RESOURCES
54  					+ "epl-1.0.txt") {
55  	};
56  	public final static FreeLicense EPL = EPL_v1;
57  
58  	/** Miscellaneous */
59  	public final static FreeLicense MIT = new FreeLicense("The MIT License",
60  			"http://opensource.org/licenses/MIT", null, RESOURCES + "mit.txt") {
61  	};
62  
63  	public final static FreeLicense BSD_NEW = new FreeLicense(
64  			"The BSD 3-Clause License",
65  			"http://opensource.org/licenses/BSD-3-Clause", null, RESOURCES
66  					+ "bsd-3-clause.txt") {
67  	};
68  
69  	public final static FreeLicense BSD = BSD_NEW;
70  
71  	public final static FreeLicense CDDL_v1 = new FreeLicense(
72  			"Common Development and Distribution License",
73  			"http://opensource.org/licenses/CDDL-1.0", null, RESOURCES
74  					+ "cddl-1.0.txt") {
75  	};
76  	public final static FreeLicense CDDL = CDDL_v1;
77  
78  	/** Public domain corner case */
79  	public final static License PUBLIC_DOMAIN = new License() {
80  
81  		public String getUri() {
82  			return "http://creativecommons.org/about/pdm";
83  		}
84  
85  		public String getText() {
86  			return "This work is free of known copyright restrictions.";
87  		}
88  
89  		public String getName() {
90  			return "Public Domain License";
91  		}
92  
93  		public String getLink() {
94  			return "http://wiki.creativecommons.org/PDM_FAQ";
95  		}
96  	};
97  
98  	private final String name, uri, link, resource;
99  
100 	public FreeLicense(String name, String uri) {
101 		this(name, uri, null, null);
102 	}
103 
104 	public FreeLicense(String name, String uri, String link) {
105 		this(name, uri, link, null);
106 	}
107 
108 	public FreeLicense(String name, String uri, String link, String resource) {
109 		if (uri == null)
110 			throw new SlcException("URI cannot be null");
111 		this.name = name;
112 		this.uri = uri;
113 		this.link = link;
114 		this.resource = resource;
115 		getText();
116 	}
117 
118 	public String getName() {
119 		return name;
120 	}
121 
122 	public String getUri() {
123 		return uri;
124 	}
125 
126 	public String getLink() {
127 		return link;
128 	}
129 
130 	@Override
131 	public String getText() {
132 		InputStream in = null;
133 		URL url = null;
134 		try {
135 			if (resource != null)
136 				url = getClass().getClassLoader().getResource(resource);
137 			else
138 				url = new URL(uri);
139 			in = url.openStream();
140 			String text = IOUtils.toString(in);
141 			return text;
142 		} catch (Exception e) {
143 			throw new SlcException("Cannot retrieve license " + name + " from "
144 					+ url, e);
145 		} finally {
146 			IOUtils.closeQuietly(in);
147 		}
148 	}
149 
150 	@Override
151 	public boolean equals(Object obj) {
152 		if (!(obj instanceof License))
153 			return false;
154 		return ((License) obj).getUri().equals(getUri());
155 	}
156 
157 	@Override
158 	public int hashCode() {
159 		return getUri().hashCode();
160 	}
161 
162 	@Override
163 	public String toString() {
164 		return name + " (" + uri + ")";
165 	}
166 }