17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.utils;
|
|
|
15 |
|
|
|
16 |
import java.io.IOException;
|
174 |
ilm |
17 |
import java.io.OutputStream;
|
|
|
18 |
import java.nio.file.Files;
|
|
|
19 |
import java.nio.file.Path;
|
41 |
ilm |
20 |
import java.util.Map;
|
17 |
ilm |
21 |
import java.util.Properties;
|
|
|
22 |
|
|
|
23 |
/**
|
41 |
ilm |
24 |
* Useful for defining product wide values, like version, from a property file.
|
149 |
ilm |
25 |
*
|
17 |
ilm |
26 |
* @author Sylvain
|
|
|
27 |
*/
|
|
|
28 |
public class ProductInfo {
|
|
|
29 |
|
41 |
ilm |
30 |
public static final String PROPERTIES_NAME = "/product.properties";
|
142 |
ilm |
31 |
public static final String ORGANIZATION_NAME = "ORGANIZATION_NAME";
|
|
|
32 |
public static final String ORGANIZATION_ID = "ORGANIZATION_ID";
|
182 |
ilm |
33 |
// e.g. LibreOffice
|
41 |
ilm |
34 |
public static final String NAME = "NAME";
|
142 |
ilm |
35 |
public static final String ID = "ID";
|
41 |
ilm |
36 |
public static final String VERSION = "VERSION";
|
182 |
ilm |
37 |
// Allow multiple end-user applications (e.g. icons) to share a product name, e.g. Writer
|
|
|
38 |
public static final String LAUNCHER = "LAUNCHER";
|
17 |
ilm |
39 |
|
41 |
ilm |
40 |
private static ProductInfo INSTANCE;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* If {@link #setInstance(ProductInfo)} was called with a non-null value, return that ;
|
|
|
44 |
* otherwise use {@link #createDefault()}.
|
149 |
ilm |
45 |
*
|
41 |
ilm |
46 |
* @return the current instance, can be <code>null</code>.
|
|
|
47 |
*/
|
17 |
ilm |
48 |
public synchronized static final ProductInfo getInstance() {
|
41 |
ilm |
49 |
if (INSTANCE == null) {
|
|
|
50 |
try {
|
142 |
ilm |
51 |
setInstance(createDefault());
|
149 |
ilm |
52 |
} catch (final IOException e) {
|
41 |
ilm |
53 |
throw new IllegalStateException("unable to load default product properties", e);
|
|
|
54 |
}
|
|
|
55 |
}
|
17 |
ilm |
56 |
return INSTANCE;
|
|
|
57 |
}
|
|
|
58 |
|
41 |
ilm |
59 |
/**
|
|
|
60 |
* Set the current instance.
|
149 |
ilm |
61 |
*
|
41 |
ilm |
62 |
* @param i the new instance, can be <code>null</code>.
|
|
|
63 |
*/
|
149 |
ilm |
64 |
public synchronized static void setInstance(final ProductInfo i) {
|
41 |
ilm |
65 |
INSTANCE = i;
|
|
|
66 |
}
|
17 |
ilm |
67 |
|
41 |
ilm |
68 |
/**
|
|
|
69 |
* Create a product info from the default properties file, {@value #PROPERTIES_NAME}.
|
149 |
ilm |
70 |
*
|
41 |
ilm |
71 |
* @return the default properties, or <code>null</code> if they couldn't be found.
|
|
|
72 |
* @throws IOException if properties couldn't be loaded.
|
|
|
73 |
*/
|
|
|
74 |
public static final ProductInfo createDefault() throws IOException {
|
|
|
75 |
final Properties p = PropertiesUtils.createFromResource(ProductInfo.class, PROPERTIES_NAME);
|
|
|
76 |
return p == null ? null : new ProductInfo(p);
|
17 |
ilm |
77 |
}
|
|
|
78 |
|
142 |
ilm |
79 |
static private final String sanitizeDomain(final String s) {
|
|
|
80 |
return FileUtils.sanitize(s).replace(' ', '_');
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
static private final String sanitizeDomainPart(final String s) {
|
|
|
84 |
return sanitizeDomain(s).replace('.', '_');
|
|
|
85 |
}
|
|
|
86 |
|
41 |
ilm |
87 |
private final Properties props;
|
|
|
88 |
|
|
|
89 |
public ProductInfo(final String name) {
|
149 |
ilm |
90 |
this(name, null);
|
41 |
ilm |
91 |
}
|
|
|
92 |
|
149 |
ilm |
93 |
public ProductInfo(final String name, final String version) {
|
|
|
94 |
this(name, version, "ILM Informatique", "fr.ilm-informatique");
|
142 |
ilm |
95 |
}
|
|
|
96 |
|
149 |
ilm |
97 |
public ProductInfo(final String name, final String version, final String orgName, final String orgID) {
|
|
|
98 |
this(CollectionUtils.createMapFromList(NAME, name, VERSION, version, ORGANIZATION_NAME, orgName, ORGANIZATION_ID, orgID));
|
|
|
99 |
}
|
|
|
100 |
|
41 |
ilm |
101 |
public ProductInfo(final Map<String, String> map) {
|
|
|
102 |
this(PropertiesUtils.createFromMap(map));
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
public ProductInfo(final Properties props) {
|
|
|
106 |
if (props == null)
|
|
|
107 |
throw new NullPointerException("Null properties");
|
|
|
108 |
if (props.getProperty(NAME) == null)
|
|
|
109 |
throw new IllegalArgumentException("Missing " + NAME);
|
|
|
110 |
this.props = props;
|
|
|
111 |
}
|
|
|
112 |
|
17 |
ilm |
113 |
/**
|
|
|
114 |
* The properties.
|
149 |
ilm |
115 |
*
|
41 |
ilm |
116 |
* @return the associated properties.
|
17 |
ilm |
117 |
*/
|
41 |
ilm |
118 |
private final Properties getProps() {
|
17 |
ilm |
119 |
return this.props;
|
|
|
120 |
}
|
|
|
121 |
|
149 |
ilm |
122 |
public final String getProperty(final String name) {
|
41 |
ilm |
123 |
return this.getProps().getProperty(name);
|
|
|
124 |
}
|
|
|
125 |
|
149 |
ilm |
126 |
public final String getProperty(final String name, final String def) {
|
142 |
ilm |
127 |
final String res = this.getProperty(name);
|
|
|
128 |
return StringUtils.isEmpty(res, true) ? def : res;
|
41 |
ilm |
129 |
}
|
|
|
130 |
|
142 |
ilm |
131 |
public final String getOrganizationName() {
|
|
|
132 |
return this.getProperty(ORGANIZATION_NAME);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
// com.acme
|
|
|
136 |
public final String getOrganizationID() {
|
|
|
137 |
final String res = this.getProperty(ORGANIZATION_ID);
|
|
|
138 |
if (res != null)
|
|
|
139 |
return sanitizeDomain(res);
|
|
|
140 |
final String name = this.getOrganizationName();
|
|
|
141 |
if (name != null)
|
|
|
142 |
return "com." + sanitizeDomainPart(this.getOrganizationName());
|
|
|
143 |
return null;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
// my app
|
41 |
ilm |
147 |
public final String getName() {
|
|
|
148 |
return this.getProperty(NAME, "unnamed product");
|
|
|
149 |
}
|
|
|
150 |
|
142 |
ilm |
151 |
// my_app
|
|
|
152 |
public final String getID() {
|
|
|
153 |
return sanitizeDomainPart(this.getProperty(ID, this.getName()));
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
// com.acme.my_app
|
|
|
157 |
public final String getFullID() {
|
|
|
158 |
final String orgID = this.getOrganizationID();
|
|
|
159 |
return orgID == null ? null : orgID + '.' + this.getID();
|
|
|
160 |
}
|
|
|
161 |
|
182 |
ilm |
162 |
public final String getLauncher() {
|
|
|
163 |
return this.getProperty(LAUNCHER);
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
public final String getFullLauncherID() {
|
|
|
167 |
final String launcher = this.getLauncher();
|
|
|
168 |
final String fullID = this.getFullID();
|
|
|
169 |
if (launcher == null || fullID == null)
|
|
|
170 |
return fullID;
|
|
|
171 |
return fullID + '-' + sanitizeDomainPart(launcher);
|
|
|
172 |
}
|
|
|
173 |
|
41 |
ilm |
174 |
public final String getVersion() {
|
|
|
175 |
return this.getProperty(VERSION);
|
|
|
176 |
}
|
83 |
ilm |
177 |
|
174 |
ilm |
178 |
public final void store(final Path p) throws IOException {
|
|
|
179 |
try (final OutputStream out = Files.newOutputStream(p)) {
|
182 |
ilm |
180 |
store(out);
|
174 |
ilm |
181 |
}
|
|
|
182 |
}
|
|
|
183 |
|
182 |
ilm |
184 |
public final void store(final OutputStream out) throws IOException {
|
|
|
185 |
this.getProps().store(out, this.getClass().getName());
|
|
|
186 |
}
|
|
|
187 |
|
83 |
ilm |
188 |
@Override
|
|
|
189 |
public String toString() {
|
|
|
190 |
return this.getClass().getSimpleName() + " for " + getName() + " " + getVersion();
|
|
|
191 |
}
|
17 |
ilm |
192 |
}
|