blob: 6948a8d93063ad0a21325cb795b512eb38da3051 [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.util.Map;
import java.util.Optional;
import org.asam.ods.AoException;
import org.asam.ods.AoSession;
import org.asam.ods.ErrorCode;
import org.asam.ods.SeverityFlag;
import org.eclipse.mdm.api.base.adapter.ModelManager;
import org.eclipse.mdm.api.base.query.DataAccessException;
import org.eclipse.mdm.api.base.query.QueryService;
import org.eclipse.mdm.api.dflt.ApplicationContext;
import org.eclipse.mdm.api.dflt.EntityManager;
import org.eclipse.mdm.api.dflt.model.EntityFactory;
import org.eclipse.mdm.api.odsadapter.lookup.config.EntityConfigRepositoryLoader;
import org.eclipse.mdm.api.odsadapter.query.ODSEntityFactory;
import org.eclipse.mdm.api.odsadapter.query.ODSModelManager;
import org.eclipse.mdm.api.odsadapter.query.ODSQueryService;
import org.omg.CORBA.ORB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* ATFXContext encapsulates a session to the ASAM ODS CORBA API of openATFX
*
*/
public class ATFXContext implements ApplicationContext {
private static final Logger LOGGER = LoggerFactory.getLogger(ATFXContext.class);
private Map<String, String> parameters;
private ODSModelManager modelManager;
private ATFXEntityManager entityManager;
/**
* Creates a new ATFX application context.
*
* @param orb the CORBA ORB used to connect to the ODS API
* @param aoSession
* @param parameters
* @throws AoException
*/
public ATFXContext(ORB orb, AoSession aoSession, Map<String, String> parameters) throws AoException {
final String ENTITYCONFIGREPOSITORYLOADERCLASS_ATTRIBUTE_NAME = "entityConfigRepositoryLoaderClass";
// check parameters for configured custom EntityConfigRepositoryLoader
String entityConfigRepositoryLoaderClassName = parameters.get(ENTITYCONFIGREPOSITORYLOADERCLASS_ATTRIBUTE_NAME);
// if custom loader is configured
if (entityConfigRepositoryLoaderClassName != null) {
EntityConfigRepositoryLoader entityConfigRepositoryLoader;
try {
entityConfigRepositoryLoader = Thread.currentThread().getContextClassLoader()
.loadClass(entityConfigRepositoryLoaderClassName).asSubclass(EntityConfigRepositoryLoader.class)
.newInstance();
this.modelManager = new ODSModelManager(orb, aoSession, entityConfigRepositoryLoader);
} catch (Exception e) {
// just log an error, but recover using the default loader
LOGGER.error("Failed to initialize EntityConfigRepositoryLoaderClass. Using default.", e);
this.modelManager = new ODSModelManager(orb, aoSession, new ATFXEntityConfigRepositoryLoader());
}
}
// if not, use default
else {
this.modelManager = new ODSModelManager(orb, aoSession, new ATFXEntityConfigRepositoryLoader());
}
this.parameters = parameters;
this.entityManager = new ATFXEntityManager(this);
LOGGER.debug("ATFXContext initialized.");
}
/**
* {@inheritDoc}
*/
@Override
public Optional<EntityManager> getEntityManager() {
return Optional.of(entityManager);
}
/**
* {@inheritDoc}
*/
@Override
public Optional<ModelManager> getModelManager() {
return Optional.of(modelManager);
}
@Override
public Optional<EntityFactory> getEntityFactory() {
try {
return Optional.of(new ODSEntityFactory(modelManager, entityManager.loadLoggedOnUser()));
} catch (DataAccessException e) {
throw new IllegalStateException("Unable to load instance of the logged in user.");
}
}
/**
* {@inheritDoc}
*/
@Override
public Optional<QueryService> getQueryService() {
return Optional.of(new ODSQueryService(modelManager));
}
/**
* {@inheritDoc}
*/
@Override
public Map<String, String> getParameters() {
return parameters;
}
/**
* @returns the string "atfx"
*/
@Override
public String getAdapterType() {
return "atfx";
}
/**
* {@inheritDoc}
*/
@Override
public void close() {
try {
modelManager.close();
} catch (AoException e) {
LOGGER.warn("Unable to close sesssion due to: " + e.reason, e);
}
}
/**
* Returns the {@link ATFXModelManager} used for this context.
*
* @return the {@link ATFXModelManager}
*/
public ODSModelManager getODSModelManager() {
return modelManager;
}
/**
* Returns the {@link AoSession} used for this context.
*
* @return {@link AoSession} used for this context.
*/
public AoSession getAoSession() {
return modelManager.getAoSession();
}
/**
* Returns the ORB used for this context
*
* @return ORB used for this context
*/
public ORB getORB() {
return modelManager.getORB();
}
/**
* Returns a new {@link ATFXContext} with a new ODS co-session.
*
* @return The created {@code ODSContext} is returned.
* @throws AoException Thrown on errors.
*/
public ATFXContext newContext() throws AoException {
throw new AoException(ErrorCode.AO_NOT_IMPLEMENTED, SeverityFlag.ERROR, 0,
"Method 'createCoSession' not implemented in openATFX");
}
}