summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/libvirt/domain/device/Graphics.java
blob: 3fd2f8123d0e4d733e0a5057c79739b13f55cfc1 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package org.openslx.libvirt.domain.device;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.openslx.libvirt.xml.LibvirtXmlNode;

/**
 * A graphics (display) device node in a Libvirt domain XML document.
 * 
 * @author Manuel Bentele
 * @version 1.0
 */
public class Graphics extends Device
{
	/**
	 * Creates an empty graphics device.
	 */
	public Graphics()
	{
		super();
	}

	/**
	 * Creates a graphics device representing an existing Libvirt XML graphics device element.
	 * 
	 * @param xmlNode existing Libvirt XML graphics device element.
	 */
	public Graphics( LibvirtXmlNode xmlNode )
	{
		super( xmlNode );
	}

	/**
	 * Returns the listen type of the graphics device.
	 * 
	 * @return listen type of the graphics device.
	 */
	public ListenType getListenType()
	{
		final String listenType = this.getXmlElementAttributeValue( "listen", "type" );
		return ListenType.fromString( listenType );
	}

	/**
	 * Sets the listen type for the graphics device.
	 * 
	 * @param type listen type for the graphics device.
	 */
	public void setListenType( ListenType type )
	{
		this.setXmlElementAttributeValue( "listen", "type", type.toString() );
	}

	/**
	 * Returns the listen address of the graphics device.
	 * 
	 * @return listen address of the graphics device.
	 */
	public InetAddress getListenAddress()
	{
		InetAddress parsedListenAddress = null;

		if ( this.getListenType() == ListenType.ADDRESS ) {
			// only read listen address, if address listen type is set
			final String rawListenAddress = this.getXmlElementAttributeValue( "listen", "address" );

			try {
				parsedListenAddress = InetAddress.getByName( rawListenAddress );
			} catch ( UnknownHostException e ) {
				parsedListenAddress = null;
			}
		}

		return parsedListenAddress;
	}

	/**
	 * Sets the listen address for the graphics device.
	 * 
	 * @param listenAddress listen address for the graphics device.
	 */
	public void setListenAddress( InetAddress listenAddress )
	{
		if ( this.getListenType() == ListenType.ADDRESS && listenAddress != null ) {
			// only set listen address, if address listen type is set
			this.setXmlElementAttributeValue( "listen", "address", listenAddress.getHostAddress() );
		}
	}

	/**
	 * Returns the listen port of the graphics device.
	 * 
	 * @return listen port of the graphics device.
	 */
	public int getListenPort()
	{
		final String listenPort = this.getXmlElementAttributeValue( "port" );
		return Integer.valueOf( listenPort );
	}

	/**
	 * Sets the listen port for the graphics device.
	 * 
	 * @param listenPort listen port for the graphics device.
	 */
	public void setListenPort( int listenPort )
	{
		if ( this.getListenType() == ListenType.ADDRESS ) {
			// only set listen port, if address listen type is set
			this.setXmlElementAttributeValue( "port", Integer.toString( listenPort ) );
		}
	}

	/**
	 * Creates a non-existent graphics device as Libvirt XML device element.
	 * 
	 * @param graphics graphics device that is created.
	 * @param xmlNode Libvirt XML node of the Libvirt XML device that is created.
	 * @return created graphics device instance.
	 */
	public static Graphics createInstance( Graphics graphics, LibvirtXmlNode xmlNode )
	{
		Graphics addedGraphics = null;

		if ( graphics instanceof GraphicsSpice ) {
			xmlNode.setXmlElementAttributeValue( "type", Type.SPICE.toString() );
			addedGraphics = GraphicsSpice.createInstance( xmlNode );
		} else if ( graphics instanceof GraphicsVnc ) {
			xmlNode.setXmlElementAttributeValue( "type", Type.VNC.toString() );
			addedGraphics = GraphicsVnc.createInstance( xmlNode );
		}

		return addedGraphics;
	}

	/**
	 * Creates a graphics device representing an existing Libvirt XML graphics device element.
	 * 
	 * @param xmlNode existing Libvirt XML graphics device element.
	 * @return graphics device instance.
	 */
	public static Graphics newInstance( LibvirtXmlNode xmlNode )
	{
		Graphics deviceGraphics = null;
		Type type = Type.fromString( xmlNode.getXmlElementAttributeValue( "type" ) );

		if ( type == null ) {
			return null;
		}

		switch ( type ) {
		case SPICE:
			deviceGraphics = GraphicsSpice.newInstance( xmlNode );
			break;
		case VNC:
			deviceGraphics = GraphicsVnc.newInstance( xmlNode );
			break;
		}

		return deviceGraphics;
	}

	/**
	 * Listen type of graphics device.
	 * 
	 * @author Manuel Bentele
	 * @version 1.0
	 */
	public enum ListenType
	{
		// @formatter:off
		NONE   ( "none" ),
		ADDRESS( "address" ),
		NETWORK( "network" ),
		SOCKET ( "socket" );
      // @formatter:on

		/**
		 * Name of graphics device listen type.
		 */
		private String type = null;

		/**
		 * Creates graphics device listen type.
		 * 
		 * @param type valid name of the graphics device listen type in a Libvirt domain XML document.
		 */
		ListenType( String type )
		{
			this.type = type;
		}

		@Override
		public String toString()
		{
			return this.type;
		}

		/**
		 * Creates graphics device listen type from its name with error check.
		 * 
		 * @param type name of the graphics device listen type in a Libvirt domain XML document.
		 * @return valid graphics device listen type.
		 */
		public static ListenType fromString( String type )
		{
			for ( ListenType t : ListenType.values() ) {
				if ( t.type.equalsIgnoreCase( type ) ) {
					return t;
				}
			}

			return null;
		}
	}

	/**
	 * Type of graphics device.
	 * 
	 * @author Manuel Bentele
	 * @version 1.0
	 */
	enum Type
	{
		// @formatter:off
		SPICE( "spice" ),
		VNC  ( "vnc" );
      // @formatter:on

		/**
		 * Name of graphics device type.
		 */
		private String type = null;

		/**
		 * Creates graphics device type.
		 * 
		 * @param type valid name of the graphics device type in a Libvirt domain XML document.
		 */
		Type( String type )
		{
			this.type = type;
		}

		@Override
		public String toString()
		{
			return this.type;
		}

		/**
		 * Creates graphics device type from its name with error check.
		 * 
		 * @param type name of the graphics device type in a Libvirt domain XML document.
		 * @return valid graphics device type.
		 */
		public static Type fromString( String type )
		{
			for ( Type t : Type.values() ) {
				if ( t.type.equalsIgnoreCase( type ) ) {
					return t;
				}
			}

			return null;
		}
	}
}