/// <summary> /// 注销服务 /// </summary> public void Unregister() { ServiceReferenceImpl reference; lock (registry) { lock (registrationLock) { if (state != REGISTERED) { /* in the process of unregisterING */ throw new IllegalStateException("The state is invalidate."); } registry.RemoveServiceRegistration(context, this); state = UNREGISTERING; /* mark unregisterING */ reference = this.reference; /* used to publish event outside sync */ } } /* must not hold the registrationLock when this event is published */ /// registry.publishServiceEvent(new ServiceEvent(ServiceEvent.UNREGISTERING, reference)); int size = 0; BundleContextImpl[] users = null; lock (registrationLock) { /* we have published the ServiceEvent, now mark the service fully unregistered */ state = UNREGISTERED; size = contextsUsing.Count; if (size > 0) { users = new BundleContextImpl[size]; contextsUsing.CopyTo(users, 0); } } /* must not hold the registrationLock while releasing services */ for (int i = 0; i < size; i++) { ReleaseService(users[i]); } lock (registrationLock) { contextsUsing.Clear(); reference = null; /* mark registration dead */ } /* The property field must remain valid after unregister completes. */ }
/// <summary> /// 检查是否可以分配给指定的服务引用 /// </summary> /// <param name="context"></param> /// <param name="reference"></param> /// <returns></returns> internal static bool IsAssignableTo(BundleContextImpl context, ServiceReferenceImpl reference) { AbstractBundle bundle = context.GetBundleImpl(); string[] clazzes = reference.GetClasses(); for (int i = 0, len = clazzes.Length; i < len; i++) { if (!reference.IsAssignableTo(bundle, clazzes[i])) { return(false); } } return(true); }
/// <summary> /// 完成注册操作 /// </summary> /// <param name="props"></param> internal void Register(IDictionary <string, object> props) { ServiceReferenceImpl reference; lock (registry) { lock (registrationLock) { reference = this.reference; /* used to publish event outside sync */ this.properties = CreateProperties(props); /* must be valid after unregister is called. */ } registry.AddServiceRegistration(context, this); } }
/// <summary> /// 检查当前对象是否与指定的服务引用对象相等 /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(object obj) { if (obj == this) { return(true); } if (!(obj is ServiceReferenceImpl)) { return(false); } ServiceReferenceImpl other = (ServiceReferenceImpl)obj; return(registration == other.registration); }
/// <summary> /// 更新服务相关的属性 /// </summary> /// <param name="props"></param> public void SetProperties(IDictionary <string, object> props) { ServiceReferenceImpl reference; ServiceProperties previousProperties; lock (registry) { lock (registrationLock) { if (state != REGISTERED) { /* in the process of unregisterING */ throw new IllegalStateException("The service has registered."); } reference = this.reference; /* used to publish event outside sync */ previousProperties = this.properties; this.properties = CreateProperties(props); } registry.ModifyServiceRegistration(context, this); } }
/// <summary> /// 获取所有的服务引用 /// </summary> /// <param name="context"></param> /// <returns></returns> public ServiceReferenceImpl[] GetRegisteredServices(BundleContextImpl context) { IList references = ChangeRegistrationsToReferences(LookupServiceRegistrations(context)); if (references == null) { return(null); } int size = references.Count; if (size == 0) { return(null); } var newReferences = new ServiceReferenceImpl[size]; references.CopyTo(newReferences, 0); return(newReferences); }
/// <summary> /// 根据过滤条件获取服务引用 /// </summary> /// <param name="context"></param> /// <param name="clazz"></param> /// <param name="filterstring"></param> /// <param name="allservices"></param> /// <returns></returns> public ServiceReferenceImpl[] GetServiceReferences(BundleContextImpl context, string clazz, string filterstring, bool allservices) { IFilter filter = (filterstring == null) ? null : context.createFilter(filterstring); ArrayList references = ChangeRegistrationsToReferences(LookupServiceRegistrations(clazz, filter)) as ArrayList; if (references == null) { return(null); } IList listWaitForDeleting = new ArrayList(); var iter = references.GetEnumerator(); while (iter.MoveNext()) { ServiceReferenceImpl reference = (ServiceReferenceImpl)iter.Current; if (!allservices && !IsAssignableTo(context, reference)) { listWaitForDeleting.Add(iter.Current); } } foreach (var item in listWaitForDeleting) { references.Remove(item); } int size = references.Count; if (size == 0) { return(null); } var newReferences = new ServiceReferenceImpl[size]; references.CopyTo(newReferences); return(newReferences); }
internal ServiceRegistrationImpl(ServiceRegistry registry, BundleContextImpl context, String[] clazzes, Object service) { this.registry = registry; this.context = context; this.bundle = context.GetBundleImpl(); this.framework = context.Framework; this.clazzes = clazzes; /* must be set before calling createProperties. */ this.service = service; this.serviceid = registry.GetNextServiceId(); /* must be set before calling createProperties. */ this.contextsUsing = new ArrayList(10); lock (registrationLock) { this.state = REGISTERED; /* We leak this from the constructor here, but it is ok * because the ServiceReferenceImpl constructor only * stores the value in a final field without * otherwise using it. */ this.reference = new ServiceReferenceImpl(this); } }
/// <summary> /// 获取已被使用的所有的服务引用 /// </summary> /// <param name="context"></param> /// <returns></returns> public ServiceReferenceImpl[] GetServicesInUse(BundleContextImpl context) { Hashtable servicesInUse = context.GetServicesInUseMap(); if (servicesInUse == null) { return(null); } IList references; lock (servicesInUse) { if (servicesInUse.Count == 0) { return(null); } references = ChangeRegistrationsToReferences(new ArrayList(servicesInUse.Keys)); } if (references != null) { int size = references.Count; if (size == 0) { return(null); } ServiceReferenceImpl[] newArray = new ServiceReferenceImpl[size]; references.CopyTo(newArray, 0); return(newArray); } return(null); }
/// <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)); }
/// <summary> /// 根据服务引用获取服务对象 /// </summary> /// <param name="context"></param> /// <param name="reference"></param> /// <returns></returns> public object GetService(BundleContextImpl context, ServiceReferenceImpl reference) { return(reference.Registration.GetService(context)); }