Search in sources :

Example 16 with ApplicationSettingsException

use of org.apache.airavata.common.exception.ApplicationSettingsException in project airavata by apache.

the class RegistryServiceDBEventHandler method onMessage.

@Override
public void onMessage(MessageContext messageContext) {
    logger.info("RegistryServiceDBEventHandler | Received a new message!");
    try {
        // construct dbeventmessage thrift datamodel
        byte[] bytes = ThriftUtils.serializeThriftObject(messageContext.getEvent());
        DBEventMessage dbEventMessage = new DBEventMessage();
        ThriftUtils.createThriftFromBytes(bytes, dbEventMessage);
        logger.info("RegistryService received db-event-message from publisher: " + dbEventMessage.getPublisherService());
        // get publisher context
        DBEventPublisherContext publisherContext = dbEventMessage.getMessageContext().getPublisher().getPublisherContext();
        logger.info("RegistryService, Replicated Entity: " + publisherContext.getEntityType());
        // this try-block is mainly for catching DuplicateEntryException
        try {
            // check type of entity-type
            switch(publisherContext.getEntityType()) {
                // Gateway related operations
                case TENANT:
                    {
                        // construct gateway datamodel from message
                        Gateway gateway = new Gateway();
                        ThriftUtils.createThriftFromBytes(publisherContext.getEntityDataModel(), gateway);
                        // call service-methods based on CRUD type
                        switch(publisherContext.getCrudType()) {
                            case CREATE:
                                {
                                    logger.info("Replicating addGateway in Registry.");
                                    registryClient.addGateway(gateway);
                                    logger.info("addGateway Replication Success!");
                                    break;
                                }
                            case UPDATE:
                                {
                                    logger.info("Replicating updateGateway in Registry.");
                                    if (!registryClient.isGatewayExist(gateway.getGatewayId())) {
                                        logger.info("Gateway doesn't exist so adding instead of updating.");
                                        registryClient.addGateway(gateway);
                                    } else {
                                        registryClient.updateGateway(gateway.getGatewayId(), gateway);
                                    }
                                    logger.info("updateGateway Replication Success!");
                                    break;
                                }
                            case DELETE:
                                {
                                    logger.info("Replicating deleteGateway in Registry.");
                                    registryClient.deleteGateway(gateway.getGatewayId());
                                    logger.info("deleteGateway Replication Success!");
                                    break;
                                }
                        }
                        // break entity: gateway
                        break;
                    }
                // UserProfile related operations
                case USER_PROFILE:
                    {
                        // construct userprofile datamodel from message
                        UserProfile userProfile = new UserProfile();
                        ThriftUtils.createThriftFromBytes(publisherContext.getEntityDataModel(), userProfile);
                        // call service-methods based on CRUD type
                        switch(publisherContext.getCrudType()) {
                            case CREATE:
                                {
                                    logger.info("Replicating addUser in Registry.");
                                    registryClient.addUser(userProfile);
                                    logger.info("addUser Replication Success!");
                                    break;
                                }
                            case UPDATE:
                                {
                                    logger.info("Replicating updateGateway in Registry.");
                                    // TODO: find appropriate method
                                    break;
                                }
                            case DELETE:
                                {
                                    logger.info("Replicating deleteGateway in Registry.");
                                    // TODO: find appropriate method
                                    break;
                                }
                        }
                        // break entity: userprofile
                        break;
                    }
                // no handler for entity
                default:
                    {
                        logger.error("Handler not defined for Entity: " + publisherContext.getEntityType());
                    }
            }
        } catch (DuplicateEntryException ex) {
            // log this exception and proceed (do nothing)
            // this exception is thrown mostly when messages are re-consumed, hence ignore
            logger.warn("DuplicateEntryException while consuming db-event message, ex: " + ex.getMessage(), ex);
        }
        // send ack for received message
        logger.info("RegistryServiceDBEventHandler | Sending ack. Message Delivery Tag: " + messageContext.getDeliveryTag());
        RegistryServiceDBEventMessagingFactory.getDBEventSubscriber().sendAck(messageContext.getDeliveryTag());
    } catch (TException ex) {
        logger.error("Error processing message: " + ex, ex);
    } catch (ApplicationSettingsException ex) {
        logger.error("Error fetching application settings: " + ex, ex);
    } catch (AiravataException ex) {
        logger.error("Error sending ack. Message Delivery Tag: " + messageContext.getDeliveryTag(), ex);
    }
}
Also used : TException(org.apache.thrift.TException) DBEventMessage(org.apache.airavata.model.dbevent.DBEventMessage) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) UserProfile(org.apache.airavata.model.user.UserProfile) DBEventPublisherContext(org.apache.airavata.model.dbevent.DBEventPublisherContext) Gateway(org.apache.airavata.model.workspace.Gateway) DuplicateEntryException(org.apache.airavata.model.error.DuplicateEntryException) AiravataException(org.apache.airavata.common.exception.AiravataException)

