/** * Get named attribute value throwing exception if not attribute found. * * @param attributes attributes list, * @param attributeName name of attribute to retrieve. * @return attribute value. * @throws SAXException if there is no attribute with requested name. */ private static String value(Attributes attributes, String attributeName) throws SAXException { String value = attributes.getValue(attributeName); if (value == null) { throw new SAXException(new ConfigException("Missing attribute <%s>.", attributeName)); } return value; } }
imagesRepositoryDir = config.getProperty("captcha.repository.path", File.class); if (imagesRepositoryDir == null) { throw new ConfigException("Missing <captcha.repository.path> property from CAPTCHA configuration."); throw new ConfigException("CAPTCHA images repository |%s| does not exist.", imagesRepositoryDir); throw new ConfigException("CAPTCHA images repository |%s| is not a directory.", imagesRepositoryDir); throw new ConfigException("CAPTCHA images repository |%s| is not absolute path.", imagesRepositoryDir); throw new ConfigException("CAPTCHA images repository |%s| is empty.", imagesRepositoryDir); throw new ConfigException("Challenge set size is larger that avaliable images count from CAPTCHA repository.");
/** * Load configuration document from file. * * @param inputStream configuration XML file, * @param loader SAX handler in charge of configuration loading. * @throws ConfigException if XML document loading fails. */ protected static void loadXML(InputStream inputStream, Loader loader) throws ConfigException { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setContentHandler(loader); reader.parse(new InputSource(inputStream)); } catch (Exception e) { throw new ConfigException("Fail to load configuration document from file |%s|: %s", inputStream, e); } }
String value = attributes.getValue(attributeName); if (value == null) { throw new SAXException(new ConfigException("Missing attribute |%s|.", attributeName));
if (implementationName == null) { if (instanceType.requiresImplementation()) { throw new ConfigException("Managed type |%s| requires <class> attribute. See class descriptor |%s|.", instanceType, descriptor); throw new ConfigException("Managed type |%s| forbids <class> attribute. See class descriptor |%s|.", instanceType, descriptor); throw new ConfigException("Managed class implementation |%s| not found.", implementationName); throw new ConfigException("Managed class implementation |%s| cannot be an interface. See class descriptor |%s|.", implementationClass, descriptor); throw new ConfigException("Managed class implementation |%s| cannot be abstract. See class descriptor |%s|.", implementationClass, descriptor); throw new ConfigException("Bad scope |%s| used with managed life cycle. See class descriptor |%s|.", instanceScope, descriptor);
/** * Return instance type loaded from class descriptor, <code>type</code> attribute. If type is not defined uses * {@link InstanceType#POJO} as default value. * * @param descriptor managed class descriptor. * @return loaded instance type. * @throws ConfigException if there is no {@link InstanceFactory} registered for loaded instance type. */ private InstanceType loadInstanceType(Config descriptor) throws ConfigException { InstanceType instanceType = descriptor.getAttribute("type", InstanceType.class, InstanceType.POJO); if (!container.hasInstanceFactory(instanceType)) { throw new ConfigException("Not registered managed instance type value |%s|. See class descriptor |%s|.", instanceType, descriptor); } return instanceType; }
/** * Load remote class URL from class descriptor, <code>url</code> attribute. This getter does not perform URL validation; it * returns URL value as declared by attribute. * * @param descriptor class descriptor. * @return remote class URL value. * @throws ConfigException if instance type is {@link InstanceType#REMOTE} and <code>url</code> attribute is missing. */ private String loadImplementationURL(Config descriptor) throws ConfigException { String implementationURL = descriptor.getAttribute("url"); if (instanceType.equals(InstanceType.REMOTE) && implementationURL == null) { throw new ConfigException("Remote managed class requires <url> attribute. See class descriptor |%s|.", descriptor); } return implementationURL; }
/** * Return instance scope loaded from class descriptor, <code>scope</code> attribute. If scope is not defined use * {@link InstanceScope#APPLICATION} as default value. * * @param descriptor managed class descriptor. * @return loaded instance scope. * @throws ConfigException if there is no {@link ScopeFactory} registered for loaded instance scope. */ private InstanceScope loadInstanceScope(Config descriptor) throws ConfigException { InstanceScope instanceScope = descriptor.getAttribute("scope", InstanceScope.class, InstanceScope.APPLICATION); if (!container.hasScopeFactory(instanceScope)) { throw new ConfigException("Not registered managed instance scope value |%s|. See class descriptor |%s|.", instanceScope, descriptor); } return instanceScope; }
/** * 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); } }
if (!descriptor.hasAttribute("interface")) { if (instanceType.requiresInterface()) { throw new ConfigException("Managed type |%s| requires <interface> attribute. See class descriptor |%s|.", instanceType, descriptor); String interfaceName = descriptor.getChild(i).getAttribute("name"); if (interfaceName == null) { throw new ConfigException("Missing <name> attribute from interface declaration. See class descriptor |%s|.", descriptor); throw new ConfigException("Managed class interface |%s| not found.", interfaceName); throw new ConfigException("Managed type |%s| requires interface to make Java Proxy happy but got |%s|.", instanceType, interfaceClass); throw new ConfigException("Implementation |%s| is not a kind of interface |%s|.", implementationClass, interfaceClass);
String fieldName = config.getAttribute("name"); if (fieldName == null) { throw new ConfigException("Missing <name> attribute from static field initialization |%s|.", config.getParent()); throw new ConfigException("Missing <value> attribute from static field initialization |%s|.", config.getParent()); throw new ConfigException("Missing managed class static field |%s#%s|.", implementationClass, fieldName); throw new ConfigException("Attempt to execute static initialization on instance field |%s#%s|.", implementationClass, fieldName);
/** * 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); } }
String pojoClassName = pojoClassElement.getAttribute("class"); if (pojoClassName == null) { throw new ConfigException("Invalid POJO class element. Missing <class> attribute."); throw new ConfigException("Missing configured POJO class |%s|.", pojoClassName); String fieldName = staticElement.getAttribute("name"); if (fieldName == null) { throw new ConfigException("Missing <name> attribute from static field initialization |%s|.", configSection); throw new ConfigException("Missing <value> attribute from static field initialization |%s|.", configSection); throw new ConfigException("Missing POJO static field |%s#%s|.", pojoClassName, fieldName); throw new ConfigException("Attempt to execute POJO |%s| static initialization on instance field |%s|.", pojoClassName, fieldName);
Class<?> valueType = Classes.forOptionalName(className); if (valueType == null) { throw new ConfigException("Invalid converter configuration. Value type class |%s| not found.", className); Class<? extends Converter> converterClass = Classes.forOptionalName(converterName); if (converterClass == null) { throw new ConfigException("Invalid converter configuration. Converter class |%s| not found.", converterName);
Class<?> implementation = Classes.forOptionalName(className); if (implementation == null) { throw new ConfigException("Unable to load view implementation |%s|.", className); throw new ConfigException("View implementation |%s| is not of proper type.", className); throw new ConfigException("View implementation |%s| is not instantiable. Ensure is not abstract or interface and have default constructor.", implementation); throw new ConfigException("Invalid views repository configuration. Missing <path> attribute."); throw new ConfigException("Invalid views repository configuration. Missing <files-pattern> attribute.");