Exemplo n.º 1
0
        /// <summary>
        /// Replaces all placeholders (e.g. {$ip_address}, {$session_id} in the specified text for the specified obj.
        /// Since ip_address can take several values over different Networks, this method returns a list of Uri for
        /// each of the different IP addresses.
        /// </summary>
        /// <param name="text">The text that contains the placeholders to be replaced.</param>
        /// <param name="obj">The object that the placeholder replacements are for.</param>
        /// <returns>A List of Uris.</returns>
        public static List <Uri> SubstituteUri(string uri, IXenObject obj)
        {
            Util.ThrowIfParameterNull(uri, "uri");
            string ipAddressName = Enum.GetName(typeof(PropertyNames), PropertyNames.ip_address);

            try
            {
                if (!uri.Contains(string.Format(PlaceholderFormat, ipAddressName)))
                {
                    return(new List <Uri> {
                        new Uri(Substitute(uri, obj))
                    });
                }

                string u   = Substitute(uri, obj, s => s != ipAddressName);
                var    ips = (List <ComparableAddress>)PropertyAccessors.Get(PropertyNames.ip_address)(obj);

                if (ips == null || ips.Count == 0)
                {
                    return(new List <Uri> {
                        new Uri(u)
                    });
                }

                return(ips.ConvertAll(ip => new Uri(u.Replace(string.Format(PlaceholderFormat, ipAddressName), ip.ToString()))));
            }
            catch (UriFormatException)
            {
                log.Warn(string.Format("Failed to parse url {0}", uri));
                return(new List <Uri> {
                    new Uri("about:blank")
                });
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the replacement for the specified placeholder for the specified object.
        /// </summary>
        /// <param name="placeholder">The placeholder to be replaced.</param>
        /// <param name="objs">The objects that the placeholder replacements are for.</param>
        /// <param name="match">A predicate indicating whether the specified placeholder should be replaced.</param>
        /// <returns>The replacement for the specified placeholder.</returns>
        private static string GetPlaceholderReplacement(string placeholder, IList <IXenObject> objs, Predicate <string> match)
        {
            if (match(placeholder))
            {
                if (objs == null || objs.Count == 0)
                {
                    return(NULL_PLACEHOLDER_KEY);
                }

                if (objs.Count > 1)
                {
                    return(MULTI_TARGET_PLACEHOLDER_KEY);
                }

                if (placeholder == "session_id")
                {
                    if (objs[0].Connection == null || objs[0].Connection.Session == null)
                    {
                        return(NULL_PLACEHOLDER_KEY);
                    }
                    return(objs[0].Connection.Session.uuid);
                }
                else
                {
                    // otherwise update url with the latest info
                    PropertyNames property = (PropertyNames)Enum.Parse(typeof(PropertyNames), placeholder);

                    object val = PropertyAccessors.Get(property)(objs[0]);
                    return(val != null?val.ToString() : NULL_PLACEHOLDER_KEY);
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Replaces all placeholders (e.g. {$ip_address}, {$session_id} in the specified text for the specified obj.
        /// Since ip_address can take several values over different Networks, this method returns a list of Uri for
        /// each of the different IP addresses.
        /// </summary>
        /// <param name="uri">The text that contains the placeholders to be replaced.</param>
        /// <param name="obj">The object that the placeholder replacements are for.</param>
        /// <returns>A List of Uris.</returns>
        public static List <Uri> SubstituteUri(string uri, IXenObject obj)
        {
            Util.ThrowIfParameterNull(uri, "uri");
            string ipAddressName = Enum.GetName(typeof(PropertyNames), PropertyNames.ip_address);

            try
            {
                if (!uri.Contains(string.Format(PlaceholderFormat, ipAddressName)))
                {
                    return new List <Uri> {
                               new Uri(Substitute(uri, obj))
                    }
                }
                ;

                var ips = (List <ComparableAddress>)PropertyAccessors.Get(PropertyNames.ip_address)(obj);
                if (ips == null || ips.Count == 0)
                {
                    log.DebugFormat("Object {0} (opaque_ref {1}) has no IPs.", obj.Name(), obj.opaque_ref);

                    return(new List <Uri> {
                        new Uri("about:blank")
                    });
                }

                string u = Substitute(uri, obj, s => s != ipAddressName);

                return(ips.ConvertAll(ip =>
                {
                    var ipstring = ip.AddressIP != null && ip.AddressIP.AddressFamily == AddressFamily.InterNetworkV6
                        ? string.Format("[{0}]", ip)
                        : ip.ToString();

                    return new Uri(u.Replace(string.Format(PlaceholderFormat, ipAddressName), ipstring));
                }));
            }
            catch (UriFormatException ex)
            {
                log.Warn("Failed to parse url.", ex);
                return(new List <Uri> {
                    new Uri("about:blank")
                });
            }
        }
Exemplo n.º 4
0
        public void Run()
        {
            IPHostEntry thisMachine = null;

            // Network on the GUI thread!!!
            try
            {
                thisMachine = Dns.GetHostEntry("");
            }
            catch
            {
                Assert.Fail("Couldn't resolve this machine's IP address");
            }

            PropertyAccessor ipAddress = PropertyAccessors.Get(PropertyNames.ip_address);

            foreach (IXenConnection connection in ConnectionsManager.XenConnectionsCopy)
            {
                foreach (IXenObject o in connection.Cache.XenSearchableObjects)
                {
                    // We want this to error if the cast fails
                    ComparableList <ComparableAddress> addresses = (ComparableList <ComparableAddress>)ipAddress(o);
                    if (addresses == null)
                    {
                        continue;
                    }

                    foreach (ComparableAddress address in addresses)
                    {
                        foreach (IPAddress hostAddress in thisMachine.AddressList)
                        {
                            Assert.False(address.Equals(hostAddress),
                                         String.Format("XenCenter address ({0}) appears on object '{1}'!", address, Helpers.GetName(o)));
                        }
                    }
                }
            }
        }