01: package org.vraptor.plugin.jpa;
02:
03: import org.apache.log4j.Logger;
04: import org.vraptor.VRaptorException;
05: import org.vraptor.component.ComponentManager;
06: import org.vraptor.component.ComponentType;
07: import org.vraptor.plugin.VRaptorPlugin;
08: import org.vraptor.webapp.WebApplication;
09:
10: import java.text.MessageFormat;
11:
12: /**
13: * Created by IntelliJ IDEA.
14: * User: fck
15: * Date: Feb 28, 2007
16: * Time: 11:30:45 AM
17: */
18: public class JavaPersistencePlugin implements VRaptorPlugin {
19: private static final Logger LOG = Logger
20: .getLogger(JavaPersistencePlugin.class);
21:
22: private final EntityManagerIntrospector introspector = new EntityManagerIntrospector();
23:
24: public void init(WebApplication application)
25: throws VRaptorException {
26: ComponentManager manager = application.getComponentManager();
27: for (ComponentType component : manager.getComponents()) {
28: if (shouldBeWrapped(component)) {
29: LOG
30: .debug(MessageFormat
31: .format(
32: "Adding persistence functionality to the component {0}",
33: component));
34: manager.register(wrapComponent(component));
35: }
36: }
37:
38: }
39:
40: private ComponentType wrapComponent(ComponentType component) {
41: return new JavaPersistenceComponent(component, introspector);
42: }
43:
44: private boolean shouldBeWrapped(ComponentType component) {
45: return introspector.dependsOnEntityManager(component)
46: || introspector.hasPersistenceContext(component);
47: }
48:
49: }
|