org.kles.m3.service.CheckM3ConnectionService.java Source code

Java tutorial

Introduction

Here is the source code for org.kles.m3.service.CheckM3ConnectionService.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.kles.m3.service;

import com.intentia.mak.core.m3.foundation.factory.M3RuntimeFactory;
//import org.kles.mak.factory.M3RuntimeFactory;
import com.intentia.mak.core.movex.ConfigurationProperties;
import com.intentia.mak.core.movex.EnvironmentProperties;
import com.intentia.mak.core.movex.MovexSystem;
import com.intentia.mak.util.MAKException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.SimpleObjectProperty;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import mak.core.movex.MovexSystemFactory;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.kles.m3.M3Connector;
import org.kles.m3.ZUserCredentialsProvider;
import org.kles.view.process.M3upgraderLauncherController;

/**
 *
 * @author Jeremy.CHAUT
 */
public class CheckM3ConnectionService extends Service<Boolean> {

    private final SimpleObjectProperty<M3Connector> m3connector;

    public CheckM3ConnectionService() {
        m3connector = new SimpleObjectProperty<>();
    }

    public CheckM3ConnectionService(M3Connector src) {
        m3connector = new SimpleObjectProperty<>(src);
    }

    public SimpleObjectProperty<M3Connector> getM3ConnectorProperty() {
        return m3connector;
    }

    @Override
    protected Task<Boolean> createTask() {
        return new Task<Boolean>() {

            @Override
            protected Boolean call() throws MAKException {
                updateMessage("Initialisation des donnes");
                waiter(1000);
                if (m3connector.get().getEnv() != null) {
                    updateMessage("Cration du runtime M3");
                    waiter(1000);
                    EnvironmentProperties e = new EnvironmentProperties();
                    e.setServerAddress(m3connector.get().getEnv().getM3BEHost());
                    e.setServerPort(m3connector.get().getEnv().getM3BEPort());
                    e.setRootPath(m3connector.get().getEnv().getM3BEPath());
                    ZUserCredentialsProvider credentialsProvider = new ZUserCredentialsProvider();
                    credentialsProvider
                            .setUserPwd(new UsernamePasswordCredentials(m3connector.get().getEnv().getM3BELogin(),
                                    m3connector.get().getEnv().getM3BEPassword()));
                    credentialsProvider.getCachedCredentials(m3connector.get().getEnv().getM3BEHost(),
                            m3connector.get().getEnv().getM3BEPort());
                    m3connector.get().setM3Runtime(M3RuntimeFactory.getRuntime(e.getServerRootPath().toString(),
                            e.getServerAddress(), e.getServerPort(), credentialsProvider));
                    m3connector.get().setInitOK(false);
                    if (m3connector.get().getM3Runtime() != null) {
                        m3connector.get().setEnvironmentProperties(e);
                        updateMessage("Vrification de la connexion");
                        waiter(1000);
                        ConfigurationProperties configurationProperties = new ConfigurationProperties();
                        configurationProperties.setName("MVX");
                        configurationProperties.setDescription("Standard Configuration");
                        configurationProperties.setClassPath(
                                m3connector.get().getM3Runtime().getClassPath(configurationProperties.getName()));
                        MovexSystem mvxSys = MovexSystemFactory.newMovexSystem(e, configurationProperties, null);
                        m3connector.get().setInitOK(mvxSys.isInitialized());
                    }
                    if (!m3connector.get().isInitOK()) {
                        new RuntimeException("Error de connexion  M3!");
                        this.failed();
                        return false;
                    }
                } else {
                    new RuntimeException("M3 Environment null");
                    this.failed();
                    return false;
                }
                updateMessage("");
                return true;
            }
        };
    }

    private void waiter(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException ex) {
            Logger.getLogger(M3upgraderLauncherController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public M3Connector getM3Connector() {
        return m3connector.get();
    }
}