org.springframework.cloud.Cloud Java Examples

The following examples show how to use org.springframework.cloud.Cloud. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringApplicationContextInitializer.java    From spring-music with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    Cloud cloud = getCloud();

    ConfigurableEnvironment appEnvironment = applicationContext.getEnvironment();

    String[] persistenceProfiles = getCloudProfile(cloud);
    if (persistenceProfiles == null) {
        persistenceProfiles = getActiveProfile(appEnvironment);
    }
    if (persistenceProfiles == null) {
        persistenceProfiles = new String[] { IN_MEMORY_PROFILE };
    }

    for (String persistenceProfile : persistenceProfiles) {
        appEnvironment.addActiveProfile(persistenceProfile);
    }
}
 
Example #2
Source File: ObjectStoreFileStorageFactoryBean.java    From multiapps-controller with Apache License 2.0 6 votes vote down vote up
private ObjectStoreServiceInfo getServiceInfo() {
    if (StringUtils.isEmpty(serviceName)) {
        LOGGER.warn("service name not specified in config files");
        return null;
    }
    try {
        CloudFactory cloudFactory = new CloudFactory();
        Cloud cloud = cloudFactory.getCloud();
        ServiceInfo serviceInfo = cloud.getServiceInfo(serviceName);
        if (serviceInfo instanceof ObjectStoreServiceInfo) {
            return (ObjectStoreServiceInfo) serviceInfo;
        }
        LOGGER.warn("Service instance did not match allowed label and plans.");
    } catch (CloudException e) {
        LOGGER.warn(MessageFormat.format("Failed to detect service info for service \"{0}\"!", serviceName), e);
    }
    return null;
}
 
Example #3
Source File: RabbitServiceAutoConfiguration.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link ConnectionFactory} using the singleton service
 * connector.
 * @param cloud {@link Cloud} instance to be used for accessing services.
 * @param connectorConfigObjectProvider the {@link ObjectProvider} for the
 * {@link RabbitConnectionFactoryConfig}.
 * @param applicationContext application context instance
 * @param rabbitProperties rabbit properties
 * @return the {@link ConnectionFactory} used by the binder.
 * @throws Exception if configuration of connection factory fails
 */
@Bean
@Primary
ConnectionFactory rabbitConnectionFactory(Cloud cloud,
		ObjectProvider<RabbitConnectionFactoryConfig> connectorConfigObjectProvider,
		ConfigurableApplicationContext applicationContext,
		RabbitProperties rabbitProperties) throws Exception {

	ConnectionFactory connectionFactory = cloud
			.getSingletonServiceConnector(ConnectionFactory.class,
					connectorConfigObjectProvider.getIfUnique());

	configureCachingConnectionFactory(
			(CachingConnectionFactory) connectionFactory,
			applicationContext, rabbitProperties);

	return connectionFactory;
}
 
Example #4
Source File: RabbitBinderModuleTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloudProfile() {
	this.context = new SpringApplicationBuilder(SimpleProcessor.class,
			MockCloudConfiguration.class).web(WebApplicationType.NONE)
					.profiles("cloud").run();
	BinderFactory binderFactory = this.context.getBean(BinderFactory.class);
	Binder<?, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
	assertThat(binder).isInstanceOf(RabbitMessageChannelBinder.class);
	DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder);
	ConnectionFactory binderConnectionFactory = (ConnectionFactory) binderFieldAccessor
			.getPropertyValue("connectionFactory");
	ConnectionFactory connectionFactory = this.context
			.getBean(ConnectionFactory.class);

	assertThat(binderConnectionFactory).isNotSameAs(connectionFactory);

	assertThat(TestUtils.getPropertyValue(connectionFactory, "addresses"))
			.isNotNull();
	assertThat(TestUtils.getPropertyValue(binderConnectionFactory, "addresses"))
			.isNull();

	Cloud cloud = this.context.getBean(Cloud.class);

	verify(cloud).getSingletonServiceConnector(ConnectionFactory.class, null);
}
 
Example #5
Source File: ContextInitializer.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Cloud getCloud() {
    try {
        CloudFactory cloudFactory = new CloudFactory();
        return cloudFactory.getCloud();
    } catch (CloudException e) {
        return null;
    }
}
 
Example #6
Source File: SolaceJavaAutoCloudConfiguration.java    From solace-java-spring-boot with Apache License 2.0 5 votes vote down vote up
@Deprecated
@Override
public List<SolaceMessagingInfo> getSolaceMessagingInfos() {
	List<SolaceMessagingInfo> solaceMessagingInfoList = new ArrayList<>();

	Cloud cloud = cloudFactory.getCloud();

	List<ServiceInfo> serviceInfos = cloud.getServiceInfos();
	for (ServiceInfo serviceInfo : serviceInfos) {
		if (serviceInfo instanceof SolaceMessagingInfo) {
			solaceMessagingInfoList.add((SolaceMessagingInfo) serviceInfo);
		}
	}
	return solaceMessagingInfoList;
}
 
