ClassNotFoundException when convereting permission names from database to classes [message #1859243] |
Mon, 22 May 2023 09:11 |
J D Messages: 103 Registered: February 2021 |
Senior Member |
|
|
Hi there everyone,
I have a list of permissions that I've retrieved from my database, and I would like to load/add these permissions for an authenticated user.
I was able to fetch the permissions as string values from the database BUT I cannot convert the string values to permission classes using:
(IPermission) Class.forName(permissionName).getConstructor().newInstance().
This is my revised ServerAccessControlService.java class
@Replace
public class ServerAccessControlService extends AccessControlService {
private static final Logger LOG = LoggerFactory.getLogger(ServerAccessControlService.class);
/*
* Fetch collections from the database based on the database userId
*/
@Override
protected IPermissionCollection execLoadPermissions(String userId) {
// Original
IPermissionCollection permissions = BEANS.get(DefaultPermissionCollection.class);
permissions.add(new RemoteServiceAccessPermission("*.shared.*", "*"), PermissionLevel.ALL);
if (ServerSession.get().getDbUserId() != "1") {
CategoryLabelResponseEntityDo response =
BEANS.get(CategoryLabelResourceClient.class).getPermissionsList("1", TEXTS.get("No"));
List<CategoryLabelEntityDo> labels = response.result().get().get(0);
labels.forEach(
label -> LOG.info(label.getString("autorisation") + " retrieved from database."));
List<String> permissionsList = new ArrayList<String>();
labels.forEach(label -> permissionsList.add(label.getString("autorisation")));
for (String permissionName : permissionsList) {
try {
// permissions.add(
// (IPermission) Class.forName(permissionName).getDeclaredConstructor().newInstance(),
// PermissionLevel.ALL);
//!!! THE NEXT LINE throws a ClassNotFound exception
permissions.add(
(IPermission) Class.forName(permissionName).getConstructor().newInstance(),
PermissionLevel.ALL);
} catch (Exception e) {
BEANS.get(ExceptionHandler.class).handle(e);
LOG.error("Cannot find permission " + permissionName + ": " + e.getMessage());
}
}
// For non-administrator roles (database userId greater than 1)
return permissions;
} else {
// For administrator role (database userId of 1)
return BEANS.get(AllPermissionCollection.class);
}
}
}
The line
permissions.add((IPermission) Class.forName(permissionName).getConstructor().newInstance(), PermissionLevel.ALL);
in the try block throws a ClassNotFoundException for each of the retrieved permission names.
Thanks a million for your kind assistance.
Cheers,
JD
[Updated on: Mon, 22 May 2023 09:12] Report message to a moderator
|
|
|
Re: ClassNotFoundException when convereting permission names from database to classes [message #1860901 is a reply to message #1859243] |
Thu, 07 September 2023 15:51 |
|
Hi JD
If a ClassNotFoundException is thrown, the class may actually be missing (e.g. because a JAR file that you have in your IDE is not present on the class path of the deployed server) or that the name is wrong. I suspect that this is the case here. What is the value of permissionName? Class.forName() requires the fully qualified class name (FQCN). If a class is named com.example.project.MyClass, it can only be loaded with the string "com.example.project.MyClass", but not "MyClass".
Regards,
Beat
|
|
|
|
Powered by
FUDForum. Page generated in 0.04024 seconds