Class ExtendableServiceLocator

    • Constructor Detail

      • ExtendableServiceLocator

        public ExtendableServiceLocator()
    • Method Detail

      • setAccessor

        @Autowired
        public void setAccessor​(@NotNull
                                ExtensionAccessor accessor)
      • registerExtension

        public <T extends TeamCityExtension> void registerExtension​(@NotNull
                                                                    Class<T> extensionClass,
                                                                    @NotNull
                                                                    String sourceId,
                                                                    @NotNull
                                                                    T extension)
        Registers an extension of the specified type
        Specified by:
        registerExtension in interface ExtensionHolder
        Parameters:
        extensionClass - extension type
        sourceId - unique id of the extension among other extensions of the same type
        extension - extension
      • unregisterExtension

        public <T extends TeamCityExtension> void unregisterExtension​(@NotNull
                                                                      Class<T> extensionClass,
                                                                      @NotNull
                                                                      String sourceId)
        Unregisters extension.
        Specified by:
        unregisterExtension in interface ExtensionHolder
        Parameters:
        extensionClass - type of the extension
        sourceId - id of the extension
      • getExtensions

        @NotNull
        public <T extends TeamCityExtensionCollection<T> getExtensions​(@NotNull
                                                                         Class<T> extensionClass)
        Returns collection of extensions of the specified type. Extensions that implement PositionAware interface are sorted accordingly.
        Specified by:
        getExtensions in interface ExtensionsProvider
        Parameters:
        extensionClass - extension type
        Returns:
        extensions
      • getExtensionsCollection

        @NotNull
        public <T extends TeamCityExtensionExtensionsCollection<T> getExtensionsCollection​(@NotNull
                                                                                             Class<T> extensionClass)
        Description copied from interface: ExtensionsProvider
        Live collection of extensions. This collection can be stored in a field of some class. It is updated automatically if a new extension of a given class is registered.
        Specified by:
        getExtensionsCollection in interface ExtensionsProvider
        Parameters:
        extensionClass - extension class
        Returns:
        collection of extension
      • getStampedExtensionsSupplier

        @NotNull
        public <T extends TeamCityExtension,​CONTEXT,​TARGET> StampedExtensionsSupplier<CONTEXT,​TARGET> getStampedExtensionsSupplier​(@NotNull
                                                                                                                                                     Class<T> extensionClass,
                                                                                                                                                     @NotNull
                                                                                                                                                     Function<StampedExtensionsSupplier.StoredData<CONTEXT,​Collection<T>>,​TARGET> converter)
        Description copied from interface: ExtensionsProvider
        Live suppler of extensions. This supplier can be safely stored in a field of some class. It is updated automatically if a new extension of a given class is registered or if outer context was changed. For example this supplier is very useful if some internal property is used to select a single extension implementation which should be used:
         StampedExtensionsSupplier<String, MyExtension> supplier = extensionsProvider.getStampedExtensionsSupplier(MyExtension.class, extensions -> {
           for (MyExtension ext : extensions.data) {
             if (ext.getClass().getName().equals(extensions.context)) return ext;
           }
        
           return new DefaultMyExtension();
         });
        
         MyExtension currentlyUsedExtension = supplier.get(TeamCityProperties.getProperty("myextension.implementationClass"));
         
        here with each change of the "myextension.implementationClass" property the supplier will reset the internal cache and will call the provided function with the registered extensions collection and with the current context (the parameter value in this example). The internal cache is also reset automatically if the registered extensions collection changes. Without the getStampedExtensionsSupplier method, same code could be implemented with getExtensionsCollection(), but will require filtering the collection each time:
         ExtensionsCollection<MyExtension> allExtensions = extensionsProvider.getExtensionsCollection(MyExtension.class);
         MyExtension currentlyUsedExtension = null;
        
         for (MyExtension ext : allExtensions.getExtensions()) {
           if (ext.getClass().getName().equals(TeamCityProperties.getProperty("myextension.implementationClass"))) {
             currentlyUsedExtension = ext;
             break;
           }
         }
        
         if (currentlyUsedExtension == null) currentlyUsedExtension = new DefaultMyExtension()
         
        Specified by:
        getStampedExtensionsSupplier in interface ExtensionsProvider
        Parameters:
        extensionClass - extension class
        converter - function to convert the base extension. Will be automatically applied if underlying collection should be reset (eg if a new extension is registered).
        Returns:
        collection of extension
      • getExtension

        @Nullable
        public <T extends TeamCityExtension> T getExtension​(@NotNull
                                                            Class<T> extensionClass,
                                                            @NotNull
                                                            String sourceId)
        Retrieves extension registered with specified type and id
        Specified by:
        getExtension in interface ExtensionsProvider
        Parameters:
        extensionClass - extension type
        sourceId - id
        Returns:
        extension or null