Example usage for org.springframework.context ApplicationContext getEnvironment

List of usage examples for org.springframework.context ApplicationContext getEnvironment

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getEnvironment.

Prototype

Environment getEnvironment();

Source Link

Document

Return the Environment associated with this component.

Usage

From source file:org.springframework.xd.dirt.server.admin.AdminServerApplication.java

@Bean
public ApplicationListener<?> xdInitializer(ApplicationContext context) {
    XdConfigLoggingInitializer delegate = new XdConfigLoggingInitializer(false);
    delegate.setEnvironment(context.getEnvironment());
    return new SourceFilteringListener(context, delegate);
}

From source file:org.teiid.spring.autoconfigure.TeiidServer.java

private ModelMetaData buildModelFromDataSource(String dsBeanName, String driverName, ApplicationContext context,
        boolean createInitTable) throws AdminException {

    ModelMetaData model = new ModelMetaData();
    model.setName(dsBeanName);/* w  ww . jav  a2 s  . c om*/
    model.setModelType(Model.Type.PHYSICAL);

    // note these can be overridden by specific ones in the configuration
    // TODO: need come up most sensible properties for this.
    model.addProperty("importer.useQualifiedName", "false");
    model.addProperty("importer.tableTypes", "TABLE,VIEW");

    SourceMappingMetadata source = new SourceMappingMetadata();
    source.setName(dsBeanName);
    source.setConnectionJndiName(dsBeanName);

    String translatorName = ExternalSource.findTransaltorNameFromDriverName(driverName);
    source.setTranslatorName(translatorName);
    String dialect = ExternalSource.findDialectFromDriverName(driverName);
    if (dialect != null) {
        model.addProperty(DIALECT, dialect);
    }

    // load the translator class
    addTranslator(translatorName);

    // add the importer properties from the configuration
    // note that above defaults can be overridden with this too.
    Collection<? extends PropertyDefinition> importProperties = getAdmin()
            .getTranslatorPropertyDefinitions(source.getTranslatorName(), TranlatorPropertyType.IMPORT);
    importProperties.forEach(prop -> {
        String key = prop.getName();
        String value = context.getEnvironment().getProperty("spring.datasource." + dsBeanName + "." + key);
        if (value == null) {
            value = context.getEnvironment().getProperty("spring.xa.datasource." + dsBeanName + "." + key);
        }
        if (value != null) {
            model.addProperty(key, value);
        }
    });

    model.addSourceMapping(source);

    // This is to avoid failing on empty schema
    if (createInitTable) {
        model.addSourceMetadata("NATIVE", "");
        model.addSourceMetadata("DDL", "create foreign table dual(id integer);");
    }
    return model;
}

From source file:org.teiid.spring.autoconfigure.TeiidServer.java

