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