diff options
author | Brett Porter <brett@apache.org> | 2010-03-11 11:05:46 +0000 |
---|---|---|
committer | Brett Porter <brett@apache.org> | 2010-03-11 11:05:46 +0000 |
commit | 3387b53fcaebe7228ad212b833e929f1c14eaa51 (patch) | |
tree | 8c3d75ab19b472b927b1aaeceae3893809b02cbd /archiva-modules/plugins/audit/src | |
parent | 51acb7acd7e06a3a38c3c8bcc46a2c0942ae7b0f (diff) | |
download | archiva-3387b53fcaebe7228ad212b833e929f1c14eaa51.tar.gz archiva-3387b53fcaebe7228ad212b833e929f1c14eaa51.zip |
remove dependency on repository-layer from audit module
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@921799 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'archiva-modules/plugins/audit/src')
9 files changed, 484 insertions, 30 deletions
diff --git a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditEvent.java b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditEvent.java new file mode 100644 index 000000000..4712520a9 --- /dev/null +++ b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditEvent.java @@ -0,0 +1,330 @@ +package org.apache.archiva.audit; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.metadata.model.MetadataFacet; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.TimeZone; + +/** + * AuditEvent + * + * @version $Id$ + */ +public class AuditEvent + implements MetadataFacet +{ + public static final String TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS"; + + public static final String CREATE_DIR = "Created Directory"; + + public static final String CREATE_FILE = "Created File"; + + public static final String REMOVE_DIR = "Removed Directory"; + + public static final String REMOVE_FILE = "Removed File"; + + public static final String MODIFY_FILE = "Modified File"; + + public static final String MOVE_FILE = "Moved File"; + + public static final String MOVE_DIRECTORY = "Moved Directory"; + + public static final String COPY_DIRECTORY = "Copied Directory"; + + public static final String COPY_FILE = "Copied File"; + + public static final String UPLOAD_FILE = "Uploaded File"; + + public static final String ADD_LEGACY_PATH = "Added Legacy Artifact Path"; + + public static final String REMOVE_LEGACY_PATH = "Removed Legacy Artifact Path"; + + public static final String PURGE_ARTIFACT = "Purged Artifact"; + + public static final String PURGE_FILE = "Purged Support File"; + + public static final String REMOVE_SCANNED = "Removed in Filesystem"; + + // configuration events + + public static final String ADD_MANAGED_REPO = "Added Managed Repository"; + + public static final String MODIFY_MANAGED_REPO = "Updated Managed Repository"; + + public static final String DELETE_MANAGED_REPO = "Deleted Managed Repository"; + + public static final String ADD_REMOTE_REPO = "Added Remote Repository"; + + public static final String MODIFY_REMOTE_REPO = "Updated Remote Repository"; + + public static final String DELETE_REMOTE_REPO = "Deleted Remote Repository"; + + public static final String ADD_REPO_GROUP = "Added Repository Group"; + + public static final String DELETE_REPO_GROUP = "Deleted Repository Group"; + + public static final String ADD_REPO_TO_GROUP = "Added Repository to Group"; + + public static final String DELETE_REPO_FROM_GROUP = "Deleted Repository from Group"; + + public static final String ENABLE_REPO_CONSUMER = "Enabled Content Consumer"; + + public static final String DISABLE_REPO_CONSUMER = "Disabled Content Consumer"; + + public static final String ADD_PATTERN = "Added File Type Pattern"; + + public static final String REMOVE_PATTERN = "Removed File Type Pattern"; + + private String repositoryId; + + private String userId; + + private String remoteIP; + + // TODO: change to artifact reference? does it ever refer to just a path? + + private String resource; + + private String action; + + private Date timestamp; + + public static final String FACET_ID = "org.apache.archiva.audit"; + + private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" ); + + private static final int TS_LENGTH = TIMESTAMP_FORMAT.length(); + + public AuditEvent() + { + /* do nothing */ + } + + public AuditEvent( String name, String repositoryId ) + { + String ts = name.substring( 0, TS_LENGTH ); + try + { + timestamp = createNameFormat().parse( ts ); + } + catch ( ParseException e ) + { + throw new IllegalArgumentException( "Improperly formatted timestamp for audit log event: " + ts ); + } + + if ( name.length() > TS_LENGTH ) + { + if ( name.charAt( TS_LENGTH ) != '/' ) + { + throw new IllegalArgumentException( + "Improperly formatted name for audit log event, no / separator between timestamp and resource: " + + name ); + } + } + + this.repositoryId = repositoryId; + } + + public AuditEvent( String repoId, String user, String resource, String action ) + { + this.repositoryId = repoId; + this.userId = user; + this.resource = resource; + this.action = action; + this.timestamp = Calendar.getInstance().getTime(); + } + + public String getRepositoryId() + { + return repositoryId; + } + + public void setRepositoryId( String repositoryId ) + { + this.repositoryId = repositoryId; + } + + public String getUserId() + { + return userId; + } + + public void setUserId( String userId ) + { + this.userId = userId; + } + + public String getResource() + { + return resource; + } + + public void setResource( String resource ) + { + this.resource = resource; + } + + public String getAction() + { + return action; + } + + public void setAction( String action ) + { + this.action = action; + } + + public String getRemoteIP() + { + return remoteIP; + } + + public void setRemoteIP( String remoteIP ) + { + this.remoteIP = remoteIP; + } + + public Date getTimestamp() + { + return timestamp; + } + + public void setTimestamp( Date timestamp ) + { + this.timestamp = timestamp; + } + + public String getFacetId() + { + return FACET_ID; + } + + public String getName() + { + // use the hashCode here to make it unique if multiple events occur at a certain timestamp. None of the other + // fields is unique on its own + return createNameFormat().format( timestamp ) + "/" + Integer.toHexString( hashCode() ); + // TODO: a simple incremental counter might be better since it will retain ordering, but then we need to do a + // bit of locking... + } + + private static SimpleDateFormat createNameFormat() + { + SimpleDateFormat fmt = new SimpleDateFormat( TIMESTAMP_FORMAT ); + fmt.setTimeZone( UTC_TIME_ZONE ); + return fmt; + } + + public Map<String, String> toProperties() + { + Map<String, String> properties = new HashMap<String, String>(); + properties.put( "action", this.action ); + if ( this.userId != null ) + { + properties.put( "user", this.userId ); + } + if ( this.remoteIP != null ) + { + properties.put( "remoteIP", this.remoteIP ); + } + if ( this.resource != null ) + { + properties.put( "resource", this.resource ); + } + return properties; + } + + public void fromProperties( Map<String, String> properties ) + { + this.action = properties.get( "action" ); + this.remoteIP = properties.get( "remoteIP" ); + this.userId = properties.get( "user" ); + this.resource = properties.get( "resource" ); + } + + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( o == null || getClass() != o.getClass() ) + { + return false; + } + + AuditEvent that = (AuditEvent) o; + + if ( !action.equals( that.action ) ) + { + return false; + } + if ( remoteIP != null ? !remoteIP.equals( that.remoteIP ) : that.remoteIP != null ) + { + return false; + } + if ( repositoryId != null ? !repositoryId.equals( that.repositoryId ) : that.repositoryId != null ) + { + return false; + } + if ( resource != null ? !resource.equals( that.resource ) : that.resource != null ) + { + return false; + } + if ( !timestamp.equals( that.timestamp ) ) + { + return false; + } + if ( userId != null ? !userId.equals( that.userId ) : that.userId != null ) + { + return false; + } + + return true; + } + + @Override + public int hashCode() + { + int result = repositoryId != null ? repositoryId.hashCode() : 0; + result = 31 * result + ( userId != null ? userId.hashCode() : 0 ); + result = 31 * result + ( remoteIP != null ? remoteIP.hashCode() : 0 ); + result = 31 * result + ( resource != null ? resource.hashCode() : 0 ); + result = 31 * result + action.hashCode(); + result = 31 * result + timestamp.hashCode(); + return result; + } + + @Override + public String toString() + { + return "AuditEvent{" + "repositoryId='" + repositoryId + '\'' + ", userId='" + userId + '\'' + ", remoteIP='" + + remoteIP + '\'' + ", resource='" + resource + '\'' + ", action='" + action + '\'' + ", timestamp=" + + timestamp + '}'; + } +} diff --git a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditEventFactory.java b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditEventFactory.java index 9573d5509..f2572054f 100644 --- a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditEventFactory.java +++ b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditEventFactory.java @@ -21,7 +21,6 @@ package org.apache.archiva.audit; import org.apache.archiva.metadata.model.MetadataFacet; import org.apache.archiva.metadata.model.MetadataFacetFactory; -import org.apache.maven.archiva.repository.audit.AuditEvent; /** * @plexus.component role="org.apache.archiva.metadata.model.MetadataFacetFactory" role-hint="org.apache.archiva.audit" @@ -38,4 +37,4 @@ public class AuditEventFactory { return new AuditEvent( name, repositoryId ); } -}
\ No newline at end of file +} diff --git a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditListener.java b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditListener.java new file mode 100644 index 000000000..204f7ae5c --- /dev/null +++ b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditListener.java @@ -0,0 +1,35 @@ +package org.apache.archiva.audit; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * AuditListener + * + * @version $Id$ + */ +public interface AuditListener +{ + /** + * Notification that an audit event occured. + * + * @param event the event details. + */ + public void auditEvent( AuditEvent event ); +} diff --git a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditLog.java b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditLog.java new file mode 100644 index 000000000..eda1d6cbc --- /dev/null +++ b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditLog.java @@ -0,0 +1,60 @@ +package org.apache.archiva.audit; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * AuditLog - Audit Log. + * + * @version $Id$ + * @plexus.component role="org.apache.maven.archiva.repository.audit.AuditListener" role-hint="logging" + */ +public class AuditLog + implements AuditListener +{ + public static final Logger logger = LoggerFactory.getLogger( "org.apache.archiva.AuditLog" ); + + private static final String NONE = "-"; + + private static final char DELIM = ' '; + + /** + * Creates a log message in the following format ... + * "{repository_id} {user_id} {remote_ip} \"{resource}\" \"{action}\"" + */ + public void auditEvent( AuditEvent event ) + { + StringBuffer msg = new StringBuffer(); + msg.append( checkNull( event.getRepositoryId() ) ).append( DELIM ); + msg.append( event.getUserId() ).append( DELIM ); + msg.append( checkNull( event.getRemoteIP() ) ).append( DELIM ); + msg.append( '\"' ).append( checkNull( event.getResource() ) ).append( '\"' ).append( DELIM ); + msg.append( '\"' ).append( event.getAction() ).append( '\"' ); + + logger.info( msg.toString() ); + } + + private String checkNull( String s ) + { + return s != null ? s : NONE; + } +} diff --git a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java index 3b3448efb..29665e5a6 100644 --- a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java +++ b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java @@ -23,8 +23,6 @@ import java.util.Collection; import java.util.Date; import java.util.List; -import org.apache.maven.archiva.repository.audit.AuditEvent; - public interface AuditManager { List<AuditEvent> getMostRecentAuditEvents( List<String> repositoryIds ); @@ -54,4 +52,4 @@ public interface AuditManager */ List<AuditEvent> getAuditEventsInRange( Collection<String> repositoryIds, String resourcePattern, Date startTime, Date endTime ); -}
\ No newline at end of file +} diff --git a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/Auditable.java b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/Auditable.java new file mode 100644 index 000000000..b2655240c --- /dev/null +++ b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/Auditable.java @@ -0,0 +1,47 @@ +package org.apache.archiva.audit; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Auditable + * + * @version $Id$ + */ +public interface Auditable +{ + /** + * Add an AuditListener. + * + * @param the listener to add. + */ + public void addAuditListener( AuditListener auditListener ); + + /** + * Remove an AuditListener. + * + * @param the listener to remove. + */ + public void removeAuditListener( AuditListener auditListener ); + + /** + * Remove all registered {@link AuditListener} objects. + */ + public void clearAuditListeners(); +} diff --git a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java index 1a5b1ef18..73c9094e4 100644 --- a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java +++ b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java @@ -19,6 +19,10 @@ package org.apache.archiva.audit; * under the License. */ +import org.apache.archiva.metadata.repository.MetadataRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -29,11 +33,6 @@ import java.util.Date; import java.util.List; import java.util.TimeZone; -import org.apache.archiva.metadata.repository.MetadataRepository; -import org.apache.maven.archiva.repository.audit.AuditEvent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * @plexus.component role="org.apache.archiva.audit.AuditManager" */ diff --git a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/MetadataAuditListener.java b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/MetadataAuditListener.java index 612eb51af..080013190 100644 --- a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/MetadataAuditListener.java +++ b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/MetadataAuditListener.java @@ -19,9 +19,6 @@ package org.apache.archiva.audit; * under the License. */ -import org.apache.maven.archiva.repository.audit.AuditEvent; -import org.apache.maven.archiva.repository.audit.AuditListener; - /** * @plexus.component role="org.apache.maven.archiva.repository.audit.AuditListener" role-hint="metadata" */ diff --git a/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java b/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java index 26b16d74d..a4e6012d3 100644 --- a/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java +++ b/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java @@ -19,6 +19,11 @@ package org.apache.archiva.audit; * under the License. */ +import junit.framework.TestCase; +import org.apache.archiva.metadata.repository.MetadataRepository; +import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.easymock.MockControl; + import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -31,15 +36,6 @@ import java.util.List; import java.util.Map; import java.util.TimeZone; -import junit.framework.TestCase; -import org.apache.archiva.metadata.repository.MetadataRepository; -import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; -import org.apache.maven.archiva.repository.RepositoryContentFactory; -import org.apache.maven.archiva.repository.audit.AuditEvent; -import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent; -import org.easymock.MockControl; -import org.easymock.classextension.MockClassControl; - public class AuditManagerTest extends TestCase { @@ -87,13 +83,6 @@ public class AuditManagerTest ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration(); repository.setId( TEST_REPO_ID ); repository.setLocation( "" ); - ManagedDefaultRepositoryContent content = new ManagedDefaultRepositoryContent(); - content.setRepository( repository ); - MockControl control = MockClassControl.createControl( RepositoryContentFactory.class ); - RepositoryContentFactory contentFactory = (RepositoryContentFactory) control.getMock(); - contentFactory.getManagedRepositoryContent( TEST_REPO_ID ); - control.setDefaultReturnValue( content ); - control.replay(); } public void testGetMostRecentEvents() @@ -602,4 +591,4 @@ public class AuditManagerTest } return names; } -}
\ No newline at end of file +} |