/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.activemq.util;

import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Helper class for accessing System properties
 *
 * @version $Revision: 1.1.1.1 $
 */
public class SystemProperties {
    private static final Log log = LogFactory.getLog(SystemProperties.class);
    private static final boolean canAccessSystemProperties;
    private static final Properties defaultProperties = new Properties();
    
    
    static {
        SecurityManager securityManager = System.getSecurityManager();
        boolean haveAccess = false;
        if ( securityManager != null ){
            try{
                securityManager.checkPropertiesAccess();
                haveAccess = true;
            }catch(Throwable e){
                haveAccess = false;
                log.warn("Do not have access to System properties",e);
            }
        }
        canAccessSystemProperties = haveAccess;
    }
        
   /**
    * @return System properties
    */
   public static Properties getSystemProperties(){
       return canAccessSystemProperties ? System.getProperties() : defaultProperties;
   }
   
   /**
    * @return the default user name
    */
   public static String getUserName(){
       return canAccessSystemProperties ? System.getProperty("user.name") : "defaultUser";
   }
}
