| 1 | // $Id: Message.java,v 1.5 2004-11-05 12:07:19 tigran Exp $ |
| 2 | |
| 3 | package diskCacheV111.vehicles; |
| 4 | |
| 5 | import dmg.cells.nucleus.HasDiagnosticContext; |
| 6 | import org.dcache.util.ReflectionUtils; |
| 7 | import diskCacheV111.util.PnfsId; |
| 8 | import javax.security.auth.Subject; |
| 9 | import org.dcache.auth.Subjects; |
| 10 | |
| 11 | // Base class for all Messages |
| 12 | |
| 13 | public class Message |
| 14 | implements java.io.Serializable, |
| 15 | HasDiagnosticContext |
| 16 | { |
| 17 | private boolean _replyRequired = false; |
| 18 | private boolean _isReply = false; |
| 19 | private int _returnCode = 0; |
| 20 | private Object _errorObject = null; |
| 21 | private long _id = 0 ; |
| 22 | private Subject _subject; |
| 23 | |
| 24 | private static final long serialVersionUID = 2056896713066252504L; |
| 25 | |
| 26 | public Message(){ |
| 27 | } |
| 28 | |
| 29 | public Message(boolean replyRequired){ |
| 30 | _replyRequired = replyRequired; |
| 31 | } |
| 32 | @Override |
| 33 | public String toString(){ |
| 34 | return _returnCode==0?"":"("+_returnCode+")="+_errorObject ; |
| 35 | } |
| 36 | public void setSucceeded(){ |
| 37 | setReply(0,null); |
| 38 | } |
| 39 | |
| 40 | public void setFailed(int errorCode, Object errorObject){ |
| 41 | setReply(errorCode, errorObject); |
| 42 | } |
| 43 | public void setReply(){ |
| 44 | _isReply = true ; |
| 45 | } |
| 46 | public void setReply(int returnCode, Object errorObject){ |
| 47 | _isReply = true; |
| 48 | _returnCode = returnCode; |
| 49 | _errorObject = errorObject; |
| 50 | } |
| 51 | |
| 52 | public boolean isReply(){ |
| 53 | return _isReply; |
| 54 | } |
| 55 | |
| 56 | public void clearReply(){ |
| 57 | //allows us to reuse message objects |
| 58 | _isReply = false; |
| 59 | _returnCode = 0; |
| 60 | _errorObject = null; |
| 61 | } |
| 62 | |
| 63 | public int getReturnCode(){ |
| 64 | return _returnCode; |
| 65 | } |
| 66 | |
| 67 | public Object getErrorObject(){ |
| 68 | return _errorObject; |
| 69 | } |
| 70 | |
| 71 | public boolean getReplyRequired(){ |
| 72 | return _replyRequired; |
| 73 | } |
| 74 | |
| 75 | public void setReplyRequired(boolean replyRequired){ |
| 76 | _replyRequired = replyRequired; |
| 77 | } |
| 78 | public void setId( long id ){ _id = id ; } |
| 79 | public long getId(){ return _id ; } |
| 80 | |
| 81 | public void setSubject(Subject subject) |
| 82 | { |
| 83 | _subject = subject; |
| 84 | } |
| 85 | |
| 86 | public Subject getSubject() |
| 87 | { |
| 88 | return (_subject == null) ? Subjects.ROOT : _subject; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns a human readable name of the message class. By default |
| 93 | * this is the short class name with the "Message" or "Msg" suffix |
| 94 | * removed. |
| 95 | */ |
| 96 | public String getMessageName() |
| 97 | { |
| 98 | String name = getClass().getSimpleName(); |
| 99 | int length = name.length(); |
| 100 | if ((length > 7) && name.endsWith("Message")) { |
| 101 | name = name.substring(0, name.length() - 7); |
| 102 | } else if ((length > 3) && name.endsWith("Msg")) { |
| 103 | name = name.substring(0, name.length() - 3); |
| 104 | } |
| 105 | |
| 106 | return name; |
| 107 | } |
| 108 | |
| 109 | public String getDiagnosticContext() |
| 110 | { |
| 111 | String name = getMessageName(); |
| 112 | PnfsId id = ReflectionUtils.getPnfsId(this); |
| 113 | return (id == null) ? name : (name + " " + id); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Returns true if this message could possibly change the effect |
| 118 | * or result of <code>message</code>. |
| 119 | * |
| 120 | * In a message queue, a message can be used to fold other |
| 121 | * messages as long as it is not invalidated by any intermediate |
| 122 | * messages. |
| 123 | */ |
| 124 | public boolean invalidates(Message message) |
| 125 | { |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Folds the reply of another Messages into this Message. |
| 131 | * |
| 132 | * For some Messages the correct reply can be derived from the |
| 133 | * reply of another Message. In those cases processing of this |
| 134 | * Message can be skipped and instead the reply of the other |
| 135 | * Message can be folded into this Message. |
| 136 | * |
| 137 | * A prerequiste for folding to succeed is that this Message is |
| 138 | * side effect free. It does however not matter whether the other |
| 139 | * Message has side effects. |
| 140 | * |
| 141 | * This method updates this Message by extracting the correct |
| 142 | * reply from the Message given as an argument. If successfull, |
| 143 | * this Message can be send as a valid reply back to the |
| 144 | * requestor. If not successful, this Message is unmodified. |
| 145 | * |
| 146 | * @param message Another Message to fold into this Message |
| 147 | * @return true if the operation succeeded, false otherwise |
| 148 | * @see invalidates |
| 149 | */ |
| 150 | public boolean fold(Message message) |
| 151 | { |
| 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | |