View Javadoc

1   /*
2    SystemUtils.java
3    Creation date : 16/01/2010
4    Copyright © Benjamin Croizet (graffity2199@yahoo.fr)
5   
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    or GNU Lesser General Public License as published by the
9    Free Software Foundation; either version 3 of the License,
10   or (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received copies of the GNU General Public License
18   and GNU Lesser General Public License along with this program;
19   if not, write to the Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21   http://www.fsf.org/licensing/licenses/gpl.html
22   http://www.gnu.org/licenses/lgpl.html
23   */
24  
25  package net.sourceforge.plantumldependency.common.utils.system;
26  
27  import static java.lang.System.getProperties;
28  import static java.util.logging.Level.FINE;
29  import static java.util.logging.Level.INFO;
30  import static java.util.logging.Logger.getLogger;
31  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.COLON_CHAR;
32  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.SPACE_CHAR;
33  import static net.sourceforge.plantumldependency.common.constants.log.FineConstants.SYSTEM_PROPERTY_NOT_TO_DISPLAY_FINE;
34  import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
35  import static net.sourceforge.plantumldependency.common.utils.string.StringUtils.convertObjectSetIntoStringSet;
36  
37  import java.io.PrintStream;
38  import java.util.Map;
39  import java.util.Set;
40  import java.util.TreeSet;
41  import java.util.logging.Level;
42  import java.util.logging.Logger;
43  
44  /**
45   * The class utilities simplifying some system tasks.
46   *
47   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
48   * @since 1.3.0
49   * @version 1.3.0
50   */
51  public abstract class SystemUtils {
52  
53      /** The class logger. */
54      private static final transient Logger LOGGER = getLogger(SystemUtils.class.getName());
55  
56      /**
57       * Gets the string description of the system properties in order to be displayed.
58       *
59       * @return the {@link Set} of all {@link String} representing system properties.
60       * @since 1.3.0
61       */
62      public static Set < String > getSystemPropertiesSetToDisplay() {
63          return getSystemPropertiesSetToDisplay(convertObjectSetIntoStringSet(getProperties().keySet()));
64      }
65  
66      /**
67       * Gets the string description of the passed properties keys in order to be displayed.
68       *
69       * @param propertiesSet
70       *            the {@link Set} containing system properties keys to get the string from, mustn't
71       *            be <code>null</code>.
72       * @return the {@link Set} of all {@link String} representing system properties.
73       * @since 1.3.0
74       */
75      public static Set < String > getSystemPropertiesSetToDisplay(final Set < String > propertiesSet) {
76          final Set < String > systemPropertiesSetToDisplay = new TreeSet < String >();
77  
78          for (final Map.Entry < Object, Object > property : getProperties().entrySet()) {
79              if (propertiesSet.contains(property.getKey())) {
80                  final String propertyStr = property.getKey() + SPACE_CHAR + COLON_CHAR + SPACE_CHAR
81                          + property.getValue();
82                  systemPropertiesSetToDisplay.add(propertyStr);
83              } else {
84                  LOGGER.log(FINE, buildLogString(SYSTEM_PROPERTY_NOT_TO_DISPLAY_FINE, property));
85              }
86          }
87  
88          return systemPropertiesSetToDisplay;
89      }
90  
91      /**
92       * This method displays all system properties, operating system, java version etc... in the
93       * default logger with the info level.
94       *
95       * @see System
96       * @since 1.3.0
97       */
98      public static void printSystemProperties() {
99          printSystemProperties(LOGGER, INFO, convertObjectSetIntoStringSet(getProperties().keySet()));
100     }
101 
102     /**
103      * This method displays all system properties, operating system, java version etc...
104      *
105      * @param logger
106      *            the {@link Logger} to print the properties, info level is used, mustn't be
107      *            <code>null</code>.
108      * @see System
109      * @since 1.3.0
110      */
111     public static void printSystemProperties(final Logger logger) {
112         printSystemProperties(logger, INFO, convertObjectSetIntoStringSet(getProperties().keySet()));
113     }
114 
115     /**
116      * This method displays all system properties, operating system, java version etc...
117      *
118      * @param logger
119      *            the {@link Logger} to print the properties, info level is used, mustn't be
120      *            <code>null</code>.
121      * @param level
122      *            the {@link Level} to log the system properties, mustn't be <code>null</code>.
123      * @param propertiesSet
124      *            the {@link Set} containing system properties keys to get the string from, mustn't
125      *            be <code>null</code>.
126      * @see System
127      * @since 1.3.0
128      */
129     public static void printSystemProperties(final Logger logger, final Level level, final Set < String > propertiesSet) {
130         final Set < String > systemPropertiesSetToDisplay = getSystemPropertiesSetToDisplay(propertiesSet);
131 
132         for (final String propertyStr : systemPropertiesSetToDisplay) {
133             logger.log(level, propertyStr);
134         }
135     }
136 
137     /**
138      * This method displays all system properties where the keys are passed.
139      *
140      * @param logger
141      *            the {@link Logger} to print the properties, info level is used, mustn't be
142      *            <code>null</code>.
143      * @param propertiesSet
144      *            the {@link Set} containing system properties keys to get the string from, mustn't
145      *            be <code>null</code>.
146      * @see System
147      * @since 1.3.0
148      */
149     public static void printSystemProperties(final Logger logger, final Set < String > propertiesSet) {
150         printSystemProperties(logger, INFO, propertiesSet);
151     }
152 
153     /**
154      * This method displays all system properties, operating system, java version etc...
155      *
156      * @param printStream
157      *            the {@link PrintStream} where to log system properties, mustn't be
158      *            <code>null</code>.
159      * @see System
160      * @since 0.7
161      */
162     public static void printSystemProperties(final PrintStream printStream) {
163         printSystemProperties(printStream, convertObjectSetIntoStringSet(getProperties().keySet()));
164     }
165 
166     /**
167      * This method displays all system properties, operating system, java version etc...
168      *
169      * @param printStream
170      *            the {@link PrintStream} where to log system properties, mustn't be
171      *            <code>null</code>.
172      * @param propertiesSet
173      *            the {@link Set} containing system properties keys to get the string from, mustn't
174      *            be <code>null</code>.
175      * @see System
176      * @since 0.7
177      */
178     public static void printSystemProperties(final PrintStream printStream, final Set < String > propertiesSet) {
179         final Set < String > systemPropertiesSetToDisplay = getSystemPropertiesSetToDisplay(propertiesSet);
180 
181         for (final String propertyStr : systemPropertiesSetToDisplay) {
182             printStream.println(propertyStr);
183         }
184     }
185 
186     /**
187      * This method displays all system properties, operating system, java version etc...
188      *
189      * @param propertiesSet
190      *            the {@link Set} containing system properties keys to get the string from, mustn't
191      *            be <code>null</code>.
192      * @see System
193      * @since 1.3.0
194      */
195     public static void printSystemProperties(final Set < String > propertiesSet) {
196         printSystemProperties(LOGGER, INFO, propertiesSet);
197     }
198 
199     /**
200      * Private constructor to prevent from instantiation.
201      *
202      * @since 1.3.0
203      */
204     private SystemUtils() {
205         super();
206     }
207 }