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