001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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    package org.apache.geronimo.system.properties;
018    
019    import java.util.Iterator;
020    import java.util.Properties;
021    import java.util.Map;
022    
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    import org.apache.geronimo.gbean.GBeanInfo;
026    import org.apache.geronimo.gbean.GBeanInfoBuilder;
027    import org.apache.geronimo.system.serverinfo.ServerInfo;
028    
029    /**
030     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
031     */
032    public class SystemProperties {
033    
034        private final Log log = LogFactory.getLog(SystemProperties.class);
035    
036        public SystemProperties(Properties systemProperties, Properties systemPathProperties, ServerInfo serverInfo, Properties sunSystemProperties, Properties ibmSystemProperties, Properties apacheSystemProperties) {
037            if (log.isDebugEnabled()) log.debug("Setting systemProperties");
038            setProperties(systemProperties, null);
039    
040            if (JvmVendor.isIBM()) {
041                if (log.isDebugEnabled()) log.debug("Setting ibmSystemProperties for the IBM JVM");
042                setProperties(ibmSystemProperties, null);
043            } else if (JvmVendor.isApache()) {
044                if (log.isDebugEnabled()) log.debug("Setting apacheSystemProperties for the Apache Harmony JVM");
045                setProperties(apacheSystemProperties, null);
046            } else {
047                if (log.isDebugEnabled()) log.debug("Setting sunSystemProperties for the Sun JVM");
048                setProperties(sunSystemProperties, null);
049            }
050    
051            if (serverInfo != null) {
052                if (log.isDebugEnabled()) log.debug("Setting systemPathProperties");
053                setProperties(systemPathProperties, serverInfo);
054            }
055        }
056    
057        private void setProperties(Properties properties, ServerInfo serverInfo) {
058            if (properties != null) {
059                for (Iterator iterator = properties.entrySet().iterator(); iterator.hasNext();) {
060                    Map.Entry entry = (Map.Entry) iterator.next();
061                    String propertyName = (String) entry.getKey();
062                    String propertyValue = (String) entry.getValue();
063                    if (serverInfo != null) {
064                        propertyValue = serverInfo.resolvePath(propertyValue);
065                    }
066                    String currentPropertyValue = System.getProperty(propertyName);
067                    if (currentPropertyValue == null) {
068                        System.setProperty(propertyName, propertyValue);
069                        log.info("Setting Property=" + propertyName + " to Value=" + propertyValue);
070                    } else {
071                        if (currentPropertyValue.equals(propertyValue)) {
072                            log.warn("Existing Property=" + propertyName + " is already set to Value=" + currentPropertyValue);
073                        } else {
074                            log.error("Not updating existing Property=" + propertyName + " to Value=" + propertyValue + ".  Property is already set to " + currentPropertyValue);
075                        }
076                    }
077                }
078            }
079        }
080    
081        public static final GBeanInfo GBEAN_INFO;
082    
083        static {
084            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(SystemProperties.class, "GBean");
085            infoBuilder.addAttribute("systemProperties", Properties.class, true, true);
086            infoBuilder.addAttribute("systemPathProperties", Properties.class, true, true);
087            infoBuilder.addReference("ServerInfo", ServerInfo.class, "GBean");
088            infoBuilder.addAttribute("sunSystemProperties", Properties.class, true, true);
089            infoBuilder.addAttribute("ibmSystemProperties", Properties.class, true, true);
090            infoBuilder.addAttribute("apacheSystemProperties", Properties.class, true, true);
091            infoBuilder.setConstructor(new String[] { "systemProperties", "systemPathProperties", "ServerInfo", "sunSystemProperties", "ibmSystemProperties", "apacheSystemProperties" });
092    
093            GBEAN_INFO = infoBuilder.getBeanInfo();
094        }
095    
096        public static GBeanInfo getGBeanInfo() {
097            return GBEAN_INFO;
098        }
099    
100    }