Esempio n. 1
0
  @SuppressWarnings("unchecked")
  public static <P extends Plugin, VPP extends P> List<P> createPluginsOfType(
      BRJS brjs, Class<P> pluginInterface, Class<VPP> virtualProxyClass) {
    ClassLoader classLoader = Plugin.class.getClassLoader();
    try {
      pluginInterface = (Class<P>) classLoader.loadClass(pluginInterface.getCanonicalName());
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }

    Logger logger = brjs.logger(BRJSPluginLocator.class);
    List<P> objectList = new ArrayList<P>();

    try {
      ServiceLoader<P> loader = ServiceLoader.load(pluginInterface, classLoader);
      Iterator<P> objectIterator = loader.iterator();

      while (objectIterator.hasNext()) {
        P object = objectIterator.next();

        if (virtualProxyClass != null) {
          object = virtualProxyClass.getConstructor(pluginInterface).newInstance(object);
        }

        objectList.add(object);
      }
    } catch (ServiceConfigurationError serviceError) {
      Throwable cause = serviceError.getCause();

      if (cause != null && cause.getClass() == InstantiationException.class) {
        if (logger != null) {
          logger.error(Messages.CANNOT_CREATE_INSTANCE_LOG_MSG, cause.getMessage());
        } else {
          System.err.println(
              String.format(Messages.CANNOT_CREATE_INSTANCE_LOG_MSG, cause.getMessage()));
        }
      } else {
        if (logger != null) {
          logger.error(Messages.ERROR_CREATING_OBJECT_LOG_MSG, serviceError);
        } else {
          System.err.println(String.format(Messages.ERROR_CREATING_OBJECT_LOG_MSG, serviceError));
        }
      }
    } catch (NullPointerException
        | NoSuchMethodException
        | InstantiationException
        | IllegalAccessException
        | IllegalArgumentException
        | InvocationTargetException
        | SecurityException e) {
      throw new RuntimeException(e);
    }

    // use this utility to set BRJS so we catch any runtime errors thrown by model observers
    PluginLocatorUtils.setBRJSForPlugins(brjs, objectList);

    return objectList;
  }
  @Override
  protected void doCommand(JSAPResult parsedArgs)
      throws CommandArgumentsException, CommandOperationException {
    String appName = parsedArgs.getString("app-name");
    String disclaimer = "/*\n* " + parsedArgs.getString("disclaimer") + "\n*/\n\n";
    App app = brjs.app(appName);

    if (!app.dirExists())
      throw new CommandArgumentsException("Could not find application '" + appName + "'", this);

    File destinationZipLocation =
        new File(brjs.storageDir("exported-app").getAbsolutePath() + "/" + appName + ".zip");

    try {
      File temporaryExportDir = FileUtility.createTemporaryDirectory(appName);

      IOFileFilter excludeUserLibraryTestsFilter = createExcludeUserLibsTestsFilter(appName);
      NotFileFilter brjsJarFilter =
          new NotFileFilter(
              new AndFileFilter(new PrefixFileFilter("brjs-"), new SuffixFileFilter(".jar")));
      IOFileFilter combinedFilter =
          new AndFileFilter(new ExcludeDirFileFilter("js-test-driver", "bundles"), brjsJarFilter);

      combinedFilter = new AndFileFilter(combinedFilter, excludeUserLibraryTestsFilter);

      createResourcesFromSdkTemplate(app.dir(), temporaryExportDir, combinedFilter);
      includeDisclaimerInDirectoryClasses(new File(temporaryExportDir, "libs"), disclaimer);
      FileUtility.zipFolder(temporaryExportDir, destinationZipLocation, false);
    } catch (Exception e) {
      throw new CommandOperationException(
          "Could not create application zip for application '" + appName + "'", e);
    }

    logger.info("Successfully exported application '" + appName + "'");
    logger.info(" " + destinationZipLocation.getAbsolutePath());
  }