Example usage for org.eclipse.jgit.lib Config getEnum

List of usage examples for org.eclipse.jgit.lib Config getEnum

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Config getEnum.

Prototype

public <T extends Enum<?>> T getEnum(final String section, final String subsection, final String name,
        final T defaultValue) 

Source Link

Document

Parse an enumeration from the configuration.

Usage

From source file:com.diffplug.gradle.spotless.GitAttributesLineEndingPolicy.java

License:Apache License

private GitAttributesLineEndingPolicy(Config config, List<AttributesRule> infoRules, File workTree,
        List<AttributesRule> globalRules) {
    this.infoRules = Objects.requireNonNull(infoRules);
    this.workTree = workTree;
    this.globalRules = Objects.requireNonNull(globalRules);
    this.defaultEnding = fromEol(config.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
            ConfigConstants.CONFIG_KEY_EOL, EOL.NATIVE)).str();
}

From source file:com.google.gerrit.httpd.rpc.SuggestServiceImpl.java

License:Apache License

@Inject
SuggestServiceImpl(final Provider<ReviewDb> schema, final AuthConfig authConfig,
        final ProjectControl.Factory projectControlFactory, final ProjectCache projectCache,
        final AccountCache accountCache, final GroupControl.Factory groupControlFactory,
        final IdentifiedUser.GenericFactory userFactory, final Provider<CurrentUser> currentUser,
        @GerritServerConfig final Config cfg, final GroupCache groupCache) {
    super(schema, currentUser);
    this.authConfig = authConfig;
    this.projectControlFactory = projectControlFactory;
    this.projectCache = projectCache;
    this.accountCache = accountCache;
    this.groupControlFactory = groupControlFactory;
    this.userFactory = userFactory;
    this.currentUser = currentUser;
    this.suggestAccounts = cfg.getEnum("suggest", null, "accounts", SuggestAccountsEnum.ALL);
    this.groupCache = groupCache;
}

From source file:com.google.gerrit.pgm.http.jetty.JettyServer.java

License:Apache License

private Connector[] listen(Server server, Config cfg) {
    // OpenID and certain web-based single-sign-on products can cause
    // some very long headers, especially in the Referer header. We
    // need to use a larger default header size to ensure we have
    // the space required.
    ////from ww  w  . j a va2  s .  c o m
    final int requestHeaderSize = cfg.getInt("httpd", "requestheadersize", 16386);
    final URI[] listenUrls = listenURLs(cfg);
    final boolean reuseAddress = cfg.getBoolean("httpd", "reuseaddress", true);
    final int acceptors = cfg.getInt("httpd", "acceptorThreads", 2);
    final AuthType authType = cfg.getEnum("auth", null, "type", AuthType.OPENID);

    reverseProxy = isReverseProxied(listenUrls);
    final Connector[] connectors = new Connector[listenUrls.length];
    for (int idx = 0; idx < listenUrls.length; idx++) {
        final URI u = listenUrls[idx];
        final int defaultPort;
        final ServerConnector c;
        HttpConfiguration config = defaultConfig(requestHeaderSize);

        if (AuthType.CLIENT_SSL_CERT_LDAP.equals(authType) && !"https".equals(u.getScheme())) {
            throw new IllegalArgumentException("Protocol '" + u.getScheme() + "' "
                    + " not supported in httpd.listenurl '" + u + "' when auth.type = '"
                    + AuthType.CLIENT_SSL_CERT_LDAP.name() + "'; only 'https' is supported");
        }

        if ("http".equals(u.getScheme())) {
            defaultPort = 80;
            c = newServerConnector(server, acceptors, config);

        } else if ("https".equals(u.getScheme())) {
            SslContextFactory ssl = new SslContextFactory();
            final Path keystore = getFile(cfg, "sslkeystore", "etc/keystore");
            String password = cfg.getString("httpd", null, "sslkeypassword");
            if (password == null) {
                password = "gerrit";
            }
            ssl.setKeyStorePath(keystore.toAbsolutePath().toString());
            ssl.setTrustStorePath(keystore.toAbsolutePath().toString());
            ssl.setKeyStorePassword(password);
            ssl.setTrustStorePassword(password);

            if (AuthType.CLIENT_SSL_CERT_LDAP.equals(authType)) {
                ssl.setNeedClientAuth(true);

                Path crl = getFile(cfg, "sslcrl", "etc/crl.pem");
                if (Files.exists(crl)) {
                    ssl.setCrlPath(crl.toAbsolutePath().toString());
                    ssl.setValidatePeerCerts(true);
                }
            }

            defaultPort = 443;

            config.addCustomizer(new SecureRequestCustomizer());
            c = new ServerConnector(server, null, null, null, 0, acceptors,
                    new SslConnectionFactory(ssl, "http/1.1"), new HttpConnectionFactory(config));

        } else if ("proxy-http".equals(u.getScheme())) {
            defaultPort = 8080;
            config.addCustomizer(new ForwardedRequestCustomizer());
            c = newServerConnector(server, acceptors, config);

        } else if ("proxy-https".equals(u.getScheme())) {
            defaultPort = 8080;
            config.addCustomizer(new ForwardedRequestCustomizer());
            config.addCustomizer(new HttpConfiguration.Customizer() {
                @Override
                public void customize(Connector connector, HttpConfiguration channelConfig, Request request) {
                    request.setScheme(HttpScheme.HTTPS.asString());
                    request.setSecure(true);
                }
            });
            c = newServerConnector(server, acceptors, config);

        } else {
            throw new IllegalArgumentException(
                    "Protocol '" + u.getScheme() + "' " + " not supported in httpd.listenurl '" + u + "';"
                            + " only 'http', 'https', 'proxy-http, 'proxy-https'" + " are supported");
        }

        try {
            if (u.getHost() == null && (u.getAuthority().equals("*") //
                    || u.getAuthority().startsWith("*:"))) {
                // Bind to all local addresses. Port wasn't parsed right by URI
                // due to the illegal host of "*" so replace with a legal name
                // and parse the URI.
                //
                final URI r = new URI(u.toString().replace('*', 'A')).parseServerAuthority();
                c.setHost(null);
                c.setPort(0 < r.getPort() ? r.getPort() : defaultPort);
            } else {
                final URI r = u.parseServerAuthority();
                c.setHost(r.getHost());
                c.setPort(0 <= r.getPort() ? r.getPort() : defaultPort);
            }
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Invalid httpd.listenurl " + u, e);
        }

        c.setReuseAddress(reuseAddress);
        connectors[idx] = c;
    }
    return connectors;
}

