1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.graph.connector.map;
25
26 import java.util.concurrent.TimeUnit;
27 import javax.transaction.xa.XAResource;
28 import org.modeshape.common.statistic.Stopwatch;
29 import org.modeshape.common.util.Logger;
30 import org.modeshape.graph.ExecutionContext;
31 import org.modeshape.graph.cache.CachePolicy;
32 import org.modeshape.graph.connector.RepositoryConnection;
33 import org.modeshape.graph.connector.RepositoryContext;
34 import org.modeshape.graph.connector.RepositorySourceException;
35 import org.modeshape.graph.observe.Observer;
36 import org.modeshape.graph.request.Request;
37 import org.modeshape.graph.request.processor.RequestProcessor;
38
39
40
41
42 public class MapRepositoryConnection implements RepositoryConnection {
43 private final MapRepositorySource source;
44 private final MapRepository repository;
45
46 public MapRepositoryConnection( MapRepositorySource source,
47 MapRepository repository ) {
48 assert source != null;
49 assert repository != null;
50 this.source = source;
51 this.repository = repository;
52 }
53
54
55
56
57 public String getSourceName() {
58 return source.getName();
59 }
60
61
62
63
64 public CachePolicy getDefaultCachePolicy() {
65 return source.getDefaultCachePolicy();
66 }
67
68
69
70
71 public XAResource getXAResource() {
72 return null;
73 }
74
75
76
77
78 public boolean ping( long time,
79 TimeUnit unit ) {
80 return true;
81 }
82
83
84
85
86 public void close() {
87
88 }
89
90
91
92
93
94
95
96 public void execute( ExecutionContext context,
97 Request request ) throws RepositorySourceException {
98 Logger logger = context.getLogger(getClass());
99 Stopwatch sw = null;
100 if (logger.isTraceEnabled()) {
101 sw = new Stopwatch();
102 sw.start();
103 }
104
105 RepositoryContext repositoryContext = this.source.getRepositoryContext();
106 Observer observer = repositoryContext != null ? repositoryContext.getObserver() : null;
107 RequestProcessor processor = new MapRequestProcessor(context, this.repository, observer, source.areUpdatesAllowed());
108
109 boolean commit = true;
110 MapRepositoryTransaction txn = repository.startTransaction(request.isReadOnly());
111 try {
112
113 processor.process(request);
114 if (request.hasError() && !request.isReadOnly()) {
115
116 commit = false;
117 }
118 } catch (Throwable error) {
119 commit = false;
120 } finally {
121 try {
122 processor.close();
123 } finally {
124
125 try {
126 if (commit) {
127 txn.commit();
128 } else {
129
130 txn.rollback();
131 }
132 } catch (Throwable commitOrRollbackError) {
133 if (commit && !request.hasError() && !request.isFrozen()) {
134
135 request.setError(commitOrRollbackError);
136 }
137 commit = false;
138 }
139 if (commit) {
140
141 processor.notifyObserverOfChanges();
142 }
143 }
144 }
145 if (logger.isTraceEnabled()) {
146 assert sw != null;
147 sw.stop();
148 logger.trace("MapRepositoryConnection.execute(...) took " + sw.getTotalDuration());
149 }
150 }
151
152
153
154
155
156
157 @Override
158 public String toString() {
159 return "Connection to the \"" + getSourceName() + "\" " + repository.getClass().getSimpleName();
160 }
161 }