KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > distributed > SearchablePropertyEntries


1 /****************************************************************************
2 * CruiseControl, a Continuous Integration Toolkit
3 * Copyright (c) 2001, ThoughtWorks, Inc.
4 * 651 W Washington Ave. Suite 600
5 * Chicago, IL 60661 USA
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * + Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * + Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 *
20 * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21 * names of its contributors may be used to endorse or promote
22 * products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 ****************************************************************************/

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