-
Notifications
You must be signed in to change notification settings - Fork 534
/
IdentityUserProfileServiceComponent.java
205 lines (184 loc) · 9 KB
/
IdentityUserProfileServiceComponent.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
/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.identity.user.profile.mgt.internal;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.identity.core.util.IdentityCoreInitializedEvent;
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.identity.user.profile.mgt.association.federation.FederatedAssociationManager;
import org.wso2.carbon.identity.user.profile.mgt.association.federation.FederatedAssociationManagerImpl;
import org.wso2.carbon.identity.user.profile.mgt.listener.ProfileMgtEventListener;
import org.wso2.carbon.identity.user.profile.mgt.util.ServiceHodler;
import org.wso2.carbon.identity.user.store.configuration.listener.UserStoreConfigListener;
import org.wso2.carbon.idp.mgt.IdpManager;
import org.wso2.carbon.user.core.UserRealm;
import org.wso2.carbon.user.core.listener.UserOperationEventListener;
import org.wso2.carbon.user.core.service.RealmService;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
@Component(
name = "identity.user.profile.mgt.component",
immediate = true)
public class IdentityUserProfileServiceComponent {
private static final Log log = LogFactory.getLog(IdentityUserProfileServiceComponent.class);
@Activate
protected void activate(ComponentContext ctxt) {
try {
if (log.isDebugEnabled()) {
log.debug("User Profile Mgt bundle is activated ");
}
ServiceRegistration userStoreConfigEventSR =
ctxt.getBundleContext().registerService(UserStoreConfigListener.class.getName(),
new UserStoreConfigListenerImpl(), null);
if (userStoreConfigEventSR != null) {
if (log.isDebugEnabled()) {
log.debug("User profile management - UserStoreConfigListener registered.");
}
} else {
log.error("User profile management - UserStoreConfigListener could not be registered.");
}
ServiceRegistration profileMgtEventSR =
ctxt.getBundleContext().registerService(UserOperationEventListener.class.getName(),
new ProfileMgtEventListener(), null);
// Check whether the IDN tables exist at the beginning.
ServiceHodler.setIsIDNTableExist(isIDNTablesExist());
if (log.isDebugEnabled()) {
log.debug("Is IDN_ASSOCIATED_ID table exist: " + ServiceHodler.isIDNTableExist());
}
if (profileMgtEventSR != null) {
if (log.isDebugEnabled()) {
log.debug("User profile management - ProfileMgtEventListener registered.");
}
} else {
log.error("User profile management - ProfileMgtEventListener could not be registered.");
}
FederatedAssociationManager federatedAssociationManager = new FederatedAssociationManagerImpl();
ServiceRegistration federatedAssociationManagerSR = ctxt.getBundleContext().registerService(
FederatedAssociationManager.class.getName(), federatedAssociationManager, null);
if (federatedAssociationManagerSR != null) {
if (log.isDebugEnabled()) {
log.debug("User profile management - FederatedAssociationManager registered.");
}
IdentityUserProfileServiceDataHolder.getInstance()
.setFederatedAssociationManager(federatedAssociationManager);
} else {
log.error("User profile management - FederatedAssociationManager could not be registered.");
}
} catch (Throwable e) {
log.error("Failed to activate ProfileMgt bundle ", e);
}
}
@Deactivate
protected void deactivate(ComponentContext ctxt) {
// Update services data holder.
IdentityUserProfileServiceDataHolder.getInstance().setFederatedAssociationManager(null);
}
// the below two methods are kept just to be sure the realm is available.
@Reference(
name = "user.realm.default",
service = org.wso2.carbon.user.core.UserRealm.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetUserRealmDefault")
protected void setUserRealmDefault(UserRealm userRealmDefault) {
if (log.isDebugEnabled()) {
log.debug("Setting DefaultRealm in User Profile Management");
}
ServiceHodler.setInternalUserStore(userRealmDefault);
}
protected void unsetUserRealmDefault(UserRealm userRealmDefault) {
if (log.isDebugEnabled()) {
log.info("Un-setting DefaultRealm in User Profile Management");
}
ServiceHodler.setInternalUserStore(null);
}
@Reference(
name = "user.realmservice.default",
service = org.wso2.carbon.user.core.service.RealmService.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetRealmService")
protected void setRealmService(RealmService realmService) {
IdentityUserProfileServiceDataHolder.getInstance().setRealmService(realmService);
}
protected void unsetRealmService(RealmService realmService) {
IdentityUserProfileServiceDataHolder.getInstance().setRealmService(null);
}
@Reference(
name = "idp.mgt.IdpManager",
service = org.wso2.carbon.idp.mgt.IdpManager.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetIdpManager")
protected void setIdpManager(IdpManager idpManager) {
IdentityUserProfileServiceDataHolder.getInstance().setIdpManager(idpManager);
}
protected void unsetIdpManager(IdpManager idpManager) {
IdentityUserProfileServiceDataHolder.getInstance().setIdpManager(null);
}
@Reference(
name = "identityCoreInitializedEventService",
service = org.wso2.carbon.identity.core.util.IdentityCoreInitializedEvent.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetIdentityCoreInitializedEventService")
protected void setIdentityCoreInitializedEventService(IdentityCoreInitializedEvent identityCoreInitializedEvent) {
/* reference IdentityCoreInitializedEvent service to guarantee that this component will wait until identity core
is started */
}
protected void unsetIdentityCoreInitializedEventService(IdentityCoreInitializedEvent identityCoreInitializedEvent) {
/* reference IdentityCoreInitializedEvent service to guarantee that this component will wait until identity core
is started */
}
/**
* Checks whether the IDN_ASSOCIATED_ID table is present.
* @return True if table exist.
*/
private boolean isIDNTablesExist() {
try (Connection connection = IdentityDatabaseUtil.getDBConnection()) {
// Try to find whether there is IDN_ASSOCIATED_ID table.
DatabaseMetaData metaData = connection.getMetaData();
String tableName = "IDN_ASSOCIATED_ID";
if (metaData.storesLowerCaseIdentifiers()) {
tableName = tableName.toLowerCase();
}
try (ResultSet resultSet = metaData.getTables(null, null, tableName, new String[] { "TABLE" })) {
if (resultSet.next()) {
IdentityDatabaseUtil.commitTransaction(connection);
return true;
}
} catch (SQLException e) {
IdentityDatabaseUtil.rollbackTransaction(connection);
return false;
}
} catch (SQLException e) {
return false;
}
return false;
}
}