Uploaded image for project: 'OpenDJ'
  1. OpenDJ
  2. OPENDJ-2743

Delay run-time linking of Unsafe until we are sure that it is available

    Details

    • Type: Bug
    • Status: Done
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.0.0
    • Fix Version/s: 4.0.0, 3.5.0
    • Component/s: backends
    • Labels:
      None

      Description

      The on disk merge import code directly references Unsafe which could potentially lead to a runtime failure in JVMs where this class is unavailable.

      A pattern for doing this sort of thing is as follows:

      class PlatfomDependentClientCode {
          // Detect whether Unsafe is available.
          static final boolean SUPPORTS_UNSAFE;
          static {
              boolean isUnsafeSupported = false;
              try {
                  isUnsafeSupported = Class.forName("sun.misc.Unsafe") != null;
              } catch (Throwable e) {
                  // Unsupported.
              }
              SUPPORTS_UNSAFE = isUnsafeSupported;
          }
      
          // Factory method creates platform specific instance.
          Xxx newXxx() {
              return SUPPORTS_UNSAFE ? new XxxUsingUnsafe() : new XxxNotUsingUnsafe();
          }
      
          private interface Xxx {
              ...
          }
      
          // This class will only be loaded if we are certain that Unsafe is available.
          private static class XxxUsingUnsafe implements Xxx {
              private static final Unsafe UNSAFE = Unsafe.getUnsafe();
              private static final long BYTE_ARRAY_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
      
              ...
          }
      
          // Fall-back for when Unsafe is not available.
          private static class XxxNotUsingUnsafe implements Xxx {
              ...
          }
      }
      

        Attachments

          Activity

            People

            • Assignee:
              matthew Matthew Swift
              Reporter:
              matthew Matthew Swift
              Dev Assignee:
              Matthew Swift
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: