package ora.jwsnut.chapter7.jaxr;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import javax.xml.registry.BulkResponse;
import javax.xml.registry.BusinessQueryManager;
import javax.xml.registry.Connection;
import javax.xml.registry.ConnectionFactory;
import javax.xml.registry.JAXRException;
import javax.xml.registry.LifeCycleManager;
import javax.xml.registry.RegistryService;
import javax.xml.registry.infomodel.AuditableEvent;
import javax.xml.registry.infomodel.Organization;
import ora.jwsnut.chapter7.util.Utils;
public class Audit {
// The BusinessQueryManager
private static BusinessQueryManager bqm;
public static void main(String[] args) {
// Validate arguments
if (args.length != 1) {
usage();
}
// Process the common arguments
String queryURL = args[0];
try {
// Get the ConnectionFactory
Properties props = new Properties();
props.put("javax.xml.registry.queryManagerURL", queryURL);
ConnectionFactory cf = ConnectionFactory.newInstance();
cf.setProperties(props);
// Get and initialize the connection
Connection conn = cf.createConnection();
conn.setSynchronous(true);
// Get the RegistryService and the BusinessQueryManager
RegistryService registry = conn.getRegistryService();
bqm = registry.getBusinessQueryManager();
// Run the example code
getAuditEvents();
// Close connection
conn.close();
} catch (Throwable t) {
System.out.println(t);
t.printStackTrace(System.out);
System.exit(1);
}
System.exit(0);
}
private static void getAuditEvents() throws JAXRException {
// Get the Organization entries
ArrayList namePatterns = new ArrayList();
namePatterns.add("%");
BulkResponse res = bqm.findOrganizations(null, namePatterns, null, null, null, null);
// Process the results
Collection coll = res.getCollection();
if (!coll.isEmpty()) {
Iterator iter = coll.iterator();
while (iter.hasNext()) {
Organization org = (Organization)iter.next();
coll = org.getAuditTrail();
System.out.println("Events for " + org.getName().getValue() + ": " + coll.size());
Iterator aIter = coll.iterator();
while (aIter.hasNext()) {
showAuditableEvent((AuditableEvent)aIter.next());
}
}
}
// Get all the auditable events
res = bqm.getRegistryObjects(LifeCycleManager.AUDITABLE_EVENT);
if (res != null) {
coll = res.getCollection();
System.out.println("Events for current user: " + coll.size());
Iterator aIter = coll.iterator();
while (aIter.hasNext()) {
showAuditableEvent((AuditableEvent)aIter.next());
}
}
}
private static void showAuditableEvent(AuditableEvent evt) throws JAXRException {
String eventType = "??";
switch (evt.getEventType()) {
case AuditableEvent.EVENT_TYPE_CREATED:
eventType = "CREATED";
break;
case AuditableEvent.EVENT_TYPE_DELETED:
eventType = "DELETED";
break;
case AuditableEvent.EVENT_TYPE_DEPRECATED:
eventType = "DEPRECATED";
break;
case AuditableEvent.EVENT_TYPE_UNDEPRECATED:
eventType = "UNDEPRECATED";
break;
case AuditableEvent.EVENT_TYPE_UPDATED:
eventType = "UPDATED";
break;
case AuditableEvent.EVENT_TYPE_VERSIONED:
eventType = "VERSIONED";
break;
}
String name = evt.getUser() == null ? "Unknown" : evt.getUser().getPersonName().getFullName();
System.out.println("\tType: " + eventType + ", date: " + evt.getTimestamp() +
", user: " + name);
}
public static void usage() {
System.out.println("Usage:\tAudit queryURL");
System.exit(1);
}
}
|