Search in sources :

Example 56 with ConfigurationException

use of com.google.inject.ConfigurationException in project cucumber-jvm by cucumber.

the class GuiceFactoryTest method factoryStartFailsIfScenarioScopeIsNotBound.

@Test
void factoryStartFailsIfScenarioScopeIsNotBound() {
    initFactory(Guice.createInjector());
    Executable testMethod = () -> factory.start();
    ConfigurationException actualThrown = assertThrows(ConfigurationException.class, testMethod);
    assertThat("Unexpected exception message", actualThrown.getMessage(), startsWith("Guice configuration errors:\n" + "\n" + "1) [Guice/MissingImplementation]: No implementation for ScenarioScope was bound."));
}
Also used : ConfigurationException(com.google.inject.ConfigurationException) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test)

Example 57 with ConfigurationException

use of com.google.inject.ConfigurationException in project shiro by apache.

the class BeanTypeListener method hear.

public <I> void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter) {
    PropertyDescriptor[] propertyDescriptors = beanUtilsBean.getPropertyUtils().getPropertyDescriptors(type.getRawType());
    final Map<PropertyDescriptor, Key<?>> propertyDependencies = new HashMap<PropertyDescriptor, Key<?>>(propertyDescriptors.length);
    final Provider<Injector> injectorProvider = encounter.getProvider(Injector.class);
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        if (propertyDescriptor.getWriteMethod() != null && Modifier.isPublic(propertyDescriptor.getWriteMethod().getModifiers())) {
            Type propertyType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0];
            propertyDependencies.put(propertyDescriptor, createDependencyKey(propertyDescriptor, propertyType));
        }
    }
    encounter.register(new MembersInjector<I>() {

        public void injectMembers(I instance) {
            for (Map.Entry<PropertyDescriptor, Key<?>> dependency : propertyDependencies.entrySet()) {
                try {
                    final Injector injector = injectorProvider.get();
                    Object value = injector.getInstance(getMappedKey(injector, dependency.getValue()));
                    dependency.getKey().getWriteMethod().invoke(instance, value);
                } catch (ConfigurationException e) {
                // This is ok, it simply means that we can't fulfill this dependency.
                // Is there a better way to do this?
                } catch (InvocationTargetException e) {
                    throw new RuntimeException("Couldn't set property " + dependency.getKey().getDisplayName(), e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("We shouldn't have ever reached this point, we don't try to inject to non-accessible methods.", e);
                }
            }
        }
    });
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) InvocationTargetException(java.lang.reflect.InvocationTargetException) Type(java.lang.reflect.Type) ConfigurationException(com.google.inject.ConfigurationException) Injector(com.google.inject.Injector) MembersInjector(com.google.inject.MembersInjector) Key(com.google.inject.Key)

Example 58 with ConfigurationException

use of com.google.inject.ConfigurationException in project gora by apache.

the class ElasticsearchMappingBuilder method readMappingFile.

/**
 * Reads Elasticsearch mappings from file.
 *
 * @param inputStream   Mapping input stream
 * @param xsdValidation Parameter for enabling XSD validation
 */
public void readMappingFile(InputStream inputStream, boolean xsdValidation) {
    try {
        SAXBuilder saxBuilder = new SAXBuilder();
        if (inputStream == null) {
            LOG.error("The mapping input stream is null!");
            throw new GoraException("The mapping input stream is null!");
        }
        // Convert input stream to a string to use it a few times
        String mappingStream = IOUtils.toString(inputStream, Charset.defaultCharset());
        // XSD validation for XML file
        if (xsdValidation) {
            Source xmlSource = new StreamSource(IOUtils.toInputStream(mappingStream, Charset.defaultCharset()));
            Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(getClass().getClassLoader().getResourceAsStream(XSD_MAPPING_FILE)));
            schema.newValidator().validate(xmlSource);
            LOG.info("Mapping file is valid.");
        }
        Document document = saxBuilder.build(IOUtils.toInputStream(mappingStream, Charset.defaultCharset()));
        if (document == null) {
            LOG.error("The mapping document is null!");
            throw new GoraException("The mapping document is null!");
        }
        Element root = document.getRootElement();
        // Extract class descriptions
        @SuppressWarnings("unchecked") List<Element> classElements = root.getChildren(TAG_CLASS);
        for (Element classElement : classElements) {
            final Class<T> persistentClass = dataStore.getPersistentClass();
            final Class<K> keyClass = dataStore.getKeyClass();
            if (haveKeyClass(keyClass, classElement) && havePersistentClass(persistentClass, classElement)) {
                loadPersistentClass(classElement, persistentClass);
                break;
            }
        }
    } catch (IOException | JDOMException | ConfigurationException | SAXException ex) {
        throw new RuntimeException(ex);
    }
    LOG.info("Gora Elasticsearch mapping file was read successfully.");
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) Element(org.jdom.Element) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXException(org.xml.sax.SAXException) GoraException(org.apache.gora.util.GoraException) ConfigurationException(com.google.inject.ConfigurationException)

