OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Go to most recent revision | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 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
 
177 ilm 16
import java.util.ArrayList;
17 ilm 17
import java.util.Arrays;
18
import java.util.Collections;
19
import java.util.List;
182 ilm 20
import java.util.function.Function;
17 ilm 21
import java.util.regex.Pattern;
22
 
23
public class SystemUtils {
24
 
25
    /**
26
     * Wrap a system property and view it as a list.
27
     *
28
     * @author Sylvain CUAZ
29
     */
30
    public static final class PropertyList {
31
        private final String name;
32
        private final Pattern p;
33
 
34
        /**
35
         * Create a new instance.
36
         *
37
         * @param name name of the property, e.g. "java.protocol.handler.pkgs".
38
         * @param sep the separator used, e.g. "|".
39
         */
40
        public PropertyList(final String name, final String sep) {
41
            this.name = name;
42
            this.p = Pattern.compile(sep, Pattern.LITERAL);
43
        }
44
 
45
        public final String getName() {
46
            return this.name;
47
        }
48
 
49
        private final String getSeparator() {
50
            return this.p.pattern();
51
        }
52
 
53
        public final String getValue() {
54
            return System.getProperty(this.name);
55
        }
56
 
57
        public final List<String> getValues() {
58
            return getList(getValue());
59
        }
60
 
61
        private final List<String> getList(final String current) {
62
            if (current == null)
63
                return null;
64
            else if (current.length() == 0)
65
                return Collections.emptyList();
66
            else {
67
                return Arrays.asList(this.p.split(current));
68
            }
69
        }
70
 
71
        /**
72
         * Adds a value to the system property list if not already present.
73
         *
74
         * @param value the value to add, e.g. "sun.net.www.protocol".
75
         * @return <code>true</code> if the property was modified.
76
         */
77
        public final boolean add(final String value) {
78
            return this.add(value, true);
79
        }
80
 
81
        public final boolean add(final String value, boolean append) {
82
            if (value == null)
83
                throw new NullPointerException("Null value");
84
 
85
            final String current = getValue();
86
            final List<String> l = getList(current);
87
            final String newVal;
88
            if (l == null || l.size() == 0)
89
                newVal = value;
90
            else if (l.contains(value))
91
                newVal = null;
92
            else if (append)
93
                newVal = current + this.getSeparator() + value;
94
            else
95
                newVal = value + this.getSeparator() + current;
96
 
97
            if (newVal != null) {
98
                System.setProperty(this.name, newVal);
99
                return true;
100
            } else
101
                return false;
102
        }
177 ilm 103
 
104
        public final boolean remove(final String value) {
105
            return this.remove(value, true);
106
        }
107
 
108
        public final boolean remove(final String value, final boolean unsetIfEmpty) {
109
            if (value == null)
110
                throw new NullPointerException("Null value");
111
            final List<String> l = getValues();
112
            if (l == null || l.size() == 0 || !l.contains(value))
113
                return false;
114
 
115
            final List<String> newList = new ArrayList<>(l.size() - 1);
116
            for (final String item : l) {
117
                if (!item.equals(value))
118
                    newList.add(item);
119
            }
120
            if (unsetIfEmpty && newList.isEmpty())
121
                System.clearProperty(this.name);
122
            else
123
                System.setProperty(this.name, CollectionUtils.join(newList, this.getSeparator()));
124
            return true;
125
        }
17 ilm 126
    }
182 ilm 127
 
128
    static public final <T extends Enum<T>> T getEnumFromProperty(final String propName, final Class<T> clazz, final T def) {
129
        final String prop = System.getProperty(propName);
130
        if (prop == null)
131
            return def;
132
        return Enum.valueOf(clazz, propName);
133
    }
134
 
135
    static public final <T> T getProperty(final String propName, final Function<String, T> func, final T def) {
136
        final String prop = System.getProperty(propName);
137
        if (prop == null)
138
            return def;
139
        return func.apply(prop);
140
    }
17 ilm 141
}