Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 48f2397b395e0b7abd8f9ca1da98c98b47ac8ca9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*******************************************************************************
 * Copyright (c) 2014 Red Hat.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat - Initial Contribution
 *******************************************************************************/
package org.eclipse.linuxtools.internal.docker.core;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.linuxtools.docker.core.IDockerConnection;
import org.eclipse.linuxtools.docker.core.IDockerContainer;
import org.eclipse.linuxtools.docker.core.IDockerContainerInfo;
import org.eclipse.linuxtools.docker.core.IDockerPortMapping;

import com.spotify.docker.client.messages.Container;

public class DockerContainer implements IDockerContainer {

	private IDockerConnection parent;
	private String id;
	private List<String> names;
	private String image;
	private String command;
	private Long created;
	private String status;
	private List<IDockerPortMapping> ports;
	private Long sizeRw;
	private Long sizeRootFs;
	private IDockerContainerInfo containerInfo;

	public DockerContainer(final IDockerConnection connection,
			Container container) {
		this.parent = connection;
		this.id = container.id();
		this.image = container.image();
		this.command = container.command();
		this.created = container.created();
		this.status = container.status();
		this.names = new ArrayList<>();
		for (String name : container.names()) {
			if (name.startsWith("/")) {
				this.names.add(name.substring(1));
			} else {
				this.names.add(name);
			}
		}
		this.sizeRw = container.sizeRw();
		this.sizeRootFs = container.sizeRootFs();
		this.ports = new ArrayList<>();
		for (Container.PortMapping port : container.ports()) {
			final DockerPortMapping portMapping = new DockerPortMapping(this,
					port.getPrivatePort(), port.getPublicPort(), port.getType(),
					port.getIp());
			ports.add(portMapping);
		}
		// TODO: include volumes
	}

	@Override
	public IDockerConnection getConnection() {
		return parent;
	}

	@Override
	public String id() {
		return id;
	}

	@Override
	public String image() {
		return image;
	}

	@Override
	public String command() {
		return command;
	}

	@Override
	public Long created() {
		return created;
	}

	@Override
	public String status() {
		return status;
	}

	@Override
	public Long sizeRw() {
		return sizeRw;
	}

	@Override
	public Long sizeRootFs() {
		return sizeRootFs;
	}

	@Override
	public List<IDockerPortMapping> ports() {
		return ports;
	}

	@Override
	public String name() {
		return names.get(0);
	}

	@Override
	public List<String> names() {
		return names;
	}

	
	@Override
	public IDockerContainerInfo info() {
		return info(false);
	}

	/**
	 * @param force
	 *            <code>true</code> to force refresh, <code>false</code> to use
	 *            existing {@link IDockerContainerInfo} if it was loaded before.
	 * @return the {@link IDockerContainerInfo} by calling the Docker daemon
	 *         using the {@link IDockerConnection} associated with this
	 *         {@link IDockerContainer}.
	 */
	// TODO: add this method in the public interface
	public IDockerContainerInfo info(final boolean force) {
		if (force || isInfoLoaded()) {
			this.containerInfo = this.parent.getContainerInfo(id);
		}
		return this.containerInfo;
	}

	/**
	 * @return <code>true</code> if the {@link IDockerContainerInfo} has been
	 *         loaded, <code>false</code> otherwise.
	 */
	// TODO: add this method in the public interface
	public boolean isInfoLoaded() {
		return this.containerInfo != null;
	}

	@Override
	public String toString() {
		return "Container: id=" + id() + "\n" + "  image=" + image() + "\n"
				+ "  created=" + created() + "\n" + "  command=" + command()
				+ "\n" + "  status=<" + status() + ">\n" + "  name="
				+ name() + "\n";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DockerContainer other = (DockerContainer) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	
}

Back to the top