You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AddManagedRepositoryAction.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package org.apache.maven.archiva.web.action.admin.repositories;
  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 com.opensymphony.xwork2.Preparable;
  21. import com.opensymphony.xwork2.Validateable;
  22. import org.apache.maven.archiva.configuration.Configuration;
  23. import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
  24. import org.codehaus.plexus.redback.role.RoleManagerException;
  25. import org.codehaus.plexus.scheduler.CronExpressionValidator;
  26. import java.io.File;
  27. import java.io.IOException;
  28. /**
  29. * AddManagedRepositoryAction
  30. *
  31. * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
  32. * @version $Id$
  33. *
  34. * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="addManagedRepositoryAction"
  35. */
  36. public class AddManagedRepositoryAction
  37. extends AbstractManagedRepositoriesAction
  38. implements Preparable, Validateable
  39. {
  40. /**
  41. * The model for this action.
  42. */
  43. private ManagedRepositoryConfiguration repository;
  44. private String action = "addRepository";
  45. public void prepare()
  46. {
  47. this.repository = new ManagedRepositoryConfiguration();
  48. this.repository.setReleases( false );
  49. this.repository.setScanned( false );
  50. }
  51. public String input()
  52. {
  53. this.repository.setReleases( true );
  54. this.repository.setScanned( true );
  55. return INPUT;
  56. }
  57. public String confirmAdd()
  58. {
  59. return save();
  60. }
  61. public String commit()
  62. {
  63. File location = new File( repository.getLocation() );
  64. if( location.exists() )
  65. {
  66. return CONFIRM;
  67. }
  68. return save();
  69. }
  70. private String save()
  71. {
  72. Configuration configuration = archivaConfiguration.getConfiguration();
  73. String result;
  74. try
  75. {
  76. addRepository( repository, configuration );
  77. addRepositoryRoles( repository );
  78. result = saveConfiguration( configuration );
  79. }
  80. catch ( RoleManagerException e )
  81. {
  82. addActionError( "Role Manager Exception: " + e.getMessage() );
  83. result = INPUT;
  84. }
  85. catch ( IOException e )
  86. {
  87. addActionError( "Role Manager Exception: " + e.getMessage() );
  88. result = INPUT;
  89. }
  90. return result;
  91. }
  92. @Override
  93. public void validate()
  94. {
  95. Configuration config = archivaConfiguration.getConfiguration();
  96. CronExpressionValidator validator = new CronExpressionValidator();
  97. String repoId = repository.getId();
  98. if ( config.getManagedRepositoriesAsMap().containsKey( repoId ) )
  99. {
  100. addFieldError( "repository.id", "Unable to add new repository with id [" + repoId
  101. + "], that id already exists as a managed repository." );
  102. }
  103. else if ( config.getRemoteRepositoriesAsMap().containsKey( repoId ) )
  104. {
  105. addFieldError( "repository.id", "Unable to add new repository with id [" + repoId
  106. + "], that id already exists as a remote repository." );
  107. }
  108. else if( config.getRepositoryGroupsAsMap().containsKey( repoId ) )
  109. {
  110. addFieldError( "repository.id", "Unable to add new repository with id [" + repoId
  111. + "], that id already exists as a repository group." );
  112. }
  113. if ( !validator.validate( repository.getRefreshCronExpression() ) )
  114. {
  115. addFieldError( "repository.refreshCronExpression", "Invalid cron expression." );
  116. }
  117. }
  118. public ManagedRepositoryConfiguration getRepository()
  119. {
  120. return repository;
  121. }
  122. public void setRepository( ManagedRepositoryConfiguration repository )
  123. {
  124. this.repository = repository;
  125. }
  126. public String getAction()
  127. {
  128. return action;
  129. }
  130. }