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