View Javadoc
1   /*
2    * Copyright 2001-2010 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package org.apache.juddi.example.wsdl2uddi;
18  
19  import java.net.URL;
20  import java.util.Map;
21  
22  import javax.xml.ws.BindingProvider;
23  
24  import org.apache.juddi.samples.HelloWorld;
25  import org.apache.juddi.samples.HelloWorld_Service;
26  import org.apache.juddi.v3.client.config.UDDIClerk;
27  import org.apache.juddi.v3.client.config.UDDIClient;
28  import org.apache.juddi.v3.client.mapping.ServiceLocator;
29  
30  public class Call {
31  	
32  	public void call() {
33  		try {
34  			UDDIClient uddiClient = new UDDIClient("META-INF/wsdl2uddi-uddi.xml");
35  			UDDIClerk clerk = uddiClient.getClerk("joe");
36          	
37          	//find the service in the UDDI registry
38          	System.out.println("The clientside of a runtime lookup usually knows the serviceKey.");
39          	System.out.println("To get updated binding information you should use the ServiceLocator with a live cache.");
40          	String helloWorldServiceKey = "uddi:uddi.joepublisher.com:service_helloworld";
41          	
42          	long startTime = System.currentTimeMillis();
43          	ServiceLocator serviceLocator = new ServiceLocator(clerk); 
44          	System.out.println("Created Cache in " + (System.currentTimeMillis() - startTime) + " [milliseconds]");
45          	System.out.println("Now adding a listener to the cache..."); //expensive
46          	startTime = System.currentTimeMillis();
47          	serviceLocator.withLiveCache(new URL("http://localhost:18079"));
48          	System.out.println("Add Listener to Cache in " + (System.currentTimeMillis() - startTime) + " [milliseconds]");
49          	
50          	//first time the lookup will have to contact UDDI
51          	startTime = System.currentTimeMillis();
52          	String endpoint = serviceLocator.lookupEndpoint(helloWorldServiceKey);
53          	long duration = System.currentTimeMillis() - startTime;
54          	System.out.println("1. UDDI Lookup - Elapsed time: " + duration + "[milliseconds] Endpoint=" + endpoint);
55          	
56          	//second lookup should be lightning fast
57          	long startTime2 = System.currentTimeMillis();;
58          	String endpoint2 = serviceLocator.lookupEndpoint(helloWorldServiceKey);
59          	long duration2 = System.currentTimeMillis() - startTime2;
60          	System.out.println("2. Cache Lookup - Elapsed time: " + duration2 + "[milliseconds] Endpoint=" + endpoint2);
61          	
62          	//Invoke the endpoint using 'endpoint2'
63          	HelloWorld_Service helloWorldService = new HelloWorld_Service();
64          	HelloWorld helloWorld = helloWorldService.getHelloWorldImplPort();
65          	Map<String, Object> requestContext = ((BindingProvider) helloWorld).getRequestContext();
66  			requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpoint2);
67  			String reply = helloWorld.sayHi("Judy");
68          	System.out.println("*************** Service reply: " + reply);
69  			//need to call shutdown to take down the LiveCache Callback Endpoint.
70          	Thread.sleep(10l);
71          	serviceLocator.shutdown();
72          	//TODO JUDDI-610
73  			//FindTModel findBindingTModel = WSDL2UDDI.createFindBindingTModelForPortType(portType, namespace);
74  			
75  		} 
76  		catch (Exception e) {
77  			e.printStackTrace();
78  		}
79  	}		
80  
81  	public static void main (String args[]) {
82  		Call sp = new Call();
83  		sp.call();	
84  	}
85  }