/
RepositoryService.java
336 lines (294 loc) · 14.8 KB
/
RepositoryService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* JBoss, Home of Professional Open Source.
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
package org.modeshape.jboss.service;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.transaction.TransactionManager;
import org.infinispan.manager.CacheContainer;
import org.infinispan.schematic.Schematic;
import org.infinispan.schematic.document.Changes;
import org.infinispan.schematic.document.Document;
import org.infinispan.schematic.document.EditableArray;
import org.infinispan.schematic.document.EditableDocument;
import org.infinispan.schematic.document.Editor;
import org.jboss.as.clustering.jgroups.ChannelFactory;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.dmr.ModelNode;
import org.jboss.msc.service.Service;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.jgroups.Channel;
import org.modeshape.common.collection.Problems;
import org.modeshape.common.logging.Logger;
import org.modeshape.jboss.subsystem.MappedAttributeDefinition;
import org.modeshape.jcr.ConfigurationException;
import org.modeshape.jcr.Environment;
import org.modeshape.jcr.JcrEngine;
import org.modeshape.jcr.JcrRepository;
import org.modeshape.jcr.NoSuchRepositoryException;
import org.modeshape.jcr.RepositoryConfiguration;
import org.modeshape.jcr.RepositoryConfiguration.FieldName;
import org.modeshape.jcr.RepositoryConfiguration.FieldValue;
import org.modeshape.jcr.RepositoryConfiguration.QueryRebuild;
/**
* A <code>RepositoryService</code> instance is the service responsible for initializing a {@link JcrRepository} in the ModeShape
* engine using the information from the configuration.
*/
public class RepositoryService implements Service<JcrRepository>, Environment {
public static final String CONTENT_CONTAINER_NAME = "content";
public static final String BINARY_STORAGE_CONTAINER_NAME = "binaries";
private final InjectedValue<JcrEngine> engineInjector = new InjectedValue<JcrEngine>();
private final InjectedValue<CacheContainer> cacheManagerInjector = new InjectedValue<CacheContainer>();
private final InjectedValue<TransactionManager> txnMgrInjector = new InjectedValue<TransactionManager>();
private final InjectedValue<ChannelFactory> channelFactoryInjector = new InjectedValue<ChannelFactory>();
private final InjectedValue<IndexStorage> indexStorageConfigInjector = new InjectedValue<IndexStorage>();
private final InjectedValue<BinaryStorage> binaryStorageInjector = new InjectedValue<BinaryStorage>();
private final InjectedValue<String> dataDirectoryPathInjector = new InjectedValue<String>();
private RepositoryConfiguration repositoryConfiguration;
public RepositoryService( RepositoryConfiguration repositoryConfiguration ) {
this.repositoryConfiguration = repositoryConfiguration;
}
@Override
public JcrRepository getValue() throws IllegalStateException, IllegalArgumentException {
return null;
}
private JcrEngine getEngine() {
return engineInjector.getValue();
}
@Override
public CacheContainer getCacheContainer( String name ) {
CacheContainer container = null;
if (BINARY_STORAGE_CONTAINER_NAME.equals(name)) {
BinaryStorage storage = binaryStorageInjector.getValue();
container = storage.getCacheContainer();
}
if (container == null) {
container = cacheManagerInjector.getValue();
}
return container;
}
@Override
public Channel getChannel( String name ) throws Exception {
return channelFactoryInjector.getValue().createChannel(name);
}
@Override
public void shutdown() {
// Do nothing; this is the Environment's shutdown method
}
public final String repositoryName() {
return repositoryConfiguration.getName();
}
@Override
public void start( StartContext arg0 ) throws StartException {
JcrEngine engine = getEngine();
Logger logger = Logger.getLogger(getClass());
try {
final String repositoryName = repositoryName();
// Get the index storage configuration ...
IndexStorage indexStorageConfig = indexStorageConfigInjector.getValue();
Document queryConfig = null;
if (indexStorageConfig != null) {
queryConfig = indexStorageConfig.getQueryConfiguration();
} else {
// We'll use the default index storage, but this will be overwritten by the *IndexStorageAdd operation
// (that we're dependent upon). The default for non-AS7 ModeShape repositories is to use
// RAM index storage, but in AS7 we want to by default store the indexes on the filesystem in the
// AS7 data directory.
// We'll do this by setting a path relative to the data directory, and then injecting
// the "${jboss.server.data.dir}/modeshape" path into the repository service
// (which will then update the configuration prior to deployment) ...
EditableDocument query = Schematic.newDocument();
EditableDocument indexing = query.getOrCreateDocument(FieldName.INDEXING);
EditableDocument indexStorage = query.getOrCreateDocument(FieldName.INDEX_STORAGE);
EditableDocument backend = indexing.getOrCreateDocument(FieldName.INDEXING_BACKEND);
query.set(FieldName.REBUILD_UPON_STARTUP, QueryRebuild.IF_MISSING.toString().toLowerCase());
backend.set(FieldName.TYPE, FieldValue.INDEXING_BACKEND_TYPE_LUCENE);
indexStorage.set(FieldName.TYPE, FieldValue.INDEX_STORAGE_FILESYSTEM);
String dataDirPath = dataDirectoryPathInjector.getValue();
indexStorage.set(FieldName.INDEX_STORAGE_LOCATION, dataDirPath + "/" + repositoryName + "/indexes");
queryConfig = query;
}
assert queryConfig != null;
// Get the binary storage configuration ...
Document binaryConfig = null;
BinaryStorage binaryStorageConfig = binaryStorageInjector.getValue();
if (binaryStorageConfig != null) {
binaryConfig = binaryStorageConfig.getBinaryConfiguration();
} else {
// By default, store the binaries in the data directory ...
EditableDocument binaries = Schematic.newDocument();
binaries.set(FieldName.TYPE, FieldValue.BINARY_STORAGE_TYPE_FILE);
String dataDirPath = dataDirectoryPathInjector.getValue();
binaries.set(FieldName.DIRECTORY, dataDirPath + "/" + repositoryName + "/binaries");
binaryConfig = binaries;
}
// Create a new configuration document ...
EditableDocument config = Schematic.newDocument(repositoryConfiguration.getDocument());
config.setDocument(FieldName.QUERY, queryConfig);
config.getOrCreateDocument(FieldName.STORAGE).setDocument(FieldName.BINARY_STORAGE, binaryConfig);
if (logger.isDebugEnabled()) {
logger.debug("ModeShape configuration for '{0}' repository: {1}", repositoryName, config);
Problems problems = repositoryConfiguration.validate();
if (problems.isEmpty()) {
logger.debug("Problems with configuration for '{0}' repository: {1}", repositoryName, problems);
}
}
// Create a new (updated) configuration ...
repositoryConfiguration = new RepositoryConfiguration(config, repositoryName);
// Deploy the repository and use this as the environment ...
engine.deploy(repositoryConfiguration.with(this));
} catch (ConfigurationException e) {
throw new StartException(e);
} catch (RepositoryException e) {
throw new StartException(e);
}
}
@Override
public void stop( StopContext context ) {
JcrEngine engine = getEngine();
if (engine != null) {
try {
// Undeploy the repository ...
engine.undeploy(repositoryName());
} catch (NoSuchRepositoryException e) {
// The repository doesn't exist, so no worries ...
}
}
}
/**
* Immediately change and apply the specified field in the current repository configuration to the new value.
*
* @param defn the attribute definition for the value; may not be null
* @param newValue the new string value
* @throws RepositoryException if there is a problem obtaining the repository configuration or applying the change
* @throws OperationFailedException if there is a problem obtaining the raw value from the supplied model node
*/
public void changeField( MappedAttributeDefinition defn,
ModelNode newValue ) throws RepositoryException, OperationFailedException {
JcrEngine engine = getEngine();
String repositoryName = repositoryName();
// Get a snapshot of the current configuration ...
RepositoryConfiguration config = engine.getRepositoryConfiguration(repositoryName);
// Now start to make changes ...
Editor editor = config.edit();
// Find the Document containing the field ...
EditableDocument fieldContainer = editor;
for (String fieldName : defn.getPathToContainerOfField()) {
fieldContainer = editor.getOrCreateDocument(fieldName);
}
// Get the raw value from the model node ...
Object rawValue = defn.getTypedValue(newValue);
// Change the field ...
String fieldName = defn.getFieldName();
fieldContainer.set(fieldName, rawValue);
// Apply the changes to the current configuration ...
Changes changes = editor.getChanges();
engine.update(repositoryName, changes);
}
/**
* Immediately change and apply the specified sequencer field in the current repository configuration to the new value.
*
* @param defn the attribute definition for the value; may not be null
* @param newValue the new string value
* @param sequencerName the name of the sequencer
* @throws RepositoryException if there is a problem obtaining the repository configuration or applying the change
* @throws OperationFailedException if there is a problem obtaining the raw value from the supplied model node
*/
public void changeSequencerField( MappedAttributeDefinition defn,
ModelNode newValue,
String sequencerName ) throws RepositoryException, OperationFailedException {
JcrEngine engine = getEngine();
String repositoryName = repositoryName();
// Get a snapshot of the current configuration ...
RepositoryConfiguration config = engine.getRepositoryConfiguration(repositoryName);
// Now start to make changes ...
Editor editor = config.edit();
// Find the array of sequencer documents ...
List<String> pathToContainer = defn.getPathToContainerOfField();
EditableDocument sequencing = editor.getOrCreateDocument(pathToContainer.get(0));
EditableArray sequencers = sequencing.getOrCreateArray(pathToContainer.get(1));
// The container should be an array ...
for (int i = 0; i != sequencers.size(); ++i) {
// All these entries should be nested documents ...
EditableDocument sequencer = (EditableDocument)sequencers.get(i);
// Look for the entry with a name that matches our sequencer name ...
if (sequencerName.equals(sequencer.getString(FieldName.NAME))) {
// Change the field ...
String fieldName = defn.getFieldName();
// Get the raw value from the model node ...
Object rawValue = defn.getTypedValue(newValue);
// And update the field ...
sequencer.set(fieldName, rawValue);
break;
}
}
// Get and apply the changes to the current configuration. Note that the 'update' call asynchronously
// updates the configuration, and returns a Future<JcrRepository> that we could use if we wanted to
// wait for the changes to take place. But we don't want/need to wait, so we'll not use the Future ...
Changes changes = editor.getChanges();
engine.update(repositoryName, changes);
}
/**
* @return the injector used to set the configuration details for the index storage
*/
public InjectedValue<IndexStorage> getIndexStorageConfigInjector() {
return indexStorageConfigInjector;
}
/**
* @return the injector used to set the configuration details for the binaries storage
*/
public InjectedValue<BinaryStorage> getBinaryStorageInjector() {
return binaryStorageInjector;
}
/**
* @return the injector used to set the JcrEngine reference
*/
public InjectedValue<JcrEngine> getEngineInjector() {
return engineInjector;
}
/**
* @return the injector used to set the TransactionManager reference
*/
public InjectedValue<TransactionManager> getTransactionManagerInjector() {
return txnMgrInjector;
}
/**
* @return the injector used to set the CacheContainer reference used for content storage
*/
public InjectedValue<CacheContainer> getCacheManagerInjector() {
return cacheManagerInjector;
}
/**
* @return the injector used to set the JGroups' ChannelFactory
*/
public InjectedValue<ChannelFactory> getChannelFactoryInjector() {
return channelFactoryInjector;
}
/**
* @return the injector used to set the data directory for this repository
*/
public InjectedValue<String> getDataDirectoryPathInjector() {
return dataDirectoryPathInjector;
}
}