From source file:com.google.gerrit.server.account.AccountVisibilityProvider.java

License:Apache License

@Inject
AccountVisibilityProvider(@GerritServerConfig Config cfg) {
    AccountVisibility av;//from   w  w w .  j  ava 2s  . c  o  m
    if (cfg.getString("accounts", null, "visibility") != null) {
        av = cfg.getEnum("accounts", null, "visibility", AccountVisibility.ALL);
    } else if (cfg.getString("suggest", null, "accounts") != null) {
        try {
            av = cfg.getEnum("suggest", null, "accounts", AccountVisibility.ALL);
            log.warn(String.format(
                    "Using legacy value %s for suggest.accounts;" + " use accounts.visibility=%s instead", av,
                    av));
        } catch (IllegalArgumentException err) {
            // If suggest.accounts is a valid boolean, it's a new-style config, and
            // we should use the default here. Invalid values are caught in
            // SuggestServiceImpl so we don't worry about them here.
            av = AccountVisibility.ALL;
        }
    } else {
        av = AccountVisibility.ALL;
    }
    accountVisibility = av;
}

From source file:com.google.gerrit.server.account.QueryAccounts.java

License:Apache License

@Inject
QueryAccounts(AccountControl.Factory accountControlFactory, AccountLoader.Factory accountLoaderFactory,
        AccountCache accountCache, AccountIndexCollection indexes, AccountQueryBuilder queryBuilder,
        AccountQueryProcessor queryProcessor, ReviewDb db, @GerritServerConfig Config cfg) {
    this.accountControl = accountControlFactory.get();
    this.accountLoaderFactory = accountLoaderFactory;
    this.accountCache = accountCache;
    this.indexes = indexes;
    this.queryBuilder = queryBuilder;
    this.queryProcessor = queryProcessor;
    this.db = db;
    this.suggestFrom = cfg.getInt("suggest", null, "from", 0);
    this.options = EnumSet.noneOf(ListAccountsOption.class);

    if ("off".equalsIgnoreCase(cfg.getString("suggest", null, "accounts"))) {
        suggestConfig = false;//from   www  .  j av  a  2 s. com
    } else {
        boolean suggest;
        try {
            AccountVisibility av = cfg.getEnum("suggest", null, "accounts", AccountVisibility.ALL);
            suggest = (av != AccountVisibility.NONE);
        } catch (IllegalArgumentException err) {
            suggest = cfg.getBoolean("suggest", null, "accounts", true);
        }
        this.suggestConfig = suggest;
    }
}

From source file:com.google.gerrit.server.account.SuggestAccounts.java

License:Apache License

@Inject
SuggestAccounts(AccountControl.Factory accountControlFactory, AccountLoader.Factory accountLoaderFactory,
        AccountCache accountCache, ReviewDb db, @GerritServerConfig Config cfg) {
    accountControl = accountControlFactory.get();
    accountLoader = accountLoaderFactory.create(true);
    this.accountCache = accountCache;
    this.db = db;
    this.suggestFrom = cfg.getInt("suggest", null, "from", 0);

    if ("off".equalsIgnoreCase(cfg.getString("suggest", null, "accounts"))) {
        suggest = false;/*from   w  ww  .  j av a 2s. c  om*/
    } else {
        boolean suggest;
        try {
            AccountVisibility av = cfg.getEnum("suggest", null, "accounts", AccountVisibility.ALL);
            suggest = (av != AccountVisibility.NONE);
        } catch (IllegalArgumentException err) {
            suggest = cfg.getBoolean("suggest", null, "accounts", true);
        }
        this.suggest = suggest;
    }
}