Example 59 with ConfigurationException

use of com.google.inject.ConfigurationException in project gora by apache.

the class IgniteMappingBuilder method readMappingFile.

/**
 * Reads Ignite mappings from file
 *
 * @param inputStream Input stream of the mapping
 */
public void readMappingFile(InputStream inputStream) {
    try {
        SAXBuilder saxBuilder = new SAXBuilder();
        if (inputStream == null) {
            LOG.error("The mapping input stream is null!");
            throw new IOException("The mapping input stream is null!");
        }
        Document document = saxBuilder.build(inputStream);
        if (document == null) {
            LOG.error("The mapping document is null!");
            throw new IOException("The mapping document is null!");
        }
        @SuppressWarnings("unchecked") List<Element> classes = document.getRootElement().getChildren("class");
        for (Element classElement : classes) {
            if (classElement.getAttributeValue("keyClass").equals(dataStore.getKeyClass().getCanonicalName()) && classElement.getAttributeValue("name").equals(dataStore.getPersistentClass().getCanonicalName())) {
                final String tableNameFromMapping = classElement.getAttributeValue("table");
                String tableName = dataStore.getSchemaName(tableNameFromMapping, dataStore.getPersistentClass());
                igniteMapping.setTableName(tableName);
                @SuppressWarnings("unchecked") List<Element> prColumns = classElement.getChildren("primarykey");
                List<Column> prFields = new ArrayList<>();
                for (Element aPrimaryKey : prColumns) {
                    String name = aPrimaryKey.getAttributeValue("column");
                    String type = aPrimaryKey.getAttributeValue("type");
                    prFields.add(new Column(name, Column.FieldType.valueOf(type)));
                }
                igniteMapping.setPrimaryKey(prFields);
                @SuppressWarnings("unchecked") List<Element> fields = classElement.getChildren("field");
                Map<String, Column> mp = new HashMap<>();
                for (Element field : fields) {
                    String fieldName = field.getAttributeValue("name");
                    String columnName = field.getAttributeValue("column");
                    String columnType = field.getAttributeValue("type");
                    mp.put(fieldName, new Column(columnName, Column.FieldType.valueOf(columnType)));
                }
                igniteMapping.setFields(mp);
                break;
            }
        }
    } catch (IOException | JDOMException | ConfigurationException e) {
        throw new RuntimeException(e);
    }
    LOG.info("Gora Ignite mapping file was read successfully.");
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) HashMap(java.util.HashMap) Element(org.jdom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) ConfigurationException(com.google.inject.ConfigurationException)

Example 60 with ConfigurationException

use of com.google.inject.ConfigurationException in project google-gin by gwtplus.

the class FactoryBindingTest method testDuplicateNamedAssistedAnnotations.

public void testDuplicateNamedAssistedAnnotations() {
    try {
        new FactoryBinding(Collections.<Key<?>, TypeLiteral<?>>emptyMap(), Key.get(TwoAssistedFooStringsFactory.class), CONTEXT, new GuiceUtil(createInjectableCollector()), null);
        fail("Expected ConfigurationException.");
    } catch (ConfigurationException e) {
        assertTrue(e.getMessage().contains("has more than one parameter of type " + "java.lang.String annotated with @Assisted(\"foo\")."));
    }
}
Also used : GuiceUtil(com.google.gwt.inject.rebind.util.GuiceUtil) ConfigurationException(com.google.inject.ConfigurationException)

Aggregations

ConfigurationException (com.google.inject.ConfigurationException)62 Injector (com.google.inject.Injector)16 Errors (com.google.inject.internal.Errors)13 InjectionPoint (com.google.inject.spi.InjectionPoint)8 Method (java.lang.reflect.Method)7 AbstractModule (com.google.inject.AbstractModule)5 Inject (com.google.inject.Inject)5 ErrorsException (com.google.inject.internal.ErrorsException)5 Annotation (java.lang.annotation.Annotation)5 TypeLiteral (com.google.inject.TypeLiteral)4 Map (java.util.Map)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 GuiceUtil (com.google.gwt.inject.rebind.util.GuiceUtil)3 Module (com.google.inject.Module)3 Message (com.google.inject.spi.Message)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Binding (com.google.inject.Binding)2