Example 17 with ApplicationSettingsException

use of org.apache.airavata.common.exception.ApplicationSettingsException in project airavata by apache.

the class ExperimentCatalogInitUtil method initializeDB.

public static void initializeDB() {
    System.setProperty("registry.initialize.state", "0");
    try {
        jdbcDriver = ServerSettings.getSetting(REGISTRY_JDBC_DRIVER);
        jdbcURl = ServerSettings.getSetting(REGISTRY_JDBC_URL);
        jdbcUser = ServerSettings.getSetting(REGISTRY_JDBC_USER);
        jdbcPassword = ServerSettings.getSetting(REGISTRY_JDBC_PASSWORD);
        jdbcURl = jdbcURl + "?" + "user=" + jdbcUser + "&" + "password=" + jdbcPassword;
    } catch (ApplicationSettingsException e) {
        logger.error("Unable to read airavata server properties", e.getMessage());
    }
    if (getDBType(jdbcURl).equals("derby") && isDerbyStartEnabled()) {
        startDerbyInServerMode();
    }
    db = new JdbcStorage(10, 50, jdbcURl, jdbcDriver, true);
    Connection conn = null;
    try {
        conn = db.connect();
        if (!DatabaseCreator.isDatabaseStructureCreated(CONFIGURATION_TABLE, conn)) {
            DatabaseCreator.createRegistryDatabase("database_scripts/expcatalog", conn);
            logger.info("New Database created for Experiment Catalog !!!");
        } else {
            logger.info("Database already created for Experiment Catalog !!!");
        }
        try {
            GatewayResource gateway;
            if (!ExpCatResourceUtils.isGatewayExist(ServerSettings.getDefaultUserGateway())) {
                gateway = (GatewayResource) ExpCatResourceUtils.createGateway(ServerSettings.getDefaultUserGateway());
                gateway.setGatewayApprovalStatus(GatewayApprovalStatus.APPROVED.toString());
                gateway.save();
            } else {
                gateway = (GatewayResource) ExpCatResourceUtils.getGateway(ServerSettings.getDefaultUserGateway());
            }
            UserResource user;
            if (!ExpCatResourceUtils.isUserExist(ServerSettings.getDefaultUser(), ServerSettings.getDefaultUserGateway())) {
                user = ExpCatResourceUtils.createUser(ServerSettings.getDefaultUser(), ServerSettings.getDefaultUserPassword(), ServerSettings.getDefaultUserGateway());
                user.save();
            } else {
                user = (UserResource) ExpCatResourceUtils.getUser(ServerSettings.getDefaultUser(), ServerSettings.getDefaultUserGateway());
            }
            WorkerResource workerResource;
            if (!gateway.isExists(ResourceType.GATEWAY_WORKER, ServerSettings.getDefaultUserGateway())) {
                workerResource = (WorkerResource) gateway.create(ResourceType.GATEWAY_WORKER);
                workerResource.setUser(user.getUserName());
                workerResource.save();
            } else {
                workerResource = (WorkerResource) gateway.get(ResourceType.GATEWAY_WORKER, ServerSettings.getDefaultUser());
            }
            ProjectResource projectResource;
            if (!workerResource.isExists(ResourceType.PROJECT, DEFAULT_PROJECT_NAME)) {
                projectResource = workerResource.createProject(DEFAULT_PROJECT_NAME);
                projectResource.setName(DEFAULT_PROJECT_NAME);
                projectResource.setGatewayId(gateway.getGatewayId());
                projectResource.save();
            }
        } catch (ApplicationSettingsException e) {
            logger.error("Unable to read airavata-server properties...", e.getMessage());
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException("Database failure", e);
    } finally {
        db.closeConnection(conn);
        try {
            if (conn != null) {
                if (!conn.getAutoCommit()) {
                    conn.commit();
                }
                conn.close();
            }
        } catch (SQLException e) {
            logger.error("Error while closing database connection...", e.getMessage(), e);
        }
    }
    System.setProperty("registry.initialize.state", "1");
}
Also used : ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) SQLException(java.sql.SQLException) WorkerResource(org.apache.airavata.registry.core.experiment.catalog.resources.WorkerResource) Connection(java.sql.Connection) GatewayResource(org.apache.airavata.registry.core.experiment.catalog.resources.GatewayResource) UserResource(org.apache.airavata.registry.core.experiment.catalog.resources.UserResource) ProjectResource(org.apache.airavata.registry.core.experiment.catalog.resources.ProjectResource) IOException(java.io.IOException) SQLException(java.sql.SQLException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException)

