List of usage examples for org.eclipse.jgit.lib Config setLong
public void setLong(final String section, final String subsection, final String name, final long value)
From source file:com.google.gerrit.server.config.ConfigUtil.java
License:Apache License
/** * Store section by inspecting Java class attributes. * <p>/*from www. j a va 2 s. c o m*/ * Optimize the storage by unsetting a variable if it is * being set to default value by the server. * <p> * Fields marked with final or transient modifiers are skipped. * * @param cfg config in which the values should be stored * @param section section * @param sub subsection * @param s instance of class with config values * @param defaults instance of class with default values * @throws ConfigInvalidException */ public static <T> void storeSection(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 c = f.get(s); Object d = f.get(defaults); Preconditions.checkNotNull(d, "Default cannot be null"); if (c == null || c.equals(d)) { cfg.unset(section, sub, n); } else { if (isString(t)) { cfg.setString(section, sub, n, (String) c); } else if (isInteger(t)) { cfg.setInt(section, sub, n, (Integer) c); } else if (isLong(t)) { cfg.setLong(section, sub, n, (Long) c); } else if (isBoolean(t)) { cfg.setBoolean(section, sub, n, (Boolean) c); } else if (t.isEnum()) { cfg.setEnum(section, sub, n, (Enum<?>) c); } else { throw new ConfigInvalidException("type is unknown: " + t.getName()); } } } } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) { throw new ConfigInvalidException("cannot save values", e); } }
From source file:com.googlesource.gerrit.plugins.lfs.PutLfsGlobalConfig.java
License:Apache License
@Override public LfsGlobalConfigInfo apply(ProjectResource resource, LfsGlobalConfigInput input) throws RestApiException { adminView.validate(resource);// ww w . j a v a 2 s . c o m Project.NameKey projectName = resource.getNameKey(); if (input == null) { input = new LfsGlobalConfigInput(); } LfsProjectsConfig config = lfsConfigFactory.getProjectsConfig(); try (MetaDataUpdate md = metaDataUpdateFactory.get().create(projectName)) { try { config.load(md); } catch (ConfigInvalidException | IOException e) { throw new ResourceConflictException("Cannot read LFS config in " + projectName); } Config cfg = new Config(); if (input.namespaces != null) { Set<String> backends = lfsConfigFactory.getGlobalConfig().getBackends().keySet(); Set<Entry<String, LfsProjectConfigInfo>> namespaces = input.namespaces.entrySet(); for (Map.Entry<String, LfsProjectConfigInfo> namespace : namespaces) { LfsProjectConfigInfo info = namespace.getValue(); if (info.enabled != null) { cfg.setBoolean(pluginName, namespace.getKey(), KEY_ENABLED, info.enabled); } if (info.maxObjectSize != null) { cfg.setLong(pluginName, namespace.getKey(), KEY_MAX_OBJECT_SIZE, info.maxObjectSize); } if (info.readOnly != null) { cfg.setBoolean(pluginName, namespace.getKey(), KEY_READ_ONLY, info.readOnly); } if (!Strings.isNullOrEmpty(info.backend)) { if (!backends.contains(info.backend)) { throw new ResourceConflictException(String .format("Namespace %s: backend %s does not exist", namespace, info.backend)); } cfg.setString(pluginName, namespace.getKey(), KEY_BACKEND, info.backend); } } } config.setProjectConfig(cfg); try { config.commit(md); } catch (IOException e) { if (e.getCause() instanceof ConfigInvalidException) { throw new ResourceConflictException( "Cannot update LFS config in " + projectName + ": " + e.getCause().getMessage()); } throw new ResourceConflictException("Cannot update LFS config in " + projectName); } } catch (RepositoryNotFoundException e) { throw new ResourceNotFoundException(projectName.get()); } catch (IOException e) { throw new ResourceNotFoundException(projectName.get(), e); } return get.apply(resource); }
From source file:com.googlesource.gerrit.plugins.rabbitmq.config.section.Sections.java
License:Apache License
public static <T extends Section> Config toConfig(T section, Config config) { Field[] fs = section.getClass().getFields(); for (Field f : fs) { try {//from www . ja v a 2s . co m Class<?> type = f.getType(); Object obj = f.get(section); if (obj != null) { if (type == String.class) { config.setString(getName(section), null, f.getName(), String.class.cast(obj)); } else if (type == Integer.class) { config.setInt(getName(section), null, f.getName(), Integer.class.cast(obj)); } else if (type == Long.class) { config.setLong(getName(section), null, f.getName(), Long.class.cast(obj)); } else if (type == Boolean.class) { config.setBoolean(getName(section), null, f.getName(), Boolean.class.cast(obj)); } } } catch (IllegalAccessException ex) { LOGGER.warn("Cannot access field {}. Cause: {}", f.getName(), ex.getMessage()); } } return config; }