This project has retired. For details please refer to its Attic page.
UDDIKeyConvention xref
View Javadoc
1   /*
2    * Copyright 2001-2009 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.v3.client.config;
18  
19  import java.net.URI;
20  import java.net.URISyntaxException;
21  import java.net.URL;
22  import java.util.Properties;
23  
24  import javax.xml.namespace.QName;
25  
26  public class UDDIKeyConvention 
27  {
28  	
29  	//Default Values
30  	public static final String DEFAULT_BUSINESS_KEY_FORMAT      = "uddi:${keyDomain}:business_${businessName}";
31  	public static final String DEFAULT_SERVICE_KEY_FORMAT       = "uddi:${keyDomain}:service_${serviceName}";
32  	public static final String DEFAULT_SUBSCRIPTION_KEY_FORMAT  = "uddi:${keyDomain}:service_cache_${serverName}";
33  	public static final String DEFAULT_BINDING_KEY_FORMAT       = "uddi:${keyDomain}:binding_${serverName}_${serviceName}_${portName}_${serverPort}";
34  	
35  	/**
36  	 * Constructs the serviceKey based on the bindingKeyFormat specified in the properties. When no
37  	 * businessKeyFormat is specific the default format of uddi:${keyDomain}:${businessName} is used. The businessName
38  	 * property needs to be set properties.
39  	 * 
40  	 * @param properties
41  	 * @return the businessKey
42  	 */
43  	public static String getBusinessKey(Properties properties) {
44  		String businessKey = properties.getProperty(Property.BUSINESS_KEY);
45  		if (businessKey==null) {
46  			String keyFormat = properties.getProperty(Property.BUSINESS_KEY_FORMAT, DEFAULT_BUSINESS_KEY_FORMAT);
47  			businessKey = TokenResolver.replaceTokens(keyFormat, properties).toLowerCase();
48  		}
49  		return businessKey;
50  	}
51  	
52  	public static String getSubscriptionKey(Properties properties) {
53  		String keyFormat = properties.getProperty(Property.SUBSCRIPTION_KEY_FORMAT, DEFAULT_SUBSCRIPTION_KEY_FORMAT);
54  		String subscriptionKey = TokenResolver.replaceTokens(keyFormat, properties).toLowerCase();
55  		return subscriptionKey;
56  	}
57  	/**
58  	 * Constructs the serviceKey based on the serviceKeyFormat specified in the properties. When no
59  	 * serviceKeyFormat is specific the default format of uddi:${keyDomain}:${serviceName} is used.
60  	 * 
61  	 * @param properties
62  	 * @param serviceName
63  	 * @return the serviceKey
64  	 */
65  	public static String getServiceKey(Properties properties, String serviceName) {
66  		Properties tempProperties = new Properties();
67  		if (properties!=null) tempProperties.putAll(properties);
68  		tempProperties.put("serviceName", serviceName);
69  		//Constructing the serviceKey
70  		String keyFormat = tempProperties.getProperty(Property.SERVICE_KEY_FORMAT, DEFAULT_SERVICE_KEY_FORMAT);
71  		String serviceKey = TokenResolver.replaceTokens(keyFormat, tempProperties).toLowerCase();
72  		return serviceKey;
73  	}
74  	
75  	public static String getBindingKey(Properties properties, QName serviceName, String portName, URL bindingUrl) {
76  		
77  		String bindingKey = null;
78  		try {
79  			URI bindingURI = bindingUrl.toURI();
80  			bindingKey =  getBindingKey(properties, serviceName, portName, bindingURI);
81  		} catch (URISyntaxException e) {
82  			
83  		}
84  		return bindingKey;
85  		
86  	}
87  	/**
88  	 * Constructs the bindingKey based on the bindingKeyFormat specified in the properties. When no
89  	 * bindingKeyFormat is specific the default format of uddi:${keyDomain}:${nodeName}-${serviceName}-{portName} is used.
90  	 * 
91  	 * @param properties
92  	 * @param serviceName
93  	 * @param portName
94           * @param bindingUrl
95  	 * @return the bindingKey
96  	 */
97  	public static String getBindingKey(Properties properties, QName serviceName, String portName, URI bindingUrl) {
98  		Properties tempProperties = new Properties();
99  		if (properties!=null) tempProperties.putAll(properties);
100 		tempProperties.put("serviceName", serviceName.getLocalPart());
101 		tempProperties.put("portName", portName);
102 		int port = bindingUrl.getPort();
103 		if (port==-1) {
104 			if ("http".equals(bindingUrl.getScheme())) {
105 				port = 80;
106 			} else if ("https".equals(bindingUrl.getScheme())) {
107 				port = 443;
108 			}
109 		}
110 		tempProperties.put("serverPort", String.valueOf(port));
111 		//Constructing the binding Key
112 		String keyFormat = tempProperties.getProperty(Property.BINDING_KEY_FORMAT, DEFAULT_BINDING_KEY_FORMAT);
113 		String bindingKey = TokenResolver.replaceTokens(keyFormat, tempProperties).toLowerCase();
114 		return bindingKey;
115 	}
116 }