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.

EditManagedRepositoryAction.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.commons.lang.StringUtils;
  23. import org.apache.maven.archiva.configuration.Configuration;
  24. import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
  25. import org.codehaus.plexus.redback.role.RoleManagerException;
  26. import org.codehaus.plexus.scheduler.CronExpressionValidator;
  27. import java.io.File;
  28. import java.io.IOException;
  29. /**
  30. * AddManagedRepositoryAction
  31. *
  32. * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
  33. * @version $Id$
  34. *
  35. * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="editManagedRepositoryAction"
  36. */
  37. public class EditManagedRepositoryAction
  38. extends AbstractManagedRepositoriesAction
  39. implements Preparable, Validateable
  40. {
  41. /**
  42. * The model for this action.
  43. */
  44. private ManagedRepositoryConfiguration repository;
  45. private String repoid;
  46. private final String action = "editRepository";
  47. public void prepare()
  48. {
  49. if ( StringUtils.isNotBlank( repoid ) )
  50. {
  51. repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoid );
  52. }
  53. else if ( repository != null )
  54. {
  55. repository.setReleases( false );
  56. repository.setScanned( false );
  57. }
  58. }
  59. public String input()
  60. {
  61. if ( repository == null )
  62. {
  63. addActionError( "Edit failure, unable to edit a repository with a blank repository id." );
  64. return ERROR;
  65. }
  66. return INPUT;
  67. }
  68. public String confirmUpdate()
  69. {
  70. return save();
  71. }
  72. public String commit()
  73. {
  74. ManagedRepositoryConfiguration existingConfig =
  75. archivaConfiguration.getConfiguration().findManagedRepositoryById( repository.getId() );
  76. // check if the location was changed
  77. if( !StringUtils.equalsIgnoreCase( existingConfig.getLocation().trim(), repository.getLocation().trim() ) )
  78. {
  79. File dir = new File( repository.getLocation() );
  80. if( dir.exists() )
  81. {
  82. return CONFIRM;
  83. }
  84. }
  85. return save();
  86. }
  87. private String save()
  88. {
  89. // Ensure that the fields are valid.
  90. Configuration configuration = archivaConfiguration.getConfiguration();
  91. // We are in edit mode, remove the old repository configuration.
  92. removeRepository( repository.getId(), configuration );
  93. // Save the repository configuration.
  94. String result;
  95. try
  96. {
  97. addRepository( repository, configuration );
  98. addRepositoryRoles( repository );
  99. result = saveConfiguration( configuration );
  100. }
  101. catch ( IOException e )
  102. {
  103. addActionError( "I/O Exception: " + e.getMessage() );
  104. result = ERROR;
  105. }
  106. catch ( RoleManagerException e )
  107. {
  108. addActionError( "Role Manager Exception: " + e.getMessage() );
  109. result = ERROR;
  110. }
  111. return result;
  112. }
  113. @Override
  114. public void validate()
  115. {
  116. CronExpressionValidator validator = new CronExpressionValidator();
  117. if ( !validator.validate( repository.getRefreshCronExpression() ) )
  118. {
  119. addFieldError( "repository.refreshCronExpression", "Invalid cron expression." );
  120. }
  121. }
  122. public String getRepoid()
  123. {
  124. return repoid;
  125. }
  126. public void setRepoid( String repoid )
  127. {
  128. this.repoid = repoid;
  129. }
  130. public ManagedRepositoryConfiguration getRepository()
  131. {
  132. return repository;
  133. }
  134. public void setRepository( ManagedRepositoryConfiguration repository )
  135. {
  136. this.repository = repository;
  137. }
  138. public String getAction()
  139. {
  140. return action;
  141. }
  142. }