Permalink
Browse files

updated to java config, later versions of Spring and Spring Data Mong…

…o and the Cloud Foundry runtime
  • Loading branch information...
joshlong committed Dec 9, 2011
1 parent c85a44e commit 701400c472773c5d9c1d08a41f4a54049530e7a3
Showing with 1,634 additions and 1,573 deletions.
  1. +385 −385 cross-store/pom.xml
  2. +123 −0 cross-store/src/main/java/org/cloudfoundry/config/ServicesConfiguration.java
  3. +49 −0 cross-store/src/main/java/org/cloudfoundry/config/WebConfiguration.java
  4. +0 −21 cross-store/src/main/java/org/cloudfoundry/services/CloudApplicationContextInitializer.java
  5. +22 −24 ...ain/java/org/springframework/data/mongodb/examples/custsvc/data/CrossStoreCustomerRepository.java
  6. +5 −6 ...tore/src/main/java/org/springframework/data/mongodb/examples/custsvc/data/CustomerRepository.java
  7. +42 −42 cross-store/src/main/java/org/springframework/data/mongodb/examples/custsvc/domain/Customer.java
  8. +25 −25 cross-store/src/main/java/org/springframework/data/mongodb/examples/custsvc/domain/Survey.java
  9. +15 −15 cross-store/src/main/java/org/springframework/data/mongodb/examples/custsvc/domain/SurveyInfo.java
  10. +35 −0 .../springframework/data/mongodb/examples/custsvc/web/CloudFoundryEnvironmentHandlerInterceptor.java
  11. +100 −109 ...-store/src/main/java/org/springframework/data/mongodb/examples/custsvc/web/CustSvcController.java
  12. +18 −19 cross-store/src/main/java/org/springframework/data/mongodb/examples/custsvc/web/NewCustomerForm.java
  13. +25 −32 ...s-store/src/main/java/org/springframework/data/mongodb/examples/custsvc/web/ViewCustomerForm.java
  14. +1 −1 cross-store/src/main/resources/META-INF/persistence.xml
  15. +0 −13 cross-store/src/main/webapp/WEB-INF/spring/appServlet/controllers.xml
  16. +0 −26 cross-store/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
  17. +0 −85 cross-store/src/main/webapp/WEB-INF/spring/root-context.xml
  18. +66 −62 cross-store/src/main/webapp/WEB-INF/views/customer/list.jsp
  19. +76 −72 cross-store/src/main/webapp/WEB-INF/views/customer/newCustomerForm.jsp
  20. +108 −104 cross-store/src/main/webapp/WEB-INF/views/customer/viewCustomerForm.jsp
  21. +43 −36 cross-store/src/main/webapp/WEB-INF/views/dump.jsp
  22. +41 −37 cross-store/src/main/webapp/WEB-INF/views/index.jsp
  23. +21 −31 cross-store/src/main/webapp/WEB-INF/web.xml
  24. +112 −112 cross-store/src/main/webapp/resources/css/colors.css
  25. +100 −94 cross-store/src/main/webapp/resources/css/local.css
  26. +222 −222 cross-store/src/main/webapp/resources/css/main.css
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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;
+ }
+}
@@ -1,21 +0,0 @@
-package org.cloudfoundry.services;
-
-import org.cloudfoundry.runtime.env.CloudEnvironment;
-import org.springframework.context.ApplicationContextInitializer;
-import org.springframework.context.ConfigurableApplicationContext;
-
-public class CloudApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
-
- @Override
- public void initialize(ConfigurableApplicationContext applicationContext) {
- CloudEnvironment env = new CloudEnvironment();
- if (env.getInstanceInfo() != null) {
- System.out.println("cloud API: " + env.getCloudApiUri());
- applicationContext.getEnvironment().setActiveProfiles("cloud");
- }
- else {
- applicationContext.getEnvironment().setActiveProfiles("default");
- }
- }
-
-}
@@ -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);
+ }
}
@@ -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);
}
@@ -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;
+ }
}
Oops, something went wrong.

0 comments on commit 701400c

Please sign in to comment.