Example #7
Source File: SolaceJavaAutoCloudConfiguration.java    From solace-java-spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the first detected {@link SolaceMessagingInfo}.
 *
 * @deprecated As of 1.1.0, usage of {@link SolaceMessagingInfo}
 * was replaced by its interface, {@link SolaceServiceCredentials}.
 * Use {@link SolaceJavaAutoConfigurationBase#findFirstSolaceServiceCredentials()} instead.
 *
 * @return If in a Cloud Foundry environment, a Solace PubSub+ service is returned, otherwise null
 */
@Deprecated
@Bean @Primary
public SolaceMessagingInfo findFirstSolaceMessagingInfo() {
	SolaceMessagingInfo solacemessaging = null;
	Cloud cloud = cloudFactory.getCloud();
	List<ServiceInfo> serviceInfos = cloud.getServiceInfos();
	for (ServiceInfo serviceInfo : serviceInfos) {
		// Stop when we find the first one...
		// TODO: Consider annotation driven selection, or sorted plan based
		// selection
		if (serviceInfo instanceof SolaceMessagingInfo) {
			solacemessaging = (SolaceMessagingInfo) serviceInfo;
			logger.info("Found Cloud Solace PubSub+ Service Instance Id: " + solacemessaging.getId());
			break;
		}
	}

	if (solacemessaging == null) {
		// The CloudCondition should shield from this happening, should not
		// arrive to this state.
		logger.error("Cloud Solace PubSub+ Info was not found, cannot auto-configure");
		throw new IllegalStateException(
				"Unable to create SpringJCSMPFactory did not find SolaceMessagingInfo in the current cloud environment");
	}

	return solacemessaging;
}
 
Example #8
Source File: ContextInitializer.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    Cloud cloud = getCloud();

    ConfigurableEnvironment appEnvironment = applicationContext.getEnvironment();

    String[] persistenceProfiles = getCloudProfiles(cloud);
    if (persistenceProfiles == null) {
        persistenceProfiles = new String[] { IN_MEMORY_PROFILE };
    }

    for (String persistenceProfile : persistenceProfiles) {
        appEnvironment.addActiveProfile(persistenceProfile);
    }
}
 
Example #9
Source File: ContextInitializer.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Cloud getCloud() {
    try {
        CloudFactory cloudFactory = new CloudFactory();
        return cloudFactory.getCloud();
    } catch (CloudException e) {
        return null;
    }
}
 
Example #10
Source File: ContextInitializer.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String[] getCloudProfiles(Cloud cloud) {
    if (cloud == null) {
        return null;
    }

    List<String> profiles = new ArrayList<>();

    List<ServiceInfo> serviceInfos = cloud.getServiceInfos();

    LOGGER.info("Found serviceInfos: " + StringUtils.collectionToCommaDelimitedString(serviceInfos));

    for (ServiceInfo serviceInfo : serviceInfos) {
        if (serviceTypeToProfileName.containsKey(serviceInfo.getClass())) {
            profiles.add(serviceTypeToProfileName.get(serviceInfo.getClass()));
        }
    }

    if (profiles.size() > 1) {
        throw new IllegalStateException(
                "Only one service of the following types may be bound to this application: " +
                        serviceTypeToProfileName.values().toString() + ". " +
                        "These services are bound to the application: [" +
                        StringUtils.collectionToCommaDelimitedString(profiles) + "]");
    }

    if (profiles.size() > 0) {
        return createProfileNames(profiles.get(0), "cloud");
    }

    return null;
}
 
Example #11
Source File: ContextInitializer.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    Cloud cloud = getCloud();

    ConfigurableEnvironment appEnvironment = applicationContext.getEnvironment();

    String[] persistenceProfiles = getCloudProfiles(cloud);
    if (persistenceProfiles == null) {
        persistenceProfiles = new String[] { IN_MEMORY_PROFILE };
    }

    for (String persistenceProfile : persistenceProfiles) {
        appEnvironment.addActiveProfile(persistenceProfile);
    }
}
 
Example #12
Source File: CustomerApplicationContextInitializer.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
/** 
 * Returns cloud object if the application runs on cloud environment
 * 
 * @return cloud object if the application runs on cloud environment
 */
private Cloud getCloud() {
	try {
		CloudFactory cloudFactory = new CloudFactory();
		return cloudFactory.getCloud();
	} catch (CloudException ce) {
		logger.error("no suitable cloud found");
		return null;
	}
}
 
