// The application must be started before creating a tunnel
startCaldecottApp(getSubMonitor(worked, progress), client);
CaldecottTunnelDescriptor oldDescriptor = CloudFoundryPlugin.getCaldecottTunnelCache().getDescriptor(
cloudServer, serviceName);
if (oldDescriptor != null) {
try {
progress.setTaskName("Stopping existing tunnel"); //$NON-NLS-1$
stopAndDeleteCaldecottTunnel(serviceName, getSubMonitor(worked, progress));
}
catch (CoreException e) {
CloudFoundryPlugin
.logError("Failed to stop existing tunnel for service " + serviceName + ". Unable to create new tunnel."); //$NON-NLS-1$ //$NON-NLS-2$
return null;
}
}
String url = getTunnelUri(client, getSubMonitor(worked, progress));
Map<String, String> info = getTunnelInfo(client, serviceName, getSubMonitor(worked, progress));
if (info == null) {
CloudFoundryPlugin.logError("Failed to obtain tunnel information for " + serviceName); //$NON-NLS-1$
return null;
}
String host = info.get("hostname"); //$NON-NLS-1$
int port = Integer.valueOf(info.get("port")); //$NON-NLS-1$
String auth = getTunnelAuthorisation(client);
String serviceUserName = info.get("username"); //$NON-NLS-1$
String servicePassword = info.get("password"); //$NON-NLS-1$
String dataBase = getServiceVendor(serviceName, getSubMonitor(worked, progress));
String name = info.get("vhost"); //$NON-NLS-1$
if (name == null) {
name = info.get("db") != null ? info.get("db") : info.get("name"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
// Use proxy settings if they exist
HttpProxyConfiguration proxyConfiguration = null;
try {
URL urlOb = new URL(url);
proxyConfiguration = CloudFoundryClientFactory.getProxy(urlOb);
}
catch (MalformedURLException e) {
// Unable to handle proxy URL. Attempt to connect anyway.
}
TunnelFactory tunnelFactory = new HttpTunnelFactory(url, host, port, auth, proxyConfiguration);
List<TunnelServer> tunnelServers = new ArrayList<TunnelServer>(1);
int localPort = getTunnelServer(tunnelFactory, tunnelServers);
if (tunnelServers.isEmpty() || localPort == -1) {
CloudFoundryPlugin.logError("Tunnel information obtained for " + serviceName + //$NON-NLS-1$
", but failed to create tunnel server for ports between: " + new Integer(BASE_PORT)
+ " and " + new Integer(MAX_PORT)); //$NON-NLS-1$ //$NON-NLS-2$
return null;
}
TunnelServer tunnelServer = tunnelServers.get(0);
progress.setTaskName("Starting tunnel server"); //$NON-NLS-1$
tunnelServer.start();
CaldecottTunnelDescriptor descriptor = new CaldecottTunnelDescriptor(serviceUserName, servicePassword,
name, serviceName, dataBase, tunnelServer, localPort);
CloudFoundryPlugin.getCaldecottTunnelCache().addDescriptor(cloudServer, descriptor);
tunnel.add(descriptor);