PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/libvirt/Error.java

https://github.com/majst01/libvirt-java
Java | 372 lines | 239 code | 31 blank | 102 comment | 1 complexity | 162f83ec3b7cfc1da21b21f1127141b8 MD5 | raw file
  1. package org.libvirt;
  2. import java.io.Serializable;
  3. import org.libvirt.jna.ConnectionPointer;
  4. import org.libvirt.jna.DomainPointer;
  5. import org.libvirt.jna.NetworkPointer;
  6. import org.libvirt.jna.virError;
  7. /**
  8. * An error which is returned from libvirt,
  9. */
  10. public class Error implements Serializable {
  11. /**
  12. * Returns the element of the given array at the specified index,
  13. * or the last element of the array if the index is not less than
  14. * {@code values.length}.
  15. *
  16. * @return n-th item of {@code values} when {@code n <
  17. * values.length}, otherwise the last item of {@code values}.
  18. */
  19. private static final <T> T safeElementAt(final int n, final T[] values) {
  20. assert(n >= 0 && values.length > 0);
  21. int idx = Math.min(n, values.length - 1);
  22. return values[idx];
  23. }
  24. public static enum ErrorDomain {
  25. VIR_FROM_NONE, VIR_FROM_XEN, /* Error at Xen hypervisor layer */
  26. VIR_FROM_XEND, /* Error at connection with xend daemon */
  27. VIR_FROM_XENSTORE, /* Error at connection with xen store */
  28. VIR_FROM_SEXPR, /* Error in the S-Expression code */
  29. VIR_FROM_XML, /* Error in the XML code */
  30. VIR_FROM_DOM, /* Error when operating on a domain */
  31. VIR_FROM_RPC, /* Error in the XML-RPC code */
  32. VIR_FROM_PROXY, /* Error in the proxy code */
  33. VIR_FROM_CONF, /* Error in the configuration file handling */
  34. VIR_FROM_QEMU, /* Error at the QEMU daemon */
  35. VIR_FROM_NET, /* Error when operating on a network */
  36. VIR_FROM_TEST, /* Error from test driver */
  37. VIR_FROM_REMOTE, /* Error from remote driver */
  38. VIR_FROM_OPENVZ, /* Error from OpenVZ driver */
  39. VIR_FROM_XENXM, /* Error at Xen XM layer */
  40. VIR_FROM_STATS_LINUX, /* Error in the Linux Stats code */
  41. VIR_FROM_LXC, /* Error from Linux Container driver */
  42. VIR_FROM_STORAGE, /* Error from storage driver */
  43. VIR_FROM_NETWORK, /* Error from network config */
  44. VIR_FROM_DOMAIN, /* Error from domain config */
  45. VIR_FROM_UML, /* Error at the UML driver */
  46. VIR_FROM_NODEDEV, /* Error from node device monitor */
  47. VIR_FROM_XEN_INOTIFY, /* Error from xen inotify layer */
  48. VIR_FROM_SECURITY, /* Error from security framework */
  49. VIR_FROM_VBOX, /* Error from VirtualBox driver */
  50. VIR_FROM_INTERFACE, /* Error when operating on an interface */
  51. VIR_FROM_ONE, /* Error from OpenNebula driver */
  52. VIR_FROM_ESX, /* Error from ESX driver */
  53. VIR_FROM_PHYP, /* Error from IBM power hypervisor */
  54. VIR_FROM_SECRET, /* Error from secret storage */
  55. VIR_FROM_CPU, /* Error from CPU driver */
  56. VIR_FROM_XENAPI, /* Error from XenAPI */
  57. VIR_FROM_NWFILTER, /* Error from network filter driver */
  58. VIR_FROM_HOOK, /* Error from Synchronous hooks */
  59. VIR_FROM_DOMAIN_SNAPSHOT, /* Error from domain snapshot */
  60. VIR_FROM_AUDIT, /* Error from auditing subsystem */
  61. VIR_FROM_SYSINFO, /* Error from sysinfo/SMBIOS */
  62. VIR_FROM_STREAMS, /* Error from I/O streams */
  63. VIR_FROM_VMWARE, /* Error from VMware driver */
  64. VIR_FROM_EVENT, /* Error from event loop impl */
  65. VIR_FROM_LIBXL, /* Error from libxenlight driver */
  66. VIR_FROM_LOCKING, /* Error from lock manager */
  67. VIR_FROM_HYPERV, /* Error from Hyper-V driver */
  68. VIR_FROM_CAPABILITIES, /* Error from capabilities */
  69. VIR_FROM_URI, /* Error from URI handling */
  70. VIR_FROM_AUTH, /* Error from auth handling */
  71. VIR_FROM_DBUS, /* Error from DBus */
  72. VIR_FROM_UNKNOWN; /* unknown error domain (must be the last entry!) */
  73. protected static final ErrorDomain wrap(int value) {
  74. return safeElementAt(value, values());
  75. }
  76. }
  77. public static enum ErrorLevel {
  78. VIR_ERR_NONE,
  79. /**
  80. * A simple warning
  81. */
  82. VIR_ERR_WARNING,
  83. /**
  84. * An error
  85. */
  86. VIR_ERR_ERROR,
  87. VIR_ERR_UNKNOWN; /* must be the last entry! */
  88. protected static final ErrorLevel wrap(int value) {
  89. return safeElementAt(value, values());
  90. }
  91. }
  92. public static enum ErrorNumber {
  93. VIR_ERR_OK, VIR_ERR_INTERNAL_ERROR, /* internal error */
  94. VIR_ERR_NO_MEMORY, /* memory allocation failure */
  95. VIR_ERR_NO_SUPPORT, /* no support for this function */
  96. VIR_ERR_UNKNOWN_HOST, /* could not resolve hostname */
  97. VIR_ERR_NO_CONNECT, /* can't connect to hypervisor */
  98. VIR_ERR_INVALID_CONN, /* invalid connection object */
  99. VIR_ERR_INVALID_DOMAIN, /* invalid domain object */
  100. VIR_ERR_INVALID_ARG, /* invalid function argument */
  101. VIR_ERR_OPERATION_FAILED, /* a command to hypervisor failed */
  102. VIR_ERR_GET_FAILED, /* a HTTP GET command to failed */
  103. VIR_ERR_POST_FAILED, /* a HTTP POST command to failed */
  104. VIR_ERR_HTTP_ERROR, /* unexpected HTTP error code */
  105. VIR_ERR_SEXPR_SERIAL, /* failure to serialize an S-Expr */
  106. VIR_ERR_NO_XEN, /* could not open Xen hypervisor control */
  107. VIR_ERR_XEN_CALL, /* failure doing an hypervisor call */
  108. VIR_ERR_OS_TYPE, /* unknown OS type */
  109. VIR_ERR_NO_KERNEL, /* missing kernel information */
  110. VIR_ERR_NO_ROOT, /* missing root device information */
  111. VIR_ERR_NO_SOURCE, /* missing source device information */
  112. VIR_ERR_NO_TARGET, /* missing target device information */
  113. VIR_ERR_NO_NAME, /* missing domain name information */
  114. VIR_ERR_NO_OS, /* missing domain OS information */
  115. VIR_ERR_NO_DEVICE, /* missing domain devices information */
  116. VIR_ERR_NO_XENSTORE, /* could not open Xen Store control */
  117. VIR_ERR_DRIVER_FULL, /* too many drivers registered */
  118. VIR_ERR_CALL_FAILED, /* not supported by the drivers (DEPRECATED) */
  119. VIR_ERR_XML_ERROR, /* an XML description is not well formed or broken */
  120. VIR_ERR_DOM_EXIST, /* the domain already exist */
  121. VIR_ERR_OPERATION_DENIED, /*
  122. * operation forbidden on read-only
  123. * connections
  124. */
  125. VIR_ERR_OPEN_FAILED, /* failed to open a conf file */
  126. VIR_ERR_READ_FAILED, /* failed to read a conf file */
  127. VIR_ERR_PARSE_FAILED, /* failed to parse a conf file */
  128. VIR_ERR_CONF_SYNTAX, /* failed to parse the syntax of a conf file */
  129. VIR_ERR_WRITE_FAILED, /* failed to write a conf file */
  130. VIR_ERR_XML_DETAIL, /* detail of an XML error */
  131. VIR_ERR_INVALID_NETWORK, /* invalid network object */
  132. VIR_ERR_NETWORK_EXIST, /* the network already exist */
  133. VIR_ERR_SYSTEM_ERROR, /* general system call failure */
  134. VIR_ERR_RPC, /* some sort of RPC error */
  135. VIR_ERR_GNUTLS_ERROR, /* error from a GNUTLS call */
  136. VIR_WAR_NO_NETWORK, /* failed to start network */
  137. VIR_ERR_NO_DOMAIN, /* domain not found or unexpectedly disappeared */
  138. VIR_ERR_NO_NETWORK, /* network not found */
  139. VIR_ERR_INVALID_MAC, /* invalid MAC address */
  140. VIR_ERR_AUTH_FAILED, /* authentication failed */
  141. VIR_ERR_INVALID_STORAGE_POOL, /* invalid storage pool object */
  142. VIR_ERR_INVALID_STORAGE_VOL, /* invalid storage vol object */
  143. VIR_WAR_NO_STORAGE, /* failed to start storage */
  144. VIR_ERR_NO_STORAGE_POOL, /* storage pool not found */
  145. VIR_ERR_NO_STORAGE_VOL, /* storage pool not found */
  146. VIR_WAR_NO_NODE, /* failed to start node driver */
  147. VIR_ERR_INVALID_NODE_DEVICE, /* invalid node device object */
  148. VIR_ERR_NO_NODE_DEVICE, /* node device not found */
  149. VIR_ERR_NO_SECURITY_MODEL, /* security model not found */
  150. VIR_ERR_OPERATION_INVALID, /* operation is not applicable at this time */
  151. VIR_WAR_NO_INTERFACE, /* failed to start interface driver */
  152. VIR_ERR_NO_INTERFACE, /* interface driver not running */
  153. VIR_ERR_INVALID_INTERFACE, /* invalid interface object */
  154. VIR_ERR_MULTIPLE_INTERFACES, /* more than one matching interface found */
  155. VIR_WAR_NO_SECRET, /* failed to start secret storage */
  156. VIR_ERR_INVALID_SECRET, /* invalid secret */
  157. VIR_ERR_NO_SECRET, /* secret not found */
  158. VIR_ERR_CONFIG_UNSUPPORTED, /* unsupported configuration construct */
  159. VIR_ERR_OPERATION_TIMEOUT, /* timeout occurred during operation */
  160. VIR_ERR_MIGRATE_PERSIST_FAILED, /* a migration worked, but making the
  161. VM persist on the dest host failed */
  162. VIR_ERR_HOOK_SCRIPT_FAILED, /* a synchronous hook script failed */
  163. VIR_ERR_INVALID_DOMAIN_SNAPSHOT, /* invalid domain snapshot */
  164. VIR_ERR_NO_DOMAIN_SNAPSHOT, /* domain snapshot not found */
  165. VIR_ERR_INVALID_STREAM, /* stream pointer not valid */
  166. VIR_ERR_ARGUMENT_UNSUPPORTED, /* valid API use but unsupported by
  167. the given driver */
  168. VIR_ERR_STORAGE_PROBE_FAILED, /* storage pool probe failed */
  169. VIR_ERR_STORAGE_POOL_BUILT, /* storage pool already built */
  170. VIR_ERR_SNAPSHOT_REVERT_RISKY, /* force was not requested for a
  171. risky domain snapshot revert */
  172. VIR_ERR_OPERATION_ABORTED, /* operation on a domain was
  173. canceled/aborted by user */
  174. VIR_ERR_AUTH_CANCELLED, /* authentication cancelled */
  175. VIR_ERR_NO_DOMAIN_METADATA, /* The metadata is not present */
  176. VIR_ERR_MIGRATE_UNSAFE, /* Migration is not safe */
  177. VIR_ERR_OVERFLOW, /* integer overflow */
  178. VIR_ERR_BLOCK_COPY_ACTIVE, /* action prevented by block copy job */
  179. VIR_ERR_UNKNOWN; /* unknown error (must be the last entry!) */
  180. protected static final ErrorNumber wrap(int value) {
  181. return safeElementAt(value, values());
  182. }
  183. }
  184. /**
  185. *
  186. */
  187. private static final long serialVersionUID = -4780109197014633842L;
  188. private ErrorNumber code;
  189. private ErrorDomain domain;
  190. private String message;
  191. private ErrorLevel level;
  192. private ConnectionPointer VCP; /* Deprecated */
  193. private DomainPointer VDP; /* Deprecated */
  194. private String str1;
  195. private String str2;
  196. private String str3;
  197. private int int1;
  198. private int int2;
  199. private NetworkPointer VNP; /* Deprecated */
  200. public Error(virError vError) {
  201. code = code.wrap(vError.code);
  202. domain = domain.wrap(vError.domain);
  203. level = level.wrap(vError.level);
  204. message = vError.message;
  205. str1 = vError.str1;
  206. str2 = vError.str2;
  207. str3 = vError.str3;
  208. int1 = vError.int1;
  209. int2 = vError.int2;
  210. VCP = vError.conn;
  211. VDP = vError.dom;
  212. VNP = vError.net;
  213. }
  214. /**
  215. * Gets he error code
  216. *
  217. * @return a VirErroNumber
  218. */
  219. public ErrorNumber getCode() {
  220. return code;
  221. }
  222. /**
  223. * returns the Connection associated with the error, if available
  224. * Deprecated, always throw an exception now
  225. *
  226. * @return the Connect object
  227. * @throws ErrorException
  228. * @deprecated
  229. */
  230. @Deprecated
  231. public Connect getConn() throws ErrorException {
  232. throw new ErrorException("No Connect object available");
  233. }
  234. /**
  235. * returns the Domain associated with the error, if available
  236. *
  237. * @return Domain object
  238. * @throws ErrorException
  239. * @deprecated
  240. */
  241. @Deprecated
  242. public Domain getDom() throws ErrorException {
  243. throw new ErrorException("No Domain object available");
  244. }
  245. /**
  246. * Tells What part of the library raised this error
  247. *
  248. * @return a ErrorDomain
  249. */
  250. public ErrorDomain getDomain() {
  251. return domain;
  252. }
  253. /**
  254. * @return extra number information
  255. */
  256. public int getInt1() {
  257. return int1;
  258. }
  259. /**
  260. * @return extra number information
  261. */
  262. public int getInt2() {
  263. return int2;
  264. }
  265. /**
  266. * Tells how consequent is the error
  267. *
  268. * @return a ErrorLevel
  269. */
  270. public ErrorLevel getLevel() {
  271. return level;
  272. }
  273. /**
  274. * Returns human-readable informative error messag
  275. *
  276. * @return error message
  277. */
  278. public String getMessage() {
  279. return message;
  280. }
  281. /**
  282. * Returns the network associated with the error, if available
  283. *
  284. * @return Network object
  285. * @throws ErrorException
  286. * @deprecated
  287. */
  288. @Deprecated
  289. public Network getNet() throws ErrorException {
  290. throw new ErrorException("No Network object available");
  291. }
  292. /**
  293. * @return extra string information
  294. */
  295. public String getStr1() {
  296. return str1;
  297. }
  298. /**
  299. * @return extra string information
  300. */
  301. public String getStr2() {
  302. return str2;
  303. }
  304. /**
  305. * @return extra string information
  306. */
  307. public String getStr3() {
  308. return str3;
  309. }
  310. /**
  311. * Does this error has a valid Connection object attached? NOTE: deprecated,
  312. * should return false
  313. *
  314. * @return false
  315. */
  316. public boolean hasConn() {
  317. return false;
  318. }
  319. /**
  320. * Does this error has a valid Domain object attached? NOTE: deprecated,
  321. * should return false
  322. *
  323. * @return false
  324. */
  325. public boolean hasDom() {
  326. return false;
  327. }
  328. /**
  329. * Does this error has a valid Network object attached? NOTE: deprecated,
  330. * should return false
  331. *
  332. * @return false
  333. */
  334. public boolean hasNet() {
  335. return false;
  336. }
  337. @Override
  338. public String toString() {
  339. return String.format("level:%s%ncode:%s%ndomain:%s%nhasConn:%b%nhasDom:%b%nhasNet:%b%nmessage:%s%nstr1:%s%nstr2:%s%nstr3:%s%nint1:%d%nint2:%d%n", level, code, domain, hasConn(), hasDom(), hasNet(), message, str1, str2, str3, int1, int2);
  340. }
  341. }