Example 18 with ApplicationSettingsException

use of org.apache.airavata.common.exception.ApplicationSettingsException in project airavata by apache.

the class WorkflowInputResource method get.

public WorkflowCatalogResource get(Object identifier) throws WorkflowCatalogException {
    HashMap<String, String> ids;
    if (identifier instanceof Map) {
        ids = (HashMap<String, String>) identifier;
    } else {
        logger.error("Identifier should be a map with the field name and it's value");
        throw new WorkflowCatalogException("Identifier should be a map with the field name and it's value");
    }
    EntityManager em = null;
    try {
        em = WorkflowCatalogJPAUtils.getEntityManager();
        em.getTransaction().begin();
        WorkflowCatalogQueryGenerator generator = new WorkflowCatalogQueryGenerator(WORKFLOW_INPUT);
        generator.setParameter(WorkflowInputConstants.WF_TEMPLATE_ID, ids.get(WorkflowInputConstants.WF_TEMPLATE_ID));
        generator.setParameter(WorkflowInputConstants.INPUT_KEY, ids.get(WorkflowInputConstants.INPUT_KEY));
        Query q = generator.selectQuery(em);
        WorkflowInput workflowInput = (WorkflowInput) q.getSingleResult();
        WorkflowInputResource workflowInputResource = (WorkflowInputResource) WorkflowCatalogJPAUtils.getResource(WorkflowCatalogResourceType.WORKFLOW_INPUT, workflowInput);
        em.getTransaction().commit();
        if (em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
        return workflowInputResource;
    } catch (ApplicationSettingsException e) {
        logger.error(e.getMessage(), e);
        throw new WorkflowCatalogException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}
Also used : ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) WorkflowCatalogException(org.apache.airavata.registry.cpi.WorkflowCatalogException) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) WorkflowCatalogQueryGenerator(org.apache.airavata.registry.core.workflow.catalog.utils.WorkflowCatalogQueryGenerator) WorkflowInput(org.apache.airavata.registry.core.workflow.catalog.model.WorkflowInput) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with ApplicationSettingsException

use of org.apache.airavata.common.exception.ApplicationSettingsException in project airavata by apache.

the class WorkflowOutputResource method get.