Example #13
Source File: ContextInitializer.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String[] getCloudProfiles(Cloud cloud) {
    if (cloud == null) {
        return null;
    }

    List<String> profiles = new ArrayList<>();

    List<ServiceInfo> serviceInfos = cloud.getServiceInfos();

    LOGGER.info("Found serviceInfos: " + StringUtils.collectionToCommaDelimitedString(serviceInfos));

    for (ServiceInfo serviceInfo : serviceInfos) {
        if (serviceTypeToProfileName.containsKey(serviceInfo.getClass())) {
            profiles.add(serviceTypeToProfileName.get(serviceInfo.getClass()));
        }
    }

    if (profiles.size() > 1) {
        throw new IllegalStateException(
                "Only one service of the following types may be bound to this application: " +
                        serviceTypeToProfileName.values().toString() + ". " +
                        "These services are bound to the application: [" +
                        StringUtils.collectionToCommaDelimitedString(profiles) + "]");
    }

    if (profiles.size() > 0) {
        return createProfileNames(profiles.get(0), "cloud");
    }

    return null;
}
 
Example #14
Source File: TestRestController.java    From bluemix-cloud-connectors with Apache License 2.0 5 votes vote down vote up
private Cloud getCloud() {
  try {
    CloudFactory cloudFactory = new CloudFactory();
    return cloudFactory.getCloud();
  } catch (CloudException ce) {
    return null;
  }
}
 
Example #15
Source File: TestRestController.java    From bluemix-cloud-connectors with Apache License 2.0 5 votes vote down vote up
@GetMapping("/infos")
public String getInfos() {
  Cloud cloud = getCloud();
  List<ServiceInfo> infos = cloud.getServiceInfos();
  String result = "Info:\n";
  for (ServiceInfo info : infos) {
    result += info.getClass().toString() + "\n";
  }
  return result;
}
 
Example #16
Source File: AbstractWebApplicationInitializer.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
protected void createWebApplicationContext(ServletContext servletContext, Class clazz) {
        log.info("Creating Web Application Context started");

        List<Class> configClasses = new ArrayList<>();
        configClasses.add(clazz);

        // let's determine if this is a cloud based server
        Cloud cloud = getCloud();

        String activeProfiles = System.getProperty(SPRING_PROFILES_ACTIVE);

        if (StringUtils.isEmpty(activeProfiles)) {
            if (cloud == null) {
                // if no active profiles are specified, we default to these profiles
                activeProfiles = String.format("%s,%s,%s,%s,%s", MONGODB_LOCAL,REDIS_LOCAL,RABBIT_LOCAL,ELASTICSEARCH_LOCAL,LOCAL);
            } else {
                activeProfiles = String.format("%s,%s,%s,%s,%s", MONGODB_CLOUD,REDIS_CLOUD,RABBIT_CLOUD,ELASTICSEARCH_CLOUD,CLOUD);
            }
        }

        log.info("Active spring profiles: " + activeProfiles);

        // load local or cloud based configs
        if (cloud != null) {

            // list available service - fail servlet initializing if we are missing one that we require below
            printAvailableCloudServices(cloud.getServiceInfos());

        }

        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(configClasses.toArray(new Class[configClasses.size()]));

        servletContext.addListener(new ContextLoaderListener(appContext));
        servletContext.addListener(new RequestContextListener());

//        log.info("Creating Web Application Context completed");
    }
 
Example #17
Source File: AbstractWebApplicationInitializer.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
protected boolean isCloudServiceAvailable(Cloud cloud, String id) {
    if (cloud.getServiceInfo(id) != null) {
        return true;
    } else {
        String error = "Required cloud service: " + id + " not available";
        log.error(error);
        throw new RuntimeException(error);
    }
}
 
Example #18
Source File: AbstractWebApplicationInitializer.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
protected Cloud getCloud() {
    try {
        CloudFactory cloudFactory = new CloudFactory();
        return cloudFactory.getCloud();
    } catch (CloudException ce) {
        return null;
    }
}
 
Example #19
Source File: SpringApplicationContextInitializer.java    From spring-music with Apache License 2.0 5 votes vote down vote up
public String[] getCloudProfile(Cloud cloud) {
    if (cloud == null) {
        return null;
    }

    List<String> profiles = new ArrayList<>();

    List<ServiceInfo> serviceInfos = cloud.getServiceInfos();

    logger.info("Found serviceInfos: " + StringUtils.collectionToCommaDelimitedString(serviceInfos));

    for (ServiceInfo serviceInfo : serviceInfos) {
        if (serviceTypeToProfileName.containsKey(serviceInfo.getClass())) {
            profiles.add(serviceTypeToProfileName.get(serviceInfo.getClass()));
        }
    }

    if (profiles.size() > 1) {
        throw new IllegalStateException(
                "Only one service of the following types may be bound to this application: " +
                        serviceTypeToProfileName.values().toString() + ". " +
                        "These services are bound to the application: [" +
                        StringUtils.collectionToCommaDelimitedString(profiles) + "]");
    }

    if (profiles.size() > 0) {
        return createProfileNames(profiles.get(0), "cloud");
    }

    return null;
}
 
