Skip to content

Commit 8c437f1

Browse files
Merge pull request #33 from ktor/master
fix: allow to read configuration files from caller bundle
2 parents c54d0fd + 62091d5 commit 8c437f1

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

src/main/java/com/ableneo/liferay/portal/setup/LiferaySetup.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.io.InputStream;
3333
import java.util.List;
3434
import java.util.Locale;
35+
36+
import org.osgi.framework.Bundle;
3537
import org.slf4j.Logger;
3638
import org.slf4j.LoggerFactory;
3739

@@ -49,7 +51,19 @@ private LiferaySetup() {}
4951
*/
5052
public static boolean setup(final File file) throws FileNotFoundException {
5153
Setup setup = MarshallUtil.unmarshall(file);
52-
return setup(setup);
54+
return setup(setup, null);
55+
}
56+
57+
/**
58+
* Helper method that unmarshalls xml configuration from data stream.
59+
*
60+
* @param inputStream data containing xml file that follows http://www.ableneo.com/liferay/setup schema
61+
* @param callerBundle caller bundle to resolve its file resources during processing
62+
* @return if setup was successfull
63+
*/
64+
public static boolean setup(final InputStream inputStream, Bundle callerBundle) {
65+
Setup setup = MarshallUtil.unmarshall(inputStream);
66+
return setup(setup, callerBundle);
5367
}
5468

5569
/**
@@ -60,7 +74,7 @@ public static boolean setup(final File file) throws FileNotFoundException {
6074
*/
6175
public static boolean setup(final InputStream inputStream) {
6276
Setup setup = MarshallUtil.unmarshall(inputStream);
63-
return setup(setup);
77+
return setup(setup, null);
6478
}
6579

6680
/**
@@ -70,6 +84,17 @@ public static boolean setup(final InputStream inputStream) {
7084
* @return true if all was set up fine
7185
*/
7286
public static boolean setup(final Setup setup) {
87+
return setup(setup, null);
88+
}
89+
90+
/**
91+
* Main setup method that sets up the data configured with xml.
92+
*
93+
* @param setup Setup object, parsed from xml configuration
94+
* @param callerBundle caller bundle to resolve its file resources during processing
95+
* @return true if all was set up fine
96+
*/
97+
public static boolean setup(final Setup setup, Bundle callerBundle) {
7398
if (setup == null) {
7499
throw new IllegalArgumentException(
75100
"Setup object cannot be null, without Setup object I cannot set up any data."
@@ -93,7 +118,7 @@ public static boolean setup(final Setup setup) {
93118
LOG.error("Could not find company: {}", company);
94119
continue;
95120
}
96-
SetupConfigurationThreadLocal.configureThreadLocalContent(runAsUserEmail, companyId);
121+
SetupConfigurationThreadLocal.configureThreadLocalContent(runAsUserEmail, companyId, callerBundle);
97122
executeSetupConfiguration(setup);
98123

99124
// iterate over group names or choose GUEST group for the company

src/main/java/com/ableneo/liferay/portal/setup/SetupConfigurationThreadLocal.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.List;
2222
import java.util.Locale;
2323
import java.util.Objects;
24+
25+
import org.osgi.framework.Bundle;
2426
import org.slf4j.Logger;
2527
import 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);

src/main/java/com/ableneo/liferay/portal/setup/core/util/ResourcesUtil.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.ableneo.liferay.portal.setup.core.util;
22

3+
import com.ableneo.liferay.portal.setup.SetupConfigurationThreadLocal;
34
import com.liferay.portal.kernel.util.FileUtil;
5+
import org.osgi.framework.Bundle;
6+
47
import java.io.IOException;
58
import java.io.InputStream;
9+
import java.net.URL;
610
import java.nio.charset.StandardCharsets;
711

812
/**
@@ -14,11 +18,26 @@ private ResourcesUtil() {}
1418

1519
public static InputStream getFileStream(String path) {
1620
ClassLoader cl = ResourcesUtil.class.getClassLoader();
17-
InputStream is = cl.getResourceAsStream(path);
18-
if (is == null) {
19-
throw new RuntimeException("Can not load file, does it exist? path:[" + path + "]");
21+
InputStream fileStream = cl.getResourceAsStream(path);
22+
if (fileStream == null) {
23+
// fail safe if caller bundle provided
24+
Bundle callerBundle = SetupConfigurationThreadLocal.getCallerBundle();
25+
if (callerBundle == null) {
26+
throw new RuntimeException("Can not load file, does it exist? path:[" + path + "]");
27+
} else {
28+
URL url = callerBundle.getEntry(path);
29+
if (url != null) {
30+
try {
31+
fileStream = url.openStream();
32+
} catch (IOException e) {
33+
throw new RuntimeException("Error loading file from bundle:" + callerBundle.getSymbolicName() + ", path:[" + path + "]", e);
34+
}
35+
} else {
36+
throw new RuntimeException("Can not load file from bundle:" + callerBundle.getSymbolicName() + ", does it exist? path:[" + path + "]");
37+
}
38+
}
2039
}
21-
return cl.getResourceAsStream(path);
40+
return fileStream;
2241
}
2342

2443
public static byte[] getFileBytes(String path) throws IOException {

0 commit comments

Comments
 (0)