2121import java .util .List ;
2222import java .util .Locale ;
2323import java .util .Objects ;
24+
25+ import org .osgi .framework .Bundle ;
2426import org .slf4j .Logger ;
2527import org .slf4j .LoggerFactory ;
2628
@@ -58,6 +60,11 @@ public final class SetupConfigurationThreadLocal {
5860 }
5961 );
6062
63+ private static final ThreadLocal <Bundle > _callerBundle = new CentralizedThreadLocal <>(
64+ SetupConfigurationThreadLocal .class + "._callerBundle" ,
65+ null
66+ );
67+
6168 private SetupConfigurationThreadLocal () {}
6269
6370 public static Long getRunAsUserId () {
@@ -84,6 +91,14 @@ public static void setRunInGroupId(Long runInGroupId) {
8491 _runInGroupId .set (runInGroupId );
8592 }
8693
94+ public static Bundle getCallerBundle () {
95+ return _callerBundle .get ();
96+ }
97+
98+ public static void setCallerBundle (Bundle callerBundle ) {
99+ _callerBundle .set (callerBundle );
100+ }
101+
87102 public static void cleanUp (
88103 String originalPrincipalName ,
89104 PermissionChecker originalPermissionChecker ,
@@ -92,6 +107,7 @@ public static void cleanUp(
92107 _runInCompanyId .remove ();
93108 _runAsUserId .remove ();
94109 _runInGroupId .remove ();
110+ _callerBundle .remove ();
95111
96112 PrincipalThreadLocal .setName (originalPrincipalName );
97113 PermissionThreadLocal .setPermissionChecker (originalPermissionChecker );
@@ -112,7 +128,7 @@ public static void cleanUp(
112128 static void configureThreadLocalContent (String runAsUserEmail , long companyId , Group group ) throws PortalException {
113129 Objects .requireNonNull (group );
114130 configureGroupExecutionContext (group );
115- configureThreadLocalContent (runAsUserEmail , companyId );
131+ configureThreadLocalContent (runAsUserEmail , companyId , ( Bundle ) null );
116132 }
117133
118134 public static void configureGroupExecutionContext (Group group ) {
@@ -131,15 +147,17 @@ public static void configureGroupExecutionContext(Group group) {
131147 * @throws PortalException user was not found, breaks the setup execution, we presume that if user email was
132148 * provided it is important to set up data as the user e.g. for easier cleanup
133149 */
134- static void configureThreadLocalContent (String runAsUserEmail , long companyId ) throws PortalException {
150+ static void configureThreadLocalContent (String runAsUserEmail , long companyId , Bundle callerBundle ) throws PortalException {
135151 if (Validator .isBlank (runAsUserEmail )) {
136152 setRunInCompanyId (companyId );
153+ setCallerBundle (callerBundle );
137154 SetupConfigurationThreadLocal .setRandomAdminPermissionCheckerForThread ();
138155 LOG .info ("Using default administrator." );
139156 } else {
140157 User user = UserLocalServiceUtil .getUserByEmailAddress (companyId , runAsUserEmail );
141158 setRunAsUserId (user .getUserId ());
142159 setRunInCompanyId (companyId );
160+ setCallerBundle (callerBundle );
143161 PrincipalThreadLocal .setName (user .getUserId ());
144162 PermissionChecker permissionChecker = PermissionCheckerFactoryUtil .create (user );
145163 PermissionThreadLocal .setPermissionChecker (permissionChecker );
0 commit comments