OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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