Java Code Examples for jenkins.model.Jenkins#getExtensionList()

The following examples show how to use jenkins.model.Jenkins#getExtensionList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RootElementConfigurator.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
static List<RootElementConfigurator> all() {
    final Jenkins jenkins = Jenkins.get();
    List<RootElementConfigurator> configurators = new ArrayList<>(
        jenkins.getExtensionList(RootElementConfigurator.class));

    for (GlobalConfigurationCategory category : GlobalConfigurationCategory.all()) {
        configurators.add(new GlobalConfigurationCategoryConfigurator(category));
    }

    for (ManagementLink link : ManagementLink.all()) {
        final String name = link.getUrlName();
        final Descriptor descriptor = Jenkins.get().getDescriptor(name);
        if (descriptor != null)
            configurators.add(new DescriptorConfigurator(descriptor));
    }

    configurators.sort(Configurator.extensionOrdinalSort());

    return configurators;
}
 
Example 2
Source File: DockerImageExtractor.java    From docker-commons-plugin with MIT License 5 votes vote down vote up
/**
 * Provides a set of repository names {@code namespace/name} that the job uses as seen by all the declared {@link DockerImageExtractor}s.
 * Returns an empty set if none is found.
 *
 * @param job the job being queried.
 * @return a set of names, or an empty set.
 */
@Nonnull
public static Set<String> getDockerImagesUsedByJobFromAll(@Nonnull Job<?,?> job) {
    Jenkins j = Jenkins.getInstance();
    Set<String> names = new TreeSet<String>();
    if (j != null) {
        //TODO use ExtensionList.lookup(DockerImageExtractor.class); when core req is past 1.572 to not have to check for Jenkins instance
        for (DockerImageExtractor extractor : j.getExtensionList(DockerImageExtractor.class)) {
            names.addAll(extractor.getDockerImagesUsedByJob(job));
        }
    }
    return names;
}
 
Example 3
Source File: DockerTraceabilityReportListener.java    From docker-traceability-plugin with MIT License 5 votes vote down vote up
/**
 * Retrieves a list of Docker event listeners.
 * @return A list of all {@link DockerTraceabilityReportListener} extensions.
 */
public static @Nonnull ExtensionList<DockerTraceabilityReportListener> all() {
    final Jenkins j = Jenkins.getInstance();
    if (j == null) {
        return ExtensionList.create((Jenkins) null, DockerTraceabilityReportListener.class);
    }
    return j.getExtensionList(DockerTraceabilityReportListener.class);
}
 
Example 4
Source File: DefaultConfiguratorRegistry.java    From configuration-as-code-plugin with MIT License 4 votes vote down vote up
private Configurator internalLookup(Type type) {
    Class clazz = Types.erasure(type);

    final Jenkins jenkins = Jenkins.get();
    final ExtensionList<Configurator> l = jenkins.getExtensionList(Configurator.class);
    for (Configurator c : l) {
        if (c.canConfigure(clazz)) {
            // this type has a dedicated Configurator implementation
            return c;
        }
    }

    //TODO: Only try to cast if we can actually get the parameterized type
    if (Collection.class.isAssignableFrom(clazz) && type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        Type actualType = pt.getActualTypeArguments()[0];
        if (actualType instanceof WildcardType) {
            actualType = ((WildcardType) actualType).getUpperBounds()[0];
        }
        if (actualType instanceof ParameterizedType) {
            actualType = ((ParameterizedType) actualType).getRawType();
        }
        if (!(actualType instanceof Class)) {
            throw new IllegalStateException("Can't handle " + type);
        }
        return lookup(actualType);
    }

    if (Configurable.class.isAssignableFrom(clazz)) {
        return new ConfigurableConfigurator(clazz);
    }

    if (Descriptor.class.isAssignableFrom(clazz)) {
        ExtensionList extensions = jenkins.getExtensionList(clazz);
        if (!extensions.isEmpty()) {
            return new DescriptorConfigurator((Descriptor) extensions.get(0));
        }
    }

    if (DataBoundConfigurator.getDataBoundConstructor(clazz) != null) {
        return new DataBoundConfigurator(clazz);
    }

    if (Modifier.isAbstract(clazz.getModifiers()) && Describable.class.isAssignableFrom(clazz)) {
        // this is a jenkins Describable component, with various implementations
        return new HeteroDescribableConfigurator(clazz);
    }

    if (Extension.class.isAssignableFrom(clazz)) {
        return new ExtensionConfigurator(clazz);
    }

    if (Stapler.lookupConverter(clazz) != null) {
        return new PrimitiveConfigurator(clazz);
    }

    if (clazz.isEnum()) {
        return new EnumConfigurator(clazz);
    }

    LOGGER.warning("Configuration-as-Code can't handle type "+ type);
    return null;
}
 
Example 5
Source File: VaultHelper.java    From hashicorp-vault-plugin with MIT License 4 votes vote down vote up
@Override
public String call() throws IOException {
    Jenkins jenkins = Jenkins.get();

    String msg = String.format(
        "Retrieving vault secret path=%s key=%s engineVersion=%s",
        secretPath, secretKey, engineVersion);
    LOGGER.info(msg);

    GlobalVaultConfiguration globalConfig = GlobalConfiguration.all()
        .get(GlobalVaultConfiguration.class);

    if (globalConfig == null) {
        throw new IllegalStateException("Vault plugin has not been configured.");
    }

    ExtensionList<VaultBuildWrapper.DescriptorImpl> extensionList = jenkins
        .getExtensionList(VaultBuildWrapper.DescriptorImpl.class);
    VaultBuildWrapper.DescriptorImpl descriptor = extensionList.get(0);

    VaultConfiguration configuration = globalConfig.getConfiguration();

    if (descriptor == null || configuration == null) {
        throw new IllegalStateException("Vault plugin has not been configured.");
    }

    try {
        SslConfig sslConfig = new SslConfig()
            .verify(configuration.isSkipSslVerification())
            .build();

        VaultConfig vaultConfig = new VaultConfig()
            .address(configuration.getVaultUrl())
            .sslConfig(sslConfig)
            .engineVersion(engineVersion);

        if (isNotEmpty(configuration.getVaultNamespace())) {
            vaultConfig.nameSpace(configuration.getVaultNamespace());
        }

        if (isNotEmpty(configuration.getPrefixPath())) {
            vaultConfig.prefixPath(configuration.getPrefixPath());
        }

        VaultCredential vaultCredential = configuration.getVaultCredential();
        if (vaultCredential == null) vaultCredential = retrieveVaultCredentials(configuration.getVaultCredentialId());

        VaultAccessor vaultAccessor = new VaultAccessor(vaultConfig, vaultCredential);
        vaultAccessor.setMaxRetries(configuration.getMaxRetries());
        vaultAccessor.setRetryIntervalMilliseconds(configuration.getRetryIntervalMilliseconds());
        vaultAccessor.init();

        Map<String, String> values = vaultAccessor.read(secretPath, engineVersion).getData();

        if (!values.containsKey(secretKey)) {
            String message = String.format(
                "Key %s could not be found in path %s",
                secretKey, secretPath);
            throw new VaultPluginException(message);
        }

        return values.get(secretKey);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}