OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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";
41 ilm 33
    public static final String NAME = "NAME";
142 ilm 34
    public static final String ID = "ID";
41 ilm 35
    public static final String VERSION = "VERSION";
17 ilm 36
 
41 ilm 37
    private static ProductInfo INSTANCE;
38
 
39
    /**
40
     * If {@link #setInstance(ProductInfo)} was called with a non-null value, return that ;
41
     * otherwise use {@link #createDefault()}.
149 ilm 42
     *
41 ilm 43
     * @return the current instance, can be <code>null</code>.
44
     */
17 ilm 45
    public synchronized static final ProductInfo getInstance() {
41 ilm 46
        if (INSTANCE == null) {
47
            try {
142 ilm 48
                setInstance(createDefault());
149 ilm 49
            } catch (final IOException e) {
41 ilm 50
                throw new IllegalStateException("unable to load default product properties", e);
51
            }
52
        }
17 ilm 53
        return INSTANCE;
54
    }
55
 
41 ilm 56
    /**
57
     * Set the current instance.
149 ilm 58
     *
41 ilm 59
     * @param i the new instance, can be <code>null</code>.
60
     */
149 ilm 61
    public synchronized static void setInstance(final ProductInfo i) {
41 ilm 62
        INSTANCE = i;
63
    }
17 ilm 64
 
41 ilm 65
    /**
66
     * Create a product info from the default properties file, {@value #PROPERTIES_NAME}.
149 ilm 67
     *
41 ilm 68
     * @return the default properties, or <code>null</code> if they couldn't be found.
69
     * @throws IOException if properties couldn't be loaded.
70
     */
71
    public static final ProductInfo createDefault() throws IOException {
72
        final Properties p = PropertiesUtils.createFromResource(ProductInfo.class, PROPERTIES_NAME);
73
        return p == null ? null : new ProductInfo(p);
17 ilm 74
    }
75
 
142 ilm 76
    static private final String sanitizeDomain(final String s) {
77
        return FileUtils.sanitize(s).replace(' ', '_');
78
    }
79
 
80
    static private final String sanitizeDomainPart(final String s) {
81
        return sanitizeDomain(s).replace('.', '_');
82
    }
83
 
41 ilm 84
    private final Properties props;
85
 
86
    public ProductInfo(final String name) {
149 ilm 87
        this(name, null);
41 ilm 88
    }
89
 
149 ilm 90
    public ProductInfo(final String name, final String version) {
91
        this(name, version, "ILM Informatique", "fr.ilm-informatique");
142 ilm 92
    }
93
 
149 ilm 94
    public ProductInfo(final String name, final String version, final String orgName, final String orgID) {
95
        this(CollectionUtils.createMapFromList(NAME, name, VERSION, version, ORGANIZATION_NAME, orgName, ORGANIZATION_ID, orgID));
96
    }
97
 
41 ilm 98
    public ProductInfo(final Map<String, String> map) {
99
        this(PropertiesUtils.createFromMap(map));
100
    }
101
 
102
    public ProductInfo(final Properties props) {
103
        if (props == null)
104
            throw new NullPointerException("Null properties");
105
        if (props.getProperty(NAME) == null)
106
            throw new IllegalArgumentException("Missing " + NAME);
107
        this.props = props;
108
    }
109
 
17 ilm 110
    /**
111
     * The properties.
149 ilm 112
     *
41 ilm 113
     * @return the associated properties.
17 ilm 114
     */
41 ilm 115
    private final Properties getProps() {
17 ilm 116
        return this.props;
117
    }
118
 
149 ilm 119
    public final String getProperty(final String name) {
41 ilm 120
        return this.getProps().getProperty(name);
121
    }
122
 
149 ilm 123
    public final String getProperty(final String name, final String def) {
142 ilm 124
        final String res = this.getProperty(name);
125
        return StringUtils.isEmpty(res, true) ? def : res;
41 ilm 126
    }
127
 
142 ilm 128
    public final String getOrganizationName() {
129
        return this.getProperty(ORGANIZATION_NAME);
130
    }
131
 
132
    // com.acme
133
    public final String getOrganizationID() {
134
        final String res = this.getProperty(ORGANIZATION_ID);
135
        if (res != null)
136
            return sanitizeDomain(res);
137
        final String name = this.getOrganizationName();
138
        if (name != null)
139
            return "com." + sanitizeDomainPart(this.getOrganizationName());
140
        return null;
141
    }
142
 
143
    // my app
41 ilm 144
    public final String getName() {
145
        return this.getProperty(NAME, "unnamed product");
146
    }
147
 
142 ilm 148
    // my_app
149
    public final String getID() {
150
        return sanitizeDomainPart(this.getProperty(ID, this.getName()));
151
    }
152
 
153
    // com.acme.my_app
154
    public final String getFullID() {
155
        final String orgID = this.getOrganizationID();
156
        return orgID == null ? null : orgID + '.' + this.getID();
157
    }
158
 
41 ilm 159
    public final String getVersion() {
160
        return this.getProperty(VERSION);
161
    }
83 ilm 162
 
174 ilm 163
    public final void store(final Path p) throws IOException {
164
        try (final OutputStream out = Files.newOutputStream(p)) {
165
            this.getProps().store(out, this.getClass().getName());
166
        }
167
    }
168
 
83 ilm 169
    @Override
170
    public String toString() {
171
        return this.getClass().getSimpleName() + " for " + getName() + " " + getVersion();
172
    }
17 ilm 173
}