Exemplo n.º 1
0
        /// <summary>
        /// 获取服务引用
        /// </summary>
        /// <param name="clazz">类</param>
        /// <param name="filterString">过滤字符串</param>
        /// <param name="context">组件</param>
        /// <param name="allservices">是否所有服务</param>
        /// <returns>IServiceReference[]</returns>
        public IServiceReference[] GetServiceReferences(string clazz, string filterString, IBundleContext context, bool allservices)
        {
            Filter filter = string.IsNullOrEmpty(filterString) ? null : new Filter(filterString);

            IServiceReference[] services = null;

            lock (_serviceRegistry)
            {
                services = _serviceRegistry.LookupServiceReferences(clazz, filter);
                if (services == null)
                {
                    return(null);
                }
                int removed = 0;
                for (int i = services.Length - 1; i >= 0; i--)
                {
                    ServiceReference reference = (ServiceReference)services[i];
                    string[]         classes   = reference.GetClasses();
                    if (allservices || context.IsAssignableTo((ServiceReference)services[i]))
                    {
                        if (clazz == null)
                        {
                            try
                            { /* test for permission to the classes */
                              //checkGetServicePermission(classes);
                            }
                            catch (SecurityException)
                            {
                                services[i] = null;
                                removed++;
                            }
                        }
                    }
                    else
                    {
                        services[i] = null;
                        removed++;
                    }
                }
                if (removed > 0)
                {
                    IServiceReference[] temp = services;
                    services = new ServiceReference[temp.Length - removed];
                    for (int i = temp.Length - 1; i >= 0; i--)
                    {
                        if (temp[i] == null)
                        {
                            removed--;
                        }
                        else
                        {
                            services[i - removed] = temp[i];
                        }
                    }
                }
            }
            return(services.Length == 0 ? null : services);
        }
Exemplo n.º 2
0
        public IServiceReference[] GetServiceReferences(string clazz, string filterString,
            IBundleContext context, bool allservices)
        {
            Filter filter = string.IsNullOrEmpty(filterString) ? null : new Filter(filterString);
            IServiceReference[] services = null;

            lock (serviceRegistry)
            {
                services = serviceRegistry.LookupServiceReferences(clazz, filter);
                if (services == null)
                {
                    return null;
                }
                int removed = 0;
                for (int i = services.Length - 1; i >= 0; i--)
                {
                    ServiceReference reference = (ServiceReference)services[i];
                    string[] classes = reference.GetClasses();
                    if (allservices || context.IsAssignableTo((ServiceReference)services[i]))
                    {
                        if (clazz == null)
                            try
                            { /* test for permission to the classes */
                                //checkGetServicePermission(classes);
                            }
                            catch (SecurityException)
                            {
                                services[i] = null;
                                removed++;
                            }
                    }
                    else
                    {
                        services[i] = null;
                        removed++;
                    }
                }
                if (removed > 0)
                {
                    IServiceReference[] temp = services;
                    services = new ServiceReference[temp.Length - removed];
                    for (int i = temp.Length - 1; i >= 0; i--)
                    {
                        if (temp[i] == null)
                            removed--;
                        else
                            services[i - removed] = temp[i];
                    }
                }

            }
            return services == null || services.Length == 0 ? null : services;
        }