import javax.xml.registry.*;
import javax.xml.registry.infomodel.*;
import javax.naming.*;
import java.util.*;

public class DoQueries
{
    public static void main(String[] args)
    {
        try
        {
            Context ctx = new InitialContext();

            ConnectionFactory factory =
                (ConnectionFactory) ctx.lookup(
                    "javax.xml.registr.ConnectionFactory");
            
            Properties props = new Properties();

// Specify the implementation for the actual connection factory
            props.put("javax.xml.registry.factoryClass",
                "com.sun.xml.registry.uddi.ConnectionFactoryImpl");

// Specify the location of the inquiry and publish API's
            props.put("javax.xml.registry.queryManagerURL",
                "www-3.ibm.com/services/uddi/testregistry/inquiryapi");
            props.put("javax.xml.registry.lifeCycleManagerURL",
                "www-3.ibm.com/services/uddi/testregistry/publish");

// Store the properties in the factory
            factory.setProperties(props);

// Create the Connection
            Connection conn = factory.createConnection();

// Get the RegistryService
            RegistryService regService = conn.getRegistryService();

// Get the BusinessQueryManager
            BusinessQueryManager query =
                regService.getBusinessQueryManager();

// Look for companies named Microsoft
            ArrayList namePatterns = new ArrayList();
            namePatterns.add("Microsoft");

// Perform the query
            BulkResponse response = query.findOrganizations(
                null, namePatterns, null, null, null, null);

// Get the collection of responses
            Collection respData = response.getCollection();

            Iterator iter = respData.iterator();

            System.out.println("Organizations matching Microsoft:");
// Loop through the responses
            while (iter.hasNext())
            {
// Get the next organization
                Organization org = (Organization) iter.next();

                System.out.println("Organization: "+org.getName());
                System.out.println(org.getDescription());
                System.out.println();

// Get the key for this organization
                Key orgKey = org.getKey();

// Find all services offered by this organization
                BulkResponse servResp =
                    query.findServices(orgKey, null, null, null, null);
                
// Get the list of services
                Collection services = servResp.getCollection();

                Iterator servIter = services.iterator();

                System.out.println("Services:");

// Loop through the services
                while (servIter.hasNext())
                {
                    Service service = (Service) servIter.next();

                    System.out.println("Service: "+service.getName());

                    Collection bindings = service.getServiceBindings();

                    Iterator bindIter = bindings.iterator();

// Loop through the service bindings
                    while (bindIter.hasNext())
                    {
                        ServiceBinding binding =
                            (ServiceBinding) bindIter.next();
                        
                        System.out.println(binding.getAccessURI());
                    }
                    System.out.println();
                }

            }

        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }
}
