1 /* 2 D-Bus Java Implementation 3 Copyright (c) 2005-2006 Matthew Johnson 4 5 This program is free software; you can redistribute it and/or modify it 6 under the terms of either the GNU Lesser General Public License Version 2 or the 7 Academic Free Licence Version 2.1. 8 9 Full licence texts are included in the COPYING file with this program. 10 */ 11 package org.freedesktop.dbus; 12 13 import static org.freedesktop.dbus.Gettext._; 14 15 import java.lang.reflect.Constructor; 16 import java.util.Vector; 17 import org.freedesktop.dbus.exceptions.DBusException; 18 import org.freedesktop.dbus.exceptions.DBusExecutionException; 19 import org.freedesktop.dbus.exceptions.MessageFormatException; 20 import org.freedesktop.dbus.exceptions.NotConnected; 21 22 import cx.ath.matthew.debug.Debug; 23 24 /** 25 * Error messages which can be sent over the bus. 26 */ 27 public class Error extends Message 28 { Error()29 Error() { } Error(String dest, String errorName, long replyserial, String sig, Object... args)30 public Error(String dest, String errorName, long replyserial, String sig, Object... args) throws DBusException 31 { 32 this(null, dest, errorName, replyserial, sig, args); 33 } Error(String source, String dest, String errorName, long replyserial, String sig, Object... args)34 public Error(String source, String dest, String errorName, long replyserial, String sig, Object... args) throws DBusException 35 { 36 super(Message.Endian.BIG, Message.MessageType.ERROR, (byte) 0); 37 38 if (null == errorName) 39 throw new MessageFormatException(_("Must specify error name to Errors.")); 40 headers.put(Message.HeaderField.REPLY_SERIAL,replyserial); 41 headers.put(Message.HeaderField.ERROR_NAME,errorName); 42 43 Vector<Object> hargs = new Vector<Object>(); 44 hargs.add(new Object[] { Message.HeaderField.ERROR_NAME, new Object[] { ArgumentType.STRING_STRING, errorName } }); 45 hargs.add(new Object[] { Message.HeaderField.REPLY_SERIAL, new Object[] { ArgumentType.UINT32_STRING, replyserial } }); 46 47 if (null != source) { 48 headers.put(Message.HeaderField.SENDER,source); 49 hargs.add(new Object[] { Message.HeaderField.SENDER, new Object[] { ArgumentType.STRING_STRING, source } }); 50 } 51 52 if (null != dest) { 53 headers.put(Message.HeaderField.DESTINATION,dest); 54 hargs.add(new Object[] { Message.HeaderField.DESTINATION, new Object[] { ArgumentType.STRING_STRING, dest } }); 55 } 56 57 if (null != sig) { 58 hargs.add(new Object[] { Message.HeaderField.SIGNATURE, new Object[] { ArgumentType.SIGNATURE_STRING, sig } }); 59 headers.put(Message.HeaderField.SIGNATURE,sig); 60 setArgs(args); 61 } 62 63 byte[] blen = new byte[4]; 64 appendBytes(blen); 65 append("ua(yv)", serial, hargs.toArray()); 66 pad((byte)8); 67 68 long c = bytecounter; 69 if (null != sig) append(sig, args); 70 marshallint(bytecounter-c, blen, 0, 4); 71 } Error(String source, Message m, Throwable e)72 public Error(String source, Message m, Throwable e) throws DBusException 73 { 74 this(source, m.getSource(), AbstractConnection.dollar_pattern.matcher(e.getClass().getName()).replaceAll("."), m.getSerial(), "s", e.getMessage()); 75 } Error(Message m, Throwable e)76 public Error(Message m, Throwable e) throws DBusException 77 { 78 this(m.getSource(), AbstractConnection.dollar_pattern.matcher(e.getClass().getName()).replaceAll("."), m.getSerial(), "s", e.getMessage()); 79 } 80 @SuppressWarnings("unchecked") createExceptionClass(String name)81 private static Class<? extends DBusExecutionException> createExceptionClass(String name) 82 { 83 if (name == "org.freedesktop.DBus.Local.Disconnected") return NotConnected.class; 84 Class<? extends DBusExecutionException> c = null; 85 do { 86 try { 87 c = (Class<? extends org.freedesktop.dbus.exceptions.DBusExecutionException>) Class.forName(name); 88 } catch (ClassNotFoundException CNFe) {} 89 name = name.replaceAll("\\.([^\\.]*)$", "\\$$1"); 90 } while (null == c && name.matches(".*\\..*")); 91 return c; 92 } 93 /** 94 * Turns this into an exception of the correct type 95 */ getException()96 public DBusExecutionException getException() 97 { 98 try { 99 Class<? extends DBusExecutionException> c = createExceptionClass(getName()); 100 if (null == c || !DBusExecutionException.class.isAssignableFrom(c)) c = DBusExecutionException.class; 101 Constructor<? extends DBusExecutionException> con = c.getConstructor(String.class); 102 DBusExecutionException ex; 103 Object[] args = getParameters(); 104 if (null == args || 0 == args.length) 105 ex = con.newInstance(""); 106 else { 107 String s = ""; 108 for (Object o: args) 109 s += o + " "; 110 ex = con.newInstance(s.trim()); 111 } 112 ex.setType(getName()); 113 return ex; 114 } catch (Exception e) { 115 if (AbstractConnection.EXCEPTION_DEBUG && Debug.debug) Debug.print(Debug.ERR, e); 116 if (AbstractConnection.EXCEPTION_DEBUG && Debug.debug && null != e.getCause()) 117 Debug.print(Debug.ERR, e.getCause()); 118 DBusExecutionException ex; 119 Object[] args = null; 120 try { 121 args = getParameters(); 122 } catch (Exception ee) {} 123 if (null == args || 0 == args.length) 124 ex = new DBusExecutionException(""); 125 else { 126 String s = ""; 127 for (Object o: args) 128 s += o + " "; 129 ex = new DBusExecutionException(s.trim()); 130 } 131 ex.setType(getName()); 132 return ex; 133 } 134 } 135 /** 136 * Throw this as an exception of the correct type 137 */ throwException()138 public void throwException() throws DBusExecutionException 139 { 140 throw getException(); 141 } 142 } 143