OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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