Example usage for org.springframework.core.env Environment getProperty

List of usage examples for org.springframework.core.env Environment getProperty

Introduction

In this page you can find the example usage for org.springframework.core.env Environment getProperty.

Prototype

String getProperty(String key, String defaultValue);

Source Link

Document

Return the property value associated with the given key, or defaultValue if the key cannot be resolved.

Usage

From source file:org.springframework.boot.SpringApplication.java

private Banner selectBanner(Environment environment) {
    String location = environment.getProperty("banner.location", "banner.txt");
    ResourceLoader resourceLoader = this.resourceLoader != null ? this.resourceLoader
            : new DefaultResourceLoader(getClassLoader());
    Resource resource = resourceLoader.getResource(location);

    if (resource.exists()) {
        return new ResourceBanner(resource);
    }/* www.j av  a 2 s .  com*/
    if (this.banner != null) {
        return this.banner;
    }
    return DEFAULT_BANNER;
}

From source file:org.springframework.boot.SpringApplication.java

private String createStringFromBanner(Banner banner, Environment environment)
        throws UnsupportedEncodingException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    banner.printBanner(environment, this.mainApplicationClass, new PrintStream(baos));
    String charset = environment.getProperty("banner.charset", "UTF-8");
    return baos.toString(charset);
}

From source file:org.springframework.boot.SpringApplicationBannerPrinter.java

private Banner getTextBanner(Environment environment) {
    String location = environment.getProperty(BANNER_LOCATION_PROPERTY, DEFAULT_BANNER_LOCATION);
    Resource resource = this.resourceLoader.getResource(location);
    if (resource.exists()) {
        return new ResourceBanner(resource);
    }//ww  w. ja  va  2 s .c  om
    return null;
}

From source file:org.springframework.boot.SpringApplicationBannerPrinter.java

private String createStringFromBanner(Banner banner, Environment environment, Class<?> mainApplicationClass)
        throws UnsupportedEncodingException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    banner.printBanner(environment, mainApplicationClass, new PrintStream(baos));
    String charset = environment.getProperty("banner.charset", "UTF-8");
    return baos.toString(charset);
}

From source file:org.springframework.xd.dirt.plugins.spark.streaming.SparkStreamingPlugin.java

/**
 * Start spark streaming context for the given streaming processor.
 *
 * @param sparkConfigs the spark configuration properties
 * @param sparkStreamingSupport the underlying processor implementation
 *//* w w w .j  ava  2s. c o m*/
private void startSparkStreamingContext(Properties sparkConfigs,
        final SparkStreamingSupport sparkStreamingSupport, final Module module) {
    final Receiver receiver = module.getComponent(Receiver.class);
    Environment env = this.getApplicationContext().getEnvironment();
    String masterURL = env.getProperty(SparkStreamingSupport.SPARK_MASTER_URL_PROP,
            SparkStreamingSupport.SPARK_DEFAULT_MASTER_URL);
    final SparkConf sparkConf = setupSparkConf(module, masterURL, sparkConfigs);
    final String batchInterval = env.getProperty(
            SparkStreamingSupport.SPARK_STREAMING_BATCH_INTERVAL_MODULE_OPTION,
            env.getProperty(SparkStreamingSupport.SPARK_STREAMING_BATCH_INTERVAL_PROP,
                    SparkStreamingSupport.SPARK_STREAMING_DEFAULT_BATCH_INTERVAL));
    final SparkStreamingListener streamingListener = new SparkStreamingListener();

    final SparkMessageSender sender = (module.getType() == ModuleType.processor)
            ? module.getComponent(SparkMessageSender.class)
            : null;
    final StreamingContext streamingContext = new StreamingContext(sparkConf,
            new Duration(Long.valueOf(batchInterval)));
    streamingContext.addStreamingListener(streamingListener);
    Executors.newSingleThreadExecutor().execute(new Runnable() {
        @Override
        @SuppressWarnings("unchecked")
        public void run() {
            try {
                JavaStreamingContext javaStreamingContext = new JavaStreamingContext(streamingContext);
                streamingContexts.put(module, javaStreamingContext);
                JavaReceiverInputDStream javaInputDStream = javaStreamingContext.receiverStream(receiver);
                if (sparkStreamingSupport instanceof Processor) {
                    new ModuleExecutor().execute(javaInputDStream, (Processor) sparkStreamingSupport, sender);
                }
                if (sparkStreamingSupport instanceof org.springframework.xd.spark.streaming.scala.Processor) {
                    ReceiverInputDStream receiverInput = javaInputDStream.receiverInputDStream();
                    new org.springframework.xd.spark.streaming.scala.ModuleExecutor().execute(receiverInput,
                            (org.springframework.xd.spark.streaming.scala.Processor) sparkStreamingSupport,
                            sender);
                }
                javaStreamingContext.start();
                javaStreamingContext.awaitTermination();
            } catch (Exception e) {
                throw new IllegalStateException("Exception when running Spark Streaming application.", e);
            }
        }
    });
    try {
        boolean started = streamingListener.receiverStartLatch.await(30, TimeUnit.SECONDS);
        if (!started) {
            logger.warn("Deployment timed out when deploying Spark Streaming module " + sparkStreamingSupport);
        }
        if (!streamingListener.receiverStartSuccess.get()) {
            throw new IllegalStateException("Failed to start Spark Streaming Receiver");
        }
    } catch (InterruptedException ie) {
        throw new RuntimeException(ie);
    }
}