Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » ClassNotFoundException when convereting permission names from database to classes(Converting permission string name to classes fails)
ClassNotFoundException when convereting permission names from database to classes [message #1859243] Mon, 22 May 2023 09:11 Go to next message
J D is currently offline J DFriend
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 Go to previous messageGo to next message
Beat Schwarzentrub is currently offline Beat SchwarzentrubFriend
Messages: 213
Registered: November 2010
Senior Member
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
Re: ClassNotFoundException when convereting permission names from database to classes [message #1860933 is a reply to message #1860901] Sun, 10 September 2023 10:46 Go to previous message
J D is currently offline J DFriend
Messages: 103
Registered: February 2021
Senior Member
Hi there Beat,

Thanks for the tip. It is working properly now.

JD
Previous Topic:Application with and without authentication
Next Topic:Application not starting because of config.properties invalid private/public key pair
Goto Forum:
  


Current Time: Wed Oct 16 01:31:50 GMT 2024

Powered by FUDForum. Page generated in 0.04024 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top