DeploymentManagerFactory(XnioWorker xnioWorker, Coffig configuration, Map<String, String> initParameters) { this.xnioWorker = xnioWorker; this.undertowConfig = configuration.get(UndertowConfig.class); this.applicationConfig = configuration.get(ApplicationConfig.class); this.serverConfig = configuration.get(WebConfig.ServerConfig.class); this.initParameters = initParameters; }
@SuppressWarnings("unchecked") public <T> Optional<T> getOptional(Class<T> configurationClass, String... path) { return (Optional<T>) getOptional((Type) configurationClass, path); }
@Override @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "Cast is verified in canHandle() method") public TreeNode unmap(Object object, Type type) { Type itemType = ((ParameterizedType) type).getActualTypeArguments()[0]; if (((Optional<?>) object).isPresent()) { return coffig.getMapper().unmap(((Optional<?>) object).get(), itemType); } else { return null; } } }
private TreeNode retrieveTreeNode(Supplier<Object> supplier) { Object o = supplier.get(); TreeNode treeNode = coffig.getMapper().unmap(o, o.getClass()); String prefix = suppliers.get(supplier); if (prefix == null || prefix.isEmpty()) { prefix = Coffig.pathOf(o.getClass()); } if (prefix != null && !prefix.isEmpty()) { MapNode mapNode = new MapNode(); mapNode.set(prefix, treeNode); return mapNode; } else { return treeNode; } } }
public Optional<Object> getOptional(Type configurationType, String... path) { LOGGER.trace( "Accessing configuration path '" + Arrays.toString(path) + "' and mapping it to '" + configurationType .getTypeName() + "'"); if (isDirty()) { refresh(); } String joinedPath; if (path != null && path.length > 0) { joinedPath = String.join(".", (CharSequence[]) path); } else { joinedPath = pathOf(rawClassOf(configurationType)); } Optional<TreeNode> resolvedTree; if (joinedPath == null || joinedPath.isEmpty()) { resolvedTree = Optional.of(this.configurationTree); } else { resolvedTree = this.configurationTree.get(joinedPath); } return resolvedTree.map(treeNode -> mapper.map(treeNode, configurationType)); }
public static void refresh() { if (initialized && !disposed) { Seed instance = getInstance(); instance.configuration.refresh(); instance.logManager.refresh(instance.configuration.get(LoggingConfig.class)); instance.proxyManager.refresh(instance.configuration.get(ProxyConfig.class)); // Trigger afterRefresh() in custom initializers for (SeedInitializer seedInitializer : seedInitializers) { try { seedInitializer.afterRefresh(); } catch (Exception e) { throw SeedException.wrap(e, CoreErrorCode.ERROR_IN_INITIALIZER) .put("initializerClass", seedInitializer.getClass().getName()); } } } }
private String computeConfigKey(Configuration configuration, Class<?> fieldType) { return configuration.value().length > 0 ? Joiner.on(".").join(configuration.value()) : Coffig.pathOf(fieldType); }
public void addObject(Object object) { Arrays.stream(object.getClass().getDeclaredMethods()) .filter(method -> method.isAnnotationPresent(Config.class)) .forEach(method -> addSupplier(() -> { method.setAccessible(true); try { return method.invoke(object); } catch (Exception e) { throw ConfigurationException.wrap(e, ConfigurationErrorCode.CANNOT_SUPPLY_CONFIGURATION_OBJECT) .put("class", object.getClass()) .put("method", method.getName()); } }, Coffig.pathOf(method))); }
@SuppressWarnings("unchecked") public <T> T get(Class<T> configurationClass, String... path) { return (T) get((Type) configurationClass, path); }
@Override @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "Cast is verified in canHandle() method") public Object map(TreeNode treeNode, Type type) { Type itemType = ((ParameterizedType) type).getActualTypeArguments()[0]; return Optional.ofNullable(coffig.getMapper().map(treeNode, itemType)); }
@Override public Object getObject() throws Exception { if (key == null) { throw new IllegalArgumentException("Property key is required for SeedConfigurationFactoryBean"); } else { Optional<String> value = configuration.getOptional(String.class, key); if (value.isPresent()) { return value.get(); } else { if (defaultValue == null && mandatory) { throw new IllegalArgumentException("Configuration value " + key + " is mandatory and has no value nor default value"); } return defaultValue; } } }
ApplicationImpl(Coffig coffig, Map<String, String> kernelParameters, String[] arguments) { this.coffig = coffig; this.applicationConfig = coffig.get(ApplicationConfig.class); this.kernelParameters = kernelParameters; this.arguments = arguments; this.storageDirectory = configureLocalStorage(applicationConfig); }
@Override @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "Cast is verified in canHandle() method") public Object map(TreeNode treeNode, Type type) { Class componentType = ((Class) type).getComponentType(); Collection<TreeNode> values = treeNode.nodes().collect(Collectors.toList()); Object array = Array.newInstance(componentType, values.size()); AtomicInteger index = new AtomicInteger(); values.stream() .map(childNode -> coffig.getMapper().map(childNode, componentType)) .forEach(item -> Array.set(array, index.getAndIncrement(), item)); return array; }
@Override @SuppressWarnings("unchecked") public <T> ClassConfiguration<T> getConfiguration(Class<T> someClass) { ClassConfiguration<T> classConfiguration = ClassConfiguration.empty(someClass); StringBuilder sb = new StringBuilder(CLASSES_CONFIGURATION_PREFIX); for (String part : someClass.getName().split("\\.")) { sb.append(".").append(part); coffig.getOptional(Types.newParameterizedType(ClassConfiguration.class, someClass), sb.toString()) .ifPresent(o -> classConfiguration.merge((ClassConfiguration<T>) o)); } return classConfiguration; }
@Inject public InternalErrorExceptionMapper(Application application) { this.exceptionMappingConfig = application.getConfiguration().get(RestConfig.ExceptionMappingConfig.class); }
@Override @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "Cast is verified in canHandle() method") public TreeNode unmap(Object object, Type type) { Type itemType = ((ParameterizedType) type).getActualTypeArguments()[0]; return new ArrayNode(((Collection<?>) object).stream() .map(item -> coffig.getMapper().unmap(item, itemType)) .collect(toList())); } }
public Object getMandatory(Type configurationClass, String... path) { return getOptional(configurationClass, path).orElseThrow(() -> (ConfigurationException) ConfigurationException.createNew(ConfigurationErrorCode.PATH_NOT_FOUND) .put("path", path == null ? "null" : String.join(".", (CharSequence[]) path)) ); }
protected <T> T getConfiguration(Class<T> configClass, String... path) { return getConfiguration().get(configClass, path); } }
TreeNode unmap() { MapNode rootNode = new MapNode(); fieldInfo.forEach(fieldInfo -> Optional.ofNullable(coffig.getMapper() .unmap(fieldInfo.supplier.get(), fieldInfo.type)) .ifPresent(treeNode -> rootNode.set(fieldInfo.alias != null ? fieldInfo.alias : fieldInfo.name, treeNode))); return rootNode; }
public Object get(Type type, String... path) { return getOptional(type, path).orElseGet(() -> instantiateDefault(rawClassOf(type))); }