boolean findAndConfigureViews(VDBMetaData vdb, ApplicationContext context,
        PhysicalNamingStrategy namingStrategy) {
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
            false);// ww w  .ja v a  2 s  .com
    provider.addIncludeFilter(new AnnotationTypeFilter(javax.persistence.Entity.class));
    provider.addIncludeFilter(new AnnotationTypeFilter(javax.persistence.Embeddable.class));
    provider.addIncludeFilter(new AnnotationTypeFilter(SelectQuery.class));
    provider.addIncludeFilter(new AnnotationTypeFilter(UserDefinedFunctions.class));

    String basePackage = context.getEnvironment().getProperty(TeiidConstants.ENTITY_SCAN_DIR);
    if (basePackage == null) {
        logger.warn("***************************************************************");
        logger.warn("\"" + TeiidConstants.ENTITY_SCAN_DIR
                + "\" is NOT set, scanning entire classpath for @Entity classes.");
        logger.warn("consider setting this property to avoid time consuming scanning");
        logger.warn("***************************************************************");
        basePackage = "*";
    }

    // check to add any source models first based on the annotations
    boolean load = false;
    Set<BeanDefinition> components = provider.findCandidateComponents(basePackage);
    for (BeanDefinition c : components) {
        try {
            Class<?> clazz = Class.forName(c.getBeanClassName());
            ExcelTable excelAnnotation = clazz.getAnnotation(ExcelTable.class);

            if (excelAnnotation != null) {
                addExcelModel(vdb, clazz, excelAnnotation);
                load = true;
            }
        } catch (ClassNotFoundException e) {
            logger.warn("Error loading entity classes");
        }
    }

    ModelMetaData model = new ModelMetaData();
    model.setName(EXPOSED_VIEW);
    model.setModelType(Model.Type.VIRTUAL);
    MetadataFactory mf = new MetadataFactory(VDBNAME, VDBVERSION,
            SystemMetadata.getInstance().getRuntimeTypeMap(), model);

    if (components.isEmpty()) {
        if (isRedirectUpdatesEnabled(context)) {
            // when no @entity classes are defined then this is service based, sniff the
            // metadata
            // from Teiid models that are defined and build Hibernate metadata from it.
            buildVirtualBaseLayer(vdb, context, mf);
        } else {
            return false;
        }
    }

    Metadata metadata = getMetadata(components, namingStrategy, mf);
    UDFProcessor udfProcessor = new UDFProcessor(metadata, vdb);
    for (BeanDefinition c : components) {
        try {
            Class<?> clazz = Class.forName(c.getBeanClassName());
            Entity entityAnnotation = clazz.getAnnotation(Entity.class);
            SelectQuery selectAnnotation = clazz.getAnnotation(SelectQuery.class);
            TextTable textAnnotation = clazz.getAnnotation(TextTable.class);
            JsonTable jsonAnnotation = clazz.getAnnotation(JsonTable.class);
            ExcelTable excelAnnotation = clazz.getAnnotation(ExcelTable.class);
            UserDefinedFunctions udfAnnotation = clazz.getAnnotation(UserDefinedFunctions.class);

            if (textAnnotation != null && entityAnnotation != null) {
                new TextTableView(metadata).buildView(clazz, mf, textAnnotation);
            } else if (jsonAnnotation != null && entityAnnotation != null) {
                new JsonTableView(metadata).buildView(clazz, mf, jsonAnnotation);
            } else if (selectAnnotation != null && entityAnnotation != null) {
                new SimpleView(metadata).buildView(clazz, mf, selectAnnotation);
            } else if (excelAnnotation != null && entityAnnotation != null) {
                new ExcelTableView(metadata).buildView(clazz, mf, excelAnnotation);
            } else if (udfAnnotation != null) {
                udfProcessor.buildFunctions(clazz, mf, udfAnnotation);
            } else if (selectAnnotation == null && entityAnnotation != null) {
                new EntityBaseView(metadata, vdb, this).buildView(clazz, mf, entityAnnotation);
            }

            // check for sequence
            if (entityAnnotation != null) {
                udfProcessor.buildSequence(clazz, mf, entityAnnotation);
            }
        } catch (ClassNotFoundException e) {
            logger.warn("Error loading entity classes");
        }
    }
    udfProcessor.finishProcessing();

    // check if the redirection is in play
    if (isRedirectUpdatesEnabled(context)) {
        String redirectedDSName = getRedirectedDataSource(context);
        try {
            // rename current view model to something else
            model.setName("internal");
            model.setVisible(false);

            DataSource redirectedDS = (DataSource) ((SBConnectionFactoryProvider) getConnectionFactoryProviders()
                    .get(redirectedDSName)).getBean();
            String driverName = getDriverName(redirectedDS);
            if (driverName == null) {
                throw new IllegalStateException("Redirection of updates enabled, however datasource"
                        + " configured for redirection is not recognized.");
            }

            RedirectionSchemaBuilder mg = new RedirectionSchemaBuilder(context, redirectedDSName);
            // if none of the annotations defined, create layer with tables from all data
            // sources
            if (mf.getSchema().getTables().isEmpty()) {
                throw new IllegalStateException("Redirection of updates enabled, however there are no "
                        + "@Entity found. There must be atleast one @Entity for this feature to work.");
            }

            // now add the modified model that does the redirection
            ModelMetaData exposedModel = mg.buildRedirectionLayer(mf, EXPOSED_VIEW);
            vdb.addModel(exposedModel);

            // we need to create the schema in the redirected data source to store the
            // ephemeral data, will use
            // hibernate metadata for schema generation techniques.
            ModelMetaData redirectedModel = vdb.getModel(redirectedDSName);
            assert (redirectedModel != null);
            String dialect = redirectedModel.getPropertyValue(DIALECT);
            if (dialect == null) {
                throw new IllegalStateException(
                        "Redirection is enabled, however data source named \"" + redirectedDSName
                                + "\" cannot be used with schema initialization, choose a different data source"
                                + "as there are no schema generation facilities for this data source.");
            }
            new RedirectionSchemaInitializer(redirectedDS, redirectedDSName, getDialect(dialect), metadata,
                    this.metadataSources.getServiceRegistry(), mf.getSchema(), context).init();

            // reload the redirection model as it has new entries now after schema
            // generation.
            try {
                vdb.addModel(buildModelFromDataSource(redirectedDSName, driverName, context, false));
            } catch (AdminException e) {
                throw new IllegalStateException("Error adding the source, cause: " + e.getMessage());
            }
            load = true;
        } catch (BeansException e) {
            throw new IllegalStateException("Redirection is enabled, however data source named \""
                    + redirectedDSName + "\" is not configured. Please configure a data source.");
        }
    }
    if (!mf.getSchema().getTables().isEmpty()) {
        load = true;
        String ddl = DDLStringVisitor.getDDLString(mf.getSchema(), null, null);
        model.addSourceMetadata("DDL", ddl);
        vdb.addModel(model);
    }
    return load;
}

From source file:org.teiid.spring.autoconfigure.TeiidServer.java

boolean isRedirectUpdatesEnabled(ApplicationContext context) {
    return Boolean.parseBoolean(context.getEnvironment().getProperty(REDIRECTED));
}

From source file:org.teiid.spring.autoconfigure.TeiidServer.java

public String getRedirectedDataSource(ApplicationContext context) {
    return context.getEnvironment().getProperty(TeiidConstants.REDIRECTED + ".datasource", "redirected");
}