/
PillarComponentFactory.java
239 lines (215 loc) · 10.2 KB
/
PillarComponentFactory.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
/*
* #%L
* Bitrepository Integration
*
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
* %%
* This program 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 program 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 General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package org.bitrepository.pillar;
import java.lang.reflect.Constructor;
import org.bitrepository.common.filestore.FileStore;
import org.bitrepository.common.settings.Settings;
import org.bitrepository.common.settings.XMLFileSettingsLoader;
import org.bitrepository.pillar.common.MessageHandlerContext;
import org.bitrepository.pillar.common.PillarAlarmDispatcher;
import org.bitrepository.pillar.common.SettingsHelper;
import org.bitrepository.pillar.store.ChecksumStorageModel;
import org.bitrepository.pillar.store.FileStorageModel;
import org.bitrepository.pillar.store.StorageModel;
import org.bitrepository.pillar.store.checksumdatabase.ChecksumDAO;
import org.bitrepository.pillar.store.checksumdatabase.ChecksumDatabaseManager;
import org.bitrepository.pillar.store.checksumdatabase.ChecksumStore;
import org.bitrepository.pillar.store.filearchive.CollectionArchiveManager;
import org.bitrepository.protocol.CoordinationLayerException;
import org.bitrepository.protocol.ProtocolComponentFactory;
import org.bitrepository.protocol.messagebus.MessageBus;
import org.bitrepository.protocol.security.BasicMessageAuthenticator;
import org.bitrepository.protocol.security.BasicMessageSigner;
import org.bitrepository.protocol.security.BasicOperationAuthorizer;
import org.bitrepository.protocol.security.BasicSecurityManager;
import org.bitrepository.protocol.security.MessageAuthenticator;
import org.bitrepository.protocol.security.MessageSigner;
import org.bitrepository.protocol.security.OperationAuthorizer;
import org.bitrepository.protocol.security.PermissionStore;
import org.bitrepository.protocol.security.SecurityManager;
import org.bitrepository.service.AlarmDispatcher;
import org.bitrepository.service.audit.AuditTrailContributerDAOFactory;
import org.bitrepository.service.audit.AuditTrailManager;
import org.bitrepository.service.contributor.ResponseDispatcher;
import org.bitrepository.service.database.DatabaseManager;
import org.bitrepository.settings.referencesettings.PillarType;
/**
* Component factory for this module.
*/
public final class PillarComponentFactory {
/** The singleton instance. */
private static PillarComponentFactory instance;
/**
* Instantiation of this singleton.
*
* @return The singleton instance of this factory class.
*/
public static synchronized PillarComponentFactory getInstance() {
// ensure singleton.
if(instance == null) {
instance = new PillarComponentFactory();
}
return instance;
}
/**
* Private constructor for initialization of the singleton.
*/
private PillarComponentFactory() {
}
/**
* Creates the pillar from on settings, key-file and pillar-ID.
* @param pathToSettings The path to the settings file.
* @param pathToKeyFile The path to the key file (can be empty).
* @param pillarID The id of the pillar (if null, the pillar-id in settings are used).
* @return The pillar.
*/
public Pillar createPillar(String pathToSettings, String pathToKeyFile, String pillarID) {
Settings settings = loadSettings(pillarID, pathToSettings);
SecurityManager securityManager = loadSecurityManager(pathToKeyFile, settings);
MessageBus messageBus = ProtocolComponentFactory.getInstance().getMessageBus(settings, securityManager);
return createPillar(settings, messageBus);
}
/**
* Creates a pillar from settings and message-bus.
* @param settings The instantiated settings.
* @param messageBus The messagebus.
* @return The pillar.
*/
public Pillar createPillar(Settings settings, MessageBus messageBus) {
ChecksumStore cache = getChecksumStore(settings);
AuditTrailManager audits = getAuditTrailManager(settings);
PillarAlarmDispatcher alarmDispatcher = new PillarAlarmDispatcher(settings, messageBus);
ResponseDispatcher responseDispatcher = new ResponseDispatcher(settings, messageBus);
StorageModel pillarModel = getPillarModel(settings, cache, alarmDispatcher);
MessageHandlerContext context = new MessageHandlerContext(
settings,
SettingsHelper.getPillarCollections(settings.getComponentID(), settings.getCollections()),
responseDispatcher,
alarmDispatcher,
audits,
ProtocolComponentFactory.getInstance().getFileExchange(settings));
return new Pillar(messageBus, settings, pillarModel, context);
}
/**
* Instantiates the ChecksumStore.
* @param settings The settings.
* @return The ChecksumStore.
*/
private ChecksumStore getChecksumStore(Settings settings) {
DatabaseManager checksumDatabaseManager = new ChecksumDatabaseManager(settings);
return new ChecksumDAO(checksumDatabaseManager);
}
/**
* Instantiates the AuditTrailManager.
* @param settings The settings.
* @return The AuditTrailManager.
*/
private AuditTrailManager getAuditTrailManager(Settings settings) {
AuditTrailContributerDAOFactory daoFactory = new AuditTrailContributerDAOFactory();
return daoFactory.getAuditTrailContributorDAO(
settings.getReferenceSettings().getPillarSettings().getAuditTrailContributerDatabase(),
settings.getComponentID());
}
/**
* Retrieves the FileStore defined in the settings.
* @param settings The settings.
* @return The filestore from settings, or the CollectionArchiveManager, if the setting is missing.
*/
@SuppressWarnings("unchecked")
private FileStore getFileStore(Settings settings) {
if(settings.getReferenceSettings().getPillarSettings().getFileStoreClass() == null) {
return new CollectionArchiveManager(settings);
}
try {
Class<FileStore> fsClass = (Class<FileStore>) Class.forName(
settings.getReferenceSettings().getPillarSettings().getFileStoreClass());
Constructor<FileStore> fsConstructor = fsClass.getConstructor(Settings.class);
return fsConstructor.newInstance(settings);
} catch (Exception e) {
throw new CoordinationLayerException("Could not instantiate the FileStore", e);
}
}
/**
* Instantiates the PillarModel.
* @param settings The settings.
* @param cache The ChecksumCache.
* @param alarmDispatcher The alarm dispatcher.
* @return The PillarModel, either for FullReferencePillar or ChecksumPillar.
*/
private StorageModel getPillarModel(Settings settings, ChecksumStore cache, AlarmDispatcher alarmDispatcher) {
PillarType pillarType = settings.getReferenceSettings().getPillarSettings().getPillarType();
if(pillarType == PillarType.CHECKSUM) {
return new ChecksumStorageModel(cache, alarmDispatcher, settings,
ProtocolComponentFactory.getInstance().getFileExchange(settings));
} else if(pillarType == PillarType.FILE) {
FileStore archive = getFileStore(settings);
return new FileStorageModel(archive, cache, alarmDispatcher, settings,
ProtocolComponentFactory.getInstance().getFileExchange(settings));
} else {
throw new IllegalStateException("Cannot instantiate a pillar of type '" + pillarType + "'.");
}
}
/** The default path for the settings in the development.*/
private static final String DEFAULT_PATH_TO_SETTINGS = "conf";
/** The default path for the settings in the development.*/
private static final String DEFAULT_PATH_TO_KEY_FILE = "conf/pillar.pem";
/**
* Method for retrieving the settings for the launcher.
* @param pathToSettings The path to the settings. If it is null or empty, then the default path is used.
* @return The settings.
*/
private static Settings loadSettings(String pillarID, String pathToSettings) {
String settingsPath;
if(pathToSettings == null || pathToSettings.isEmpty()) {
settingsPath = DEFAULT_PATH_TO_SETTINGS;
} else {
settingsPath = pathToSettings;
}
PillarSettingsProvider settingsLoader =
new PillarSettingsProvider(new XMLFileSettingsLoader(settingsPath), pillarID);
return settingsLoader.getSettings();
}
/**
* Instantiates the security manager based on the settings and the path to the key file.
* @param pathToPrivateKeyFile The path to the key file.
* @param settings The settings.
* @return The security manager.
*/
private static BasicSecurityManager loadSecurityManager(String pathToPrivateKeyFile, Settings settings) {
String privateKeyFile;
if(pathToPrivateKeyFile == null || pathToPrivateKeyFile.isEmpty()) {
privateKeyFile = DEFAULT_PATH_TO_KEY_FILE;
} else {
privateKeyFile = pathToPrivateKeyFile;
}
PermissionStore permissionStore = new PermissionStore();
MessageAuthenticator authenticator = new BasicMessageAuthenticator(permissionStore);
MessageSigner signer = new BasicMessageSigner();
OperationAuthorizer authorizer = new BasicOperationAuthorizer(permissionStore);
return new BasicSecurityManager(settings.getRepositorySettings(), privateKeyFile,
authenticator, signer, authorizer, permissionStore,
settings.getComponentID());
}
}