Example #20
Source File: SpringApplicationContextInitializer.java    From spring-music with Apache License 2.0 5 votes vote down vote up
private Cloud getCloud() {
    try {
        CloudFactory cloudFactory = new CloudFactory();
        return cloudFactory.getCloud();
    } catch (CloudException ce) {
        return null;
    }
}
 
Example #21
Source File: WorkerContextInitializer.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
	ConfigurableEnvironment applicationEnvironment = applicationContext.getEnvironment();
	Cloud cloud = getCloud();
	if (cloud != null) {
		applicationEnvironment.setActiveProfiles("cloud");

	} else {
		applicationEnvironment.setActiveProfiles("local");
	}

}
 
Example #22
Source File: SaleApplicationContextInitializer.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
	ConfigurableEnvironment applicationEnvironment = applicationContext.getEnvironment();
	Cloud cloud = getCloud();
	if (cloud != null) {
		applicationEnvironment.setActiveProfiles("cloud");

	} else {
		applicationEnvironment.setActiveProfiles("local");
	}

}
 
Example #23
Source File: SaleApplicationContextInitializer.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
/** 
 * Returns cloud object if the application runs on cloud environment
 * 
 * @return cloud object if the application runs on cloud environment
 */
private Cloud getCloud() {
	try {
		CloudFactory cloudFactory = new CloudFactory();
		return cloudFactory.getCloud();
	} catch (CloudException ce) {
		logger.error("no suitable cloud found");
		return null;
	}
}
 
Example #24
Source File: EnterpriseMessagingConfig.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
@Bean
public MessagingServiceFactory getMessagingServiceFactory() {
    ServiceConnectorConfig config = null; // currently there are no configurations for the MessagingService supported
    Cloud cloud = new CloudFactory().getCloud();
    // get the MessagingService via the service connector
    MessagingService messagingService = cloud.getSingletonServiceConnector(MessagingService.class, config);
    if (messagingService == null) {
        throw new IllegalStateException("Unable to create the MessagingService.");
    }
    return MessagingServiceFactoryCreator.createFactory(messagingService);
}
 
Example #25
Source File: TaxApplicationContextInitializer.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
/**
 * Returns cloud object if the application runs on cloud environment
 * 
 * @return cloud object if the application runs on cloud environment
 */
private Cloud getCloud() {
	try {
		CloudFactory cloudFactory = new CloudFactory();
		return cloudFactory.getCloud();
	} catch (CloudException ce) {
		logger.error("no suitable cloud found");
		return null;
	}
}
 
Example #26
Source File: TaxApplicationContextInitializer.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
	ConfigurableEnvironment applicationEnvironment = applicationContext.getEnvironment();
	Cloud cloud = getCloud();
	if (cloud != null) {
		logger.info("**********Initializing the application context for cloud env**********");
		applicationEnvironment.setActiveProfiles("cloud");

	} else {
		logger.info("**********Initializing the application context for local env**********");
		applicationEnvironment.setActiveProfiles("local");
	}

}
 
Example #27
Source File: ProductApplicationContextInitializer.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
/** 
 * Returns cloud object if the application runs on cloud environment
 * 
 * @return cloud object if the application runs on cloud environment
 */
private Cloud getCloud() {
	try {
		CloudFactory cloudFactory = new CloudFactory();
		return cloudFactory.getCloud();
	} catch (CloudException ce) {
		logger.error("no suitable cloud found");
		return null;
	}
}
 
Example #28
Source File: ProductApplicationContextInitializer.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
	ConfigurableEnvironment applicationEnvironment = applicationContext.getEnvironment();
	Cloud cloud = getCloud();
	if (cloud != null) {
		applicationEnvironment.addActiveProfile("cloud");

	} else {
		applicationEnvironment.addActiveProfile("local");
	}

}
 
Example #29
Source File: EnterpriseMessagingConfig.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
@Bean
public MessagingServiceFactory getMessagingServiceFactory() {
    ServiceConnectorConfig config = null; // currently there are no configurations for the MessagingService supported
    Cloud cloud = new CloudFactory().getCloud();
    // get the MessagingService via the service connector
    MessagingService messagingService = cloud.getSingletonServiceConnector(MessagingService.class, config);
    if (messagingService == null) {
        throw new IllegalStateException("Unable to create the MessagingService.");
    }
    return MessagingServiceFactoryCreator.createFactory(messagingService);
}
 
Example #30
Source File: WorkerContextInitializer.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
private Cloud getCloud() {
	try {
		CloudFactory cloudFactory = new CloudFactory();
		return cloudFactory.getCloud();
	} catch (CloudException ce) {
		logger.error("no suitable cloud found");
		return null;
	}
}