package ora.jwsnut.chapter7.jaxr;
import java.net.PasswordAuthentication;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import javax.security.auth.x500.X500PrivateCredential;
import javax.xml.registry.BulkResponse;
import javax.xml.registry.BusinessLifeCycleManager;
import javax.xml.registry.BusinessQueryManager;
import javax.xml.registry.Connection;
import javax.xml.registry.ConnectionFactory;
import javax.xml.registry.JAXRException;
import javax.xml.registry.RegistryService;
import javax.xml.registry.infomodel.Association;
import javax.xml.registry.infomodel.Classification;
import javax.xml.registry.infomodel.ClassificationScheme;
import javax.xml.registry.infomodel.Concept;
import javax.xml.registry.infomodel.Organization;
import ora.jwsnut.chapter7.util.Utils;
public class AssociationExample {
// The BusinessQueryManager
private static BusinessQueryManager bqm;
// The BusinessLifeCycleManager
private static BusinessLifeCycleManager blcm;
public static void main(String[] args) {
boolean install = false;
boolean isUddi = false;
HashSet credentials = new HashSet();
String authMethod = null;
// Validate arguments
if (args.length == 0) {
usage();
}
String registryType = args[0];
int index = -1;
if (registryType.equals("-uddi")) {
if (args.length != 5) {
usage();
}
// UDDI registry uses username/password for authentication
isUddi = true;
index = 3;
authMethod = "UDDI_GET_AUTHTOKEN";
String user = args[1];
String password = args[2];
PasswordAuthentication auth =
new PasswordAuthentication(user, password.toCharArray());
credentials.add(auth);
} else if (registryType.equals("-ebxml")) {
if (args.length != 7) {
usage();
}
// ebXML registry requires a certificate
index = 5;
authMethod = "CLIENT_CERTIFICATE";
String keyStoreName = args[1];
String keyStoreAlias = args[2];
String keyStorePwd = args[3];
String keyPwd = args[4];
try {
X500PrivateCredential cred = Utils.getCredentials(keyStoreName, keyStoreAlias,
keyPwd, keyStorePwd);
credentials.add(cred);
} catch (Throwable t) {
System.out.println("Failed when getting certificate");
t.printStackTrace(System.out);
System.exit(1);
}
} else {
usage();
}
// Process the common arguments
String queryURL = args[index];
String lifecycleURL = args[index + 1];
try {
// Get the ConnectionFactory
Properties props = new Properties();
props.put("javax.xml.registry.queryManagerURL", queryURL);
props.put("javax.xml.registry.lifeCycleManagerURL", lifecycleURL);
props.put("javax.xml.registry.security.authenticationMethod", authMethod);
// Use the JAXR default postal address mapping scheme
if (isUddi) {
props.setProperty("javax.xml.registry.postalAddressScheme",
"PostalAddressAttributes");
props.setProperty("javax.xml.registry.semanticEquivalences",
"urn:uuid:PostalAddressAttributes/StreetNumber," +
"urn:uuid:PostalAddressAttributes/StreetNumber|" +
"urn:uuid:PostalAddressAttributes/Street," +
"urn:uuid:PostalAddressAttributes/Street|" +
"urn:uuid:PostalAddressAttributes/City," +
"urn:uuid:PostalAddressAttributes/City|" +
"urn:uuid:PostalAddressAttributes/State," +
"urn:uuid:PostalAddressAttributes/State|" +
"urn:uuid:PostalAddressAttributes/PostalCode," +
"urn:uuid:PostalAddressAttributes/PostalCode|" +
"urn:uuid:PostalAddressAttributes/Country," +
"urn:uuid:PostalAddressAttributes/Country");
}
ConnectionFactory cf = ConnectionFactory.newInstance();
cf.setProperties(props);
// Get and initialize the connection
Connection conn = cf.createConnection();
conn.setCredentials(credentials);
conn.setSynchronous(true);
// Get the RegistryService and the managers
RegistryService registry = conn.getRegistryService();
bqm = registry.getBusinessQueryManager();
blcm = registry.getBusinessLifeCycleManager();
// Run the example code
doAssociation();
// Close connection
conn.close();
} catch (Throwable t) {
System.out.println(t);
t.printStackTrace(System.out);
System.exit(1);
}
System.exit(0);
}
private static void doAssociation() throws JAXRException {
ArrayList list = new ArrayList();
list.add("%Reilly%");
BulkResponse res = bqm.findOrganizations(null, list, null, null, null, null);
Collection coll = res.getCollection();
Iterator iter = coll.iterator();
if (!iter.hasNext()) {
System.out.println("Please load sample data into the registry");
System.exit(1);
}
Organization ora = (Organization)iter.next();
list.clear();
list.add("%Keyboard%");
res = bqm.findOrganizations(null, list, null, null, null, null);
coll = res.getCollection();
iter = coll.iterator();
if (!iter.hasNext()) {
System.out.println("Please load sample data into the registry");
System.exit(1);
}
Organization kbedge = (Organization)iter.next();
list.clear();
// Get the concept for the association type
ClassificationScheme types = bqm.findClassificationSchemeByName(null, "AssociationType");
if (types == null) {
System.out.println("Cannot find AssociationTypes scheme");
System.exit(1);
}
String path = "/" + types.getKey().getId() + "/Uses";
Concept uses = bqm.findConceptByPath(path);
if (uses == null) {
System.out.println("Cannot find 'Uses' concept");
System.exit(1);
}
// Create the association
Association a = blcm.createAssociation(kbedge, uses);
ora.addAssociation(a);
// Save the association
list.add(a);
res = blcm.saveAssociations(list, false);
if (res.getExceptions() != null) {
System.out.println("Failed to save association");
iter = res.getExceptions().iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
System.exit(1);
}
list.clear();
// Get all associations for the source object
coll = ora.getAssociations();
iter = coll.iterator();
while (iter.hasNext()) {
a = (Association)iter.next();
System.out.println(a.getSourceObject().getName().getValue() + " -> " +
a.getTargetObject().getName().getValue());
System.out.println("\tType = " + a.getAssociationType().getValue() + "; " +
"extramural? " + a.isExtramural() + "; " +
"confirmed? " + a.isConfirmed());
}
}
public static void usage() {
System.out.println("Usage:\tAssociation -uddi username password queryURL updateURL");
System.out.println("Usage:\tAssociation -ebxml keystore alias storepwd keypwd queryURL updateURL");
System.exit(1);
}
}
|