1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.data.jpa;
17
18 import org.kuali.rice.core.api.config.property.ConfigContext;
19 import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
20 import org.springframework.beans.factory.BeanClassLoaderAware;
21 import org.springframework.beans.factory.BeanFactory;
22 import org.springframework.beans.factory.BeanFactoryAware;
23 import org.springframework.beans.factory.BeanNameAware;
24 import org.springframework.beans.factory.DisposableBean;
25 import org.springframework.beans.factory.FactoryBean;
26 import org.springframework.beans.factory.InitializingBean;
27 import org.springframework.context.ResourceLoaderAware;
28 import org.springframework.context.weaving.LoadTimeWeaverAware;
29 import org.springframework.core.io.Resource;
30 import org.springframework.core.io.ResourceLoader;
31 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
32 import org.springframework.core.io.support.ResourcePatternResolver;
33 import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
34 import org.springframework.core.type.classreading.MetadataReader;
35 import org.springframework.core.type.classreading.MetadataReaderFactory;
36 import org.springframework.core.type.filter.AnnotationTypeFilter;
37 import org.springframework.core.type.filter.TypeFilter;
38 import org.springframework.dao.DataAccessException;
39 import org.springframework.dao.support.PersistenceExceptionTranslator;
40 import org.springframework.instrument.classloading.LoadTimeWeaver;
41 import org.springframework.orm.jpa.EntityManagerFactoryInfo;
42 import org.springframework.orm.jpa.JpaDialect;
43 import org.springframework.orm.jpa.JpaVendorAdapter;
44 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
45 import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
46 import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo;
47 import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
48 import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor;
49 import org.springframework.util.ClassUtils;
50
51 import javax.persistence.Converter;
52 import javax.persistence.EntityManager;
53 import javax.persistence.EntityManagerFactory;
54 import javax.persistence.PersistenceException;
55 import javax.persistence.spi.PersistenceProvider;
56 import javax.persistence.spi.PersistenceUnitInfo;
57 import javax.sql.DataSource;
58 import java.io.IOException;
59 import java.util.ArrayList;
60 import java.util.Arrays;
61 import java.util.HashMap;
62 import java.util.List;
63 import java.util.Map;
64 import java.util.Properties;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 public class KradEntityManagerFactoryBean implements FactoryBean<EntityManagerFactory>, BeanClassLoaderAware,
154 BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean, EntityManagerFactoryInfo,
155 PersistenceExceptionTranslator, ResourceLoaderAware, LoadTimeWeaverAware {
156
157 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
158 .getLogger(KradEntityManagerFactoryBean.class);
159
160
161
162
163
164
165
166
167
168
169
170
171 public static final String JPA_PROPERTY_PREFIX = "rice.krad.jpa.";
172
173
174
175
176
177
178
179
180
181
182
183 public static final String GLOBAL_JPA_PROPERTY_PREFIX = JPA_PROPERTY_PREFIX + "global.";
184
185 private static final boolean DEFAULT_EXCLUDE_UNLISTED_CLASSES = true;
186 private static final String DEFAULT_CONVERTERS_PACKAGE = BooleanYNConverter.class.getPackage().getName();
187
188 private final DefaultPersistenceUnitManager persistenceUnitManager;
189 private final LocalContainerEntityManagerFactoryBean internalFactoryBean;
190
191 private List<PersistenceUnitPostProcessor> persistenceUnitPostProcessors;
192 private List<String> managedClassNames;
193 private List<String> converterPackageNames;
194
195
196
197
198 public KradEntityManagerFactoryBean() {
199 this.persistenceUnitPostProcessors = new ArrayList<PersistenceUnitPostProcessor>();
200 this.managedClassNames = new ArrayList<String>();
201 this.converterPackageNames = new ArrayList<String>();
202 converterPackageNames.add(DEFAULT_CONVERTERS_PACKAGE);
203 this.persistenceUnitManager = createPersistenceUnitManager();
204 this.internalFactoryBean = createInternalFactoryBean(this.persistenceUnitManager);
205 this.internalFactoryBean.setJpaPropertyMap(createDefaultJpaProperties());
206 }
207
208
209
210
211
212
213
214
215
216 protected LocalContainerEntityManagerFactoryBean getInternalFactoryBean() {
217 return this.internalFactoryBean;
218 }
219
220
221
222
223
224
225 protected DefaultPersistenceUnitManager createPersistenceUnitManager() {
226 DefaultPersistenceUnitManager pum = new DefaultPersistenceUnitManager();
227
228
229
230
231
232
233 pum.setPersistenceXmlLocations(new String[0]);
234 pum.setMappingResources(new String[0]);
235 pum.setPackagesToScan(new String[0]);
236 return pum;
237 }
238
239
240
241
242
243
244
245 protected LocalContainerEntityManagerFactoryBean createInternalFactoryBean(PersistenceUnitManager manager) {
246 LocalContainerEntityManagerFactoryBean delegate = new LocalContainerEntityManagerFactoryBean();
247 delegate.setPersistenceUnitManager(manager);
248 return delegate;
249 }
250
251
252
253
254 @Override
255 public void afterPropertiesSet() throws PersistenceException {
256 if (persistenceUnitManager.getDefaultJtaDataSource() != null &&
257 persistenceUnitManager.getDefaultDataSource() != null) {
258 throw new IllegalStateException(getPersistenceUnitName() + ": " + getClass().getSimpleName()
259 + " was configured with both a JTA and Non-JTA "
260 + " datasource. Must configure one or the other, but not both.");
261 }
262
263 this.internalFactoryBean.setJpaPropertyMap(defaultAndMergeJpaProperties());
264 persistenceUnitManager.setPersistenceUnitPostProcessors(assemblePersistenceUnitPostProcessors());
265 persistenceUnitManager.afterPropertiesSet();
266 internalFactoryBean.afterPropertiesSet();
267 }
268
269
270
271
272
273
274 private Map<String, ?> createDefaultJpaProperties() {
275 Map<String, String> jpaProperties = new HashMap<String, String>();
276 loadGlobalJpaDefaults(jpaProperties);
277 loadPersistenceUnitJpaDefaults(jpaProperties);
278 loadCustomJpaDefaults(jpaProperties);
279
280 return jpaProperties;
281 }
282
283
284
285
286
287
288 private Map<String, ?> defaultAndMergeJpaProperties() {
289 Map<String, Object> jpaProperties = new HashMap<String, Object>(createDefaultJpaProperties());
290 Map<String, Object> configuredJpaPropertyMap = this.internalFactoryBean.getJpaPropertyMap();
291 jpaProperties.putAll(configuredJpaPropertyMap);
292 if (LOG.isDebugEnabled()) {
293 LOG.debug(getPersistenceUnitName() + ": JPA Properties Set:\n" + jpaProperties);
294 }
295
296 return jpaProperties;
297 }
298
299
300
301
302
303
304
305
306
307
308
309
310
311 protected void loadCustomJpaDefaults(Map<String, String> jpaProperties) {
312
313 }
314
315
316
317
318
319
320 protected void loadGlobalJpaDefaults(Map<String, String> jpaProperties) {
321 jpaProperties.putAll(ConfigContext.getCurrentContextConfig().getPropertiesWithPrefix(GLOBAL_JPA_PROPERTY_PREFIX,
322 true));
323 }
324
325
326
327
328
329
330 protected void loadPersistenceUnitJpaDefaults(Map<String, String> jpaProperties) {
331 jpaProperties.putAll(ConfigContext.getCurrentContextConfig().getPropertiesWithPrefix(
332 constructPersistenceUnitJpaPropertyPrefix(), true));
333 }
334
335
336
337
338
339 protected String constructPersistenceUnitJpaPropertyPrefix() {
340 return JPA_PROPERTY_PREFIX + getPersistenceUnitName() + ".";
341 }
342
343
344
345
346
347
348 protected PersistenceUnitPostProcessor[] assemblePersistenceUnitPostProcessors() {
349 this.persistenceUnitPostProcessors = new ArrayList<PersistenceUnitPostProcessor>(this.persistenceUnitPostProcessors);
350 this.persistenceUnitPostProcessors.add(new InternalPersistenceUnitPostProcessor());
351 return this.persistenceUnitPostProcessors.toArray(new PersistenceUnitPostProcessor[this.persistenceUnitPostProcessors.size()]);
352 }
353
354
355
356
357
358
359
360
361 public List<String> getManagedClassNames() {
362 return managedClassNames;
363 }
364
365
366
367
368
369
370
371 public PersistenceUnitPostProcessor[] getPersistenceUnitPostProcessors() {
372 return persistenceUnitPostProcessors.toArray(new PersistenceUnitPostProcessor[persistenceUnitPostProcessors.size()]);
373 }
374
375
376
377
378
379
380 protected DefaultPersistenceUnitManager getPersistenceUnitManager() {
381 return persistenceUnitManager;
382 }
383
384
385
386
387 @Override
388 public void destroy() {
389 internalFactoryBean.destroy();
390 }
391
392
393
394
395 @Override
396 public Class<? extends EntityManagerFactory> getObjectType() {
397 return internalFactoryBean.getObjectType();
398 }
399
400
401
402
403 @Override
404 public boolean isSingleton() {
405 return internalFactoryBean.isSingleton();
406 }
407
408
409
410
411 @Override
412 public EntityManagerFactory getObject() {
413 return internalFactoryBean.getObject();
414 }
415
416
417
418
419 @Override
420 public EntityManagerFactory getNativeEntityManagerFactory() {
421 return internalFactoryBean.getNativeEntityManagerFactory();
422 }
423
424
425
426
427 @Override
428 public void setBeanName(String name) {
429 internalFactoryBean.setBeanName(name);
430 }
431
432
433
434
435 @Override
436 public void setBeanFactory(BeanFactory beanFactory) {
437 internalFactoryBean.setBeanFactory(beanFactory);
438 }
439
440
441
442
443 @Override
444 public ClassLoader getBeanClassLoader() {
445 return internalFactoryBean.getBeanClassLoader();
446 }
447
448
449
450
451 @Override
452 public void setBeanClassLoader(ClassLoader classLoader) {
453 internalFactoryBean.setBeanClassLoader(classLoader);
454 }
455
456
457
458
459 @Override
460 public Class<? extends EntityManager> getEntityManagerInterface() {
461 return internalFactoryBean.getEntityManagerInterface();
462 }
463
464
465
466
467 @Override
468 public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) {
469 persistenceUnitManager.setLoadTimeWeaver(loadTimeWeaver);
470 }
471
472
473
474
475 @Override
476 public void setResourceLoader(ResourceLoader resourceLoader) {
477 persistenceUnitManager.setResourceLoader(resourceLoader);
478 }
479
480
481
482
483 @Override
484 public PersistenceUnitInfo getPersistenceUnitInfo() {
485 return internalFactoryBean.getPersistenceUnitInfo();
486 }
487
488
489
490
491 @Override
492 public String getPersistenceUnitName() {
493 return internalFactoryBean.getPersistenceUnitName();
494 }
495
496
497
498
499 @Override
500 public DataSource getDataSource() {
501 PersistenceUnitInfo pui = internalFactoryBean.getPersistenceUnitInfo();
502 if (internalFactoryBean.getPersistenceUnitInfo() != null) {
503 return (pui.getJtaDataSource() != null ?
504 pui.getJtaDataSource() :
505 pui.getNonJtaDataSource());
506 }
507 return (persistenceUnitManager.getDefaultJtaDataSource() != null ?
508 persistenceUnitManager.getDefaultJtaDataSource() :
509 this.persistenceUnitManager.getDefaultDataSource());
510 }
511
512
513
514
515 @Override
516 public JpaDialect getJpaDialect() {
517 return internalFactoryBean.getJpaDialect();
518 }
519
520
521
522
523 @Override
524 public PersistenceProvider getPersistenceProvider() {
525 return internalFactoryBean.getPersistenceProvider();
526 }
527
528
529
530
531 @Override
532 public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
533 return internalFactoryBean.translateExceptionIfPossible(ex);
534 }
535
536
537
538
539
540
541
542 public void setManagedClassNames(List<String> managedClassNames) {
543 if (managedClassNames == null) {
544 managedClassNames = new ArrayList<String>();
545 }
546 if (LOG.isInfoEnabled()) {
547 LOG.info(getPersistenceUnitName() + ": Setting Managed Class Names JPA:\n" + managedClassNames);
548 }
549 this.managedClassNames = managedClassNames;
550 }
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566 public void setEntityManagerInterface(Class<? extends EntityManager> emInterface) {
567 internalFactoryBean.setEntityManagerInterface(emInterface);
568 }
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583 public void setEntityManagerFactoryInterface(Class<? extends EntityManagerFactory> emfInterface) {
584 internalFactoryBean.setEntityManagerFactoryInterface(emfInterface);
585 }
586
587
588
589
590
591
592
593
594
595 public Map<String, Object> getJpaPropertyMap() {
596 return internalFactoryBean.getJpaPropertyMap();
597 }
598
599
600
601
602
603
604
605
606
607
608
609 public void setJpaPropertyMap(Map<String, ?> jpaProperties) {
610 internalFactoryBean.setJpaPropertyMap(jpaProperties);
611 }
612
613
614
615
616
617
618
619
620
621
622
623 public void setJpaProperties(Properties jpaProperties) {
624 internalFactoryBean.setJpaProperties(jpaProperties);
625 }
626
627
628
629
630
631
632
633
634
635
636
637
638
639 public void setPersistenceUnitName(String persistenceUnitName) {
640 internalFactoryBean.setPersistenceUnitName(persistenceUnitName);
641 persistenceUnitManager.setDefaultPersistenceUnitName(persistenceUnitName);
642 }
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664 public void setPackagesToScan(String... packagesToScan) {
665 if (LOG.isInfoEnabled()) {
666 LOG.info(getPersistenceUnitName() + ": Setting Packages to Scan for JPA Annotations:\n"
667 + Arrays.deepToString(packagesToScan));
668 }
669 persistenceUnitManager.setPackagesToScan(packagesToScan);
670 converterPackageNames = Arrays.asList(packagesToScan);
671 }
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691 public void setMappingResources(String... mappingResources) {
692 persistenceUnitManager.setMappingResources(mappingResources);
693 }
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715 public void setDataSource(DataSource dataSource) {
716 persistenceUnitManager.setDefaultDataSource(dataSource);
717 }
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737 public void setJtaDataSource(DataSource jtaDataSource) {
738 persistenceUnitManager.setDefaultJtaDataSource(jtaDataSource);
739 }
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755 public void setPersistenceProvider(PersistenceProvider persistenceProvider) {
756 this.internalFactoryBean.setPersistenceProvider(persistenceProvider);
757 }
758
759
760
761
762
763
764
765
766
767
768
769
770
771 public void setJpaDialect(JpaDialect jpaDialect) {
772 this.internalFactoryBean.setJpaDialect(jpaDialect);
773 }
774
775
776
777
778
779
780
781
782
783
784
785 public void setJpaVendorAdapter(JpaVendorAdapter jpaVendorAdapter) {
786 this.internalFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
787 }
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805 public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... postProcessors) {
806
807
808 this.persistenceUnitPostProcessors = new ArrayList<PersistenceUnitPostProcessor>(Arrays.asList(postProcessors));
809 }
810
811
812
813
814 private final class InternalPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor {
815
816 private final TypeFilter converterAnnotationTypeFilter = new AnnotationTypeFilter(Converter.class);
817 private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
818 private static final String ENTITY_CLASS_RESOURCE_PATTERN = "/**/*.class";
819
820
821
822
823 @Override
824 public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
825 pui.setExcludeUnlistedClasses(DEFAULT_EXCLUDE_UNLISTED_CLASSES);
826 processConverterPackages(pui);
827 for (String managedClassName : getManagedClassNames()) {
828 pui.addManagedClassName(managedClassName);
829 }
830 }
831
832
833
834
835
836
837 private void processConverterPackages(MutablePersistenceUnitInfo pui) {
838 if (converterPackageNames != null) {
839 for (String converterPackage : converterPackageNames) {
840
841 try {
842 String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
843 + ClassUtils.convertClassNameToResourcePath(converterPackage)
844 + ENTITY_CLASS_RESOURCE_PATTERN;
845 if (LOG.isInfoEnabled()) {
846 LOG.info(getPersistenceUnitName() + ": Scanning for JPA @Converter annotations in: "
847 + pattern);
848 }
849 Resource[] resources = this.resourcePatternResolver.getResources(pattern);
850 MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(
851 this.resourcePatternResolver);
852 for (Resource resource : resources) {
853 if (!resource.isReadable()) {
854 continue;
855 }
856 if (LOG.isDebugEnabled()) {
857 LOG.debug(getPersistenceUnitName() + ": Found Matching Resource: " + resource);
858 }
859 MetadataReader reader = readerFactory.getMetadataReader(resource);
860 String className = reader.getClassMetadata().getClassName();
861 if (!pui.getManagedClassNames().contains(className)
862 && converterAnnotationTypeFilter.match(reader, readerFactory)) {
863 pui.addManagedClassName(className);
864 if (LOG.isDebugEnabled()) {
865 LOG.debug(getPersistenceUnitName()
866 + ": Registering Converter in JPA Persistence Unit: " + className);
867 }
868 }
869 }
870 } catch (IOException ex) {
871 throw new PersistenceException("Failed to scan classpath converters in package: "
872 + converterPackage, ex);
873 }
874 }
875 }
876 }
877 }
878 }