1 37 38 package net.sourceforge.cruisecontrol.distributed; 39 40 import java.net.InetAddress ; 41 import java.net.UnknownHostException ; 42 import java.util.ArrayList ; 43 import java.util.Iterator ; 44 import java.util.List ; 45 import java.util.Map ; 46 import java.util.Properties ; 47 48 import net.jini.core.entry.Entry; 49 import net.sourceforge.cruisecontrol.distributed.util.PropertiesHelper; 50 51 import org.apache.log4j.Logger; 52 53 public class SearchablePropertyEntries { 54 55 private static final Logger LOG = Logger.getLogger(SearchablePropertyEntries.class); 56 57 private static final String OS_NAME = "os.name"; 58 private static final String JAVA_VM_VERSION = "java.vm.version"; 59 private static final String HOSTNAME = "hostname"; 60 61 private Properties entryProperties = new Properties (); 62 63 public Properties getProperties() { 64 return entryProperties; 65 } 66 67 public SearchablePropertyEntries(final String userDefinedPropertiesFilename) { 68 try { 69 String osName = System.getProperty(OS_NAME); 70 entryProperties.put(OS_NAME, osName); 71 LOG.debug("Set search entry " + OS_NAME + " to: " + osName); 72 73 String javaVmVersion = System.getProperty(JAVA_VM_VERSION); 74 entryProperties.put(JAVA_VM_VERSION, javaVmVersion); 75 LOG.debug("Set search entry " + JAVA_VM_VERSION + " to: " + javaVmVersion); 76 77 String hostname = InetAddress.getLocalHost().getHostName(); 78 entryProperties.put(HOSTNAME, hostname); 79 LOG.debug("Set search entry " + HOSTNAME + " to: " + hostname); 80 81 Map tempProperties = PropertiesHelper.loadOptionalProperties(userDefinedPropertiesFilename); 82 for (Iterator iter = tempProperties.keySet().iterator(); iter.hasNext();) { 83 String key = (String ) iter.next(); 84 String value = (String ) tempProperties.get(key); 85 entryProperties.put(key, value); 86 LOG.debug("Set user-defined search entry " + key + " to: " + value); 87 } 88 } catch (UnknownHostException e) { 89 String message = "Failed to set hostname"; 90 LOG.error(message, e); 91 System.err.println(message + " - " + e.getMessage()); 92 throw new RuntimeException (message, e); 93 } 94 } 95 96 public static Entry[] getPropertiesAsEntryArray(Properties properties) { 97 List entries = new ArrayList (); 98 for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) { 99 Map.Entry entry = (Map.Entry ) iter.next(); 100 entries.add(new PropertyEntry((String ) entry.getKey(), (String ) entry.getValue())); 101 } 102 return (Entry[]) entries.toArray(new PropertyEntry[entries.size()]); 103 } 104 105 } 106 | Popular Tags |