001    /**
002     * Copyright (C) 2012 FuseSource, Inc.
003     * http://fusesource.com
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *    http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.fusesource.hawtdispatch.transport;
019    
020    import java.io.IOException;
021    import java.net.URISyntaxException;
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    /**
026     *
027     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
028     */
029    public class PipeTransportRegistry {
030    
031        public static final HashMap<String, PipeTransportServer> servers = new HashMap<String, PipeTransportServer>();
032    
033        synchronized static public TransportServer bind(String location) throws URISyntaxException, IOException {
034            if (servers.containsKey(location)) {
035                throw new IOException("Server already bound: " + location);
036            }
037            PipeTransportServer server = new PipeTransportServer();
038            server.setConnectURI(location);
039            server.setName(location);
040            servers.put(location, server);
041            return server;
042        }
043    
044        synchronized static public Transport connect(String location) throws IOException, URISyntaxException {
045            PipeTransportServer server = lookup(location);
046            if (server == null) {
047                throw new IOException("Server is not bound: " + location);
048            }
049            return server.connect();
050        }
051    
052        synchronized static public PipeTransportServer lookup(String name) {
053            return servers.get(name);
054            }
055    
056        synchronized static public Map<String, PipeTransportServer> getServers() {
057                    return new HashMap<String, PipeTransportServer>(servers);
058        }
059    
060        synchronized static public void unbind(PipeTransportServer server) {
061            servers.remove(server.getName());
062        }
063    }