/** * Create configuration object with tag name and initialize its attributes. If tag name is <code>property</code> add new * property to last configuration object from stack. In any case reset text builder. */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { textBuilder.setLength(0); if (qName.equals("property")) { Config config = stack.peek(); config.setProperty(value(attributes, "name"), value(attributes, "value")); } else { Config config = new Config(qName); if (!stack.isEmpty()) { Config parent = stack.peek(); parent.addChild(config); } stack.push(config); for (int i = 0; i < attributes.getLength(); ++i) { config.setAttribute(attributes.getQName(i), attributes.getValue(i)); } } }
@Override public void config(Config config) { log.trace("config(Config.Element)"); period = config.getAttribute("period", int.class, 0); }
@Override public void config(Config config) throws ConfigException { super.config(config); // by convention configuration object name is the web application name appName = config.getName(); privateDir = server.getAppDir(appName); if (!privateDir.exists()) { privateDir.mkdir(); } Config loginConfig = config.getChild("login"); if (loginConfig != null) { loginRealm = loginConfig.getProperty("realm", appName); loginPage = loginConfig.getProperty("page"); if (loginPage != null && !loginPage.startsWith("/")) { loginPage = Strings.concat('/', appName, '/', loginPage); } } }
/** * Get named child string value or null if child not found or it has no value. * * @param name name of the child to retrieve value from. * @return named child string value, possible null. */ public String getChildValue(String name) { Config child = getChild(name); return child != null ? child.getValue() : null; }
@Override public Config build() throws ConfigException { Config config = new Config("i18n-repository"); config.setProperty("multi-locale", multiLocale); config.setProperty("base-dir", baseDir); config.setProperty("sub-path", subpath); config.setProperty("files-pattern", filesPattern); return config; } }
Config pojoClassesSection = config.getChild("pojo-classes"); if (pojoClassesSection == null) { return; for (Config pojoClassElement : pojoClassesSection.getChildren()) { String pojoClassName = pojoClassElement.getAttribute("class"); if (pojoClassName == null) { throw new ConfigException("Invalid POJO class element. Missing <class> attribute."); Config configSection = config.getChild(pojoClassElement.getName()); Class<?> pojoClass = Classes.forOptionalName(pojoClassName); if (pojoClass == null) { for (Config staticElement : configSection.findChildren("static-field")) { String fieldName = staticElement.getAttribute("name"); if (fieldName == null) { throw new ConfigException("Missing <name> attribute from static field initialization |%s|.", configSection); if (!staticElement.hasAttribute("value")) { throw new ConfigException("Missing <value> attribute from static field initialization |%s|.", configSection); Object value = staticElement.getAttribute("value", staticField.getType()); log.debug("Intialize static field |%s#%s| |%s|", pojoClassName, fieldName, value); Classes.setFieldValue(null, staticField, value);
for (Config descriptorsSection : config.findChildren("managed-classes", "web-sockets")) { CLASS_DESCRIPTORS: for (Config classDescriptor : descriptorsSection.getChildren()) { if (!classDescriptor.hasChildren()) { if (!classDescriptor.hasAttribute("interface")) { classDescriptor.setAttribute("interface", classDescriptor.getAttribute("class")); String interfaceClass = classDescriptor.getAttribute("interface"); for (int i = 0; i < classDescriptors.size(); ++i) { if (classDescriptors.get(i).hasAttribute("interface", interfaceClass)) { log.debug("Override class descriptor for interface |%s|.", interfaceClass); classDescriptors.set(i, classDescriptor);
List<String> interfaceNames = new ArrayList<>(); if (!descriptor.hasChildren()) { if (!descriptor.hasAttribute("interface")) { if (instanceType.requiresInterface()) { throw new ConfigException("Managed type |%s| requires <interface> attribute. See class descriptor |%s|.", instanceType, descriptor); interfaceNames.add(descriptor.getAttribute("interface")); } else { for (int i = 0; i < descriptor.getChildrenCount(); ++i) { String interfaceName = descriptor.getChild(i).getAttribute("name"); if (interfaceName == null) { throw new ConfigException("Missing <name> attribute from interface declaration. See class descriptor |%s|.", descriptor);
return; for (Config config : config.findChildren("static-field")) { String fieldName = config.getAttribute("name"); if (fieldName == null) { throw new ConfigException("Missing <name> attribute from static field initialization |%s|.", config.getParent()); if (!config.hasAttribute("value")) { throw new ConfigException("Missing <value> attribute from static field initialization |%s|.", config.getParent()); Object value = config.getAttribute("value", field.getType()); log.debug("Intialize static field |%s#%s| |%s|", implementationClass, fieldName, value); Classes.setFieldValue(null, field, config.getAttribute("value", field.getType()));
Config section = config.getChild("converters"); if (section == null) { return; for (Config el : section.findChildren("type")) { String className = el.getAttribute("class"); Class<?> valueType = Classes.forOptionalName(className); if (valueType == null) { String converterName = el.getAttribute("converter"); Class<? extends Converter> converterClass = Classes.forOptionalName(converterName); if (converterClass == null) {
/** * Get configuration object property value or default value if there is no property with requested name. * * @param name property name, * @param defaultValue default value. * @return configuration object property value or given default value. * @throws IllegalArgumentException if <code>name</code> argument is null or empty. */ public String getProperty(String name, String defaultValue) { String value = getProperty(name); return value != null ? value : defaultValue; }
/** * Loads this managed class state from class descriptor then delegates {@link #scanAnnotations()}. Annotations scanning is * performed only if this managed class type requires implementation, see {@link InstanceType#requiresImplementation()}. * * @param container parent container, * @param descriptor class descriptor from <code>managed-class</code> section. * @throws ConfigException if configuration is invalid. */ public ManagedClass(Container container, Config descriptor) throws ConfigException { this.container = container; // if configuration section is missing this.config field remains null this.config = descriptor.getRoot().getChild(descriptor.getName()); // loading order matters; do not change it this.instanceScope = loadInstanceScope(descriptor); this.instanceType = loadInstanceType(descriptor); this.implementationClass = loadImplementationClass(descriptor); this.interfaceClasses = loadInterfaceClasses(descriptor); this.implementationURL = loadImplementationURL(descriptor); // get declared constructor return null if no implementation class this.constructor = getDeclaredConstructor(this.implementationClass); // scan dependencies return empty collection if no implementation class this.dependencies = scanDependencies(this.implementationClass); if (this.instanceType.requiresImplementation()) { scanAnnotations(); initializeStaticFields(); } this.key = Integer.toString(KEY_SEED.getAndIncrement()); this.string = buildStringRepresentation(descriptor); }
for (Config repositorySection : config.findChildren("repository")) { String className = repositorySection.getAttribute("class", DEF_IMPLEMENTATION); Class<?> implementation = Classes.forOptionalName(className); if (implementation == null) { String repositoryPath = repositorySection.getAttribute("path"); if (repositoryPath == null) { throw new ConfigException("Invalid views repository configuration. Missing <path> attribute."); String filesPattern = repositorySection.getAttribute("files-pattern"); if (filesPattern == null) { throw new ConfigException("Invalid views repository configuration. Missing <files-pattern> attribute."); Properties properties = repositorySection.getProperties();
/** * Build configuration object. Configuration object is not reusable so this factory creates a new instance for every call. * * @return newly created configuration object. * @throws ConfigException if XML stream read operation fails or is not well formed. */ public Config build() throws ConfigException { if (properties != null) { Config config = new Config("properties"); config.setProperties(properties); return config; } try { Loader loader = new Loader(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setContentHandler(loader); reader.parse(new InputSource(xmlStream)); return loader.getConfig(); } catch (Exception e) { throw new ConfigException(e); } }
/** * Initialize instance fields from managed class configuration object. * * @param managedClass managed class, * @param instance instance of managed class. * @throws ConverterException if configured value cannot be converted to field type. * @throws BugError if attempt to assign instance field to not POJO type. */ @Override public void postProcessInstance(ManagedClassSPI managedClass, Object instance) { Config config = managedClass.getConfig(); if (config == null) { return; } List<Config> fields = config.findChildren("instance-field"); if (!fields.isEmpty() && !InstanceType.POJO.equals(managedClass.getInstanceType())) { throw new BugError("Cannot assign instance field on non %s type.", InstanceType.POJO); } for (Config field : fields) { Classes.setFieldValue(instance, field.getAttribute("name"), field.getAttribute("value")); } } }
/** * Construct application configuration instance with information from given servlet context. * * @param context Servlet context bound to application, * @param properties context properties defining variables to inject on descriptors. * @throws ConfigException if application descriptor is not well formed or invalid. */ public TinyConfigBuilder(ServletContext context, Properties properties) throws ConfigException { config = new Config(getAppName(context.getContextPath())); File contextDir = new File(context.getRealPath("")); File webinfDir = new File(contextDir, "WEB-INF"); Loader loader = new Loader(config, properties); // first load library descriptor loadXML(Classes.getResourceAsStream("lib-descriptor.xml"), loader); // load application specific configuration document(s) File file = new File(webinfDir, "app.xml"); try { loadXML(new FileInputStream(file), loader); } catch (FileNotFoundException e) { log.debug("Application |%s| has no descriptor. No application specific configuration.", config.getName()); new ConfigException(e); } }
/** * Test constructor creates and empty configuration object with given name. * * @param configName configuration object name. */ protected TinyConfigBuilder(String configName) { config = new Config(configName); }
/** * Get configuration object property converter to requested type or null if there is no property with given name. * * @param name property name. * @param type type to convert property value to. * @param <T> value type. * @return newly created value object or null. * @throws IllegalArgumentException if <code>name</code> argument is null or empty. * @throws IllegalArgumentException if <code>type</code> argument is null. * @throws ConverterException if there is no converter registered for value type or value parse fails. */ public <T> T getProperty(String name, Class<T> type) { return getProperty(name, type, null); }
config.setProperty(value(attributes, "name"), value(attributes, "value")); } else { Config config = new Config(qName); Config parent = parentsStack.peek(); parent.addChild(config); parentsStack.push(config); config.setAttribute(name, value(attributes, name));
/** * Get attribute value or given default value if there is no attribute with requested name. If given default value is * null and attribute is not found this method still returns null, that is, requested default value. * * @param name attribute name. * @param defaultValue default value, null or empty accepted. * @return attribute value or default value. * @throws IllegalArgumentException if <code>name</code> argument is null or empty. */ public String getAttribute(String name, String defaultValue) { Params.notNullOrEmpty(name, "Attribute name"); return getAttribute(name, String.class, defaultValue); }