blob: 45462f41c8b5f0a592e8ff9f3bba2f4747b3066a [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2015-2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
********************************************************************************/
package org.eclipse.mdm.api.atfxadapter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Map;
import org.asam.ods.AoException;
import org.asam.ods.AoSession;
import org.eclipse.mdm.api.base.ConnectionException;
import org.eclipse.mdm.api.dflt.ApplicationContext;
import org.eclipse.mdm.api.dflt.ApplicationContextFactory;
import org.omg.CORBA.ORB;
import de.rechner.openatfx.AoServiceFactory;
public class ATFXContextFactory implements ApplicationContextFactory {
private final ORB orb = ORB.init(new String[] {}, System.getProperties());
@Override
public ApplicationContext connect(Map<String, String> parameters) throws ConnectionException {
File atfxFile = new File(parameters.get("atfxfile"));
try {
AoSession aoSession;
if (atfxFile.exists() && Files.size(atfxFile.toPath()) > 0L) {
aoSession = AoServiceFactory.getInstance().newAoSession(orb, atfxFile);
} else {
throw new ConnectionException(
"Cannot open session on a not existing or empty ATFX file: " + atfxFile.getAbsolutePath());
}
return new ATFXContext(orb, aoSession, parameters);
} catch (AoException e) {
throw new ConnectionException("Error " + e.reason, e);
} catch (IOException e) {
throw new ConnectionException("Error reading ATFX file " + atfxFile.getAbsolutePath(), e);
}
}
}