001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2007, 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.dashboard.service;
037:
038: import net.sourceforge.cruisecontrol.CruiseControlException;
039: import net.sourceforge.cruisecontrol.dashboard.Projects;
040: import net.sourceforge.cruisecontrol.dashboard.exception.ConfigurationException;
041: import org.apache.commons.collections.CollectionUtils;
042: import org.springframework.beans.factory.InitializingBean;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.ArrayList;
047: import java.util.Arrays;
048: import java.util.Collection;
049:
050: public class ConfigurationService implements InitializingBean {
051:
052: private DashboardXmlConfigService service;
053:
054: private File configFile;
055:
056: private final BuildLoopQueryService buildLoopQueryService;
057:
058: private EnvironmentService envService;
059:
060: public ConfigurationService(EnvironmentService envService,
061: DashboardXmlConfigService service,
062: BuildLoopQueryService buildLoopQueryService)
063: throws CruiseControlException, ConfigurationException {
064: this .envService = envService;
065: this .service = service;
066: this .buildLoopQueryService = buildLoopQueryService;
067: }
068:
069: public void afterPropertiesSet() throws Exception {
070: configFile = service.getConfigurationFile();
071: }
072:
073: public String getDashboardConfigLocation() {
074: if (configFile == null) {
075: return null;
076: }
077: try {
078: return configFile.getCanonicalPath();
079: } catch (IOException e) {
080: return configFile.getAbsolutePath();
081: }
082: }
083:
084: public void setDashboardConfigLocation(String configLocation)
085: throws CruiseControlException, ConfigurationException {
086: if (service
087: .isDashboardConfigFileValid(new File(configLocation))) {
088: this .configFile = new File(configLocation);
089: }
090: }
091:
092: public boolean isForceBuildEnabled() {
093: return envService.isForceBuildEnabled();
094: }
095:
096: public File getLogsRoot() {
097: return envService.getLogDir();
098: }
099:
100: public File getArtifactsRoot() {
101: return envService.getArtifactsDir();
102: }
103:
104: public File getArtifactRoot(String projectName) {
105: return getProjects() == null ? null : getProjects()
106: .getArtifactRoot(projectName);
107: }
108:
109: public File getLogRoot(String projectName) {
110: return getProjects() == null ? null : getProjects().getLogRoot(
111: projectName);
112: }
113:
114: public File[] getProjectDirectoriesFromBuildloopRepository() {
115: return getProjects().getProjectsRegistedInBuildLoop();
116: }
117:
118: public File[] getProjectDirectoriesFromFileSystem() {
119: return getProjects().getProjectsFromFileSystem();
120: }
121:
122: public Collection getActiveProjects() {
123: File[] projectsInFileSystem = getProjectDirectoriesFromFileSystem();
124: File[] projectsInBuildLoop = getProjectDirectoriesFromBuildloopRepository();
125: return CollectionUtils.intersection(
126: asList(projectsInFileSystem),
127: asList(projectsInBuildLoop));
128: }
129:
130: public Collection getInactiveProjects() {
131: File[] projectsInFileSystem = getProjectDirectoriesFromFileSystem();
132: File[] projectsInBuildLoop = getProjectDirectoriesFromBuildloopRepository();
133: return CollectionUtils.subtract(asList(projectsInBuildLoop),
134: asList(projectsInFileSystem));
135: }
136:
137: public Collection getDiscontinuedProjects() {
138: File[] projectsInBuildLoop = getProjectDirectoriesFromBuildloopRepository();
139: File[] projectsInFileSystem = getProjectDirectoriesFromFileSystem();
140: return CollectionUtils.subtract(asList(projectsInFileSystem),
141: asList(projectsInBuildLoop));
142: }
143:
144: private Collection asList(File[] ary) {
145: if (ary == null) {
146: return new ArrayList();
147: } else {
148: return Arrays.asList(ary);
149: }
150: }
151:
152: private Projects getProjects() {
153: return buildLoopQueryService.getProjects();
154: }
155: }
|