/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Specialized; using Apache.NMS.Util; namespace Apache.NMS.Commands { /// /// Summary description for Destination. /// public abstract class Destination : IDestination, ICloneable { /// /// Topic Destination object /// public const int TOPIC = 1; /// /// Temporary Topic Destination object /// public const int TEMPORARY_TOPIC = 2; /// /// Queue Destination object /// public const int QUEUE = 3; /// /// Temporary Queue Destination object /// public const int TEMPORARY_QUEUE = 4; private const String TEMP_PREFIX = "{TD{"; private const String TEMP_POSTFIX = "}TD}"; private String physicalName = ""; private StringDictionary options = null; private bool disposed = false; /// /// The Default Constructor /// protected Destination() { } /// /// Construct the Destination with a defined physical name; /// /// protected Destination(String name) { setPhysicalName(name); } ~Destination() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if(disposed) { return; } if(disposing) { try { OnDispose(); } catch(Exception ex) { Tracer.ErrorFormat("Exception disposing Destination {0}: {1}", this.physicalName, ex.Message); } } disposed = true; } /// /// Child classes can override this method to perform clean-up logic. /// protected virtual void OnDispose() { } public bool IsTopic { get { int destinationType = GetDestinationType(); return TOPIC == destinationType || TEMPORARY_TOPIC == destinationType; } } public bool IsQueue { get { int destinationType = GetDestinationType(); return QUEUE == destinationType || TEMPORARY_QUEUE == destinationType; } } public bool IsTemporary { get { int destinationType = GetDestinationType(); return TEMPORARY_QUEUE == destinationType || TEMPORARY_TOPIC == destinationType; } } /// /// Dictionary of name/value pairs representing option values specified /// in the URI used to create this Destination. A null value is returned /// if no options were specified. /// internal StringDictionary Options { get { return this.options; } } private void setPhysicalName(string name) { this.physicalName = name; int p = name.IndexOf('?'); if(p >= 0) { String optstring = physicalName.Substring(p + 1); this.physicalName = name.Substring(0, p); options = URISupport.ParseQuery(optstring); } } /// /// /// /// public static Destination Transform(IDestination destination) { Destination result = null; if(destination != null) { if(destination is Destination) { result = (Destination) destination; } else { if(destination is ITemporaryQueue) { result = new TempQueue(((IQueue) destination).QueueName); } else if(destination is ITemporaryTopic) { result = new TempTopic(((ITopic) destination).TopicName); } else if(destination is IQueue) { result = new Queue(((IQueue) destination).QueueName); } else if(destination is ITopic) { result = new Topic(((ITopic) destination).TopicName); } } } return result; } /// /// Create a temporary name from the clientId /// /// /// public static String CreateTemporaryName(String clientId) { return TEMP_PREFIX + clientId + TEMP_POSTFIX; } /// /// From a temporary destination find the clientId of the Connection that created it /// /// /// the clientId or null if not a temporary destination public static String GetClientId(Destination destination) { String answer = null; if(destination != null && destination.IsTemporary) { String name = destination.PhysicalName; int start = name.IndexOf(TEMP_PREFIX); if(start >= 0) { start += TEMP_PREFIX.Length; int stop = name.LastIndexOf(TEMP_POSTFIX); if(stop > start && stop < name.Length) { answer = name.Substring(start, stop); } } } return answer; } /// /// /// object to compare /// 1 if this is less than o else 0 if they are equal or -1 if this is less than o public int CompareTo(Object o) { if(o is Destination) { return CompareTo((Destination) o); } return -1; } /// /// Lets sort by name first then lets sort topics greater than queues /// /// another destination to compare against /// 1 if this is less than o else 0 if they are equal or -1 if this is less than o public int CompareTo(Destination that) { int answer = 0; if(physicalName != that.physicalName) { if(physicalName == null) { return -1; } else if(that.physicalName == null) { return 1; } answer = physicalName.CompareTo(that.physicalName); } if(answer == 0) { if(IsTopic) { if(that.IsQueue) { return 1; } } else { if(that.IsTopic) { return -1; } } } return answer; } /// /// /// Returns the Destination type public abstract int GetDestinationType(); public String PhysicalName { get { return this.physicalName; } set { this.physicalName = value; } } /// /// /// string representation of this instance public override String ToString() { switch(DestinationType) { case DestinationType.Topic: return "topic://" + PhysicalName; case DestinationType.TemporaryTopic: return "temp-topic://" + PhysicalName; case DestinationType.TemporaryQueue: return "temp-queue://" + PhysicalName; default: return "queue://" + PhysicalName; } } /// /// /// hashCode for this instance public override int GetHashCode() { int answer = 37; if(this.physicalName != null) { answer = physicalName.GetHashCode(); } if(IsTopic) { answer ^= 0xfabfab; } return answer; } /// /// if the object passed in is equivalent, return true /// /// the object to compare /// true if this instance and obj are equivalent public override bool Equals(Object obj) { bool result = this == obj; if(!result && obj != null && obj is Destination) { Destination other = (Destination) obj; result = this.GetDestinationType() == other.GetDestinationType() && this.physicalName.Equals(other.physicalName); } return result; } /// /// Factory method to create a child destination if this destination is a composite /// /// /// the created Destination public abstract Destination CreateDestination(String name); public abstract DestinationType DestinationType { get; } public virtual Object Clone() { // Since we are the lowest level base class, do a // shallow copy which will include the derived classes. // From here we would do deep cloning of other objects // if we had any. return this.MemberwiseClone(); } } }