public WorkflowCatalogResource get(Object identifier) throws WorkflowCatalogException {
    HashMap<String, String> ids;
    if (identifier instanceof Map) {
        ids = (HashMap) identifier;
    } else {
        logger.error("Identifier should be a map with the field name and it's value");
        throw new WorkflowCatalogException("Identifier should be a map with the field name and it's value");
    }
    EntityManager em = null;
    try {
        em = WorkflowCatalogJPAUtils.getEntityManager();
        em.getTransaction().begin();
        WorkflowCatalogQueryGenerator generator = new WorkflowCatalogQueryGenerator(WORKFLOW_OUTPUT);
        generator.setParameter(WorkflowOutputConstants.WF_TEMPLATE_ID, ids.get(WorkflowOutputConstants.WF_TEMPLATE_ID));
        generator.setParameter(WorkflowOutputConstants.OUTPUT_KEY, ids.get(WorkflowOutputConstants.OUTPUT_KEY));
        Query q = generator.selectQuery(em);
        WorkflowOutput wfOutput = (WorkflowOutput) q.getSingleResult();
        WorkflowOutputResource workflowOutputResource = (WorkflowOutputResource) WorkflowCatalogJPAUtils.getResource(WorkflowCatalogResourceType.WORKFLOW_OUTPUT, wfOutput);
        em.getTransaction().commit();
        if (em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
        return workflowOutputResource;
    } catch (ApplicationSettingsException e) {
        logger.error(e.getMessage(), e);
        throw new WorkflowCatalogException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}
Also used : WorkflowOutput(org.apache.airavata.registry.core.workflow.catalog.model.WorkflowOutput) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) WorkflowCatalogException(org.apache.airavata.registry.cpi.WorkflowCatalogException) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) WorkflowCatalogQueryGenerator(org.apache.airavata.registry.core.workflow.catalog.utils.WorkflowCatalogQueryGenerator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with ApplicationSettingsException

use of org.apache.airavata.common.exception.ApplicationSettingsException in project airavata by apache.

the class WorkflowResource method get.

@Override
public List<WorkflowCatalogResource> get(String fieldName, Object value) throws WorkflowCatalogException {
    List<WorkflowCatalogResource> workflowResources = new ArrayList<WorkflowCatalogResource>();
    EntityManager em = null;
    try {
        em = WorkflowCatalogJPAUtils.getEntityManager();
        em.getTransaction().begin();
        WorkflowCatalogQueryGenerator generator = new WorkflowCatalogQueryGenerator(WORKFLOW);
        Query q;
        if ((fieldName.equals(WorkflowConstants.TEMPLATE_ID)) || (fieldName.equals(WorkflowConstants.GATEWAY_ID))) {
            generator.setParameter(fieldName, value);
            q = generator.selectQuery(em);
            List<?> results = q.getResultList();
            for (Object result : results) {
                Workflow workflow = (Workflow) result;
                WorkflowResource workflowResource = (WorkflowResource) WorkflowCatalogJPAUtils.getResource(WorkflowCatalogResourceType.WORKFLOW, workflow);
                workflowResources.add(workflowResource);
            }
        } else {
            em.getTransaction().commit();
            if (em.isOpen()) {
                if (em.getTransaction().isActive()) {
                    em.getTransaction().rollback();
                }
                em.close();
            }
            logger.error("Unsupported field name for Workflow Resource.", new IllegalArgumentException());
            throw new IllegalArgumentException("Unsupported field name for Workflow Resource.");
        }
        em.getTransaction().commit();
        if (em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    } catch (ApplicationSettingsException e) {
        logger.error(e.getMessage(), e);
        throw new WorkflowCatalogException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
    return workflowResources;
}
Also used : ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) WorkflowCatalogException(org.apache.airavata.registry.cpi.WorkflowCatalogException) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) Workflow(org.apache.airavata.registry.core.workflow.catalog.model.Workflow) EntityManager(javax.persistence.EntityManager) WorkflowCatalogQueryGenerator(org.apache.airavata.registry.core.workflow.catalog.utils.WorkflowCatalogQueryGenerator)

Aggregations

ApplicationSettingsException (org.apache.airavata.common.exception.ApplicationSettingsException)263 EntityManager (javax.persistence.EntityManager)193 AppCatalogException (org.apache.airavata.registry.cpi.AppCatalogException)172 Query (javax.persistence.Query)147 AppCatalogQueryGenerator (org.apache.airavata.registry.core.app.catalog.util.AppCatalogQueryGenerator)129 HashMap (java.util.HashMap)71 Map (java.util.Map)69 ArrayList (java.util.ArrayList)47 WorkflowCatalogException (org.apache.airavata.registry.cpi.WorkflowCatalogException)26 IOException (java.io.IOException)23 IamAdminServicesException (org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException)20 TException (org.apache.thrift.TException)19 WorkflowCatalogQueryGenerator (org.apache.airavata.registry.core.workflow.catalog.utils.WorkflowCatalogQueryGenerator)18 GFacException (org.apache.airavata.gfac.core.GFacException)11 PasswordCredential (org.apache.airavata.model.credential.store.PasswordCredential)11 Keycloak (org.keycloak.admin.client.Keycloak)11 RegistryServiceException (org.apache.airavata.registry.api.exception.RegistryServiceException)7 CompositeIdentifier (org.apache.airavata.registry.cpi.CompositeIdentifier)7 File (java.io.File)6 Connection (java.sql.Connection)6