Пример #1
0
  public static void main(String[] args) {
    Options options = new Options();

    Option inputOpt = OptionBuilder.create(IN);
    inputOpt.setArgName("INPUT");
    inputOpt.setDescription("Input model");
    inputOpt.setArgs(1);
    inputOpt.setRequired(true);

    Option inClassOpt = OptionBuilder.create(EPACKAGE_CLASS);
    inClassOpt.setArgName("CLASS");
    inClassOpt.setDescription("FQN of EPackage implementation class");
    inClassOpt.setArgs(1);
    inClassOpt.setRequired(true);

    options.addOption(inputOpt);
    options.addOption(inClassOpt);

    CommandLineParser parser = new PosixParser();

    try {
      CommandLine commandLine = parser.parse(options, args);

      URI uri = URI.createFileURI(commandLine.getOptionValue(IN));

      Class<?> inClazz =
          XmiTraverser.class.getClassLoader().loadClass(commandLine.getOptionValue(EPACKAGE_CLASS));
      inClazz.getMethod("init").invoke(null);

      ResourceSet resourceSet = new ResourceSetImpl();
      resourceSet
          .getResourceFactoryRegistry()
          .getExtensionToFactoryMap()
          .put("xmi", new XMIResourceFactoryImpl());
      resourceSet
          .getResourceFactoryRegistry()
          .getExtensionToFactoryMap()
          .put("zxmi", new XMIResourceFactoryImpl());

      Resource resource = resourceSet.createResource(uri);

      Map<String, Object> loadOpts = new HashMap<String, Object>();
      resource.load(loadOpts);

      LOG.log(Level.INFO, "Start counting");
      int count = 0;
      long begin = System.currentTimeMillis();
      for (Iterator<EObject> iterator = resource.getAllContents();
          iterator.hasNext();
          iterator.next(), count++) ;
      long end = System.currentTimeMillis();
      LOG.log(Level.INFO, "End counting");
      LOG.log(Level.INFO, MessageFormat.format("Resource {0} contains {1} elements", uri, count));
      LOG.log(
          Level.INFO,
          MessageFormat.format("Time spent: {0}", MessageUtil.formatMillis(end - begin)));

      resource.unload();

    } catch (ParseException e) {
      MessageUtil.showError(e.toString());
      MessageUtil.showError("Current arguments: " + Arrays.toString(args));
      HelpFormatter formatter = new HelpFormatter();
      formatter.printHelp("java -jar <this-file.jar>", options, true);
    } catch (Throwable e) {
      MessageUtil.showError(e.toString());
    }
  }
  public static void main(String[] args) {
    Options options = new Options();

    Option inputOpt = OptionBuilder.create(IN);
    inputOpt.setArgName("INPUT");
    inputOpt.setDescription("Input CDO resource directory");
    inputOpt.setArgs(1);
    inputOpt.setRequired(true);

    Option inClassOpt = OptionBuilder.create(EPACKAGE_CLASS);
    inClassOpt.setArgName("CLASS");
    inClassOpt.setDescription("FQN of EPackage implementation class");
    inClassOpt.setArgs(1);
    inClassOpt.setRequired(true);

    Option repoOpt = OptionBuilder.create(REPO_NAME);
    repoOpt.setArgName("REPO_NAME");
    repoOpt.setDescription("CDO Repository name");
    repoOpt.setArgs(1);
    repoOpt.setRequired(true);

    options.addOption(inputOpt);
    options.addOption(inClassOpt);
    options.addOption(repoOpt);

    CommandLineParser parser = new PosixParser();

    try {
      CommandLine commandLine = parser.parse(options, args);

      String repositoryDir = commandLine.getOptionValue(IN);
      String repositoryName = commandLine.getOptionValue(REPO_NAME);

      Class<?> inClazz =
          CdoQueryGetBranchStatements.class
              .getClassLoader()
              .loadClass(commandLine.getOptionValue(EPACKAGE_CLASS));
      inClazz.getMethod("init").invoke(null);

      EmbeddedCDOServer server = new EmbeddedCDOServer(repositoryDir, repositoryName);
      try {
        server.run();
        CDOSession session = server.openSession();
        CDOTransaction transaction = session.openTransaction();
        Resource resource = transaction.getRootResource();
        {
          Runtime.getRuntime().gc();
          long initialUsedMemory =
              Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
          LOG.log(
              Level.INFO,
              MessageFormat.format(
                  "Used memory before query: {0}",
                  MessageUtil.byteCountToDisplaySize(initialUsedMemory)));
          LOG.log(Level.INFO, "Start query");
          long begin = System.currentTimeMillis();
          Set<TextElement> list = ASE2015JavaQueries.getCommentsTagContent(resource);
          long end = System.currentTimeMillis();
          LOG.log(Level.INFO, "End query");
          LOG.log(
              Level.INFO, MessageFormat.format("Query result contains {0} elements", list.size()));
          LOG.log(
              Level.INFO,
              MessageFormat.format("Time spent: {0}", MessageUtil.formatMillis(end - begin)));
          Runtime.getRuntime().gc();
          long finalUsedMemory =
              Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
          LOG.log(
              Level.INFO,
              MessageFormat.format(
                  "Used memory after query: {0}",
                  MessageUtil.byteCountToDisplaySize(finalUsedMemory)));
          LOG.log(
              Level.INFO,
              MessageFormat.format(
                  "Memory use increase: {0}",
                  MessageUtil.byteCountToDisplaySize(finalUsedMemory - initialUsedMemory)));
        }

        transaction.close();
        session.close();
      } finally {
        server.stop();
      }
    } catch (ParseException e) {
      MessageUtil.showError(e.toString());
      MessageUtil.showError("Current arguments: " + Arrays.toString(args));
      HelpFormatter formatter = new HelpFormatter();
      formatter.printHelp("java -jar <this-file.jar>", options, true);
    } catch (Throwable e) {
      MessageUtil.showError(e.toString());
    }
  }