001 /** 002 * 003 * Copyright 2004 Protique Ltd 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 **/ 018 package org.activemq.util; 019 020 import java.util.Properties; 021 import org.apache.commons.logging.Log; 022 import org.apache.commons.logging.LogFactory; 023 024 /** 025 * Helper class for accessing System properties 026 * 027 * @version $Revision: 1.1.1.1 $ 028 */ 029 public class SystemProperties { 030 private static final Log log = LogFactory.getLog(SystemProperties.class); 031 private static final boolean canAccessSystemProperties; 032 private static final Properties defaultProperties = new Properties(); 033 034 035 static { 036 SecurityManager securityManager = System.getSecurityManager(); 037 boolean haveAccess = false; 038 if ( securityManager != null ){ 039 try{ 040 securityManager.checkPropertiesAccess(); 041 haveAccess = true; 042 }catch(Throwable e){ 043 haveAccess = false; 044 log.warn("Do not have access to System properties",e); 045 } 046 } 047 canAccessSystemProperties = haveAccess; 048 } 049 050 /** 051 * @return System properties 052 */ 053 public static Properties getSystemProperties(){ 054 return canAccessSystemProperties ? System.getProperties() : defaultProperties; 055 } 056 057 /** 058 * @return the default user name 059 */ 060 public static String getUserName(){ 061 return canAccessSystemProperties ? System.getProperty("user.name") : "defaultUser"; 062 } 063 }