public static final int LOCAL_PORT = 10000;
public static final String LOCAL_HOST = "localhost";
public static void main(String[] args) {
Console console = System.console();
//read user name, using java.util.Formatter syntax :
if (vcap_email == null) {
vcap_email = console.readLine("Login E-Mail? ");
}
//read the password, without echoing the output
if (vcap_passwd == null) {
vcap_passwd = new String(console.readPassword("Password? "));
}
CloudFoundryClient client = clientInit();
String version = client.getCloudInfo().getVersion();
if (Float.valueOf(version) >= 2.0) {
//read org, using java.util.Formatter syntax :
if (vcap_org == null) {
vcap_org = console.readLine("Org to use? ");
}
//read space, using java.util.Formatter syntax :
if (vcap_space == null) {
vcap_space = console.readLine("Space to use? ");
}
client = clientOrgSpace(client);
}
CloudApplication serverApp = null;
try {
serverApp = client.getApplication(TunnelHelper.getTunnelAppName());
}
catch (CloudFoundryException e) {}
if (serverApp == null) {
System.out.println("Deploying Caldecott server app");
TunnelHelper.deployTunnelApp(client);
}
try {
serverApp = client.getApplication(TunnelHelper.getTunnelAppName());
}
catch (CloudFoundryException e) {
System.err.println("Unable to deploy Caldecott server app: " + e.getMessage());
throw e;
}
if (!serverApp.getState().equals(CloudApplication.AppState.STARTED)) {
System.out.println("Starting Caldecott server app");
client.startApplication(serverApp.getName());
}
while (vcap_service == null) {
System.out.println("You have the following services defined:");
List<CloudService> services = client.getServices();
int i = 0;
for (CloudService svc : services) {
i++;
System.out.println(i + ": " + svc.getName());
}
if (i ==0) {
System.err.println("It looks like you don't have any services defined. Please create one first!");
System.exit(1);
}
String svc = console.readLine("Which Service to connect to (" + 1 + "-" + i +")? ");
int svc_ix = 0;
try {
svc_ix = Integer.parseInt(svc);
} catch (NumberFormatException e) {
System.err.println(svc + " is not a valid choice!");
continue;
}
if (svc_ix < 1 || svc_ix > i) {
System.err.println(svc + " is not a valid choice!");
continue;
}
vcap_service = services.get(svc_ix - 1).getName();
}
System.out.println("Starting tunnel on " + CC_URL + " to service " + vcap_service + " on behalf of " + vcap_email);
TunnelHelper.bindServiceToTunnelApp(client, vcap_service);
InetSocketAddress local = new InetSocketAddress(LOCAL_HOST, LOCAL_PORT);
String url = TunnelHelper.getTunnelUri(client);
Map<String, String> info = TunnelHelper.getTunnelServiceInfo(client, vcap_service);
String host = info.get("hostname");
int port = Integer.valueOf(info.get("port"));
String auth = TunnelHelper.getTunnelAuth(client);
String svc_username = info.get("username");
String svc_passwd = info.get("password");
String svc_dbname = info.get("db") != null ? info.get("db") : info.get("name");
String txt_dbname = info.get("db") != null ? "db" : "name";
String svc_vhost = info.get("vhost");
TunnelServer server = new TunnelServer(local, new HttpTunnelFactory(url, host, port, auth));
server.start();
System.out.println("Tunnel is running on " + LOCAL_HOST +" port " + LOCAL_PORT + " with auth=" + auth);
if (svc_vhost != null) {
System.out.println("Connect client with username=" + svc_username +" password=" + svc_passwd + " " + "vhost=" + svc_vhost);
}
else {
System.out.println("Connect client with username=" + svc_username +" password=" + svc_passwd + " " + txt_dbname + "=" + svc_dbname);
}
while (true) {
if (console != null) {
String command = console.readLine("Enter exit to stop: ");
if (command.toLowerCase().equals("exit")) {
break;
}
}
try {