net.sourceforge.jnlp.runtime

## Class JNLPClassLoader

- java.lang.Object
- - java.lang.ClassLoader
  - - java.security.SecureClassLoader
    - - java.net.URLClassLoader
      - - net.sourceforge.jnlp.runtime.JNLPClassLoader

- All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

      public class JNLPClassLoader
      extends java.net.URLClassLoader

  Classloader that takes it's resources from a JNLP file. If the JNLP file defines extensions, separate classloaders for these will be created automatically. Classes are loaded with the security context when the classloader was created.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `JNLPClassLoader.CodeBaseClassLoader`
    `static class` | `JNLPClassLoader.DownloadAction` Actions to specify how cache is to be managed
    `static interface` | `JNLPClassLoader.SecurityDelegate` SecurityDelegate, in real usage, relies on having a "parent" JNLPClassLoader instance.
    `static class` | `JNLPClassLoader.SecurityDelegateImpl` Handles security decision logic for the JNLPClassLoader, eg which permission level to assign to JARs.
    `static class` | `JNLPClassLoader.SigningState`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static java.lang.String` | `APPLICATION`
    `static java.lang.String` | `TEMPLATE` Signed JNLP File and Template

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `JNLPClassLoader(JNLPFile file, UpdatePolicy policy)` Create a new JNLPClassLoader from the specified file.
    `protected` | `JNLPClassLoader(JNLPFile file, UpdatePolicy policy, java.lang.String mainName, boolean enableCodeBase)` Create a new JNLPClassLoader from the specified file.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `activateJars(java.util.List<JARDesc> jars)` Ensures that the list of jars have all been transferred, and makes them available to the classloader.
    `protected void` | `addAvailable()` Adds whatever resources have already been downloaded in the background.
    `protected JNLPClassLoader` | `addNextResource()` Adds the next unused resource to the classloader.
    `protected void` | `addPermission(java.security.Permission p)`
    `java.lang.String` | `checkForAttributeInJars(java.util.List<JARDesc> jars, java.util.jar.Attributes.Name name)` Checks for the jar that contains the attribute.
    `void` | `decrementLoaderUseCount()` Decrements loader use count by 1 If count reaches 0, loader is removed from list of available loaders
    `void` | `enableCodeBase()` Add applet's codebase URL.
    `protected void` | `fillInPartJars(java.util.List<JARDesc> jars)` Adds to the specified list of JARS any other JARs that need to be loaded at the same time as the JARs specified (ie, are in the same part).
    `protected java.lang.Class<?>` | `findClass(java.lang.String name)` Find the class in this loader or any of its extension loaders.
    `protected java.lang.String` | `findLibrary(java.lang.String lib)` Return the absolute path to the native library.
    `protected java.lang.String` | `findLibraryExt(java.lang.String lib)` Try to find the library path from another peer classloader.
    `protected java.lang.Class<?>` | `findLoadedClassAll(java.lang.String name)` Find the loaded class in this loader or any of its extension loaders.
    `java.net.URL` | `findResource(java.lang.String name)` Finds the resource in this, the parent, or the extension class loaders.
    `java.util.Enumeration<java.net.URL>` | `findResources(java.lang.String name)` Find the resources in this, the parent, or the extension class loaders.
    `java.security.AccessControlContext` | `getAccessControlContextForClassLoading()` Returns an appropriate AccessControlContext for loading classes in the running instance.
    `ApplicationInstance` | `getApplication()`
    `protected SecurityDesc` | `getCodeSourceSecurity(java.net.URL source)` Returns the security descriptor for given code source URL
    `java.lang.String` | `getExtensionHREF()` Deprecated.
    `java.lang.String` | `getExtensionName()` Deprecated.
    `static JNLPClassLoader` | `getInstance(JNLPFile file, UpdatePolicy policy, boolean enableCodeBase)` Returns a JNLP classloader for the specified JNLP file.
    `static JNLPClassLoader` | `getInstance(JNLPFile file, UpdatePolicy policy, java.lang.String mainName, boolean enableCodeBase)` Returns a JNLP classloader for the specified JNLP file.
    `static JNLPClassLoader` | `getInstance(java.net.URL location, java.lang.String uniqueKey, Version version, ParserSettings settings, UpdatePolicy policy, java.lang.String mainName, boolean enableCodeBase)` Returns a JNLP classloader for the JNLP file at the specified location.
    `JNLPFile` | `getJNLPFile()`
    `java.lang.String` | `getMainClass()`
    `java.lang.String` | `getManifestAttribute(java.net.URL location, java.util.jar.Attributes.Name attribute)` Gets the name of the main method if specified in the manifest
    `protected java.security.PermissionCollection` | `getPermissions(java.security.CodeSource cs)` Returns the permissions for the CodeSource.
    `protected SecurityDesc` | `getSecurity()`
    `boolean` | `getSigning()`
    `JNLPClassLoader.SigningState` | `getSigningState()`
    `boolean` | `hasMainJar()`
    `boolean` | `isStrict()`
    `java.lang.Class<?>` | `loadClass(java.lang.String name)` Find a JAR in the shared 'extension' classloaders, this classloader, or one of the classloaders for the JNLP file's extensions.
    `boolean` | `resourceAvailableLocally(java.lang.String s)` Returns if the specified resource is available locally from a cached jar
    `void` | `setApplication(ApplicationInstance app)` Sets the JNLP app this group is for; can only be called once.
    `void` | `setRunInSandbox()`
    `boolean` | `userPromptedForSandbox()`

    - ### Methods inherited from class java.net.URLClassLoader

      `addURL, close, definePackage, getResourceAsStream, getURLs, newInstance, newInstance`

    - ### Methods inherited from class java.security.SecureClassLoader

      `defineClass, defineClass`

    - ### Methods inherited from class java.lang.ClassLoader

      `clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLoadedClass, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### TEMPLATE

          public static final java.lang.String TEMPLATE

      Signed JNLP File and Template
      See Also:
        Constant Field Values

    - #### APPLICATION

          public static final java.lang.String APPLICATION

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### JNLPClassLoader

          protected JNLPClassLoader(JNLPFile file,
                                    UpdatePolicy policy)
                             throws LaunchException

      Create a new JNLPClassLoader from the specified file.
      Parameters:
        `file` - the JNLP file
        `policy` - update policy of loader
      Throws:
        `LaunchException` - if app can not be loaded

    - #### JNLPClassLoader

          protected JNLPClassLoader(JNLPFile file,
                                    UpdatePolicy policy,
                                    java.lang.String mainName,
                                    boolean enableCodeBase)
                             throws LaunchException

      Create a new JNLPClassLoader from the specified file.
      Parameters:
        `file` - the JNLP file
        `policy` - the UpdatePolicy for this class loader
        `mainName` - name of the application's main class
        `enableCodeBase` - switch whether this classloader can search in codebase or not
      Throws:
        `LaunchException` - when need to kill an app comes.

  - ### Method Detail

    - #### isStrict

          public boolean isStrict()

    - #### getInstance

          public static JNLPClassLoader getInstance(JNLPFile file,
                                                    UpdatePolicy policy,
                                                    boolean enableCodeBase)
                                             throws LaunchException

      Returns a JNLP classloader for the specified JNLP file.
      Parameters:
        `file` - the file to load classes for
        `policy` - the update policy to use when downloading resources
        `enableCodeBase` - true if codebase can be searched (ok for applets,false for apps)
      Returns:
        existing classloader. creates new if none reliable exists
      Throws:
        `LaunchException` - when launch is doomed

    - #### getInstance

          public static JNLPClassLoader getInstance(JNLPFile file,
                                                    UpdatePolicy policy,
                                                    java.lang.String mainName,
                                                    boolean enableCodeBase)
                                             throws LaunchException

      Returns a JNLP classloader for the specified JNLP file.
      Parameters:
        `file` - the file to load classes for
        `policy` - the update policy to use when downloading resources
        `mainName` - Overrides the main class name of the application
        `enableCodeBase` - ue if codebase can be searched (ok for applets,false for apps)
      Returns:
        existing classloader. creates new if none reliable exists
      Throws:
        `LaunchException` - when launch is doomed

    - #### getInstance

          public static JNLPClassLoader getInstance(java.net.URL location,
                                                    java.lang.String uniqueKey,
                                                    Version version,
                                                    ParserSettings settings,
                                                    UpdatePolicy policy,
                                                    java.lang.String mainName,
                                                    boolean enableCodeBase)
                                             throws java.io.IOException,
                                                    ParseException,
                                                    LaunchException

      Returns a JNLP classloader for the JNLP file at the specified location.
      Parameters:
        `location` - the file's location
        `uniqueKey` - key to manage applets/applications in shared vm
        `version` - the file's version
        `settings` - settings of parser
        `policy` - the update policy to use when downloading resources
        `mainName` - Overrides the main class name of the application
        `enableCodeBase` - whether to enable codebase search or not
      Returns:
        classlaoder of this appp
      Throws:
        `java.io.IOException` - when IO fails
        `ParseException` - when parsing fails
        `LaunchException` - when launch is doomed

    - #### checkForAttributeInJars

          public java.lang.String checkForAttributeInJars(java.util.List<JARDesc> jars,
                                                          java.util.jar.Attributes.Name name)

      Checks for the jar that contains the attribute.
      Parameters:
        `jars` - Jars that are checked to see if they contain the main class
        `name` - attribute to be found
      Returns:
        value of attribute if found

    - #### getManifestAttribute

          public java.lang.String getManifestAttribute(java.net.URL location,
                                                       java.util.jar.Attributes.Name attribute)

      Gets the name of the main method if specified in the manifest
      Parameters:
        `location` - The JAR location
        `attribute` - name of the attribute to find
      Returns:
        the attribute value, null if there isn't one of if there was an error

    - #### hasMainJar

          public boolean hasMainJar()

      Returns:
        true if this loader has the main jar

    - #### setRunInSandbox

          public void setRunInSandbox()
                               throws LaunchException

      Throws:
        `LaunchException`

    - #### userPromptedForSandbox

          public boolean userPromptedForSandbox()

    - #### enableCodeBase

          public void enableCodeBase()

      Add applet's codebase URL. This allows compatibility with applets that load resources from their codebase instead of through JARs, but can slow down resource loading. Resources loaded from the codebase are not cached.

    - #### setApplication

          public void setApplication(ApplicationInstance app)

      Sets the JNLP app this group is for; can only be called once.
      Parameters:
        `app` - application to be ser to this group

    - #### getApplication

          public ApplicationInstance getApplication()

      Returns:
        the JNLP app for this classloader

    - #### getJNLPFile

          public JNLPFile getJNLPFile()

      Returns:
        the JNLP file the classloader was created from.

    - #### getPermissions

          protected java.security.PermissionCollection getPermissions(java.security.CodeSource cs)

      Returns the permissions for the CodeSource.
      Overrides:
        `getPermissions` in class `java.net.URLClassLoader`

    - #### addPermission

          protected void addPermission(java.security.Permission p)

    - #### fillInPartJars

          protected void fillInPartJars(java.util.List<JARDesc> jars)

      Adds to the specified list of JARS any other JARs that need to be loaded at the same time as the JARs specified (ie, are in the same part).
      Parameters:
        `jars` - jar archives to be added

    - #### activateJars

          protected void activateJars(java.util.List<JARDesc> jars)

      Ensures that the list of jars have all been transferred, and makes them available to the classloader. If a jar contains native code, the libraries will be extracted and placed in the path.
      Parameters:
        `jars` - the list of jars to load

    - #### findLibrary

          protected java.lang.String findLibrary(java.lang.String lib)

      Return the absolute path to the native library.
      Overrides:
        `findLibrary` in class `java.lang.ClassLoader`

    - #### findLibraryExt

          protected java.lang.String findLibraryExt(java.lang.String lib)

      Try to find the library path from another peer classloader.
      Parameters:
        `lib` - library to be found
      Returns:
        location of library

    - #### findLoadedClassAll

          protected java.lang.Class<?> findLoadedClassAll(java.lang.String name)

      Find the loaded class in this loader or any of its extension loaders.
      Parameters:
        `name` - name of class
      Returns:
        the class found by name

    - #### loadClass

          public java.lang.Class<?> loadClass(java.lang.String name)
                                       throws java.lang.ClassNotFoundException

      Find a JAR in the shared 'extension' classloaders, this classloader, or one of the classloaders for the JNLP file's extensions. This method used to be qualified "synchronized." This was done solely for the purpose of ensuring only one thread entered the method at a time. This was not strictly necessary - ensuring that all affected fields are thread-safe is sufficient. Locking on the JNLPClassLoader instance when this method is called can result in deadlock if another thread is dealing with the CodebaseClassLoader at the same time. This solution is very heavy-handed as the instance lock is not truly required, and taking the lock on the classloader instance when not needed is not in general a good idea because it can and will lead to deadlock when multithreaded classloading is in effect. The solution is to keep the fields thread safe on their own. This is accomplished by wrapping them in Collections.synchronized* to provide atomic add/remove operations, and synchronizing on them when iterating or performing multiple mutations. See bug report RH976833. On some systems this bug will manifest itself as deadlock on every webpage with more than one Java applet, potentially also causing the browser process to hang. More information in the mailing list archives: http://mail.openjdk.java.net/pipermail/distro-pkg-dev/2013-September/024536.html Affected fields: available, classpaths, jarIndexes, jarEntries, jarLocationSecurityMap
      Overrides:
        `loadClass` in class `java.lang.ClassLoader`
      Throws:
        `java.lang.ClassNotFoundException`

    - #### findClass

          protected java.lang.Class<?> findClass(java.lang.String name)
                                          throws java.lang.ClassNotFoundException

      Find the class in this loader or any of its extension loaders.
      Overrides:
        `findClass` in class `java.net.URLClassLoader`
      Throws:
        `java.lang.ClassNotFoundException`

    - #### findResource

          public java.net.URL findResource(java.lang.String name)

      Finds the resource in this, the parent, or the extension class loaders.
      Overrides:
        `findResource` in class `java.net.URLClassLoader`
      Returns:
        a `URL` for the resource, or `null` if the resource could not be found.

    - #### findResources

          public java.util.Enumeration<java.net.URL> findResources(java.lang.String name)
                                                            throws java.io.IOException

      Find the resources in this, the parent, or the extension class loaders. Load lazy resources if not found in current resources.
      Overrides:
        `findResources` in class `java.net.URLClassLoader`
      Throws:
        `java.io.IOException`

    - #### resourceAvailableLocally

          public boolean resourceAvailableLocally(java.lang.String s)

      Returns if the specified resource is available locally from a cached jar
      Parameters:
        `s` - The name of the resource
      Returns:
        Whether or not the resource is available locally

    - #### addAvailable

          protected void addAvailable()

      Adds whatever resources have already been downloaded in the background.

    - #### addNextResource

          protected JNLPClassLoader addNextResource()
                                             throws LaunchException

      Adds the next unused resource to the classloader. That resource and all those in the same part will be downloaded and added to the classloader before returning. If there are no more resources to add, the method returns immediately.
      Returns:
        the classloader that resources were added to, or null
      Throws:
        `LaunchException` - Thrown if the signed JNLP file, within the main jar, fails to be verified or does not match

    - #### getExtensionName

          @Deprecated
          public java.lang.String getExtensionName()

      Deprecated.
      Returns:
        title if available. Substitutions if not.

    - #### getExtensionHREF

          @Deprecated
          public java.lang.String getExtensionHREF()

      Deprecated.
      Returns:
        location if jnlp

    - #### getSigning

          public boolean getSigning()

    - #### getSigningState

          public JNLPClassLoader.SigningState getSigningState()

    - #### getSecurity

          protected SecurityDesc getSecurity()

    - #### getCodeSourceSecurity

          protected SecurityDesc getCodeSourceSecurity(java.net.URL source)

      Returns the security descriptor for given code source URL
      Parameters:
        `source` - the origin (remote) url of the code
      Returns:
        The SecurityDescriptor for that source

    - #### decrementLoaderUseCount

          public void decrementLoaderUseCount()

      Decrements loader use count by 1 If count reaches 0, loader is removed from list of available loaders
      Throws:
        `java.lang.SecurityException` - if caller is not trusted

    - #### getAccessControlContextForClassLoading

          public java.security.AccessControlContext getAccessControlContextForClassLoading()

      Returns an appropriate AccessControlContext for loading classes in the running instance. The default context during class-loading only allows connection to codebase. However applets are allowed to load jars from arbitrary locations and the codebase only access falls short if a class from one location needs a class from another. Given protected access since CodeBaseClassloader uses this function too.
      Returns:
        The appropriate AccessControlContext for loading classes for this instance

    - #### getMainClass

          public java.lang.String getMainClass()

Submit a bug or feature