From source file:com.google.gerrit.server.auth.ldap.LdapRealm.java

License:Apache License

static SearchScope scope(final Config c, final String setting) {
    return c.getEnum("ldap", null, setting, SearchScope.SUBTREE);
}

From source file:com.google.gerrit.server.config.AuthConfig.java

License:Apache License

private static AuthType toType(final Config cfg) {
    return cfg.getEnum("auth", null, "type", AuthType.OPENID);
}

From source file:com.google.gerrit.server.config.ConfigUtil.java

License:Apache License

/**
 * Load section by inspecting Java class attributes.
 * <p>/*from ww w .  j a  v  a2  s.c  o m*/
 * Config values are stored optimized: no default values are stored.
 * The loading is performed eagerly: all values are set.
 * <p>
 * Fields marked with final or transient modifiers are skipped.
 * <p>
 * Boolean fields are only set when their values are true.
 *
 * @param cfg config from which the values are loaded
 * @param section section
 * @param sub subsection
 * @param s instance of class in which the values are set
 * @param defaults instance of class with default values
 * @return loaded instance
 * @throws ConfigInvalidException
 */
public static <T> T loadSection(Config cfg, String section, String sub, T s, T defaults)
        throws ConfigInvalidException {
    try {
        for (Field f : s.getClass().getDeclaredFields()) {
            if (skipField(f)) {
                continue;
            }
            Class<?> t = f.getType();
            String n = f.getName();
            f.setAccessible(true);
            Object d = f.get(defaults);
            Preconditions.checkNotNull(d, "Default cannot be null");
            if (isString(t)) {
                f.set(s, MoreObjects.firstNonNull(cfg.getString(section, sub, n), d));
            } else if (isInteger(t)) {
                f.set(s, cfg.getInt(section, sub, n, (Integer) d));
            } else if (isLong(t)) {
                f.set(s, cfg.getLong(section, sub, n, (Long) d));
            } else if (isBoolean(t)) {
                boolean b = cfg.getBoolean(section, sub, n, (Boolean) d);
                if (b) {
                    f.set(s, b);
                }
            } else if (t.isEnum()) {
                f.set(s, cfg.getEnum(section, sub, n, (Enum<?>) d));
            } else {
                throw new ConfigInvalidException("type is unknown: " + t.getName());
            }
        }
    } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
        throw new ConfigInvalidException("cannot load values", e);
    }
    return s;
}

From source file:com.google.gerrit.server.git.ProjectConfig.java

License:Apache License

/**
 * Parses the [notify] sections out of the configuration file.
 *
 * <pre>//from  w w  w . ja v a  2 s .com
 *   [notify "reviewers"]
 *     email = group Reviewers
 *     type = new_changes
 *
 *   [notify "dev-team"]
 *     email = dev-team@example.com
 *     filter = branch:master
 *
 *   [notify "qa"]
 *     email = qa@example.com
 *     filter = branch:\"^(maint|stable)-.*\"
 *     type = submitted_changes
 * </pre>
 */
private void loadNotifySections(Config rc, Map<String, GroupReference> groupsByName) {
    notifySections = Maps.newHashMap();
    for (String sectionName : rc.getSubsections(NOTIFY)) {
        NotifyConfig n = new NotifyConfig();
        n.setName(sectionName);
        n.setFilter(rc.getString(NOTIFY, sectionName, KEY_FILTER));

        EnumSet<NotifyType> types = EnumSet.noneOf(NotifyType.class);
        types.addAll(ConfigUtil.getEnumList(rc, NOTIFY, sectionName, KEY_TYPE, NotifyType.ALL));
        n.setTypes(types);
        n.setHeader(rc.getEnum(NOTIFY, sectionName, KEY_HEADER, NotifyConfig.Header.BCC));

        for (String dst : rc.getStringList(NOTIFY, sectionName, KEY_EMAIL)) {
            if (dst.startsWith("group ")) {
                String groupName = dst.substring(6).trim();
                GroupReference ref = groupsByName.get(groupName);
                if (ref == null) {
                    ref = new GroupReference(null, groupName);
                    groupsByName.put(ref.getName(), ref);
                }
                if (ref.getUUID() != null) {
                    n.addEmail(ref);
                } else {
                    error(new ValidationError(PROJECT_CONFIG,
                            "group \"" + ref.getName() + "\" not in " + GroupList.FILE_NAME));
                }
            } else if (dst.startsWith("user ")) {
                error(new ValidationError(PROJECT_CONFIG, dst + " not supported"));
            } else {
                try {
                    n.addEmail(Address.parse(dst));
                } catch (IllegalArgumentException err) {
                    error(new ValidationError(PROJECT_CONFIG,
                            "notify section \"" + sectionName + "\" has invalid email \"" + dst + "\""));
                }
            }
        }
        notifySections.put(sectionName, n);
    }
}