Esempio n. 1
0
  public Extension getExtension(String name) {
    if (name == null) return null;

    for (int i = 0; i < extensionList.size(); i++) {
      Extension p = getExtension(i);
      if (p.getName().equalsIgnoreCase(name)) {
        return p;
      }
    }

    return null;
  }
  /**
   * Loads all extensions within the GUACAMOLE_HOME/extensions directory, if any, adding their
   * static resource to the given resoure collections.
   *
   * @param javaScriptResources A modifiable collection of static JavaScript resources which may
   *     receive new JavaScript resources from extensions.
   * @param cssResources A modifiable collection of static CSS resources which may receive new CSS
   *     resources from extensions.
   */
  private void loadExtensions(
      Collection<Resource> javaScriptResources, Collection<Resource> cssResources) {

    // Retrieve and validate extensions directory
    File extensionsDir = new File(environment.getGuacamoleHome(), EXTENSIONS_DIRECTORY);
    if (!extensionsDir.isDirectory()) return;

    // Retrieve list of all extension files within extensions directory
    File[] extensionFiles =
        extensionsDir.listFiles(
            new FileFilter() {

              @Override
              public boolean accept(File file) {
                return file.isFile() && file.getName().endsWith(EXTENSION_SUFFIX);
              }
            });

    // Verify contents are accessible
    if (extensionFiles == null) {
      logger.warn(
          "Although GUACAMOLE_HOME/"
              + EXTENSIONS_DIRECTORY
              + " exists, its contents cannot be read.");
      return;
    }

    // Sort files lexicographically
    Arrays.sort(extensionFiles);

    // Load each extension within the extension directory
    for (File extensionFile : extensionFiles) {

      logger.debug("Loading extension: \"{}\"", extensionFile.getName());

      try {

        // Load extension from file
        Extension extension = new Extension(getParentClassLoader(), extensionFile);

        // Validate Guacamole version of extension
        if (!isCompatible(extension.getGuacamoleVersion())) {
          logger.debug(
              "Declared Guacamole version \"{}\" of extension \"{}\" is not compatible with this version of Guacamole.",
              extension.getGuacamoleVersion(),
              extensionFile.getName());
          throw new GuacamoleServerException(
              "Extension \""
                  + extension.getName()
                  + "\" is not "
                  + "compatible with this version of Guacamole.");
        }

        // Add any JavaScript / CSS resources
        javaScriptResources.addAll(extension.getJavaScriptResources().values());
        cssResources.addAll(extension.getCSSResources().values());

        // Attempt to load all authentication providers
        bindAuthenticationProviders(extension.getAuthenticationProviderClasses());

        // Add any translation resources
        serveLanguageResources(extension.getTranslationResources());

        // Add all HTML patch resources
        patchResourceService.addPatchResources(extension.getHTMLResources().values());

        // Add all static resources under namespace-derived prefix
        String staticResourcePrefix = "/app/ext/" + extension.getNamespace() + "/";
        serveStaticResources(staticResourcePrefix, extension.getStaticResources());

        // Serve up the small favicon if provided
        if (extension.getSmallIcon() != null)
          serve("/images/logo-64.png").with(new ResourceServlet(extension.getSmallIcon()));

        // Serve up the large favicon if provided
        if (extension.getLargeIcon() != null)
          serve("/images/logo-144.png").with(new ResourceServlet(extension.getLargeIcon()));

        // Log successful loading of extension by name
        logger.info("Extension \"{}\" loaded.", extension.getName());

      } catch (GuacamoleException e) {
        logger.error(
            "Extension \"{}\" could not be loaded: {}", extensionFile.getName(), e.getMessage());
        logger.debug("Unable to load extension.", e);
      }
    }
  }