/*
 * Copyright 2009 Lari Natri <lari.natri@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package compid.application;

import net.aavalla.utils.general.DynamicClassLoader;
import net.aavalla.utils.general.FileUtils;
import net.aavalla.utils.jobpool.Job;
import net.aavalla.utils.jobpool.JobPriority;
import java.io.File;

/**
 *
 * @author Lari Natri
 */
class MascotParserLoader extends Job<Boolean,CompidApp> {

    MascotParserLoader(CompidApp compidApp) {
        super(compidApp);
        this.setName("Mascot Parser loader");
        this.setSilent(true);
        this.setPriority(JobPriority.IMMEDIATE);
    }

    @Override
    protected Boolean work(CompidApp app) throws Throwable {
        this.reportStatus("Searching...");
        String home = System.getProperty("user.home");
        String sep = File.separator;
        File pathFirst = FileUtils.getClassPath(CompidApp.class);
        File pathSecond = new File(app.getSoftProperties().getProperty("msparser.path",
                home+sep+app.getHardResource().getString("Application.DefaultCompidSubdir"))); //NOI18N

        System.out.println(pathFirst.toString());
        pathFirst = new File(pathFirst.toString()+File.separator+"lib");
        File[] paths = new File[] { pathFirst, pathSecond };
        boolean ok = false;
        try {
            ok = DynamicClassLoader.loadJar(paths, "msparser.jar",
                    "matrix_science.msparser.ms_datfile"); //NOI18N
        } catch (Exception ex) {
            ex.printStackTrace();
            ok = false;
        }
        if (ok) {
            System.out.println("msparser.jar loaded");
            ok = DynamicClassLoader.loadNativeLibrary("msparserj", paths); //NOI18N

        }
        if (ok) {
            this.reportStatus("Loaded");
        } else {
            this.reportStatus("Not loaded");
        }
        return ok;

    }

}

