package edu.colostate.vchill.connection; import edu.colostate.vchill.ControlMessage; import edu.colostate.vchill.cache.CacheMainLRU; import edu.colostate.vchill.socket.SocketConnection; /** * Factory for creating different Connection objects based on the url * <p/> * the connection represents * * @author Jochen Deyke * @author jpont * @version 2009-04-07 */ public final class ConnectionFactory { /** * Private default constructor prevents instantiation */ private ConnectionFactory() { } /** * Creates different Connection objects based on the url the connection represents * * @param key ControlMessage containing the url to connect to (other fields are ignored) * @param cache cache shared by entire backend * @return the new connection * @throws IllegalArgumentException if the url contained in the key is not valid */ public static Connection createConnection(final ControlMessage key, final CacheMainLRU cache, final boolean promptLogin) { String url = key.getURL(); int splitpoint = url.lastIndexOf(":"); if (splitpoint < 0) throw new IllegalArgumentException("ConnectionFactory: \"" + url + "\" is not a valid URL"); String name = url.substring(0, splitpoint); String portString = url.substring(splitpoint + 1); int port = Integer.parseInt(portString); //if (port < 0) return new FileConnection(name, cache); if (port < 0) return null; else return new SocketConnection(name, port, cache, promptLogin); } }