001    package org.apache.archiva.admin.repository;
002    /*
003     * Licensed to the Apache Software Foundation (ASF) under one
004     * or more contributor license agreements.  See the NOTICE file
005     * distributed with this work for additional information
006     * regarding copyright ownership.  The ASF licenses this file
007     * to you under the Apache License, Version 2.0 (the
008     * "License"); you may not use this file except in compliance
009     * with the License.  You may obtain a copy of the License at
010     *
011     *   http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing,
014     * software distributed under the License is distributed on an
015     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016     * KIND, either express or implied.  See the License for the
017     * specific language governing permissions and limitations
018     * under the License.
019     */
020    
021    import org.apache.archiva.admin.model.AuditInformation;
022    import org.apache.archiva.admin.model.RepositoryAdminException;
023    import org.apache.archiva.admin.model.RepositoryCommonValidator;
024    import org.apache.archiva.audit.AuditEvent;
025    import org.apache.archiva.audit.AuditListener;
026    import org.apache.archiva.configuration.ArchivaConfiguration;
027    import org.apache.archiva.configuration.Configuration;
028    import org.apache.archiva.configuration.IndeterminateConfigurationException;
029    import org.apache.archiva.redback.users.User;
030    import org.apache.archiva.redback.components.registry.Registry;
031    import org.slf4j.Logger;
032    import org.slf4j.LoggerFactory;
033    
034    import javax.inject.Inject;
035    import javax.inject.Named;
036    import java.util.ArrayList;
037    import java.util.List;
038    
039    /**
040     * @author Olivier Lamy
041     * @since 1.4-M1
042     */
043    public abstract class AbstractRepositoryAdmin
044    {
045        protected Logger log = LoggerFactory.getLogger( getClass() );
046    
047        @Inject
048        private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
049    
050    
051        @Inject
052        private RepositoryCommonValidator repositoryCommonValidator;
053    
054        @Inject
055        private ArchivaConfiguration archivaConfiguration;
056    
057        @Inject
058        @Named( value = "commons-configuration" )
059        private Registry registry;
060    
061        protected void triggerAuditEvent( String repositoryId, String resource, String action,
062                                          AuditInformation auditInformation )
063        {
064            User user = auditInformation == null ? null : auditInformation.getUser();
065            AuditEvent event =
066                new AuditEvent( repositoryId, user == null ? "null" : user.getUsername(), resource, action );
067            event.setRemoteIP( auditInformation == null ? "null" : auditInformation.getRemoteAddr() );
068    
069            for ( AuditListener listener : getAuditListeners() )
070            {
071                listener.auditEvent( event );
072            }
073    
074        }
075    
076        protected void saveConfiguration( Configuration config )
077            throws RepositoryAdminException
078        {
079            try
080            {
081                getArchivaConfiguration().save( config );
082            }
083            catch ( org.apache.archiva.redback.components.registry.RegistryException e )
084            {
085                throw new RepositoryAdminException( "Error occurred in the registry: " + e.getLocalizedMessage(), e );
086            }
087            catch ( IndeterminateConfigurationException e )
088            {
089                throw new RepositoryAdminException(
090                    "Error occurred while saving the configuration: " + e.getLocalizedMessage(), e );
091            }
092        }
093    
094        public List<AuditListener> getAuditListeners()
095        {
096            return auditListeners;
097        }
098    
099        public void setAuditListeners( List<AuditListener> auditListeners )
100        {
101            this.auditListeners = auditListeners;
102        }
103    
104        public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
105        {
106            this.archivaConfiguration = archivaConfiguration;
107        }
108    
109        public ArchivaConfiguration getArchivaConfiguration()
110        {
111            return archivaConfiguration;
112        }
113    
114        public RepositoryCommonValidator getRepositoryCommonValidator()
115        {
116            return repositoryCommonValidator;
117        }
118    
119        public void setRepositoryCommonValidator( RepositoryCommonValidator repositoryCommonValidator )
120        {
121            this.repositoryCommonValidator = repositoryCommonValidator;
122        }
123    
124        public Registry getRegistry()
125        {
126            return registry;
127        }
128    
129        public void setRegistry( org.apache.archiva.redback.components.registry.Registry registry )
130        {
131            this.registry = registry;
132        }
133    }