View Javadoc
1   /*
2    * Copyright 2001-2008 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  
18  package org.apache.juddi.keygen;
19  
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.UUID;
23  
24  import org.apache.commons.configuration.ConfigurationException;
25  import org.apache.juddi.config.AppConfig;
26  import org.apache.juddi.config.Property;
27  import org.apache.juddi.model.UddiEntityPublisher;
28  import org.apache.juddi.v3.client.UDDIConstants;
29  import org.apache.juddi.v3.error.ErrorMessage;
30  import org.apache.juddi.v3.error.FatalErrorException;
31  import org.uddi.v3_service.DispositionReportFaultMessage;
32  
33  /**
34   * The default jUDDI key generator.  Generates a key like this:
35   * 
36   * uddiScheme : domain : UUID
37   * 
38   * where domain is the shortest keyGenerator domain. 
39   * 
40   * If no domain is set for this publihser, or the publisher is 'uddi' then
41   * it defaults to the RootDomain:
42   * 
43   * uddiScheme : RootDomain : UUID
44   * 
45   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
46   */
47  public class DefaultKeyGenerator implements KeyGenerator {
48  
49  	public String generate(UddiEntityPublisher publisher) throws DispositionReportFaultMessage {
50  		String domain = getDomain(publisher);
51  		if (domain==null) {
52  			String rootPartition = "";
53  			try 
54  			{ rootPartition = AppConfig.getConfiguration().getString(Property.JUDDI_ROOT_PARTITION); }
55  			catch(ConfigurationException ce) 
56  			{ throw new FatalErrorException(new ErrorMessage("errors.configuration.Retrieval", Property.JUDDI_ROOT_PARTITION));}
57  			domain = rootPartition;
58  		}
59  		return domain + PARTITION_SEPARATOR + UUID.randomUUID();
60  	}
61  	
62  	public static String getDomain(UddiEntityPublisher publisher) {
63  		String domain = null;
64  		if (publisher==null || "uddi".equalsIgnoreCase(publisher.getAuthorizedName())) return domain; //default to the rootPartition
65  		List<String> domains = publisher.getKeyGeneratorKeys();
66  		if (domains!=null && !domains.isEmpty()) {
67  			Iterator<String> iter = domains.iterator();
68  			int partsMax = 1000;
69  			//pick the KeyGenerator with the fewest amount of parts, ignoring the subdomain keys. 
70  			while (iter.hasNext()) {
71  				String thisDomain = iter.next();
72  				String[] parts = thisDomain.split(":");
73  				if ((domain == null || (2 <= parts.length && parts.length < partsMax)) && thisDomain.length() + 37 < 255) {
74  					partsMax = parts.length;
75  					domain = thisDomain;
76  				}
77  			}
78  			domain = domain.substring(0, domain.lastIndexOf(":"));
79  		}
80  		return domain;
81  	}
82  
83  }