]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/FreeLicense.java
Improve licenses and sources
[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 /** 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 CDDL_v1 = new FreeLicense(
64 "Common Development and Distribution License",
65 "http://opensource.org/licenses/CDDL-1.0", null, RESOURCES
66 + "cddl-1.0.txt") {
67 };
68 public final static FreeLicense CDDL = CDDL_v1;
69
70 /** Public domain corner case */
71 public final static License PUBLIC_DOMAIN = new License() {
72
73 public String getUri() {
74 return "http://creativecommons.org/about/pdm";
75 }
76
77 public String getText() {
78 return "This work is free of known copyright restrictions.";
79 }
80
81 public String getName() {
82 return "Public Domain License";
83 }
84
85 public String getLink() {
86 return "http://wiki.creativecommons.org/PDM_FAQ";
87 }
88 };
89
90 private final String name, uri, link, resource;
91
92 public FreeLicense(String name, String uri) {
93 this(name, uri, null, null);
94 }
95
96 public FreeLicense(String name, String uri, String link) {
97 this(name, uri, link, null);
98 }
99
100 public FreeLicense(String name, String uri, String link, String resource) {
101 if (uri == null)
102 throw new SlcException("URI cannot be null");
103 this.name = name;
104 this.uri = uri;
105 this.link = link;
106 this.resource = resource;
107 getText();
108 }
109
110 public String getName() {
111 return name;
112 }
113
114 public String getUri() {
115 return uri;
116 }
117
118 public String getLink() {
119 return link;
120 }
121
122 @Override
123 public String getText() {
124 InputStream in = null;
125 URL url = null;
126 try {
127 if (resource != null)
128 url = getClass().getClassLoader().getResource(resource);
129 else
130 url = new URL(uri);
131 in = url.openStream();
132 String text = IOUtils.toString(in);
133 return text;
134 } catch (Exception e) {
135 throw new SlcException("Cannot retrieve license " + name + " from "
136 + url, e);
137 } finally {
138 IOUtils.closeQuietly(in);
139 }
140 }
141
142 @Override
143 public boolean equals(Object obj) {
144 if (!(obj instanceof License))
145 return false;
146 return ((License) obj).getUri().equals(getUri());
147 }
148
149 @Override
150 public int hashCode() {
151 return getUri().hashCode();
152 }
153
154 @Override
155 public String toString() {
156 return name + " (" + uri + ")";
157 }
158 }