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.path;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.io.ObjectInputStream;
29 import java.util.Enumeration;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.UUID;
33 import javax.naming.BinaryRefAddr;
34 import javax.naming.RefAddr;
35 import javax.naming.Reference;
36 import javax.naming.StringRefAddr;
37 import net.jcip.annotations.ThreadSafe;
38 import org.modeshape.common.util.CheckArg;
39 import org.modeshape.graph.connector.RepositoryContext;
40 import org.modeshape.graph.connector.RepositorySourceException;
41 import org.modeshape.graph.connector.path.cache.NoCachePolicy;
42 import org.modeshape.graph.connector.path.cache.PathCachePolicy;
43 import org.modeshape.graph.connector.path.cache.PathRepositoryCache;
44
45
46
47
48
49 @ThreadSafe
50 public abstract class AbstractPathRepositorySource implements PathRepositorySource {
51
52 private static final long serialVersionUID = 1L;
53
54
55
56
57 public static final String DEFAULT_ROOT_NODE_UUID = "cafebabe-cafe-babe-cafe-babecafebabe";
58
59
60
61
62 public static final int DEFAULT_RETRY_LIMIT = 0;
63
64
65
66
67 public static final PathCachePolicy DEFAULT_CACHE_POLICY = new NoCachePolicy();
68
69 protected int retryLimit = DEFAULT_RETRY_LIMIT;
70 protected String name;
71
72 protected transient RepositoryContext repositoryContext;
73 protected transient UUID rootNodeUuid = UUID.fromString(DEFAULT_ROOT_NODE_UUID);
74 protected transient PathCachePolicy cachePolicy = DEFAULT_CACHE_POLICY;
75
76 private transient PathRepositoryCache repositoryCache = new PathRepositoryCache(cachePolicy);
77
78
79
80
81
82
83 public boolean areUpdatesAllowed() {
84 return false;
85 }
86
87
88
89
90
91
92 public RepositoryContext getRepositoryContext() {
93 return repositoryContext;
94 }
95
96
97
98
99
100
101 public void initialize( RepositoryContext context ) throws RepositorySourceException {
102 CheckArg.isNotNull(context, "context");
103 this.repositoryContext = context;
104 }
105
106
107
108
109
110
111 public PathCachePolicy getCachePolicy() {
112 return this.cachePolicy;
113 }
114
115
116
117
118
119
120
121 public void setCachePolicy( PathCachePolicy cachePolicy ) {
122 CheckArg.isNotNull(cachePolicy, "cachePolicy");
123
124 PathRepositoryCache oldCache = repositoryCache;
125 this.cachePolicy = cachePolicy;
126 this.repositoryCache = new PathRepositoryCache(cachePolicy);
127
128 oldCache.close();
129 }
130
131
132
133
134 public UUID getRootNodeUuid() {
135 return rootNodeUuid;
136 }
137
138
139
140
141
142 public void setRootNodeUuid( String rootNodeUuid ) {
143 if (rootNodeUuid != null && rootNodeUuid.trim().length() == 0) rootNodeUuid = DEFAULT_ROOT_NODE_UUID;
144 this.rootNodeUuid = UUID.fromString(rootNodeUuid);
145 }
146
147
148
149
150
151
152 public void close() {
153 }
154
155
156
157
158
159
160 public String getName() {
161 return name;
162 }
163
164
165
166
167
168
169 public void setName( String name ) {
170 if (name != null) {
171 name = name.trim();
172 if (name.length() == 0) name = null;
173 }
174
175 this.name = name;
176 }
177
178
179
180
181
182
183 public int getRetryLimit() {
184 return retryLimit;
185 }
186
187
188
189
190
191
192 public void setRetryLimit( int limit ) {
193 this.retryLimit = limit < 0 ? 0 : limit;
194 }
195
196
197
198
199
200
201
202
203
204
205 protected Map<String, Object> valuesFrom( Reference ref ) throws IOException, ClassNotFoundException {
206 Map<String, Object> values = new HashMap<String, Object>();
207
208 Enumeration<?> en = ref.getAll();
209 while (en.hasMoreElements()) {
210 RefAddr subref = (RefAddr)en.nextElement();
211 if (subref instanceof StringRefAddr) {
212 String key = subref.getType();
213 Object value = subref.getContent();
214 if (value != null) values.put(key, value.toString());
215 } else if (subref instanceof BinaryRefAddr) {
216 String key = subref.getType();
217 Object value = subref.getContent();
218 if (value instanceof byte[]) {
219
220 ByteArrayInputStream bais = new ByteArrayInputStream((byte[])value);
221 ObjectInputStream ois = new ObjectInputStream(bais);
222 value = ois.readObject();
223 values.put(key, value);
224 }
225 }
226 }
227
228 return values;
229 }
230
231
232
233
234 public PathRepositoryCache getPathRepositoryCache() {
235 return repositoryCache;
236 }
237 }