Class DescriptorImpl

java.lang.Object
org.glassfish.hk2.utilities.DescriptorImpl
All Implemented Interfaces:
Externalizable, Serializable, Descriptor
Direct Known Subclasses:
AbstractActiveDescriptor

public class DescriptorImpl extends Object implements Descriptor, Externalizable
The implementation of the descriptor itself, with the bonus of being externalizable, and having writeable fields
See Also:
  • Field Details

  • Constructor Details

    • DescriptorImpl

      public DescriptorImpl()
      For serialization
    • DescriptorImpl

      public DescriptorImpl(Descriptor copyMe)
      Does a deep copy of the incoming descriptor
      Parameters:
      copyMe - The descriptor to copy
    • DescriptorImpl

      public DescriptorImpl(Set<String> contracts, String name, String scope, String implementation, Map<String,List<String>> metadatas, Set<String> qualifiers, DescriptorType descriptorType, DescriptorVisibility descriptorVisibility, HK2Loader loader, int rank, Boolean proxiable, Boolean proxyForSameScope, String analysisName, Long id, Long locatorId)
      This creates this descriptor impl, taking all of the fields as given
      Parameters:
      contracts - The set of contracts this descriptor impl should advertise (should not be null)
      name - The name of this descriptor (may be null)
      scope - The scope of this descriptor. If null PerLookup is assumed
      implementation - The name of the implementation class (should not be null)
      metadatas - The metadata associated with this descriptor (should not be null)
      qualifiers - The set of qualifiers associated with this descriptor (should not be null)
      descriptorType - The type of this descriptor (should not be null)
      descriptorVisibility - The visibility this descriptor should have
      loader - The HK2Loader to associated with this descriptor (may be null)
      rank - The rank to initially associate with this descriptor
      proxiable - The proxiable value to associate with this descriptor (may be null)
      proxyForSameScope - The proxyForSameScope value to associate with this descriptor (may be null)
      analysisName - The name of the ClassAnalysis service to use
      id - The ID this descriptor should take (may be null)
      locatorId - The locator ID this descriptor should take (may be null)
  • Method Details

    • getAdvertisedContracts

      public Set<String> getAdvertisedContracts()
      Description copied from interface: Descriptor
      Returns the base class name of the contracts that this service describes. If the contract is a paramterized type this will return the raw class. If this service can be looked up by its implementation class then the name of the implementation class must also be found in this list
      Specified by:
      getAdvertisedContracts in interface Descriptor
      Returns:
      Will never return null, but may return an empty set. The returned strings are the fully qualified class names of contracts the predicate describes
    • addAdvertisedContract

      public void addAdvertisedContract(String addMe)
      Adds an advertised contract to the set of contracts advertised by this descriptor
      Parameters:
      addMe - The contract to add. May not be null
    • removeAdvertisedContract

      public boolean removeAdvertisedContract(String removeMe)
      Removes an advertised contract from the set of contracts advertised by this descriptor
      Parameters:
      removeMe - The contract to remove. May not be null
      Returns:
      true if removeMe was removed from the set
    • getImplementation

      public String getImplementation()
      Description copied from interface: Descriptor
      Returns the fully qualified class name of the implementation class. If this is a factory descriptor then this will return the fully qualified name of the class implementing the factory interface.
      Specified by:
      getImplementation in interface Descriptor
      Returns:
      Might return null in some cases, but will usually return the fully qualified class name of the implementation class or of the factory class for this descriptor
    • setImplementation

      public void setImplementation(String implementation)
      Sets the implementation
      Parameters:
      implementation - The implementation this descriptor should have
    • getScope

      public String getScope()
      Description copied from interface: Descriptor
      Returns the fully qualified class name of the scope annotation that should be associated with this descriptor.
      Specified by:
      getScope in interface Descriptor
      Returns:
      If this returns null then this descriptor is assumed to be in the default scope, which is PerLookup
    • setScope

      public void setScope(String scope)
      Sets the scope this descriptor should have
      Parameters:
      scope - The scope of this descriptor
    • getName

      public String getName()
      Description copied from interface: Descriptor
      The name of this descriptor. Note that if this returns not null then there must also be the Named qualifier in the set of qualifiers with the same value

      Specified by:
      getName in interface Descriptor
      Returns:
      The name of this descriptor, or null if there is no name associated with this qualifier
    • setName

      public void setName(String name)
      Sets the name this descriptor should have
      Parameters:
      name - The name for this descriptor
    • getQualifiers

      public Set<String> getQualifiers()
      Description copied from interface: Descriptor
      Returns all of the annotation classes that this object should be registered with or looked up by
      Specified by:
      getQualifiers in interface Descriptor
      Returns:
      Never returns null, but may return an empty set. The set of fully qualified class names that are annotations that this object must have
    • addQualifier

      public void addQualifier(String addMe)
      Adds the given string to the list of qualifiers
      Parameters:
      addMe - The fully qualified class name of the qualifier to add. May not be null
    • removeQualifier

      public boolean removeQualifier(String removeMe)
      Removes the given qualifier from the list of qualifiers
      Parameters:
      removeMe - The fully qualifier class name of the qualifier to remove. May not be null
      Returns:
      true if the given qualifier was removed
    • getDescriptorType

      public DescriptorType getDescriptorType()
      Description copied from interface: Descriptor
      Returns CLASS if this is a class descriptor and FACTORY if this is a descriptor describing a factory for a type, in which case the implClass should point to the implementation class of the factory
      Specified by:
      getDescriptorType in interface Descriptor
      Returns:
      Either CLASS or PROVIDE_METHOD
    • setDescriptorType

      public void setDescriptorType(DescriptorType descriptorType)
      Sets the descriptor type
      Parameters:
      descriptorType - The descriptor type. May not be null
    • getDescriptorVisibility

      public DescriptorVisibility getDescriptorVisibility()
      Description copied from interface: Descriptor
      Returns the visibility of this descriptor.

      If the visibility is NORMAL then this descriptor may be seen by all children locators of the locator in which this descriptor is bound

      If the visibility is LOCAL then this descriptor may only be seen by the servcie locator in which it is bound, and in none of the children of that locator

      Specified by:
      getDescriptorVisibility in interface Descriptor
      Returns:
      Either NORMAL or LOCAL
    • setDescriptorVisibility

      public void setDescriptorVisibility(DescriptorVisibility descriptorVisibility)
      Sets the descriptor visilibity
      Parameters:
      descriptorVisibility - The visibility this descriptor should have
    • getMetadata

      public Map<String,List<String>> getMetadata()
      Description copied from interface: Descriptor
      Returns all of the metadata associated that this object should be registered with or looked up by
      Specified by:
      getMetadata in interface Descriptor
      Returns:
      Never returns null, but may return an empty set. The set of metadata associated with the object. The values in the returned map will never be null, and will always have at least one member
    • setMetadata

      public void setMetadata(Map<String,List<String>> metadata)
      Sets the metadata of this DescriptorImpl to exactly the set of metadata in the incoming map. Any previous metadata values will be removed. A deep copy of the incoming map will be made, so it is safe to use the input map after use of this API
      Parameters:
      metadata - The non-null metadata that this descriptor should have
    • addMetadata

      public void addMetadata(Map<String,List<String>> metadata)
      Adds all of the entries from this map to the existing descriptor's metadata. None of the keys in the map may have the '=' character
      Parameters:
      metadata - The non-null but possibly empty list of fields to add to the metadata map
    • addMetadata

      public void addMetadata(String key, String value)
      Adds a value to the list of values associated with this key
      Parameters:
      key - The key to which to add the value. May not be null. May not contain the character '='
      value - The value to add. May not be null
    • removeMetadata

      public boolean removeMetadata(String key, String value)
      Removes the given value from the given key
      Parameters:
      key - The key of the value to remove. May not be null, and may not contain the character '='
      value - The value to remove. May not be null
      Returns:
      true if the value was removed
    • removeAllMetadata

      public boolean removeAllMetadata(String key)
      Removes all the metadata values associated with key
      Parameters:
      key - The key of the metadata values to remove
      Returns:
      true if any value was removed
    • clearMetadata

      public void clearMetadata()
      Removes all metadata values
    • getLoader

      public HK2Loader getLoader()
      Description copied from interface: Descriptor
      This returns the loader that should be used when classloading this descriptor. If this method returns null then the class will be loaded with the classloader of the Injectee if there is one, or with the classloader that loaded HK2 itself; failing that the context class loader on the thread will be tried.
      Specified by:
      getLoader in interface Descriptor
      Returns:
      An HK2Loader that can be used to load this descriptor, or null if the default classloading algorithm should be used
    • setLoader

      public void setLoader(HK2Loader loader)
      Sets the loader to use with this descriptor
      Parameters:
      loader - The loader to use with this descriptor
    • getRanking

      public int getRanking()
      Description copied from interface: Descriptor
      Returns the ranking of this descriptor. Rankings with higher value will be considered "better" than rankings of lower value. Descriptors with the same ranking will be returned in the reverse ServiceID order (in other words, the least service ID is considered "better" than any higher service ID).
      Specified by:
      getRanking in interface Descriptor
      Returns:
      the ranking that should be associated with this descriptor
    • setRanking

      public int setRanking(int ranking)
      Description copied from interface: Descriptor
      Returns the ranking of this descriptor. Rankings with higher value will be considered "better" than rankings of lower value. Descriptors with the same ranking will be returned in the reverse ServiceID order (in other words, the least service ID is considered "better" than any higher service ID).

      The ranking of a service may change at any time during the life of the descriptor

      Specified by:
      setRanking in interface Descriptor
      Parameters:
      ranking - The new ranking this descriptor should have
      Returns:
      the previous ranking that this descriptor had
    • getServiceId

      public Long getServiceId()
      Description copied from interface: Descriptor
      This returns the unique identifier for this descriptor. This field will be ignored upon binding, and will then be assigned by the system. However, this field can be set on search operations, in which case this search will match exactly one Provider in the system.
      Specified by:
      getServiceId in interface Descriptor
      Returns:
      The service id for this object. Note that this field may return null if this is an initial bind, as it is not the responsibility of the binder to set this value. If this returns non-null on a bind operation the return value will be ignored
    • setServiceId

      public void setServiceId(Long id)
      Sets the service id for this descriptor
      Parameters:
      id - the service id for this descriptor
    • isProxiable

      public Boolean isProxiable()
      Description copied from interface: Descriptor
      If this returns true then the system will create a proxy for instances of this descriptor. As with all proxies, the proxy created will also implement the ProxyCtl interface

      It is an error for this method to return true if the scope that this descriptor is in is Unproxiable (such as PerLookup).

      Specified by:
      isProxiable in interface Descriptor
      Returns:
      true if this descriptor must be proxied, false if this descriptor must NOT be proxied (even if it is in an Unproxiable scope) and null if this descriptor should take its proxiable status from the scope it is in (i.e., it will only be proxied if the scope is marked Proxiable)
    • setProxiable

      public void setProxiable(Boolean proxiable)
      Sets whether or not this descriptor should be proxied
      Parameters:
      proxiable - if true then this descriptor will be proxied. If false then this descriptor will not be proxied. If null this descriptor will follow the rules of the scope it is in
    • isProxyForSameScope

      public Boolean isProxyForSameScope()
      Description copied from interface: Descriptor
      This value determines whether or not this service should be proxied when being injected into other services of the same scope. If a scope is proxiable then it will have a default setting for whether or not its services should be proxied when being injected into the same scope. If this method returns null then the default setting for the scope will be used. Otherwise the value returned will determine whether or not this service will be proxied when being injected into a service of the same scope.
      Specified by:
      isProxyForSameScope in interface Descriptor
      Returns:
      null if this descriptor should use the default ProxyForSameScope value for the scope. If it returns true then this service will be proxied even when being injected into the same scope. If it returns false then this service will NOT be proxied when being injected into the same scope (i.e., it cannot be used for lazy initialization of the service when injected into the same scope)
    • setProxyForSameScope

      public void setProxyForSameScope(Boolean proxyForSameScope)
      Sets whether or not to proxy this descriptor for other services in the same scope
      Parameters:
      proxyForSameScope - if true then this descriptor will be proxied for services in the same scope. If false then this descriptor will not be proxied for services in the same scope. If null this descriptor will follow the rules of the scope it is in
    • getClassAnalysisName

      public String getClassAnalysisName()
      Description copied from interface: Descriptor
      Returns the name of the ClassAnalyzer service that should be used to discover the constructors, initialization methods, field and postConstruct and preDestory methods. If null the default implementation will be used. Will be ignored for descriptors that are not automatically analyzed by hk2
      Specified by:
      getClassAnalysisName in interface Descriptor
      Returns:
      the possibly null name of the ClassAnalysis service that should be used to analyze the class represented by this descriptor. If null then the HK2 default analysis will be performed
    • setClassAnalysisName

      public void setClassAnalysisName(String name)
      Sets the name of the service that will be used to analyze this class
      Parameters:
      name - The name of the ClassAnalyzer service that should be used to analyze this descriptor
    • getLocatorId

      public Long getLocatorId()
      Description copied from interface: Descriptor
      This returns the id of the ServiceLocator which this descriptor is registered with. Returns null if this descriptor is not yet registered with a ServiceLocator
      Specified by:
      getLocatorId in interface Descriptor
      Returns:
      The id of the ServiceLocator this Descriptor is registered with, or null if this Descriptor is not registered with a ServiceLocator
    • setLocatorId

      public void setLocatorId(Long locatorId)
      Sets the locator id for this descriptor
      Parameters:
      locatorId - the locator id for this descriptor
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equalOrderedCollection

      private static <T> boolean equalOrderedCollection(Collection<T> a, Collection<T> b)
    • equalMetadata

      private static <T> boolean equalMetadata(Map<String,List<String>> a, Map<String,List<String>> b)
    • descriptorEquals

      public static boolean descriptorEquals(Descriptor a, Descriptor b)
      Tests if two descriptors are equal not taking into account the locator-id and server-id by comparing the following fields:
      • implementation
      • contracts
      • name
      • scope
      • qualifiers
      • descriptorType
      • descriptorVisibility
      • metadata
      • proxiable
      • proxyForSameScope
      • analysisName
      Parameters:
      a - The possibly null descriptor to compare
      b - The possibly null descriptor to compare
      Returns:
      true if they are the same, false otherwise
    • equals

      public boolean equals(Object a)
      This equals matches only if the following fields of the descriptor match:
      • implementation
      • contracts
      • name
      • scope
      • qualifiers
      • descriptorType
      • descriptorVisibility
      • metadata
      • proxiable
      • proxyForSameScope
      • analysisName
      Overrides:
      equals in class Object
      Parameters:
      a - The object to compare to this one. May be null (which will result in a false)
      Returns:
      true if the descriptors are equal
    • pretty

      public static void pretty(StringBuffer sb, Descriptor d)
      Will pretty print a descriptor
      Parameters:
      sb - The string buffer put the pretty print into
      d - The descriptor to write
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • writeObject

      public void writeObject(PrintWriter out) throws IOException
      This writes this object to the data output stream in a human-readable format, excellent for writing out data files
      Parameters:
      out - The output stream to write this object out to
      Throws:
      IOException - on failure
    • reinitialize

      private void reinitialize()
    • readObject

      public boolean readObject(BufferedReader in) throws IOException
      This can be used to read in instances of this object that were previously written out with writeObject. Useful for reading from external data files
      Parameters:
      in - The reader to read from
      Returns:
      true if a descriptor was read, false otherwise. This is useful if reading a file that might have comments at the end
      Throws:
      IOException - on failure
    • writeExternal

      public void writeExternal(ObjectOutput out) throws IOException
      Specified by:
      writeExternal in interface Externalizable
      Throws:
      IOException
    • readExternal

      public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
      Specified by:
      readExternal in interface Externalizable
      Throws:
      IOException
      ClassNotFoundException