Wednesday, November 30, 2011

JAVA client to search and publish documents to Orace Service Registery

JAVA client to search and publish documents to Orace Service Registery

The below JAVA code snippet will help us to perform search and publish operations on OSR.

import javax.xml.registry.*;
import javax.xml.registry.infomodel.*;
import java.net.*;
import java.security.*;
import java.util.*;

public class OSRClient {
Connection connection = null;

public OSRClient() {
}

public static void main(String[] args) {


OSRClient jq = new OSRClient();

jq.makeConnection("http://<<OSR Host>>:<<OSR Port>>/registry/uddi/inquiry", "http://<<OSR Host>>:<<OSR Port>>/registry/uddi/publishing");

jq.executePublish("admin","Password123");

//jq.executeQuery("admin","Password123");
}

/**
* Establishes a connection to a registry.
*
* @param queryUrl the URnL of the query registry
* @param publishUrl the URL of the publish registry
*/
public void makeConnection(
String queryUrl,
String publishUrl) {
/*
* Specify proxy information in case you
* are going beyond your firewall.
*/


/*
* Define connection configuration properties.
* For simple queries, you need the query URL.
*/
Properties props = new Properties();
props.setProperty("javax.xml.registry.queryManagerURL", queryUrl);
props.setProperty("javax.xml.registry.lifeCycleManagerURL", publishUrl);

try {
// Create the connection, passing it the
// configuration properties
ConnectionFactory factory = ConnectionFactory.newInstance();
factory.setProperties(props);
connection = factory.createConnection();
System.out.println("Created connection to registry");
} catch (Exception e) {
e.printStackTrace();

if (connection != null) {
try {
connection.close();
} catch (JAXRException je) {
}
}
}
}

/**
* Searches for objects owned by the user and
* displays data about them.
*
* @param username the username for the registry
* @param password the password for the registry
*/
public void executeQuery(
String username,
String password) {
RegistryService rs = null;
BusinessQueryManager bqm = null;

try {
// Get registry service and query manager
rs = connection.getRegistryService();
bqm = rs.getBusinessQueryManager();
System.out.println("Got registry service and " + "query manager");

// Get authorization from the registry
PasswordAuthentication passwdAuth = new PasswordAuthentication(
username,
password.toCharArray());

HashSet<PasswordAuthentication> creds = new HashSet<PasswordAuthentication>();
creds.add(passwdAuth);
connection.setCredentials(creds);
System.out.println("Established security credentials");

// Get all objects owned by me
BulkResponse response = bqm.getRegistryObjects();

Collection objects = response.getCollection();

// Display information on the objects found
if (objects.isEmpty()) {
System.out.println("No objects found");
} else {
for (Object o : objects) {
RegistryObject obj = (RegistryObject) o;
System.out.println("Object key id: " + getKey(obj));
System.out.println("Object name is: " + getName(obj));
System.out.println(
"Object description is: " + getDescription(obj));

// Print spacer between objects
System.out.println(" --- ");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// At end, close connection to registry
if (connection != null) {
try {
connection.close();
} catch (JAXRException je) {
}
}
}
}

/**
* Returns the name value for a registry object.
*
* @param ro a RegistryObject
* @return the String value
*/
private String getName(RegistryObject ro) throws JAXRException {
try {
return ro.getName()
.getValue();
} catch (NullPointerException npe) {
return "No Name";
}
}

/**
* Returns the description value for a registry object.
*
* @param ro a RegistryObject
* @return the String value
*/
private String getDescription(RegistryObject ro) throws JAXRException {
try {
return ro.getDescription()
.getValue();
} catch (NullPointerException npe) {
return "No Description";
}
}

/**
* Returns the key id value for a registry object.
*
* @param ro a RegistryObject
* @return the String value
*/
private String getKey(RegistryObject ro) throws JAXRException {
try {
return ro.getKey()
.getId();
} catch (NullPointerException npe) {
return "No Key";
}
}

public void executePublish(String username, String password) {
RegistryService rs;
BusinessLifeCycleManager blcm;
BusinessQueryManager bqm;

try {
rs = connection.getRegistryService();
blcm = rs.getBusinessLifeCycleManager();
bqm = rs.getBusinessQueryManager();
System.out.println("Got registry service,query manager, and life cycle manager");

// Get authorization from the registry
PasswordAuthentication passwdAuth = new
PasswordAuthentication(username, password.toCharArray());
Set credits = new HashSet();
credits.add(passwdAuth);
connection.setCredentials(credits);
System.out.println("Established security credentials");

// Create organization name and description
// Replace this information with your organization information
Organization org = blcm.createOrganization("javacourses.com");
InternationalString s =
blcm.createInternationalString("Java training and consulting services");
org.setDescription(s);

// Create primary contact, set name
User primaryContact = blcm.createUser();
PersonName pName = blcm.createPersonName("Qusay H. Mahmoud");
primaryContact.setPersonName(pName);

// Set primary contact phone number
TelephoneNumber phoneNum = blcm.createTelephoneNumber();
phoneNum.setNumber("(604) 285-2000");
Collection phoneNums = new ArrayList();
phoneNums.add(phoneNum);
primaryContact.setTelephoneNumbers(phoneNums);

// Set primary contact email address
EmailAddress emailAddress =
blcm.createEmailAddress("qmahmoud@javacourses.com");
Collection emailAddresses = new ArrayList();
emailAddresses.add(emailAddress);
primaryContact.setEmailAddresses(emailAddresses);

// Set primary contact for organization
org.setPrimaryContact(primaryContact);

// Set classification scheme to NAICS
ClassificationScheme cScheme =
bqm.findClassificationSchemeByName(null,"ntis-gov:naics:1997");

// Create and add classification
Classification classification = (Classification)
blcm.createClassification(cScheme,
"Computer Training", "61142");
Collection classifications = new ArrayList();
classifications.add(classification);
org.addClassifications(classifications);

// Create services and service
Collection services = new ArrayList();
Service service =
blcm.createService("Buy a Java Course");
InternationalString is =
blcm.createInternationalString("This service allows you to register for a Java course and download its manuals.");
service.setDescription(is);

// Create service bindings
Collection serviceBindings = new ArrayList();
ServiceBinding binding = blcm.createServiceBinding();
is = blcm.createInternationalString("Service Binding "
+ "Access this services using the given URL.");
binding.setDescription(is);
binding.setAccessURI("http://javacourses.com/register");
serviceBindings.add(binding);

// Add service bindings to service
service.addServiceBindings(serviceBindings);

// Add service to services, then add services to organization
services.add(service);
org.addServices(services);

// Add organization and submit to registry
// Retrieve key if successful
Collection orgs = new ArrayList();
orgs.add(org);
BulkResponse response = blcm.saveOrganizations(orgs);
Collection exceptions = response.getExceptions();
if (exceptions == null) {
System.out.println("Organization saved");
Collection keys = response.getCollection();
Iterator keyIter = keys.iterator();
if (keyIter.hasNext()) {
javax.xml.registry.infomodel.Key orgKey =
(javax.xml.registry.infomodel.Key) keyIter.next();
String id = orgKey.getId();
System.out.println("Organization key is " + id);
org.setKey(orgKey);
}
} else {
Iterator excIter = exceptions.iterator();
Exception exception = null;
while (excIter.hasNext()) {
exception = (Exception) excIter.next();
System.err.println("Exception on save: " +
exception.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
if (connection != null) {
try {
connection.close();
} catch (JAXRException je) {
System.err.println("Connection close failed");
}
}
}
}

}


No comments:

Post a Comment