Author: kstam
Date: Mon May 23 16:58:54 2011
New Revision: 1126581

URL: http://svn.apache.org/viewvc?rev=1126581&view=rev
Log:
JUDDI-479 adding a servicelocator and policies to select an endpoint if 
multiple endpoints are found.

Added:
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/PolicyLocalFirst.java
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/PolicyRoundRobin.java
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/SelectionPolicy.java
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ServiceLocator.java
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/Topology.java
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIClientSubscriptionListenerImpl.java
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIServiceCache.java
    
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/SelectionTest.java
    
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/ServiceLocatorTest.java

Added: 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/PolicyLocalFirst.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/PolicyLocalFirst.java?rev=1126581&view=auto
==============================================================================
--- 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/PolicyLocalFirst.java
 (added)
+++ 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/PolicyLocalFirst.java
 Mon May 23 16:58:54 2011
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2001-2011 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.juddi.v3.client.mapping;
+
+import java.util.Properties;
+
+public class PolicyLocalFirst extends PolicyRoundRobin {
+
+       public final static String JUDDI_CLIENT_LOCAL   = "juddi.client.local";
+       public final static String DEFAULT_CLIENT_LOCAL = "localhost";
+       private static String local;
+       
+       /**
+        * This policy prefers 'local' EPR over remote EPRs. By default 'local' 
means
+        * the EPR contains the String 'localhost'. This setting can be 
overwritten
+        * by setting the 'juddi.client.local' property. An example would be 
'localhost:8080'.
+        * 
+        * @param properties
+        */
+       public PolicyLocalFirst(Properties properties) {
+               super(properties);
+               if (properties!=null) {
+                       local = properties.getProperty(JUDDI_CLIENT_LOCAL, 
DEFAULT_CLIENT_LOCAL);
+               } else {
+                       local = DEFAULT_CLIENT_LOCAL;
+               }
+       }
+       
+       public String select(Topology topology) {
+               
+               if (topology.getEprs().size()==0) return null;
+               
+               if ((topology.getHasLocal()==null)) {
+                       int pointer = 0;
+                       topology.setHasLocal(Boolean.FALSE);
+                       for (String epr : topology.getEprs()) {
+                               if 
(epr.toLowerCase().contains(local.toLowerCase())) {
+                                       topology.setPointer(pointer);
+                                       topology.setHasLocal(Boolean.TRUE);
+                                       break;
+                               }
+                               pointer++;
+                       }
+                       
+               }
+               
+               if (topology.getHasLocal()) {
+                       //return the localEpr
+                       return topology.getEprs().get(topology.getPointer());
+               } else {
+                       //no local EPR, fall back on roundrobin
+                       return super.select(topology);
+               }
+       }
+
+}

Added: 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/PolicyRoundRobin.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/PolicyRoundRobin.java?rev=1126581&view=auto
==============================================================================
--- 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/PolicyRoundRobin.java
 (added)
+++ 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/PolicyRoundRobin.java
 Mon May 23 16:58:54 2011
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2001-2011 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.juddi.v3.client.mapping;
+
+import java.util.Properties;
+
+public class PolicyRoundRobin implements SelectionPolicy {
+
+       public PolicyRoundRobin(Properties properties) {}
+       
+       public String select(Topology topology) {
+               
+               if (topology.getEprs().size()==0) return null;
+               
+               int pointer = topology.getPointer();
+               pointer = (pointer + 1) % topology.getEprs().size();
+               
+               topology.setPointer(pointer);
+               
+               return topology.getEprs().get(pointer);
+       }
+
+}

Added: 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/SelectionPolicy.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/SelectionPolicy.java?rev=1126581&view=auto
==============================================================================
--- 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/SelectionPolicy.java
 (added)
+++ 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/SelectionPolicy.java
 Mon May 23 16:58:54 2011
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2001-2011 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.juddi.v3.client.mapping;
+
+public interface SelectionPolicy {
+       
+       public String select(Topology topology);
+       
+}

