protected void process()

in remoting/server/indexer/impl/src/org/netbeans/modules/jackpot30/backend/impl/OptionProcessorImpl.java [94:304]


    protected void process(Env env, Map<Option, String[]> optionValues) throws CommandException {
        String categoryId = null;
        String categoryName = null;

        if (optionValues.containsKey(CATEGORY_ID)) {
            categoryId = optionValues.get(CATEGORY_ID)[0];
        }

        if (optionValues.containsKey(CATEGORY_NAME)) {
            categoryName = optionValues.get(CATEGORY_NAME)[0];
        }

        if (optionValues.containsKey(CATEGORY_PROJECTS)) {
            if (categoryId == null) {
                env.getErrorStream().println("Error: no category-id specified!");
                return;
            }

            if (categoryName == null) {
                env.getErrorStream().println("Warning: no category-name specified.");
                return;
            }
        }

        String cacheTarget = optionValues.get(CACHE_TARGET)[0];
        File cache = FileUtil.normalizeFile(new File(cacheTarget));

        cache.getParentFile().mkdirs();

        if (categoryId == null) {
            env.getErrorStream().println("Error: no category-id specified!");
            return;
        }

        File baseDirFile = new File(optionValues.get(CATEGORY_ROOT_DIR)[0]);
        FileObject baseDir = FileUtil.toFileObject(baseDirFile);
        IndexWriter w = null;

        FileObject cacheFolder = CacheFolder.getCacheFolder();
        FileObject cacheTemp = cacheFolder.getFileObject("index");
        Map<String, String> classpath;
        Map<FileObject, String> extraJars = new HashMap<FileObject, String>();

        try {
            if (cacheTemp != null) cacheTemp.delete();

            cacheTemp = cacheFolder.createFolder("index");
            w = new IndexWriter(FSDirectory.open(FileUtil.toFile(cacheTemp)), new KeywordAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);

            IndexAccessor.current = new IndexAccessor(w, baseDir);
            Set<FileObject> roots = getRoots(optionValues.get(CATEGORY_PROJECTS), env);

            classpath = indexProjects(roots, extraJars, env);
        } catch (InterruptedException ex) {
            LOG.log(Level.FINE, null, ex);
            throw (CommandException) new CommandException(0).initCause(ex);
        } catch (IOException ex) {
            LOG.log(Level.FINE, null, ex);
            throw (CommandException) new CommandException(0).initCause(ex);
        } finally {
            if (w != null) {
                try {
                    w.optimize(true);
                    w.close(true);
                } catch (CorruptIndexException ex) {
                    Exceptions.printStackTrace(ex);
                } catch (IOException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }

        Map<String, Long> statistics = Collections.emptyMap();
        IndexReader r = null;

        try {
            r = IndexReader.open(FSDirectory.open(FileUtil.toFile(cacheTemp)), true);

            statistics = StatisticsGenerator.generateStatistics(r);
        } catch (CorruptIndexException ex) {
            Exceptions.printStackTrace(ex);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        } finally {
            if (r != null) {
                try {
                    r.close();
                } catch (IOException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }

        cacheTemp.refresh();

        JarOutputStream out = null;
        InputStream segments = null;

        try {
            out = new JarOutputStream(new FileOutputStream(cache));
            pack(out, cacheTemp, null, "index", new StringBuilder(categoryId));

            segments = cacheFolder.getFileObject("segments").getInputStream();
            Properties in = new Properties();

            in.load(segments);

            segments.close();//XXX: should be in finally!

            String baseDirPath = baseDirFile.toURI().toString();

            Properties outSegments = new Properties();

            for (String segment : in.stringPropertyNames()) {
                String url = in.getProperty(segment);
                String rel;
                
                if (url.startsWith(baseDirPath)) rel = "rel:/" + url.substring(baseDirPath.length());
                else if (url.startsWith("jar:" + baseDirPath)) rel = "jar:rel:/" + url.substring(4 + baseDirPath.length());
                else rel = url;

                outSegments.setProperty(segment, rel);
            }

            out.putNextEntry(new ZipEntry(categoryId + "/segments"));

            outSegments.store(out, "");

            out.putNextEntry(new ZipEntry(categoryId + "/info"));

            out.write("{\n".getBytes("UTF-8"));
            out.write(("\"displayName\": \"" + categoryName + "\"").getBytes("UTF-8"));
            if (optionValues.containsKey(INFO)) {
                for (String infoValue : optionValues.get(INFO)[0].split(";")) {
                    int eqSign = infoValue.indexOf('=');
                    if (eqSign == (-1)) {
                        LOG.log(Level.INFO, "No ''='' sign in: {0}", infoValue);
                        continue;
                    }
                    out.write((",\n\"" + infoValue.substring(0, eqSign) + "\": \"" + infoValue.substring(eqSign + 1) + "\"").getBytes("UTF-8"));
                }
            }
            out.write(",\n \"statistics\" : {\n".getBytes("UTF-8"));
            boolean wasEntry = false;
            for (Entry<String, Long> e : statistics.entrySet()) {
                if (wasEntry) out.write(", \n".getBytes("UTF-8"));
                out.write(("\"" + e.getKey() + "\" : " + e.getValue()).getBytes("UTF-8"));
                wasEntry = true;
            }
            out.write("\n}\n".getBytes("UTF-8"));
            out.write("\n}\n".getBytes("UTF-8"));

            if (STORE_CLASSPATH) {
                out.putNextEntry(new ZipEntry(categoryId + "/classpath"));

                for (Entry<String, String> e : classpath.entrySet()) {
                    out.write((e.getKey() + "=" + e.getValue() + "\n").getBytes("UTF-8"));
                }

                for (Entry<FileObject, String> ej : extraJars.entrySet()) {
                    out.putNextEntry(new ZipEntry(categoryId + "/" + ej.getValue()));

                    InputStream jarIn = ej.getKey().getInputStream();

                    try {
                        FileUtil.copy(jarIn, out);
                    } finally {
                        jarIn.close();
                    }
                }
            }

            for (FileObject s : cacheFolder.getChildren()) {
                if (!s.isFolder() || !s.getNameExt().startsWith("s") || s.getChildren().length == 0) continue;

                JarOutputStream local = null;
                try {
                    out.putNextEntry(new ZipEntry(categoryId + "/" + s.getNameExt()));

                    local = new JarOutputStream(out);

                    pack(local, s, baseDir.toURI().toString(), "", new StringBuilder(""));
                } finally {
                    if (local != null) {
                        local.finish();
                    }
                }
            }
        } catch (IOException ex) {
            LOG.log(Level.FINE, null, ex);
            throw (CommandException) new CommandException(0).initCause(ex);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ex) {
                    throw (CommandException) new CommandException(0).initCause(ex);
                }
            }

            if (segments != null) {
                try {
                    segments.close();
                } catch (IOException ex) {
                    throw (CommandException) new CommandException(0).initCause(ex);
                }
            }
        }
        
        LifecycleManager.getDefault().exit();
    }