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.7KB

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