Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Commit

Permalink
updated to java config, later versions of Spring and Spring Data Mong…
Browse files Browse the repository at this point in the history
…o and the Cloud Foundry runtime
  • Loading branch information
joshlong committed Dec 9, 2011
1 parent c85a44e commit 701400c
Show file tree
Hide file tree
Showing 26 changed files with 1,634 additions and 1,573 deletions.
770 changes: 385 additions & 385 deletions cross-store/pom.xml

Large diffs are not rendered by default.

@@ -0,0 +1,123 @@
package org.cloudfoundry.config;

import javax.sql.DataSource;


import org.cloudfoundry.runtime.env.CloudEnvironment;
import org.cloudfoundry.runtime.env.MongoServiceInfo;
import org.cloudfoundry.runtime.env.RdbmsServiceInfo;
import org.cloudfoundry.runtime.service.document.MongoServiceCreator;
import org.cloudfoundry.runtime.service.relational.RdbmsServiceCreator;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoExceptionTranslator;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.crossstore.MongoChangeSetPersister;
import org.springframework.data.mongodb.crossstore.MongoDocumentBacking;
import org.springframework.data.mongodb.examples.custsvc.data.CrossStoreCustomerRepository;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ComponentScan(basePackageClasses = CrossStoreCustomerRepository.class)
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
public class ServicesConfiguration {


private String mongoDatabaseServiceName = "survey-mongo";

private String mysqlDatabaseServiceName = "survey-mysql";

@Bean
public CloudEnvironment cloudEnvironment() {
return new CloudEnvironment();
}

@Bean
public MongoServiceInfo mongoServiceInfo() {
return cloudEnvironment().getServiceInfo(mongoDatabaseServiceName, MongoServiceInfo.class);
}

@Bean
public MongoDbFactory mongoDbFactory() {
MongoServiceCreator mongoServiceCreator = new MongoServiceCreator();
return mongoServiceCreator.createService(mongoServiceInfo());
}

@Bean
public DataSource dataSource() {
RdbmsServiceInfo rdbmsServiceInfo = cloudEnvironment().getServiceInfo(mysqlDatabaseServiceName, RdbmsServiceInfo.class);
RdbmsServiceCreator rdbmsServiceCreator = new RdbmsServiceCreator();
DataSource dataSource = rdbmsServiceCreator.createService(rdbmsServiceInfo);
return dataSource;
}

@Bean
public MongoTemplate mongoTemplate() {
return new MongoTemplate(mongoDbFactory());
}

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
hibernateJpaVendorAdapter.setGenerateDdl(true);
return hibernateJpaVendorAdapter;
}

@Bean
public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() {
LocalContainerEntityManagerFactoryBean lcfb = new LocalContainerEntityManagerFactoryBean();
lcfb.setJpaVendorAdapter(jpaVendorAdapter());
lcfb.setDataSource(dataSource());
return lcfb;
}

@Bean
public PlatformTransactionManager platformTransactionManager() {
JpaTransactionManager txMan = new JpaTransactionManager();
txMan.setEntityManagerFactory(localContainerEntityManagerFactoryBean().getObject());
return txMan;
}

@Bean
public MongoDocumentBacking mongoDocumentBacking() {
MongoDocumentBacking mdb = MongoDocumentBacking.aspectOf();
mdb.setChangeSetPersister(changeSetPersister());
return mdb;
}

@Bean
public MongoChangeSetPersister changeSetPersister() {
MongoChangeSetPersister mongoChangeSetPersister = new MongoChangeSetPersister();
mongoChangeSetPersister.setEntityManagerFactory(localContainerEntityManagerFactoryBean().getObject());
mongoChangeSetPersister.setMongoTemplate(mongoTemplate());
return mongoChangeSetPersister;
}

@Bean
public MongoExceptionTranslator mongoExceptionTranslator() {
return new MongoExceptionTranslator();
}

@Bean
public PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor() {
return new PersistenceAnnotationBeanPostProcessor();
}

@Bean
public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
return new PersistenceExceptionTranslationPostProcessor();
}

}
@@ -0,0 +1,49 @@
package org.cloudfoundry.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.examples.custsvc.web.CloudFoundryEnvironmentHandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@Import(ServicesConfiguration.class)
@ComponentScan("org.springframework.data.mongodb.examples.custsvc.web")
public class WebConfiguration extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public CloudFoundryEnvironmentHandlerInterceptor interceptor() {
return new CloudFoundryEnvironmentHandlerInterceptor();
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(this.interceptor());
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver ir = new InternalResourceViewResolver();
ir.setPrefix("/WEB-INF/views/");
ir.setSuffix(".jsp");
return ir;
}
}

This file was deleted.

Expand Up @@ -13,29 +13,27 @@
@Repository
public class CrossStoreCustomerRepository implements CustomerRepository {

@PersistenceContext
EntityManager em;

@Override
public List<Customer> findAll() {
List<Customer> results = em.createQuery("select c from Customer c", Customer.class).getResultList();
return results;
}

@Override
public Customer findOne(Long id) {
Customer found = em.find(Customer.class, id);
return found;
}

@Override
public void save(Customer customer) {
em.merge(customer);
}

@Override
public void delete(Customer customer) {
em.remove(customer);
}
@PersistenceContext
EntityManager em;

@Override
public List<Customer> findAll() {
return em.createQuery("select c from Customer c", Customer.class).getResultList();
}

@Override
public Customer findOne(Long id) {
return em.find(Customer.class, id);
}

@Override
public void save(Customer customer) {
em.merge(customer);
}

@Override
public void delete(Customer customer) {
em.remove(customer);
}

}
Expand Up @@ -4,14 +4,13 @@

import org.springframework.data.mongodb.examples.custsvc.domain.Customer;


public interface CustomerRepository {

List<Customer> findAll();
List<Customer> findAll();

Customer findOne(Long id);

Customer findOne(Long id);

void save(Customer customer);
void save(Customer customer);

void delete(Customer customer);
void delete(Customer customer);
}
Expand Up @@ -5,51 +5,51 @@
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.springframework.data.document.annotation.RelatedDocument;
import org.springframework.data.mongodb.crossstore.RelatedDocument;

@Entity
public class Customer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
@RelatedDocument
private SurveyInfo surveyInfo;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Long getId() {
return id;
}

public SurveyInfo getSurveyInfo() {
if (surveyInfo == null) {
surveyInfo = new SurveyInfo();
}
return surveyInfo;
}
public void setSurveyInfo(SurveyInfo surveyInfo) {
this.surveyInfo = surveyInfo;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String firstName;

private String lastName;

@RelatedDocument
private SurveyInfo surveyInfo;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Long getId() {
return id;
}

public SurveyInfo getSurveyInfo() {
if (surveyInfo == null) {
surveyInfo = new SurveyInfo();
}
return surveyInfo;
}

public void setSurveyInfo(SurveyInfo surveyInfo) {
this.surveyInfo = surveyInfo;
}

}

0 comments on commit 701400c

Please sign in to comment.