View Javadoc

1   package org.apache.continuum.web.action;
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.configuration.BuildAgentConfiguration;
23  import org.apache.continuum.configuration.BuildAgentGroupConfiguration;
24  import org.apache.continuum.release.distributed.DistributedReleaseUtil;
25  import org.apache.maven.continuum.installation.InstallationService;
26  import org.apache.maven.continuum.model.system.Installation;
27  import org.apache.maven.continuum.model.system.Profile;
28  import org.apache.maven.continuum.web.action.ContinuumActionSupport;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  import java.util.Collections;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  public class AbstractReleaseAction
37      extends ContinuumActionSupport
38  {
39      protected Map<String, String> getEnvironments( Profile profile, String defaultBuildagent )
40      {
41          if ( profile == null )
42          {
43              if ( defaultBuildagent != null )
44              {
45                  return Collections.singletonMap( DistributedReleaseUtil.KEY_BUILD_AGENT_URL, defaultBuildagent );
46              }
47              else
48              {
49                  return Collections.emptyMap();
50              }
51          }
52  
53          Map<String, String> envVars = new HashMap<String, String>();
54  
55          if ( defaultBuildagent != null && defaultBuildagent.length() > 0 )
56          {
57              // get buildagent to be used from the buildagent group for distributed builds setup
58              BuildAgentGroupConfiguration group = getContinuum().getConfiguration().getBuildAgentGroup(
59                  profile.getBuildAgentGroup() );
60  
61              if ( group != null )
62              {
63                  List<BuildAgentConfiguration> agents = group.getBuildAgents();
64                  if ( agents != null )
65                  {
66                      if ( isDefaultBuildAgentEnabledInGroup( defaultBuildagent, agents ) )
67                      {
68                          envVars.put( DistributedReleaseUtil.KEY_BUILD_AGENT_URL, defaultBuildagent );
69                      }
70                      else
71                      {
72                          for ( BuildAgentConfiguration agent : agents )
73                          {
74                              if ( agent.isEnabled() == true )
75                              {
76                                  envVars.put( DistributedReleaseUtil.KEY_BUILD_AGENT_URL, agent.getUrl() );
77                                  break;
78                              }
79                          }
80                      }
81                  }
82              }
83          }
84  
85          String javaHome = getJavaHomeValue( profile );
86          if ( !StringUtils.isEmpty( javaHome ) )
87          {
88              envVars.put( getContinuum().getInstallationService().getEnvVar( InstallationService.JDK_TYPE ), javaHome );
89          }
90  
91          Installation builder = profile.getBuilder();
92          if ( builder != null )
93          {
94              envVars.put( getContinuum().getInstallationService().getEnvVar( InstallationService.MAVEN2_TYPE ),
95                           builder.getVarValue() );
96          }
97  
98          List<Installation> installations = profile.getEnvironmentVariables();
99          for ( Installation installation : installations )
100         {
101             envVars.put( installation.getVarName(), installation.getVarValue() );
102         }
103         return envVars;
104     }
105 
106     private boolean isDefaultBuildAgentEnabledInGroup( String defaultBuildagent, List<BuildAgentConfiguration> agents )
107     {
108         boolean isInGroup = false;
109 
110         for ( BuildAgentConfiguration agent : agents )
111         {
112             if ( agent.isEnabled() == true )
113             {
114                 if ( defaultBuildagent.equals( agent.getUrl() ) )
115                 {
116                     isInGroup = true;
117                     break;
118                 }
119             }
120         }
121 
122         return isInGroup;
123     }
124 
125     private String getJavaHomeValue( Profile profile )
126     {
127         Installation jdk = profile.getJdk();
128         if ( jdk == null )
129         {
130             return null;
131         }
132         return jdk.getVarValue();
133     }
134 }