PageRenderTime 95ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/continuum-core/src/main/java/org/apache/maven/continuum/core/action/CreateProjectsFromMetadataAction.java

https://github.com/apache/continuum
Java | 359 lines | 267 code | 60 blank | 32 comment | 25 complexity | 14535d2e23caba1b4995539c6be55794 MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.apache.maven.continuum.core.action;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.maven.continuum.ContinuumException;
  21. import org.apache.maven.continuum.execution.SettingsConfigurationException;
  22. import org.apache.maven.continuum.model.project.BuildDefinitionTemplate;
  23. import org.apache.maven.continuum.model.project.Project;
  24. import org.apache.maven.continuum.project.builder.ContinuumProjectBuilder;
  25. import org.apache.maven.continuum.project.builder.ContinuumProjectBuilderException;
  26. import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult;
  27. import org.apache.maven.continuum.project.builder.manager.ContinuumProjectBuilderManager;
  28. import org.apache.maven.continuum.project.builder.manager.ContinuumProjectBuilderManagerException;
  29. import org.apache.maven.continuum.utils.ContinuumUrlValidator;
  30. import org.apache.maven.continuum.utils.URLUserInfo;
  31. import org.apache.maven.settings.MavenSettingsBuilder;
  32. import org.apache.maven.settings.Server;
  33. import org.apache.maven.settings.Settings;
  34. import org.codehaus.plexus.component.annotations.Component;
  35. import org.codehaus.plexus.component.annotations.Requirement;
  36. import org.codehaus.plexus.util.StringUtils;
  37. import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
  38. import java.io.IOException;
  39. import java.net.MalformedURLException;
  40. import java.net.URISyntaxException;
  41. import java.net.URL;
  42. import java.util.List;
  43. import java.util.Map;
  44. /**
  45. * Resolve the project url being passed in and gather authentication information
  46. * if the url is so configured, then create the projects
  47. * Supports:
  48. * - standard maven-scm url
  49. * - MungedUrl https://username:password@host
  50. * - maven settings based, server = host and scm info set to username and password
  51. *
  52. * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
  53. */
  54. @Component( role = org.codehaus.plexus.action.Action.class, hint = "create-projects-from-metadata" )
  55. public class CreateProjectsFromMetadataAction
  56. extends AbstractContinuumAction
  57. {
  58. /**
  59. * Metadata url for adding projects.
  60. */
  61. private static final String KEY_URL = "url";
  62. private static final String KEY_PROJECT_BUILDER_ID = "builderId";
  63. private static final String KEY_PROJECT_BUILDING_RESULT = "projectBuildingResult";
  64. private static final String KEY_LOAD_RECURSIVE_PROJECTS = "loadRecursiveProjects";
  65. public static final String KEY_CHECKOUT_PROJECTS_IN_SINGLE_DIRECTORY = "checkoutProjectsInSingleDirectory";
  66. @Requirement
  67. private ContinuumProjectBuilderManager projectBuilderManager;
  68. @Requirement
  69. private MavenSettingsBuilder mavenSettingsBuilder;
  70. @Requirement( hint = "continuumUrl" )
  71. private ContinuumUrlValidator urlValidator;
  72. public void execute( Map context )
  73. throws ContinuumException, ContinuumProjectBuilderManagerException, ContinuumProjectBuilderException
  74. {
  75. String projectBuilderId = getProjectBuilderId( context );
  76. boolean loadRecursiveProjects = isLoadRecursiveProject( context );
  77. boolean checkoutProjectsInSingleDirectory = getBoolean( context, KEY_CHECKOUT_PROJECTS_IN_SINGLE_DIRECTORY );
  78. int projectGroupId = getProjectGroupId( context );
  79. String curl = getUrl( context );
  80. URL url;
  81. ContinuumProjectBuilder projectBuilder = projectBuilderManager.getProjectBuilder( projectBuilderId );
  82. ContinuumProjectBuildingResult result;
  83. try
  84. {
  85. BuildDefinitionTemplate buildDefinitionTemplate = getBuildDefinitionTemplate( context );
  86. if ( buildDefinitionTemplate == null )
  87. {
  88. buildDefinitionTemplate = projectBuilder.getDefaultBuildDefinitionTemplate();
  89. }
  90. if ( !curl.startsWith( "http" ) )
  91. {
  92. url = new URL( curl );
  93. result = projectBuilder.buildProjectsFromMetadata( url, null, null, loadRecursiveProjects,
  94. buildDefinitionTemplate,
  95. checkoutProjectsInSingleDirectory, projectGroupId );
  96. }
  97. else
  98. {
  99. url = new URL( curl );
  100. String host = url.getHost();
  101. String username = null;
  102. String password = null;
  103. try
  104. {
  105. getLogger().info( "consulting settings for credentials to " + host );
  106. Settings settings = getSettings();
  107. Server server = settings.getServer( url.getHost() );
  108. if ( server != null )
  109. {
  110. username = server.getUsername();
  111. password = server.getPassword();
  112. getLogger().info( "credentials found in settings, will fetch metadata as " + username );
  113. }
  114. else
  115. {
  116. getLogger().info( "credentials not found for server " + host );
  117. }
  118. }
  119. catch ( SettingsConfigurationException se )
  120. {
  121. getLogger().warn( "problem with settings file, disabling scm resolution of username and password" );
  122. }
  123. if ( username == null )
  124. {
  125. URLUserInfo urlUserInfo = urlValidator.extractURLUserInfo( curl );
  126. username = urlUserInfo.getUsername();
  127. password = urlUserInfo.getPassword();
  128. }
  129. if ( urlValidator.isValid( curl ) )
  130. {
  131. result = projectBuilder.buildProjectsFromMetadata( url, username, password, loadRecursiveProjects,
  132. buildDefinitionTemplate,
  133. checkoutProjectsInSingleDirectory,
  134. projectGroupId );
  135. }
  136. else
  137. {
  138. result = new ContinuumProjectBuildingResult();
  139. getLogger().info( "Malformed URL (MungedHttpsURL is not valid): " + hidePasswordInUrl( curl ) );
  140. result.addError( ContinuumProjectBuildingResult.ERROR_MALFORMED_URL );
  141. }
  142. }
  143. if ( result.getProjects() != null )
  144. {
  145. String scmRootUrl = getScmRootUrl( result.getProjects() );
  146. if ( scmRootUrl == null || scmRootUrl.equals( "" ) )
  147. {
  148. if ( curl.indexOf( "pom.xml" ) > 0 )
  149. {
  150. scmRootUrl = curl.substring( 0, curl.indexOf( "pom.xml" ) - 1 );
  151. }
  152. else
  153. {
  154. scmRootUrl = curl;
  155. }
  156. }
  157. //setUrl( context, scmRootUrl );
  158. setProjectScmRootUrl( context, scmRootUrl );
  159. }
  160. }
  161. catch ( MalformedURLException e )
  162. {
  163. getLogger().info( "Malformed URL: " + hidePasswordInUrl( curl ), e );
  164. result = new ContinuumProjectBuildingResult();
  165. result.addError( ContinuumProjectBuildingResult.ERROR_MALFORMED_URL );
  166. }
  167. catch ( URISyntaxException e )
  168. {
  169. getLogger().info( "Malformed URL: " + hidePasswordInUrl( curl ), e );
  170. result = new ContinuumProjectBuildingResult();
  171. result.addError( ContinuumProjectBuildingResult.ERROR_MALFORMED_URL );
  172. }
  173. setProjectBuildingResult( context, result );
  174. }
  175. private String hidePasswordInUrl( String url )
  176. {
  177. int indexAt = url.indexOf( "@" );
  178. if ( indexAt < 0 )
  179. {
  180. return url;
  181. }
  182. String s = url.substring( 0, indexAt );
  183. int pos = s.lastIndexOf( ":" );
  184. return s.substring( 0, pos + 1 ) + "*****" + url.substring( indexAt );
  185. }
  186. private Settings getSettings()
  187. throws SettingsConfigurationException
  188. {
  189. try
  190. {
  191. return mavenSettingsBuilder.buildSettings();
  192. }
  193. catch ( IOException e )
  194. {
  195. throw new SettingsConfigurationException( "Error reading settings file", e );
  196. }
  197. catch ( XmlPullParserException e )
  198. {
  199. throw new SettingsConfigurationException( e.getMessage(), e.getDetail(), e.getLineNumber(),
  200. e.getColumnNumber() );
  201. }
  202. }
  203. private String getScmRootUrl( List<Project> projects )
  204. {
  205. String scmRootUrl = "";
  206. for ( Project project : projects )
  207. {
  208. String scmUrl = project.getScmUrl();
  209. scmRootUrl = getCommonPath( scmUrl, scmRootUrl );
  210. }
  211. return scmRootUrl;
  212. }
  213. private String getCommonPath( String path1, String path2 )
  214. {
  215. if ( path2 == null || path2.equals( "" ) )
  216. {
  217. return path1;
  218. }
  219. else
  220. {
  221. int indexDiff = StringUtils.differenceAt( path1, path2 );
  222. String commonPath = path1.substring( 0, indexDiff );
  223. if ( commonPath.lastIndexOf( '/' ) != commonPath.length() - 1 && !( path1.contains( new String(
  224. commonPath + "/" ) ) || path2.contains( new String( commonPath + "/" ) ) ) )
  225. {
  226. while ( commonPath.lastIndexOf( '/' ) != commonPath.length() - 1 )
  227. {
  228. commonPath = commonPath.substring( 0, commonPath.length() - 1 );
  229. }
  230. }
  231. return commonPath;
  232. }
  233. }
  234. public ContinuumProjectBuilderManager getProjectBuilderManager()
  235. {
  236. return projectBuilderManager;
  237. }
  238. public void setProjectBuilderManager( ContinuumProjectBuilderManager projectBuilderManager )
  239. {
  240. this.projectBuilderManager = projectBuilderManager;
  241. }
  242. public MavenSettingsBuilder getMavenSettingsBuilder()
  243. {
  244. return mavenSettingsBuilder;
  245. }
  246. public void setMavenSettingsBuilder( MavenSettingsBuilder mavenSettingsBuilder )
  247. {
  248. this.mavenSettingsBuilder = mavenSettingsBuilder;
  249. }
  250. public ContinuumUrlValidator getUrlValidator()
  251. {
  252. return urlValidator;
  253. }
  254. public void setUrlValidator( ContinuumUrlValidator urlValidator )
  255. {
  256. this.urlValidator = urlValidator;
  257. }
  258. public static String getUrl( Map<String, Object> context )
  259. {
  260. return getString( context, KEY_URL );
  261. }
  262. public static void setUrl( Map<String, Object> context, String url )
  263. {
  264. context.put( KEY_URL, url );
  265. }
  266. public static String getProjectBuilderId( Map<String, Object> context )
  267. {
  268. return getString( context, KEY_PROJECT_BUILDER_ID );
  269. }
  270. public static void setProjectBuilderId( Map<String, Object> context, String projectBuilderId )
  271. {
  272. context.put( KEY_PROJECT_BUILDER_ID, projectBuilderId );
  273. }
  274. public static ContinuumProjectBuildingResult getProjectBuildingResult( Map<String, Object> context )
  275. {
  276. return (ContinuumProjectBuildingResult) getObject( context, KEY_PROJECT_BUILDING_RESULT );
  277. }
  278. private static void setProjectBuildingResult( Map<String, Object> context, ContinuumProjectBuildingResult result )
  279. {
  280. context.put( KEY_PROJECT_BUILDING_RESULT, result );
  281. }
  282. public static boolean isLoadRecursiveProject( Map<String, Object> context )
  283. {
  284. return getBoolean( context, KEY_LOAD_RECURSIVE_PROJECTS );
  285. }
  286. public static void setLoadRecursiveProject( Map<String, Object> context, boolean loadRecursiveProject )
  287. {
  288. context.put( KEY_LOAD_RECURSIVE_PROJECTS, loadRecursiveProject );
  289. }
  290. public static boolean isCheckoutProjectsInSingleDirectory( Map<String, Object> context )
  291. {
  292. return getBoolean( context, KEY_CHECKOUT_PROJECTS_IN_SINGLE_DIRECTORY );
  293. }
  294. public static void setCheckoutProjectsInSingleDirectory( Map<String, Object> context,
  295. boolean checkoutProjectsInSingleDirectory )
  296. {
  297. context.put( KEY_CHECKOUT_PROJECTS_IN_SINGLE_DIRECTORY, checkoutProjectsInSingleDirectory );
  298. }
  299. }