Added: 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ServiceLocator.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ServiceLocator.java?rev=1126581&view=auto
==============================================================================
--- 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ServiceLocator.java
 (added)
+++ 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ServiceLocator.java
 Mon May 23 16:58:54 2011
@@ -0,0 +1,147 @@
+/*
+ * Copyright 2001-2011 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.juddi.v3.client.mapping;
+
+import java.lang.reflect.InvocationTargetException;
+import java.net.MalformedURLException;
+import java.rmi.RemoteException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import javax.wsdl.WSDLException;
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.namespace.QName;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.juddi.api_v3.AccessPointType;
+import org.apache.juddi.v3.client.ClassUtil;
+import org.apache.juddi.v3.client.config.Property;
+import org.apache.juddi.v3.client.config.UDDIClerk;
+import org.apache.juddi.v3.client.transport.TransportException;
+import org.uddi.api_v3.AccessPoint;
+import org.uddi.api_v3.BindingTemplate;
+import org.uddi.api_v3.BindingTemplates;
+import org.uddi.api_v3.BusinessService;
+
+/**
+ * @author <a href="mailto:[email protected]";>Kurt T Stam</a>
+ */
+public class ServiceLocator {
+       
+       private Log log = LogFactory.getLog(this.getClass());
+       
+       private String keyDomainURI;
+       private UDDIClerk clerk;
+       //private String lang;
+       //private String businessKey;
+       private Properties properties = new Properties();
+       private UDDIServiceCache serviceCache = null;
+       private SelectionPolicy selectionPolicy = null;
+       
+       public ServiceLocator(UDDIClerk clerk, URLLocalizer urlLocalizer, 
Properties properties) throws ClassNotFoundException, IllegalArgumentException, 
SecurityException, InstantiationException, IllegalAccessException, 
InvocationTargetException, NoSuchMethodException, 
DatatypeConfigurationException, MalformedURLException, RemoteException, 
ConfigurationException, WSDLException, TransportException {
+               super();
+
+               this.clerk = clerk;
+               this.properties = properties;
+               
+               //Obtaining values from the properties
+               this.keyDomainURI = "uddi:" + 
properties.getProperty("keyDomain") + ":";
+               //this.businessKey = Property.getBusinessKey(properties);
+               //this.lang = 
properties.getProperty(Property.LANG,Property.DEFAULT_LANG);
+               
+               serviceCache = new UDDIServiceCache(clerk, urlLocalizer, 
properties);
+               String policy = 
properties.getProperty("juddi.client.selection.policy", 
"org.apache.juddi.v3.client.mapping.PolicyLocalFirst");
+               @SuppressWarnings("unchecked")
+               Class<? extends SelectionPolicy> selectionPolicyClass = 
(Class<? extends SelectionPolicy>)
+                       ClassUtil.forName(policy, this.getClass());
+               selectionPolicy =  
selectionPolicyClass.getConstructor(Properties.class).newInstance(properties);
+       }
+       
+       public void shutdown() throws RemoteException, ConfigurationException, 
TransportException {
+               serviceCache.shutdown();
+       }
+       
+       public void addService(String serviceKey) {
+               Topology topology = lookupEndpointInUDDI(serviceKey);
+               serviceCache.addService(serviceKey, topology);
+       }
+       
+       public void removeService(String serviceKey) {
+               serviceCache.removeService(serviceKey);
+       }
+       
+       
+       public String lookupEndpoint(QName serviceQName, String portName) {
+               String epr =null;
+               String serviceKey = Property.getServiceKey(properties, 
serviceQName);
+               Topology topology = serviceCache.lookupService(serviceKey);
+               if (topology==null) {
+                       topology = lookupEndpointInUDDI(serviceKey);
+               }
+           if (topology.getEprs().size() > 0) {
+               epr = selectionPolicy.select(topology);
+           }
+               return epr;
+       }
+       
+       private Topology lookupEndpointInUDDI(String serviceKey) {
+               Topology topology = null;
+               try {
+                       
+                       BusinessService service = clerk.findService(serviceKey);
+                       if (service==null) {
+                               log.debug("No Service with key " + serviceKey + 
" was found in the registry.");
+                               //TODO find service by tModel
+                       }
+                       if (service!=null && 
service.getBindingTemplates()!=null && 
service.getBindingTemplates().getBindingTemplate() != null) 
+                       {
+                               ArrayList<String> eprs = new 
ArrayList<String>();
+                               BindingTemplates bindingTemplates = 
service.getBindingTemplates();
+                               if (bindingTemplates==null) {
+                                       log.warn("Found service " + 
service.getName().get(0).getValue()
+                                                         + " with serviceKey 
'" + serviceKey + "'" 
+                                                         + " but no EPRs");
+                               } else {
+                                       log.info("Found service " + 
service.getName().get(0).getValue()
+                                                         + " with serviceKey 
'" + serviceKey + "'" 
+                                                         + " and " + 
bindingTemplates.getBindingTemplate().size() + " EPRs");
+                                       //Loop over all bindingTemplates found 
and get the endpoints.
+                                       for (BindingTemplate bindingTemplate : 
bindingTemplates.getBindingTemplate()) {
+                                               AccessPoint accessPoint = 
bindingTemplate.getAccessPoint();
+                                               if 
(AccessPointType.END_POINT.toString().equals(accessPoint.getUseType())) {
+                                                       String url = 
accessPoint.getValue();
+                                                       log.info("epr= " + url);
+                                                       eprs.add(url);
+                                               }
+                                       }
+                                       if (eprs.size()>0) {
+                                               topology = new Topology(eprs);
+                                       }
+                               }
+                       } 
+                   
+               } catch (Exception e) {
+                       log.error(e.getMessage(),e);
+               }
+               
+               return topology;
+       }
+       
+       
+       
+}

