001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. 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 package org.fusesource.hawtdispatch.transport;
018
019 import java.io.IOException;
020 import java.net.URISyntaxException;
021 import java.util.HashMap;
022 import java.util.Map;
023
024 /**
025 *
026 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
027 */
028 public class PipeTransportRegistry {
029
030 public static final HashMap<String, PipeTransportServer> servers = new HashMap<String, PipeTransportServer>();
031
032 synchronized static public TransportServer bind(String location) throws URISyntaxException, IOException {
033 if (servers.containsKey(location)) {
034 throw new IOException("Server already bound: " + location);
035 }
036 PipeTransportServer server = new PipeTransportServer();
037 server.setConnectURI(location);
038 server.setName(location);
039 servers.put(location, server);
040 return server;
041 }
042
043 synchronized static public Transport connect(String location) throws IOException, URISyntaxException {
044 PipeTransportServer server = lookup(location);
045 if (server == null) {
046 throw new IOException("Server is not bound: " + location);
047 }
048 return server.connect();
049 }
050
051 synchronized static public PipeTransportServer lookup(String name) {
052 return servers.get(name);
053 }
054
055 synchronized static public Map<String, PipeTransportServer> getServers() {
056 return new HashMap<String, PipeTransportServer>(servers);
057 }
058
059 synchronized static public void unbind(PipeTransportServer server) {
060 servers.remove(server.getName());
061 }
062 }