]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/FreeLicense.java
0ce7fe8d753d654a5b0bc2677df8ea522e5bf555
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / FreeLicense.java
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 public final static FreeLicense GPL_v3 = new FreeLicense(
15 "GNU General Public License, version 3.0",
16 "http://www.gnu.org/licenses/gpl-3.0.txt",
17 "http://www.gnu.org/licenses/", RESOURCES + "gpl-3.0.txt") {
18 };
19
20 public final static FreeLicense GPL_v2 = new FreeLicense(
21 "GNU General Public License, version 2.0",
22 "http://www.gnu.org/licenses/gpl-2.0.txt",
23 "http://www.gnu.org/licenses/", RESOURCES + "gpl-2.0.txt") {
24 };
25
26 public final static FreeLicense APACHE_v2 = new FreeLicense(
27 "Apache License, Version 2.0",
28 "http://www.apache.org/licenses/LICENSE-2.0.txt",
29 "http://www.apache.org/licenses/", RESOURCES + "apache-2.0.txt") {
30 };
31
32 public final static FreeLicense EPL_v1 = new FreeLicense(
33 "Eclipse Public License, Version 1.0",
34 "http://www.eclipse.org/legal/epl-v10.html",
35 "http://www.eclipse.org/legal/eplfaq.php", RESOURCES
36 + "epel-1.0.txt") {
37 };
38
39 public final static FreeLicense MIT = new FreeLicense("The MIT License",
40 "http://opensource.org/licenses/MIT", null, RESOURCES + "mit.txt") {
41 };
42
43 public final static FreeLicense LGPL_v3 = new FreeLicense(
44 "GNU Lesser General Public License, version 3.0",
45 "http://www.gnu.org/licenses/lgpl-3.0.txt",
46 "http://www.gnu.org/licenses/", RESOURCES + "lgpl-3.0.txt") {
47 };
48
49 public final static FreeLicense LGPL_v2 = new FreeLicense(
50 "GNU Lesser General Public License, version 2.1",
51 "http://www.gnu.org/licenses/lgpl-2.1.txt",
52 "http://www.gnu.org/licenses/", RESOURCES + "lgpl-2.1.txt") {
53 };
54
55 private final String name, uri, link, resource;
56
57 public FreeLicense(String name, String uri) {
58 this(name, uri, null, null);
59 }
60
61 public FreeLicense(String name, String uri, String link) {
62 this(name, uri, link, null);
63 }
64
65 public FreeLicense(String name, String uri, String link, String resource) {
66 if (uri == null)
67 throw new SlcException("URI cannot be null");
68 this.name = name;
69 this.uri = uri;
70 this.link = link;
71 this.resource = resource;
72 getText();
73 }
74
75 public String getName() {
76 return name;
77 }
78
79 public String getUri() {
80 return uri;
81 }
82
83 public String getLink() {
84 return link;
85 }
86
87 @Override
88 public String getText() {
89 InputStream in = null;
90 URL url = null;
91 try {
92 if (resource != null)
93 url = getClass().getClassLoader().getResource(resource);
94 else
95 url = new URL(uri);
96 in = url.openStream();
97 String text = IOUtils.toString(in);
98 return text;
99 } catch (Exception e) {
100 throw new SlcException("Cannot retrieve license " + name + " from "
101 + url, e);
102 } finally {
103 IOUtils.closeQuietly(in);
104 }
105 }
106
107 @Override
108 public boolean equals(Object obj) {
109 if (!(obj instanceof License))
110 return false;
111 return ((License) obj).getUri().equals(getUri());
112 }
113
114 @Override
115 public int hashCode() {
116 return getUri().hashCode();
117 }
118
119 @Override
120 public String toString() {
121 return name + " (" + uri + ")";
122 }
123 }