-
Notifications
You must be signed in to change notification settings - Fork 37
/
SMPMetaManager.java
333 lines (291 loc) · 11.5 KB
/
SMPMetaManager.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
/**
* Copyright (C) 2015-2019 Philip Helger and contributors
* philip[at]helger[dot]com
*
* The Original Code is Copyright The PEPPOL project (http://www.peppol.eu)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.helger.peppol.smpserver.domain;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.helger.commons.annotation.UsedViaReflection;
import com.helger.commons.exception.InitializationException;
import com.helger.commons.lang.ClassHelper;
import com.helger.peppol.identifier.factory.BDXRIdentifierFactory;
import com.helger.peppol.identifier.factory.IIdentifierFactory;
import com.helger.peppol.identifier.factory.PeppolIdentifierFactory;
import com.helger.peppol.identifier.factory.SimpleIdentifierFactory;
import com.helger.peppol.identifier.generic.participant.IParticipantIdentifier;
import com.helger.peppol.smpserver.ESMPIdentifierType;
import com.helger.peppol.smpserver.SMPServerConfiguration;
import com.helger.peppol.smpserver.backend.SMPBackendRegistry;
import com.helger.peppol.smpserver.domain.businesscard.ISMPBusinessCard;
import com.helger.peppol.smpserver.domain.businesscard.ISMPBusinessCardManager;
import com.helger.peppol.smpserver.domain.redirect.ISMPRedirectManager;
import com.helger.peppol.smpserver.domain.servicegroup.ISMPServiceGroupCallback;
import com.helger.peppol.smpserver.domain.servicegroup.ISMPServiceGroupManager;
import com.helger.peppol.smpserver.domain.servicegroup.LoggingSMPServiceGroupCallback;
import com.helger.peppol.smpserver.domain.servicegroup.SMPServiceGroup;
import com.helger.peppol.smpserver.domain.serviceinfo.ISMPServiceInformationManager;
import com.helger.peppol.smpserver.domain.sml.ISMLInfoManager;
import com.helger.peppol.smpserver.domain.transportprofile.ISMPTransportProfileManager;
import com.helger.peppol.smpserver.domain.user.ISMPUserManager;
import com.helger.peppol.smpserver.security.SMPKeyManager;
import com.helger.peppol.smpserver.security.SMPTrustManager;
import com.helger.peppol.smpserver.settings.ISMPSettings;
import com.helger.peppol.smpserver.settings.ISMPSettingsManager;
import com.helger.peppol.url.IPeppolURLProvider;
import com.helger.peppol.url.PeppolURLProvider;
import com.helger.scope.IScope;
import com.helger.scope.singleton.AbstractGlobalSingleton;
/**
* The central SMP meta manager containing all the singleton manager instances.
*
* @author Philip Helger
*/
public final class SMPMetaManager extends AbstractGlobalSingleton
{
private static final Logger LOGGER = LoggerFactory.getLogger (SMPMetaManager.class);
private static ISMPManagerProvider s_aManagerProvider = null;
private IIdentifierFactory m_aIdentifierFactory;
private IPeppolURLProvider m_aPeppolURLProvider;
private ISMLInfoManager m_aSMLInfoMgr;
private ISMPSettingsManager m_aSettingsMgr;
private ISMPTransportProfileManager m_aTransportProfileMgr;
private ISMPUserManager m_aUserMgr;
private ISMPServiceGroupManager m_aServiceGroupMgr;
private ISMPRedirectManager m_aRedirectMgr;
private ISMPServiceInformationManager m_aServiceInformationMgr;
private ISMPBusinessCardManager m_aBusinessCardMgr;
/**
* Set the manager provider to be used. This must be called exactly once
* before {@link #getInstance()} is called.
*
* @param aManagerProvider
* The manager factory to be used. May be <code>null</code> for testing
* purposes.
* @throws IllegalStateException
* If another manager provider is already present.
*/
public static void setManagerProvider (@Nullable final ISMPManagerProvider aManagerProvider)
{
if (s_aManagerProvider != null && aManagerProvider != null)
throw new IllegalStateException ("A manager provider is already set. You cannot set this twice! Call it with null before setting a new one");
if (isGlobalSingletonInstantiated (SMPMetaManager.class))
LOGGER.warn ("Setting the manager provider after singleton instantiation may not have the desired effect.");
s_aManagerProvider = aManagerProvider;
if (LOGGER.isInfoEnabled ())
{
if (aManagerProvider == null)
LOGGER.info ("Using no backend manager provider");
else
LOGGER.info ("Using " + aManagerProvider + " as the backend manager provider");
}
}
/**
* @return The currently set manager provider. May be <code>null</code>.
* @see #setManagerProvider(ISMPManagerProvider)
*/
@Nullable
public static ISMPManagerProvider getManagerProvider ()
{
return s_aManagerProvider;
}
@Deprecated
@UsedViaReflection
public SMPMetaManager ()
{}
private void _initCallbacks ()
{
// Always log
m_aServiceGroupMgr.serviceGroupCallbacks ().add (new LoggingSMPServiceGroupCallback ());
if (m_aBusinessCardMgr != null)
{
// If service group is deleted, also delete respective business card
m_aServiceGroupMgr.serviceGroupCallbacks ().add (new ISMPServiceGroupCallback ()
{
@Override
public void onSMPServiceGroupDeleted (@Nonnull final IParticipantIdentifier aParticipantID)
{
final String sServiceGroupID = SMPServiceGroup.createSMPServiceGroupID (aParticipantID);
final ISMPBusinessCard aBusinessCard = m_aBusinessCardMgr.getSMPBusinessCardOfID (sServiceGroupID);
if (aBusinessCard != null)
m_aBusinessCardMgr.deleteSMPBusinessCard (aBusinessCard);
}
});
}
}
@SuppressWarnings ("null")
@Override
protected void onAfterInstantiation (@Nonnull final IScope aScope)
{
if (s_aManagerProvider == null)
throw new InitializationException ("No ManagerProvider is set. Please call setManagerProvider before you call getInstance!");
try
{
final ESMPIdentifierType eIdentifierType = SMPServerConfiguration.getIdentifierType ();
switch (eIdentifierType)
{
case SIMPLE:
m_aIdentifierFactory = SimpleIdentifierFactory.INSTANCE;
break;
case PEPPOL:
m_aIdentifierFactory = PeppolIdentifierFactory.INSTANCE;
break;
case BDXR:
m_aIdentifierFactory = BDXRIdentifierFactory.INSTANCE;
break;
default:
throw new IllegalStateException ("Unsupporeted identifier type " + eIdentifierType + "!");
}
// Initialize first because the service group manager initializes the
// RegistrationHookFactory
try
{
SMPTrustManager.getInstance ();
}
catch (final Exception ex)
{
// fall through. No special trust store - no problem :)
}
try
{
SMPKeyManager.getInstance ();
}
catch (final Exception ex)
{
// fall through. Certificate stays invalid, no SML access possible.
}
// TODO make customizable
m_aPeppolURLProvider = PeppolURLProvider.INSTANCE;
m_aSMLInfoMgr = s_aManagerProvider.createSMLInfoMgr ();
if (m_aSMLInfoMgr == null)
throw new IllegalStateException ("Failed to create SML Info manager!");
m_aSettingsMgr = s_aManagerProvider.createSettingsMgr ();
if (m_aSettingsMgr == null)
throw new IllegalStateException ("Failed to create Settings manager!");
m_aTransportProfileMgr = s_aManagerProvider.createTransportProfileMgr ();
if (m_aTransportProfileMgr == null)
throw new IllegalStateException ("Failed to create TransportProfile manager!");
m_aUserMgr = s_aManagerProvider.createUserMgr ();
if (m_aUserMgr == null)
throw new IllegalStateException ("Failed to create User manager!");
// Service group manager must be before redirect and service information!
m_aServiceGroupMgr = s_aManagerProvider.createServiceGroupMgr ();
if (m_aServiceGroupMgr == null)
throw new IllegalStateException ("Failed to create ServiceGroup manager!");
m_aRedirectMgr = s_aManagerProvider.createRedirectMgr ();
if (m_aRedirectMgr == null)
throw new IllegalStateException ("Failed to create Redirect manager!");
m_aServiceInformationMgr = s_aManagerProvider.createServiceInformationMgr ();
if (m_aServiceInformationMgr == null)
throw new IllegalStateException ("Failed to create ServiceInformation manager!");
// May be null!
m_aBusinessCardMgr = s_aManagerProvider.createBusinessCardMgr (m_aServiceGroupMgr);
_initCallbacks ();
LOGGER.info (ClassHelper.getClassLocalName (this) + " was initialized");
}
catch (final Exception ex)
{
throw new InitializationException ("Failed to init " + ClassHelper.getClassLocalName (this), ex);
}
}
@Nonnull
public static SMPMetaManager getInstance ()
{
return getGlobalSingleton (SMPMetaManager.class);
}
@Nonnull
public static IIdentifierFactory getIdentifierFactory ()
{
return getInstance ().m_aIdentifierFactory;
}
@Nonnull
public static IPeppolURLProvider getPeppolURLProvider ()
{
return getInstance ().m_aPeppolURLProvider;
}
@Nonnull
public static ISMLInfoManager getSMLInfoMgr ()
{
return getInstance ().m_aSMLInfoMgr;
}
@Nonnull
public static ISMPSettingsManager getSettingsMgr ()
{
return getInstance ().m_aSettingsMgr;
}
@Nonnull
public static ISMPSettings getSettings ()
{
return getSettingsMgr ().getSettings ();
}
@Nonnull
public static ISMPTransportProfileManager getTransportProfileMgr ()
{
return getInstance ().m_aTransportProfileMgr;
}
@Nonnull
public static ISMPUserManager getUserMgr ()
{
return getInstance ().m_aUserMgr;
}
@Nonnull
public static ISMPServiceGroupManager getServiceGroupMgr ()
{
return getInstance ().m_aServiceGroupMgr;
}
@Nonnull
public static ISMPRedirectManager getRedirectMgr ()
{
return getInstance ().m_aRedirectMgr;
}
@Nonnull
public static ISMPServiceInformationManager getServiceInformationMgr ()
{
return getInstance ().m_aServiceInformationMgr;
}
@Nullable
public static ISMPBusinessCardManager getBusinessCardMgr ()
{
return getInstance ().m_aBusinessCardMgr;
}
public static boolean hasBusinessCardMgr ()
{
return getBusinessCardMgr () != null;
}
/**
* This is the initialization routine that must be called upon application
* startup. It performs the SPI initialization of all registered manager
* provider ({@link ISMPManagerProvider}) and selects the one specified in the
* SMP server configuration file.
*
* @throws InitializationException
* If an unsupported backend is provided in the configuration.
* @see SMPServerConfiguration#getBackend()
* @see SMPBackendRegistry
* @see ISMPManagerProvider
* @see #setManagerProvider(ISMPManagerProvider)
*/
public static void initBackendFromConfiguration ()
{
// Determine backend
final SMPBackendRegistry aBackendRegistry = SMPBackendRegistry.getInstance ();
final String sBackendID = SMPServerConfiguration.getBackend ();
final ISMPManagerProvider aManagerProvider = aBackendRegistry.getManagerProvider (sBackendID);
if (aManagerProvider == null)
throw new InitializationException ("Invalid backend '" +
sBackendID +
"' provided. Supported ones are: " +
aBackendRegistry.getAllBackendIDs ());
// Remember the manager provider
setManagerProvider (aManagerProvider);
// Now we can call getInstance to ensure everything is initialized correctly
getInstance ();
}
}