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  package org.apache.juddi.v3.client.config;
17  
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  import org.apache.commons.configuration.ConfigurationException;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * Internal jUDDI class to handle multiple clients on the same classloader.
28   * 
29   * @author kstam
30   *
31   */
32  public class UDDIClientContainer {
33  
34  	private static Log log = LogFactory.getLog(UDDIClientContainer.class);
35  	private static Map<String,UDDIClient> clients = new HashMap<String, UDDIClient>();
36  	
37  	public static UDDIClient getUDDIClient(String clientName) 
38  		throws ConfigurationException {
39  
40  		if (clientName!=null) {
41  			if (clients.containsKey(clientName)) {
42  				return (clients.get(clientName));
43  			} else {
44                                  StringBuilder sb = new StringBuilder();
45                                  Iterator<String> iterator = clients.keySet().iterator();
46                                  while (iterator.hasNext()){
47                                          sb.append(iterator.next());
48                                          if (iterator.hasNext())
49                                                  sb.append(",");
50                                  }
51  				throw new ConfigurationException("No client by name " + clientName + " was found. " +
52  						" Please check your client uddi.xml files, and make sure this client was started. Available clients: " + sb.toString());
53  			}
54  		} else throw new IllegalArgumentException("clientName is a required argument");
55  	}
56  	
57          /**
58           * adds the named client, but only if it isn't registered already
59           * @param manager
60           * @return true if successful
61           */
62  	public static boolean addClient(UDDIClient manager) {
63  		if (!clients.containsKey(manager.getClientConfig().getClientName())) {
64  			clients.put(manager.getClientConfig().getClientName(), manager);
65  			return true;
66  		} else {
67  			return false;
68  		}
69  	}
70  	
71  	public static void removeClerkManager(String clientName)
72  		throws ConfigurationException {
73  		if (clients.containsKey(clientName)) {
74  			clients.remove(clientName);
75  		} else if (clients.size()==1 && clientName==null) {
76  			String name = clients.keySet().iterator().next();
77  			log.info("Removing " + name + " from UDDIClient.");
78  			clients.remove(name);
79  		} else {
80  			throw new ConfigurationException("Could not remove UDDIClient for name " + clientName);
81  		}
82  	}
83  
84          /**
85           * return true if the client exists in the current client collection
86           * @param name
87           * @return  true/false
88           */
89      public static boolean contains(String name) {
90          return 	(clients.containsKey(name)) ;
91      }
92  
93          public static void removeAll() {
94                  clients.clear();
95          }
96  	
97  }