Added: 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/Topology.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/Topology.java?rev=1126581&view=auto
==============================================================================
--- 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/Topology.java
 (added)
+++ 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/Topology.java
 Mon May 23 16:58:54 2011
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2001-2011 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.juddi.v3.client.mapping;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:[email protected]";>Kurt T Stam</a>
+ */
+public class Topology implements Serializable{
+       
+       public Topology(List<String> eprs) {
+               super();
+               this.eprs = eprs;
+       }
+       private static final long serialVersionUID = 3884817160534937195L;
+       private List<String> eprs;
+       private int pointer = 0;
+       private Boolean hasLocal = null;
+       
+       
+       public Boolean getHasLocal() {
+               return hasLocal;
+       }
+       public void setHasLocal(Boolean hasLocal) {
+               this.hasLocal = hasLocal;
+       }
+       public List<String> getEprs() {
+               return eprs;
+       }
+       public void setEprs(List<String> eprs) {
+               this.eprs = eprs;
+       }
+       public int getPointer() {
+               return pointer;
+       }
+       public void setPointer(int pointer) {
+               this.pointer = pointer;
+       }
+}

Added: 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIClientSubscriptionListenerImpl.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIClientSubscriptionListenerImpl.java?rev=1126581&view=auto
==============================================================================
--- 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIClientSubscriptionListenerImpl.java
 (added)
