View Javadoc

1   package org.apache.continuum.buildagent.configuration;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.continuum.buildagent.model.Installation;
23  import org.apache.continuum.buildagent.model.LocalRepository;
24  import org.codehaus.plexus.util.FileUtils;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.List;
31  import javax.annotation.Resource;
32  
33  public class DefaultBuildAgentConfigurationService
34      implements BuildAgentConfigurationService
35  {
36      private static final Logger log = LoggerFactory.getLogger( DefaultBuildAgentConfigurationService.class );
37  
38      @Resource
39      private BuildAgentConfiguration buildAgentConfiguration;
40  
41      private GeneralBuildAgentConfiguration generalBuildAgentConfiguration;
42  
43      public void initialize()
44          throws BuildAgentConfigurationException
45      {
46          loadData();
47      }
48  
49      public BuildAgentConfiguration getBuildAgentConfiguration()
50      {
51          return buildAgentConfiguration;
52      }
53  
54      public void setBuildAgentConfiguration( BuildAgentConfiguration buildAgentConfiguration )
55      {
56          this.buildAgentConfiguration = buildAgentConfiguration;
57      }
58  
59      public File getBuildOutputDirectory()
60      {
61          return generalBuildAgentConfiguration.getBuildOutputDirectory();
62      }
63  
64      public File getBuildOutputDirectory( int projectId )
65      {
66          File dir = new File( getBuildOutputDirectory(), Integer.toString( projectId ) );
67  
68          try
69          {
70              dir = dir.getCanonicalFile();
71          }
72          catch ( IOException e )
73          {
74          }
75  
76          return dir;
77      }
78  
79      public File getWorkingDirectory()
80      {
81          return generalBuildAgentConfiguration.getWorkingDirectory();
82      }
83  
84      public File getWorkingDirectory( int projectId )
85      {
86          File dir = new File( generalBuildAgentConfiguration.getWorkingDirectory(), Integer.toString( projectId ) );
87  
88          try
89          {
90              dir = dir.getCanonicalFile();
91          }
92          catch ( IOException e )
93          {
94          }
95  
96          return dir;
97      }
98  
99      public String getBuildOutput( int projectId )
100         throws BuildAgentConfigurationException
101     {
102         File file = getBuildOutputFile( projectId );
103 
104         try
105         {
106             if ( file.exists() )
107             {
108                 return FileUtils.fileRead( file.getAbsolutePath() );
109             }
110             else
111             {
112                 return "There is no output for this build.";
113             }
114         }
115         catch ( IOException e )
116         {
117             log.warn( "Error reading build output for project '" + projectId + "'.", e );
118 
119             return null;
120         }
121     }
122 
123     public File getBuildOutputFile( int projectId )
124         throws BuildAgentConfigurationException
125     {
126         File dir = getBuildOutputDirectory( projectId );
127 
128         if ( !dir.exists() && !dir.mkdirs() )
129         {
130             throw new BuildAgentConfigurationException(
131                 "Could not make the build output directory: " + "'" + dir.getAbsolutePath() + "'." );
132         }
133 
134         return new File( dir, "build.log.txt" );
135     }
136 
137     public String getContinuumServerUrl()
138     {
139         return generalBuildAgentConfiguration.getContinuumServerUrl();
140     }
141 
142     public List<Installation> getAvailableInstallations()
143     {
144         return generalBuildAgentConfiguration.getInstallations();
145     }
146 
147     public List<LocalRepository> getLocalRepositories()
148     {
149         return generalBuildAgentConfiguration.getLocalRepositories();
150     }
151 
152     public String getSharedSecretPassword()
153     {
154         return generalBuildAgentConfiguration.getSharedSecretPassword();
155     }
156 
157     public void store()
158         throws BuildAgentConfigurationException
159     {
160         buildAgentConfiguration.setContinuumBuildAgentConfiguration( generalBuildAgentConfiguration );
161 
162         buildAgentConfiguration.save();
163     }
164 
165     private void loadData()
166         throws BuildAgentConfigurationException
167     {
168         generalBuildAgentConfiguration = buildAgentConfiguration.getContinuumBuildAgentConfiguration();
169     }
170 }