Ejemplo n.º 1
0
        /// <summary>
        /// 比较当前服务注册对象和用户指定的服务注册对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            ServiceRegistrationImpl other = (ServiceRegistrationImpl)obj;

            int thisRanking  = this.Ranking;
            int otherRanking = other.Ranking;

            if (thisRanking != otherRanking)
            {
                if (thisRanking < otherRanking)
                {
                    return(1);
                }
                return(-1);
            }
            long thisId  = this.Id;
            long otherId = other.Id;

            if (thisId == otherId)
            {
                return(0);
            }
            if (thisId < otherId)
            {
                return(-1);
            }
            return(1);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 删除服务注册
        /// </summary>
        /// <param name="context"></param>
        /// <param name="registration"></param>
        internal void RemoveServiceRegistration(BundleContextImpl context, ServiceRegistrationImpl registration)
        {
            // Remove the ServiceRegistrationImpl from the list of Services published by BundleContextImpl.
            IList contextServices = (IList)publishedServicesByContext[context];

            if (contextServices != null)
            {
                contextServices.Remove(registration);
            }

            // Remove the ServiceRegistrationImpl from the list of Services published by Class Name.
            String[] clazzes = registration.GetClasses();
            for (int i = 0, size = clazzes.Length; i < size; i++)
            {
                String clazz    = clazzes[i];
                IList  services = (IList)publishedServicesByClass[clazz];
                services.Remove(registration);
                if (services.Count == 0)
                { // remove empty list
                    publishedServicesByClass.Remove(clazz);
                }
            }

            // Remove the ServiceRegistrationImpl from the list of all published Services.
            allPublishedServices.Remove(registration);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 修改服务注册
        /// </summary>
        /// <param name="context"></param>
        /// <param name="registration"></param>
        internal void ModifyServiceRegistration(BundleContextImpl context, ServiceRegistrationImpl registration)
        {
            // The list of Services published by BundleContextImpl is not sorted, so
            // we do not need to modify it.

            // Remove the ServiceRegistrationImpl from the list of Services published by Class Name
            // and then add at the correct index.
            String[] clazzes = registration.GetClasses();
            int      insertIndex;

            for (int i = 0, size = clazzes.Length; i < size; i++)
            {
                String    clazz    = clazzes[i];
                ArrayList services = (ArrayList)publishedServicesByClass[clazz];
                services.Remove(registration);
                // The list is sorted, so we must find the proper location to insert
                insertIndex = -services.BinarySearch(registration) - 1;
                services.Insert(insertIndex, registration);
            }

            // Remove the ServiceRegistrationImpl from the list of all published Services
            // and then add at the correct index.
            allPublishedServices.Remove(registration);
            // The list is sorted, so we must find the proper location to insert
            insertIndex = -allPublishedServices.BinarySearch(registration) - 1;
            allPublishedServices.Insert(insertIndex, registration);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 注销服务
        /// </summary>
        /// <param name="context"></param>
        public void UnregisterServices(BundleContextImpl context)
        {
            IList registrations = LookupServiceRegistrations(context);

            if (registrations == null)
            {
                return;
            }
            var iter = registrations.GetEnumerator();

            while (iter.MoveNext())
            {
                ServiceRegistrationImpl registration = (ServiceRegistrationImpl)iter.Current;
                try
                {
                    registration.Unregister();
                }
                catch (Exception e)
                {
                    Log.Debug(e);
                    /* already unregistered */
                }
            }
            RemoveServiceRegistrations(context); // remove empty list
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 根据过滤条件和接口契约查找服务注册者
        /// </summary>
        /// <param name="clazz"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        private IList LookupServiceRegistrations(string clazz, IFilter filter)
        {
            IList result;

            lock (this)
            {
                if (clazz == null)
                { /* all services */
                    result = allPublishedServices;
                }
                else
                {
                    /* services registered under the class name */
                    result = (IList)publishedServicesByClass[clazz];
                }

                if ((result == null) || (result.Count == 0))
                {
                    return(null);
                }

                result = new ArrayList(result); /* make a new list since we don't want to change the real list */
            }

            if (filter == null)
            {
                return(result);
            }

            IList listWaitForDeleting = new ArrayList();
            var   iter = result.GetEnumerator();

            while (iter.MoveNext())
            {
                ServiceRegistrationImpl registration = (ServiceRegistrationImpl)iter.Current;
                ServiceReferenceImpl    reference;
                try
                {
                    reference = registration.GetReferenceImpl();
                }
                catch (Exception e)
                {
                    Log.Warn(e);
                    listWaitForDeleting.Add(iter.Current); /* service was unregistered after we left the synchronized block above */
                    continue;
                }
                if (!filter.Match(reference))
                {
                    listWaitForDeleting.Add(iter.Current);
                }
            }

            foreach (var item in listWaitForDeleting)
            {
                result.Remove(item);
            }

            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 注册服务
        /// </summary>
        /// <param name="context"></param>
        /// <param name="clazzes"></param>
        /// <param name="service"></param>
        /// <param name="property"></param>
        /// <returns></returns>
        public ServiceRegistrationImpl RegisterService(BundleContextImpl context, string[] clazzes, object service, IDictionary <string, object> properties)
        {
            if (service == null)
            {
                throw new ArgumentException("service");
            }

            int size = clazzes.Length;

            if (size == 0)
            {
                throw new ArgumentException("size");
            }

            /* copy the array so that changes to the original will not affect us. */
            IList copy = new ArrayList(size);

            // intern the strings and remove duplicates
            for (int i = 0; i < size; i++)
            {
                string clazz = string.Intern(clazzes[i]);
                if (!copy.Contains(clazz))
                {
                    copy.Add(clazz);
                }
            }

            clazzes = new string[copy.Count];
            copy.CopyTo(clazzes, 0);


            if (!(service is IServiceFactory))
            {
                //string invalidService = checkServiceClass(clazzes, service);
                //if (string.IsNullOrEmpty(invalidService))
                //{
                //    throw new ArgumentException("invalidService");
                //}
            }

            var registration = new ServiceRegistrationImpl(this, context, clazzes, service);

            registration.Register(properties);

            return(registration);
        }
Ejemplo n.º 7
0
        public ServiceUse(BundleContextImpl context, ServiceRegistrationImpl registration)
        {
            this.useCount = 0;
            Object service = registration.ServiceObject;

            if (service is IServiceFactory)
            {
                this.factory       = (IServiceFactory)service;
                this.cachedService = null;
            }
            else
            {
                this.factory       = null;
                this.cachedService = service;
            }
            this.context      = context;
            this.registration = registration;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 添加服务注册
        /// </summary>
        /// <param name="context"></param>
        /// <param name="registration"></param>
        internal void AddServiceRegistration(BundleContextImpl context, ServiceRegistrationImpl registration)
        {
            // Add the ServiceRegistrationImpl to the list of Services published by BundleContextImpl.
            ArrayList contextServices = (ArrayList)publishedServicesByContext[context];

            if (contextServices == null)
            {
                contextServices = new ArrayList(initialSubCapacity);
                publishedServicesByContext.Add(context, contextServices);
            }
            // The list is NOT sorted, so we just add
            contextServices.Add(registration);

            // Add the ServiceRegistrationImpl to the list of Services published by Class Name.
            String[] clazzes = registration.GetClasses();
            int      insertIndex;

            for (int i = 0, size = clazzes.Length; i < size; i++)
            {
                String clazz = clazzes[i];

                ArrayList services = (ArrayList)publishedServicesByClass[clazz];

                if (services == null)
                {
                    services = new ArrayList(initialSubCapacity);
                    publishedServicesByClass.Add(clazz, services);
                }

                // The list is sorted, so we must find the proper location to insert
                insertIndex = -services.BinarySearch(registration) - 1;
                services.Insert(insertIndex, registration);
            }

            // Add the ServiceRegistrationImpl to the list of all published Services.
            // The list is sorted, so we must find the proper location to insert
            insertIndex = -allPublishedServices.BinarySearch(registration) - 1;
            allPublishedServices.Insert(insertIndex, registration);
        }
Ejemplo n.º 9
0
 public ServiceReferenceImpl(ServiceRegistrationImpl registration)
 {
     this.registration = registration;
 }
Ejemplo n.º 10
0
        /// <summary>
        /// 根据服务引用释放服务
        /// </summary>
        /// <param name="context"></param>
        /// <param name="reference"></param>
        /// <returns></returns>
        public bool UngetService(BundleContextImpl context, ServiceReferenceImpl reference)
        {
            ServiceRegistrationImpl registration = reference.Registration;

            return(registration.UngetService(context));
        }