+++ 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIClientSubscriptionListenerImpl.java
 Mon May 23 16:58:54 2011
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2001-2008 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.juddi.v3.client.mapping;
+
+import java.io.StringWriter;
+
+import javax.jws.WebService;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.uddi.api_v3.DispositionReport;
+import org.uddi.api_v3.Result;
+import org.uddi.subr_v3.NotifySubscriptionListener;
+import org.uddi.v3_service.DispositionReportFaultMessage;
+import org.uddi.v3_service.UDDISubscriptionListenerPortType;
+
+/**
+ * WebService which implements the UDDI v3 SubscriptionListener API. This 
service will be called by
+ * the UDDI registry when any change to a Service or BindingTemplate
+ * call in to it.
+ * 
+ * @author kstam
+ *
+ */
+@WebService(serviceName="UDDIClientSubscriptionListenerService", 
+                       
endpointInterface="org.uddi.v3_service.UDDISubscriptionListenerPortType",
+                       targetNamespace = "urn:uddi-org:v3_service")
+public class UDDIClientSubscriptionListenerImpl implements 
UDDISubscriptionListenerPortType {
+       
+       private static final long serialVersionUID = 100157393329807903L;
+       private static Log logger = 
LogFactory.getLog(UDDIClientSubscriptionListenerImpl.class);
+       private UDDIServiceCache serviceCache;
+       
+       public UDDIClientSubscriptionListenerImpl(UDDIServiceCache 
serviceCache) {
+               super();
+               this.serviceCache = serviceCache;
+       }
+       
+       public DispositionReport notifySubscriptionListener(
+                       NotifySubscriptionListener body)
+                       throws DispositionReportFaultMessage 
+       {
+               try {
+                       JAXBContext context = 
JAXBContext.newInstance(body.getClass());
+                       Marshaller marshaller = context.createMarshaller();
+                       StringWriter sw = new StringWriter();
+                       marshaller.marshal(body, sw);
+                       logger.info("Notification received by 
UDDISubscriptionListenerService : " + sw.toString());
+                       
+                       //Update the current subscription
+                       serviceCache.registerSubscription();
+                       //reset the cache
+                       serviceCache.removeAll();
+                       
+               } catch (Exception e) {
+                       e.printStackTrace();
+               }
+                       
+               DispositionReport dr = new DispositionReport();
+               Result res = new Result();
+               dr.getResult().add(res);
+               return dr;
+       }
+       
+}

Added: 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIServiceCache.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIServiceCache.java?rev=1126581&view=auto
==============================================================================
--- 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIServiceCache.java
 (added)
+++ 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIServiceCache.java
 Mon May 23 16:58:54 2011
@@ -0,0 +1,157 @@
+/*
+ * Copyright 2001-2011 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.juddi.v3.client.mapping;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.rmi.RemoteException;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.wsdl.Definition;
+import javax.wsdl.WSDLException;
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.datatype.DatatypeFactory;
+import javax.xml.datatype.Duration;
+import javax.xml.namespace.QName;
+import javax.xml.ws.Endpoint;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.juddi.v3.client.config.Property;
+import org.apache.juddi.v3.client.config.UDDIClerk;
+import org.apache.juddi.v3.client.transport.TransportException;
+import org.uddi.api_v3.FindQualifiers;
+import org.uddi.api_v3.FindService;
+import org.uddi.api_v3.Name;
+import org.uddi.sub_v3.Subscription;
+import org.uddi.sub_v3.SubscriptionFilter;
+
+/**
+ * @author <a href="mailto:[email protected]";>Kurt T Stam</a>
+ */
+public class UDDIServiceCache {
+       
+       private Endpoint endpoint = null;
+       private String bindingKey = null;
+       private String subscriptionKey = null;
+       private Log log = LogFactory.getLog(this.getClass());
+       private UDDIClerk clerk = null;
+       private URLLocalizer urlLocalizer = null;
+       private Integer port = null;
+       private String serverName = "localhost";
+       private Properties properties = null;
+       URL serviceUrl = null;
+       private ConcurrentHashMap<String, Topology> serviceLocationMap = new 
ConcurrentHashMap<String, Topology>();
+       
+       public UDDIClerk getClerk() {
+               return clerk;
+       }
+
+       public void setClerk(UDDIClerk clerk) {
+               this.clerk = clerk;
+       }
+
+       public UDDIServiceCache(UDDIClerk clerk, URLLocalizer urlLocalizer, 
Properties properties) throws DatatypeConfigurationException, 
MalformedURLException, RemoteException, ConfigurationException, WSDLException, 
TransportException {
+               super();
+               this.clerk = clerk;
+               this.urlLocalizer = urlLocalizer;
+               this.properties = properties;
+               this.port = 
Integer.valueOf(properties.getProperty("serverPort", "8080")) + 7;
+               this.serverName = 
properties.getProperty("serverName","localhost");
+               this.subscriptionKey = Property.getSubscriptionKey(properties);
+               
+               init();
+       }
+
+       private void init() throws DatatypeConfigurationException, 
MalformedURLException, WSDLException, RemoteException, ConfigurationException, 
TransportException {
+               //TODO make the URL more configurable (https)
+               serviceUrl = new URL("http://localhost:"; + port + 
"/uddiclientsubscriptionlistener");
+               endpoint = Endpoint.publish(serviceUrl.toExternalForm(), new 
UDDIClientSubscriptionListenerImpl(this));
+               
+               QName serviceQName = new QName("urn:uddi-org:v3_service", 
"UDDISubscriptionListenerService");
+               String portName = "UDDISubscriptionListenerImplPort";
+               
+               WSDL2UDDI wsdl2UDDI = new WSDL2UDDI(clerk, urlLocalizer, 
properties);
+               Definition wsdlDefinition = new 
ReadWSDL().readWSDL("uddi_v3_service.wsdl");
+               bindingKey = wsdl2UDDI.register(serviceQName, portName, 
serviceUrl, wsdlDefinition).getBindingKey();
+               
+               registerSubscription();
+       }
+       
+       public void shutdown() throws RemoteException, ConfigurationException, 
TransportException {
+               unRegisterSubscription();
+               QName serviceQName = new QName("urn:uddi-org:v3_service", 
"UDDISubscriptionListenerService");
+               String portName = "UDDISubscriptionListenerImplPort";
+               WSDL2UDDI wsdl2UDDI = new WSDL2UDDI(clerk, urlLocalizer, 
properties);
+               wsdl2UDDI.unRegister(serviceQName, portName, serviceUrl);
+               endpoint.stop();
+       }
+       
+       public void removeAll() {
+               for (String key : serviceLocationMap.keySet()) {
+                       serviceLocationMap.remove(key);
+               }
+       }
+       /**
+        * Adds or updates epr information for the given serviceKey.
+        * @param serviceKey
+        * @param eprs
+        */
+       public void addService(String serviceKey, Topology topology) {
+               serviceLocationMap.put(serviceKey, topology);
+       }
+       
+       public Topology lookupService(String serviceKey) {
+               return serviceLocationMap.get(serviceKey);
+       }
+       
+       public void removeService(String serviceKey) {
+               serviceLocationMap.remove(serviceKey);
+       }
+       
+       public void registerSubscription() throws 
DatatypeConfigurationException {
+               
+               //Create a subscription for changes in any Service in the 
Registry
+               FindQualifiers qualifiers = new FindQualifiers();
+               qualifiers.getFindQualifier().add("approxateMatch");
+               
+               FindService findAllServices = new FindService();
+               Name name = new Name();
+               name.setValue("%");
+               findAllServices.getName().add(name);
+               
+               SubscriptionFilter filter = new SubscriptionFilter();
+               filter.getFindService().setFindQualifiers(qualifiers);
+               filter.getFindService().getName().add(name);
+               
+               Subscription subscription = new Subscription();
+               subscription.setSubscriptionFilter(filter);
+               subscription.setBindingKey(bindingKey);
+               subscription.setBrief(true);
+               Duration oneMinute = 
DatatypeFactory.newInstance().newDuration("PT1M");
+               subscription.setNotificationInterval(oneMinute);
+               subscription.setSubscriptionKey("create the key");
+               clerk.register(subscription);
+       }
+       
+       public void unRegisterSubscription() {
+               clerk.unRegisterSubscription(subscriptionKey);
+       }
+       
+       
+}

Added: 
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/SelectionTest.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/SelectionTest.java?rev=1126581&view=auto
==============================================================================
--- 
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/SelectionTest.java
 (added)
+++ 
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/SelectionTest.java
 Mon May 23 16:58:54 2011
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2001-2009 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.juddi.v3.client.mapping;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:[email protected]";>Kurt T Stam</a>
+ */
+public class SelectionTest {
+
+       
+       @Test
+       public void testRoundRobin() {
+               List<String> eprs = new ArrayList<String>();
+               eprs.add("epr1");
+               eprs.add("epr2");
+               eprs.add("epr3");
+               Topology topology = new Topology(eprs);
+               SelectionPolicy selection = new PolicyRoundRobin(null);
+               
+               Assert.assertEquals("epr2",selection.select(topology));
+               Assert.assertEquals("epr3",selection.select(topology));
+               Assert.assertEquals("epr1",selection.select(topology));
+               Assert.assertEquals("epr2",selection.select(topology));
+               Assert.assertEquals("epr3",selection.select(topology));
+               Assert.assertEquals("epr1",selection.select(topology));
+               Assert.assertEquals("epr2",selection.select(topology));
+               
+       }
+       
+       @Test
+       public void testLocalFirst() {
+               List<String> eprs = new ArrayList<String>();
+               eprs.add("localhost:epr1");
+               eprs.add("remotehost:epr2");
+               eprs.add("remotehost:epr3");
+               Topology topology = new Topology(eprs);
+               
+               SelectionPolicy selection = new PolicyLocalFirst(null);
+               
+               
Assert.assertEquals("localhost:epr1",selection.select(topology));
+               
Assert.assertEquals("localhost:epr1",selection.select(topology));
+               
Assert.assertEquals("localhost:epr1",selection.select(topology));
+               
+               
+       }
+       
+       @Test
+       public void testLocalFirst2() {
+               List<String> eprs = new ArrayList<String>();
+               eprs.add("host1:epr1");
+               eprs.add("host2:epr2");
+               eprs.add("host3:epr3");
+               Topology topology = new Topology(eprs);
+               
+               //If the epr contains the String 'host2:' it should get picked 
+               Properties properties = new Properties();
+               properties.put(PolicyLocalFirst.JUDDI_CLIENT_LOCAL, "host2:");
+               SelectionPolicy selection = new PolicyLocalFirst(properties);
+               
+               Assert.assertEquals("host2:epr2",selection.select(topology));
+               Assert.assertEquals("host2:epr2",selection.select(topology));
+               Assert.assertEquals("host2:epr2",selection.select(topology));
+               
+       }
+       
+}

Added: 
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/ServiceLocatorTest.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/ServiceLocatorTest.java?rev=1126581&view=auto
==============================================================================
--- 
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/ServiceLocatorTest.java
 (added)
+++ 
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/ServiceLocatorTest.java
 Mon May 23 16:58:54 2011
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2001-2009 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.juddi.v3.client.mapping;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:[email protected]";>Kurt T Stam</a>
+ */
+public class ServiceLocatorTest {
+       
+       @Test @Ignore
+       public void testRoundRobin() {
+               try {
+                       List<String> eprs = new ArrayList<String>();
+                       eprs.add("epr1");
+                       eprs.add("epr2");
+                       eprs.add("epr3");
+                       
+                       
+                       Properties properties = new Properties();
+                       ServiceLocator locator = new ServiceLocator(null, new 
URLLocalizerImpl(), properties);
+                       System.out.println(locator);
+                       locator.shutdown();
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       Assert.fail(e.getMessage());
+                       
+               }
+               
+       }
+       
+       
+       
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to