赞
踩
import org.json.simple.JsonObject; //导入方法依赖的package包/类
public DockerContainerDetail getDetail(DockerContainer container) throws DockerException {
JSONObject value = (JSONObject) doGetRequest("/containers/" + container.getId() + "/json",
Collections.singleton(HttpURLConnection.HTTP_OK));
String name = (String) value.get("Name");
DockerContainer.Status status = DockerContainer.Status.STOPPED;
JSONObject state = (JSONObject) value.get("State");
if (state != null) {
boolean paused = (Boolean) getOrDefault(state, "Paused", false);
if (paused) {
status = DockerContainer.Status.PAUSED;
} else {
boolean running = (Boolean) getOrDefault(state, "Running", false);
if (running) {
status = DockerContainer.Status.RUNNING;
}
}
}
boolean tty = false;
boolean stdin = false;
JSONObject config = (JSONObject) value.get("Config");
if (config != null) {
tty = (boolean) getOrDefault(config, "Tty", false);
stdin = (boolean) getOrDefault(config, "OpenStdin", false);
}
JSONObject ports = (JSONObject) ((JSONObject) value.get("NetworkSettings")).get("Ports");
if (ports == null || ports.isEmpty()) {
return new DockerContainerDetail(name, status, stdin, tty);
} else {
List portMapping = new ArrayList<>();
for (String containerPortData : (Set) ports.keySet()) {
JSONArray hostPortsArray = (JSONArray) ports.get(containerPortData);
if (hostPortsArray != null && !hostPortsArray.isEmpty()) {
Matcher m = PORT_PATTERN.matcher(containerPortData);
if (m.matches()) {
int containerPort = Integer.parseInt(m.group(1));
String type = m.group(2).toUpperCase(Locale.ENGLISH);
int hostPort = Integer.parseInt((String) ((JSONObject) hostPortsArray.get(0)).get("HostPort"));
String hostIp = (String) ((JSONObject) hostPortsArray.get(0)).get("HostIp");
portMapping.add(new PortMapping(ExposedPort.Type.valueOf(type), containerPort, hostPort, hostIp));
} else {
LOGGER.log(Level.FINE, "Unparsable port: {0}", containerPortData);
}
}
}
return new DockerContainerDetail(name, status, stdin, tty, portMapping);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。