001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol;
037:
038: import net.sourceforge.cruisecontrol.util.Util;
039: import org.apache.log4j.Logger;
040: import org.jdom.Attribute;
041: import org.jdom.DataConversionException;
042: import org.jdom.Element;
043:
044: import java.io.File;
045:
046: /**
047: * @author Jared Richardson jared.richardson@sas.com
048: * <p/>
049: * this routine encapsulates the config file for the application.
050: * it fetches a value from the config file and handles errors.
051: */
052:
053: public class ServerXMLHelper {
054: private static final Logger LOG = Logger
055: .getLogger(ServerXMLHelper.class);
056:
057: private int numThreads = 1;
058:
059: public ServerXMLHelper(File configFile) {
060: try {
061: Element configLogElement = Util.loadRootElement(configFile);
062: Element systemElement = configLogElement.getChild("system");
063: if (systemElement == null) {
064: LOG.debug("no system element found in config.xml");
065: return;
066: }
067: Element configurationElement = systemElement
068: .getChild("configuration");
069: if (configurationElement == null) {
070: LOG
071: .debug("no configuraiton element found in config.xml");
072: return;
073: }
074: Element threadElement = configurationElement
075: .getChild("threads");
076: if (threadElement == null) {
077: LOG.debug("no threads element found in config.xml");
078: return;
079: }
080: Attribute threadCount = threadElement.getAttribute("count");
081: LOG.debug("count attribute on threads is "
082: + threadCount.toString());
083: try {
084: numThreads = threadCount.getIntValue();
085: } catch (DataConversionException dce) {
086: LOG
087: .error(
088: "Expected a numeric value for system-configuration-threads-count in config.xml but found "
089: + threadCount.toString(), dce);
090: numThreads = 1;
091: }
092: } catch (Exception e) {
093: LOG
094: .warn(
095: "error parsing thread count from config file; defaulting to 1 thread.",
096: e);
097: numThreads = 1;
098: }
099: }
100:
101: /**
102: * @return int the number of threads that CruiseControl should allow to run at the same time
103: * to process pending build requests
104: */
105: public int getNumThreads() {
106: return numThreads;
107: }
108: }
|