Author: ppoddar
Date: Thu Aug 25 01:06:11 2011
New Revision: 1161348
URL: http://svn.apache.org/viewvc?rev=1161348&view=rev
Log:
OPENJPA-2030: First version of Audit facility
Added:
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/AuditLogger.java
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/Auditable.java
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/AuditableOperation.java
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/Auditor.java
Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/AuditLogger.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/AuditLogger.java?rev=1161348&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/AuditLogger.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/AuditLogger.java Thu
Aug 25 01:06:11 2011
@@ -0,0 +1,90 @@
+/*
+ * 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.
+ */
+package org.apache.openjpa.audit;
+
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.PrintStream;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.openjpa.kernel.Audited;
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.lib.conf.Configuration;
+
+/**
+ * A default auditor that simply prints the audited instances.
+ * The output could be directed to a file, defaults to <tt>System.out</tt>.
+ *
+ * @author Pinaki Poddar
+ *
+ */
+public class AuditLogger implements Auditor {
+ private PrintStream _out = System.out;
+ private String _file;
+
+ @Override
+ public void audit(Broker broker, Collection<Audited> newObjects, Collection<Audited>
updates,
+ Collection<Audited> deletes) {
+ for (Audited audited : newObjects) {
+ _out.print(audited.getType() + ": [" + audited.getManagedObject() + "]");
+ _out.println(" Fields:" + Arrays.toString(audited.getUpdatedFields()));
+ }
+ for (Audited audited : updates) {
+ _out.print(audited.getType() + ": [" + audited.getOriginalObject() + "] to [" + audited.getManagedObject()
+ "]");
+ _out.println(" Fields:" + Arrays.toString(audited.getUpdatedFields()));
+ }
+ for (Audited audited : deletes) {
+ _out.print(audited.getType() + ": [" + audited.getOriginalObject() + "]");
+ _out.println(" Fields:" + Arrays.toString(audited.getUpdatedFields()));
+ }
+ }
+
+ public void setFile(String file) throws FileNotFoundException {
+ _out = new PrintStream(new FileOutputStream(_file = file), true);
+ }
+
+ public String getFile() {
+ return _file;
+ }
+
+ public boolean isRollbackOnError() {
+ return false;
+ }
+
+ @Override
+ public void setConfiguration(Configuration conf) {
+ }
+
+ @Override
+ public void startConfiguration() {
+ }
+
+ @Override
+ public void endConfiguration() {
+ }
+
+ @Override
+ public void close() throws Exception {
+ if (_out != System.out) {
+ _out.close();
+ }
+ }
+
+}
Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/Auditable.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/Auditable.java?rev=1161348&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/Auditable.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/Auditable.java Thu
Aug 25 01:06:11 2011
@@ -0,0 +1,39 @@
+/*
+ * 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.
+ */
+package org.apache.openjpa.audit;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * Annotates a persistent type for {@link Auditor audit}.
+ * <br>
+ * A type can be audited for specific {@link AuditableOperation operations}.
+ * By default, this annotation enables all operations.
+ *
+ * @author Pinaki Poddar
+ *
+ */
+@Target({TYPE})
+@Retention(RUNTIME)
+public @interface Auditable {
+ AuditableOperation[] values() default AuditableOperation.ALL;
+}
Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/AuditableOperation.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/AuditableOperation.java?rev=1161348&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/AuditableOperation.java
(added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/AuditableOperation.java
Thu Aug 25 01:06:11 2011
@@ -0,0 +1,32 @@
+/*
+ * 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.
+ */
+package org.apache.openjpa.audit;
+
+/**
+ * Operations of persistent type that can be audited.
+ *
+ * @author Pinaki Poddar
+ *
+ */
+public enum AuditableOperation {
+ CREATE,
+ UPDATE,
+ DELETE,
+ ALL
+}
Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/Auditor.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/Auditor.java?rev=1161348&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/Auditor.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/audit/Auditor.java Thu Aug
25 01:06:11 2011
@@ -0,0 +1,57 @@
+/*
+ * 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.
+ */
+package org.apache.openjpa.audit;
+
+import java.util.Collection;
+
+import org.apache.openjpa.kernel.Audited;
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.lib.conf.Configurable;
+import org.apache.openjpa.lib.util.Closeable;
+
+/**
+ * An auditor is responsible for recoding the audited information.
+ * OpenJPA runtime will track the auditable instances and invoke
+ * implementation of this interface within a transaction.
+ * <br>
+ * The user implementation of this interface is configurable via
+ * standard OpenJPA plug-in configuration framework.
+ *
+ * @author Pinaki Poddar
+ *
+ */
+public interface Auditor extends Configurable, Closeable {
+ /**
+ * OpenJPA runtime will invoke this method with the given parameters
+ * within a transaction.
+ *
+ * @param broker the active persistence context.
+ * @param newObjects the set of auditable objects being created. Can be empty, but never
null.
+ * @param updates the set of auditable objects being updated. Can be empty, but never null.
+ * @param deletes the set of auditable objects being deleted. Can be empty, but never null.
+ */
+ public void audit(Broker broker, Collection<Audited> newObjects, Collection<Audited>
updates,
+ Collection<Audited> deletes);
+
+ /**
+ * Affirm if the transaction be rolled back if {@link #audit(Broker, Collection, Collection,
Collection) audit}
+ * operation fails with an exception.
+ */
+ public boolean isRollbackOnError();
+}
|