1 /*------------------------------------------------------------------------------
  2 Name:      MsgQueueUnSubscribeEntry.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 package org.xmlBlaster.client.queuemsg;
  7 
  8 import org.xmlBlaster.client.key.UnSubscribeKey;
  9 import org.xmlBlaster.client.qos.UnSubscribeQos;
 10 import org.xmlBlaster.util.Global;
 11 import org.xmlBlaster.util.MsgUnit;
 12 import org.xmlBlaster.util.SessionName;
 13 import org.xmlBlaster.util.Timestamp;
 14 import org.xmlBlaster.util.XmlBlasterException;
 15 import org.xmlBlaster.util.def.MethodName;
 16 import org.xmlBlaster.util.def.PriorityEnum;
 17 import org.xmlBlaster.util.queue.StorageId;
 18 import org.xmlBlaster.util.queuemsg.MsgQueueEntry;
 19 
 20 
 21 /**
 22  * Wraps an unSubscribe() message into an entry for a sorted queue.
 23  * @author xmlBlaster@marcelruff.info
 24  */
 25 public final class MsgQueueUnSubscribeEntry extends MsgQueueEntry
 26 {
 27    private static final long serialVersionUID = 1L;
 28    private final static String ME = "UnSubscribeQueueEntry";
 29    private final UnSubscribeQos unSubscribeQos;
 30    private final UnSubscribeKey unSubscribeKey;
 31    private SessionName receiver;
 32    private final long immutableSizeInBytes;
 33 
 34    /**
 35     * Use this constructor if a new message object is fed by method unSubscribe(). 
 36     * <p />
 37     * @param unSubscribeQos
 38     */
 39    public MsgQueueUnSubscribeEntry(Global glob, StorageId storageId, 
 40                                  UnSubscribeKey unSubscribeKey, UnSubscribeQos unSubscribeQos)
 41          throws XmlBlasterException {
 42       super(glob, MethodName.UNSUBSCRIBE, PriorityEnum.NORM_PRIORITY, storageId,
 43             (unSubscribeQos == null) ? false : unSubscribeQos.getData().isPersistent());
 44       this.unSubscribeQos = (unSubscribeQos == null) ? new UnSubscribeQos(glob) : unSubscribeQos;
 45       if (unSubscribeKey == null) throw new IllegalArgumentException(ME + " UnSubscribeKey is null");
 46       this.unSubscribeKey = unSubscribeKey;
 47       this.immutableSizeInBytes = 567 + this.unSubscribeQos.getData().size() + this.unSubscribeKey.getData().size();
 48    }
 49 
 50    /**
 51     * For persistence recovery
 52     */
 53    public MsgQueueUnSubscribeEntry(Global glob, PriorityEnum priority, StorageId storageId,
 54                                 Timestamp timestamp, long sizeInBytes,
 55                                 UnSubscribeKey unSubscribeKey, UnSubscribeQos unSubscribeQos) {
 56       super(glob, MethodName.UNSUBSCRIBE.toString(), priority,
 57             timestamp, storageId, unSubscribeQos.getData().isPersistent());
 58       this.unSubscribeQos = unSubscribeQos;
 59       this.unSubscribeKey = unSubscribeKey;
 60       this.immutableSizeInBytes = sizeInBytes;
 61    }
 62 
 63    /**
 64     * @see MsgQueueEntry#isExpired
 65     */
 66    public boolean isExpired() {
 67       return false;
 68    }
 69 
 70    /**
 71     * @see MsgQueueEntry#isDestroyed
 72     */
 73    public boolean isDestroyed() {
 74       return false;
 75    }
 76 
 77    public UnSubscribeQos getUnSubscribeQos() {
 78       return this.unSubscribeQos;
 79    }
 80 
 81    public UnSubscribeKey getUnSubscribeKey() {
 82       return this.unSubscribeKey;
 83    }
 84 
 85    public MsgUnit getMsgUnit() {
 86       return new MsgUnit(getUnSubscribeKey().getData(), null, getUnSubscribeQos().getData());
 87    }
 88 
 89    /**
 90     * Access the unique login name of the unSubscriber. 
 91     * @return loginName of the source
 92     * @see MsgQueueEntry#getSender()
 93     */
 94    public SessionName getSender() {
 95       return null;
 96    }
 97 
 98    /**
 99     * @return The name of the receiver (data sink) or null
100     * @see MsgQueueEntry#getReceiver()
101     */
102    public void setReceiver(SessionName receiver) {
103       this.receiver = receiver;
104    }
105 
106    /**
107     * @return The name of the receiver (data sink) or null
108     * @see MsgQueueEntry#getReceiver()
109     */
110    public SessionName getReceiver() {
111       return this.receiver;
112    }
113 
114    /**
115     * @see MsgQueueEntry#getKeyOid()
116     */
117    public String getKeyOid() {
118       return null;
119    }
120 
121    /**
122     * return null
123     */
124    public Timestamp getRcvTimestamp() {
125       return null;
126    }
127 
128    /**
129     * The embeddded object for this implementing class is an Object[2] where
130     * Object[0] = qos.toXml()
131     * Object[1] = key.toXml()
132     */
133    public Object getEmbeddedObject() {
134       Object[] obj = { this.unSubscribeQos.toXml(), this.unSubscribeKey.toXml() };
135       return obj;
136    }
137 
138    public long getSizeInBytes() {
139       return this.immutableSizeInBytes;
140    }
141 
142    /**
143     * @return true
144     */
145    public boolean isInternal() {
146       return true;
147    }
148 
149    public final void embeddedObjectToXml(java.io.OutputStream out, java.util.Properties props) throws java.io.IOException {
150       out.write(this.unSubscribeKey.toXml().getBytes());
151       out.write(this.unSubscribeQos.toXml(props).getBytes());
152    }
153       
154    /**
155     * Returns a shallow clone
156     */
157    public Object clone() {
158       MsgQueueUnSubscribeEntry entry = null;
159       entry = (MsgQueueUnSubscribeEntry)super.clone();
160       return entry;
161    }
162 }


syntax highlighted by Code2HTML, v. 0.9.1