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.

AbstractRepositoryAdmin.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package org.apache.archiva.admin.repository;
  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.archiva.admin.model.AuditInformation;
  21. import org.apache.archiva.admin.model.RepositoryAdminException;
  22. import org.apache.archiva.admin.model.RepositoryCommonValidator;
  23. import org.apache.archiva.configuration.ArchivaConfiguration;
  24. import org.apache.archiva.configuration.Configuration;
  25. import org.apache.archiva.configuration.IndeterminateConfigurationException;
  26. import org.apache.archiva.metadata.model.facets.AuditEvent;
  27. import org.apache.archiva.redback.users.User;
  28. import org.apache.archiva.redback.components.registry.Registry;
  29. import org.apache.archiva.repository.events.AuditListener;
  30. import org.modelmapper.ModelMapper;
  31. import org.modelmapper.convention.MatchingStrategies;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import javax.inject.Inject;
  36. import javax.inject.Named;
  37. import java.util.ArrayList;
  38. import java.util.List;
  39. /**
  40. * @author Olivier Lamy
  41. * @since 1.4-M1
  42. */
  43. public abstract class AbstractRepositoryAdmin
  44. {
  45. protected Logger log = LoggerFactory.getLogger( getClass() );
  46. @Inject
  47. @Autowired(required = false)
  48. private List<AuditListener> auditListeners = new ArrayList<>();
  49. @Inject
  50. private RepositoryCommonValidator repositoryCommonValidator;
  51. @Inject
  52. private ArchivaConfiguration archivaConfiguration;
  53. @Inject
  54. @Named(value = "commons-configuration")
  55. private Registry registry;
  56. protected void triggerAuditEvent( String repositoryId, String resource, String action,
  57. AuditInformation auditInformation )
  58. {
  59. User user = auditInformation == null ? null : auditInformation.getUser();
  60. AuditEvent event = new AuditEvent( repositoryId, user == null ? "null" : user.getUsername(), resource, action );
  61. event.setRemoteIP( auditInformation == null ? "null" : auditInformation.getRemoteAddr() );
  62. for ( AuditListener listener : getAuditListeners() )
  63. {
  64. listener.auditEvent( event );
  65. }
  66. }
  67. protected void saveConfiguration( Configuration config )
  68. throws RepositoryAdminException
  69. {
  70. try
  71. {
  72. getArchivaConfiguration().save( config );
  73. }
  74. catch ( org.apache.archiva.redback.components.registry.RegistryException e )
  75. {
  76. throw new RepositoryAdminException( "Error occurred in the registry: " + e.getLocalizedMessage(), e );
  77. }
  78. catch ( IndeterminateConfigurationException e )
  79. {
  80. throw new RepositoryAdminException(
  81. "Error occurred while saving the configuration: " + e.getLocalizedMessage(), e );
  82. }
  83. }
  84. private static class ModelMapperHolder
  85. {
  86. private static ModelMapper MODEL_MAPPER = new ModelMapper();
  87. static
  88. {
  89. MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT );
  90. }
  91. }
  92. protected ModelMapper getModelMapper()
  93. {
  94. return ModelMapperHolder.MODEL_MAPPER;
  95. }
  96. public List<AuditListener> getAuditListeners()
  97. {
  98. return auditListeners;
  99. }
  100. public void setAuditListeners( List<AuditListener> auditListeners )
  101. {
  102. this.auditListeners = auditListeners;
  103. }
  104. public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
  105. {
  106. this.archivaConfiguration = archivaConfiguration;
  107. }
  108. public ArchivaConfiguration getArchivaConfiguration()
  109. {
  110. return archivaConfiguration;
  111. }
  112. public RepositoryCommonValidator getRepositoryCommonValidator()
  113. {
  114. return repositoryCommonValidator;
  115. }
  116. public void setRepositoryCommonValidator( RepositoryCommonValidator repositoryCommonValidator )
  117. {
  118. this.repositoryCommonValidator = repositoryCommonValidator;
  119. }
  120. public Registry getRegistry()
  121. {
  122. return registry;
  123. }
  124. public void setRegistry( org.apache.archiva.redback.components.registry.Registry registry )
  125. {
  126. this.registry = registry;
  127. }
  128. }