Search in sources :

Example 1 with IRegistrationHook

use of com.helger.phoss.smp.smlhook.IRegistrationHook in project phoss-smp by phax.

the class SMPServiceGroupManagerJDBC method createSMPServiceGroup.

@Nonnull
public SMPServiceGroup createSMPServiceGroup(@Nonnull @Nonempty final String sOwnerID, @Nonnull final IParticipantIdentifier aParticipantID, @Nullable final String sExtension, final boolean bCreateInSML) throws SMPServerException {
    ValueEnforcer.notEmpty(sOwnerID, "OwnerID");
    ValueEnforcer.notNull(aParticipantID, "ParticipantID");
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("createSMPServiceGroup (" + sOwnerID + ", " + aParticipantID.getURIEncoded() + ", " + (StringHelper.hasText(sExtension) ? "with extension" : "without extension") + ", " + bCreateInSML + ")");
    final MutableBoolean aCreatedSGHook = new MutableBoolean(false);
    final MutableBoolean aCreatedSGDB = new MutableBoolean(false);
    final IRegistrationHook aHook = RegistrationHookFactory.getInstance();
    final Wrapper<Exception> aCaughtException = new Wrapper<>();
    final DBExecutor aExecutor = newExecutor();
    final ESuccess eSuccess = aExecutor.performInTransaction(() -> {
        // Check if the passed service group ID is already in use
        final SMPServiceGroup aDBServiceGroup = getSMPServiceGroupOfID(aParticipantID);
        if (aDBServiceGroup != null)
            throw new IllegalStateException("The service group with ID " + aParticipantID.getURIEncoded() + " already exists!");
        if (bCreateInSML) {
            // It's a new service group - Create in SML and remember that
            // Throws exception in case of an error
            aHook.createServiceGroup(aParticipantID);
            aCreatedSGHook.set(true);
        }
        // Did not exist. Create it.
        if (aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_service_group (businessIdentifierScheme, businessIdentifier, extension) VALUES (?, ?, ?)", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue(), sExtension)) > 0) {
            aCreatedSGDB.set(true);
            aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_ownership (businessIdentifierScheme, businessIdentifier, username) VALUES (?, ?, ?)", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue(), sOwnerID));
        }
    }, aCaughtException::set);
    if (aCreatedSGHook.booleanValue() && !aCreatedSGDB.booleanValue()) {
        // Undo creation in SML again
        try {
            aHook.undoCreateServiceGroup(aParticipantID);
        } catch (final RegistrationHookException ex) {
            LOGGER.error("Failed to undoCreateServiceGroup (" + aParticipantID.getURIEncoded() + ")", ex);
        }
    }
    if (eSuccess.isFailure() || aCaughtException.isSet() || !aCreatedSGDB.booleanValue()) {
        AuditHelper.onAuditCreateFailure(SMPServiceGroup.OT, aParticipantID.getURIEncoded(), sOwnerID, sExtension, Boolean.valueOf(bCreateInSML));
        // Propagate contained exception
        final Exception ex = aCaughtException.get();
        if (ex instanceof SMPServerException)
            throw (SMPServerException) ex;
        if (ex instanceof RegistrationHookException)
            throw new SMPSMLException("Failed to create '" + aParticipantID.getURIEncoded() + "' in SML", ex);
        throw new SMPInternalErrorException("Error creating ServiceGroup '" + aParticipantID.getURIEncoded() + "'", ex);
    }
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("createSMPServiceGroup succeeded");
    AuditHelper.onAuditCreateSuccess(SMPServiceGroup.OT, aParticipantID.getURIEncoded(), sOwnerID, sExtension, Boolean.valueOf(bCreateInSML));
    final SMPServiceGroup aServiceGroup = new SMPServiceGroup(sOwnerID, aParticipantID, sExtension);
    if (m_aCache != null)
        m_aCache.put(aParticipantID.getURIEncoded(), aServiceGroup);
    m_aCBs.forEach(x -> x.onSMPServiceGroupCreated(aServiceGroup, bCreateInSML));
    return aServiceGroup;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) Wrapper(com.helger.commons.wrapper.Wrapper) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) MutableBoolean(com.helger.commons.mutable.MutableBoolean) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) IRegistrationHook(com.helger.phoss.smp.smlhook.IRegistrationHook) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) Nonnull(javax.annotation.Nonnull)

Example 2 with IRegistrationHook

use of com.helger.phoss.smp.smlhook.IRegistrationHook in project phoss-smp by phax.

the class SMPServiceGroupManagerMongoDB method deleteSMPServiceGroup.

@Nonnull
public EChange deleteSMPServiceGroup(@Nonnull final IParticipantIdentifier aParticipantID, final boolean bDeleteInSML) throws SMPServerException {
    ValueEnforcer.notNull(aParticipantID, "ParticipantID");
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("deleteSMPServiceGroup (" + aParticipantID.getURIEncoded() + ", " + bDeleteInSML + ")");
    // Check first in memory, to avoid unnecessary deletion
    final ISMPServiceGroup aServiceGroup = getSMPServiceGroupOfID(aParticipantID);
    if (aServiceGroup == null)
        return EChange.UNCHANGED;
    final IRegistrationHook aHook = RegistrationHookFactory.getInstance();
    if (bDeleteInSML) {
        // Delete in SML - throws exception in case of error
        try {
            aHook.deleteServiceGroup(aParticipantID);
        } catch (final RegistrationHookException ex) {
            throw new SMPSMLException("Failed to delete '" + aParticipantID.getURIEncoded() + "' in SML", ex);
        }
    }
    // Delete all redirects (must be done before the SG is deleted)
    final ISMPRedirectManager aRedirectMgr = SMPMetaManager.getRedirectMgr();
    aRedirectMgr.deleteAllSMPRedirectsOfServiceGroup(aServiceGroup);
    // Delete all service information (must be done before the SG is deleted)
    final ISMPServiceInformationManager aServiceInfoMgr = SMPMetaManager.getServiceInformationMgr();
    aServiceInfoMgr.deleteAllSMPServiceInformationOfServiceGroup(aServiceGroup);
    final String sServiceGroupID = SMPServiceGroup.createSMPServiceGroupID(aParticipantID);
    final DeleteResult aDR = getCollection().deleteOne(new Document(BSON_ID, sServiceGroupID));
    if (!aDR.wasAcknowledged() || aDR.getDeletedCount() == 0) {
        AuditHelper.onAuditDeleteFailure(SMPServiceGroup.OT, aParticipantID, "no-such-id");
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("deleteSMPServiceGroup - failure");
        // restore in SML
        if (bDeleteInSML) {
            // Undo deletion in SML!
            try {
                aHook.undoDeleteServiceGroup(aParticipantID);
            } catch (final RegistrationHookException ex) {
                LOGGER.error("Failed to undoDeleteServiceGroup (" + aParticipantID.getURIEncoded() + ")", ex);
            }
        }
        throw new SMPNotFoundException("No such service group '" + aParticipantID.getURIEncoded() + "'");
    }
    AuditHelper.onAuditDeleteSuccess(SMPServiceGroup.OT, aParticipantID, Boolean.valueOf(bDeleteInSML));
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("deleteSMPServiceGroup - success");
    m_aCBs.forEach(x -> x.onSMPServiceGroupDeleted(aParticipantID, bDeleteInSML));
    return EChange.CHANGED;
}
Also used : IRegistrationHook(com.helger.phoss.smp.smlhook.IRegistrationHook) ISMPRedirectManager(com.helger.phoss.smp.domain.redirect.ISMPRedirectManager) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) ISMPServiceInformationManager(com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformationManager) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) Document(org.bson.Document) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) DeleteResult(com.mongodb.client.result.DeleteResult) Nonnull(javax.annotation.Nonnull)

Example 3 with IRegistrationHook

use of com.helger.phoss.smp.smlhook.IRegistrationHook in project phoss-smp by phax.

the class SMPServiceGroupManagerJDBC method deleteSMPServiceGroup.

@Nonnull
public EChange deleteSMPServiceGroup(@Nonnull final IParticipantIdentifier aParticipantID, final boolean bDeleteInSML) throws SMPServerException {
    ValueEnforcer.notNull(aParticipantID, "ParticipantID");
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("deleteSMPServiceGroup (" + aParticipantID.getURIEncoded() + ")");
    final IRegistrationHook aHook = RegistrationHookFactory.getInstance();
    final MutableBoolean aDeletedServiceGroupInSML = new MutableBoolean(false);
    final Wrapper<EChange> aWrappedChange = new Wrapper<>(EChange.UNCHANGED);
    final Wrapper<Exception> aCaughtException = new Wrapper<>();
    final DBExecutor aExecutor = newExecutor();
    final ESuccess eSuccess = aExecutor.performInTransaction(() -> {
        // Check if the passed service group ID is already in use
        final SMPServiceGroup aDBServiceGroup = getSMPServiceGroupOfID(aParticipantID);
        if (aDBServiceGroup == null)
            throw new SMPNotFoundException("The service group with ID " + aParticipantID.getURIEncoded() + " does not exist!");
        if (bDeleteInSML) {
            // Delete in SML - and remember that
            // throws exception in case of error
            aHook.deleteServiceGroup(aParticipantID);
            aDeletedServiceGroupInSML.set(true);
        }
        final long nCount = aExecutor.insertOrUpdateOrDelete("DELETE FROM smp_service_group" + " WHERE businessIdentifierScheme=? AND businessIdentifier=?", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue()));
        if (nCount != 1)
            throw new IllegalStateException("Failed to delete service group");
        aWrappedChange.set(EChange.CHANGED);
    }, aCaughtException::set);
    if (eSuccess.isFailure()) {
        // Error writing to the DB
        if (bDeleteInSML && aDeletedServiceGroupInSML.booleanValue()) {
            // Undo deletion in SML!
            try {
                aHook.undoDeleteServiceGroup(aParticipantID);
            } catch (final RegistrationHookException ex) {
                LOGGER.error("Failed to undoDeleteServiceGroup (" + aParticipantID.getURIEncoded() + ")", ex);
            }
        }
    }
    if (aCaughtException.isSet()) {
        AuditHelper.onAuditDeleteFailure(SMPServiceGroup.OT, aParticipantID.getURIEncoded(), "database-error");
        final Exception ex = aCaughtException.get();
        if (ex instanceof SMPServerException)
            throw (SMPServerException) ex;
        if (ex instanceof RegistrationHookException)
            throw new SMPSMLException("Failed to delete '" + aParticipantID.getURIEncoded() + "' in SML", ex);
        throw new SMPInternalErrorException("Failed to delete ServiceGroup '" + aParticipantID.getURIEncoded() + "'", ex);
    }
    final EChange eChange = aWrappedChange.get();
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("deleteSMPServiceGroup succeeded. Change=" + eChange.isChanged());
    if (eChange.isChanged()) {
        AuditHelper.onAuditDeleteSuccess(SMPServiceGroup.OT, aParticipantID.getURIEncoded());
        if (m_aCache != null)
            m_aCache.remove(aParticipantID.getURIEncoded());
        m_aCBs.forEach(x -> x.onSMPServiceGroupDeleted(aParticipantID, bDeleteInSML));
    }
    return eChange;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) Wrapper(com.helger.commons.wrapper.Wrapper) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) MutableBoolean(com.helger.commons.mutable.MutableBoolean) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) IRegistrationHook(com.helger.phoss.smp.smlhook.IRegistrationHook) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) EChange(com.helger.commons.state.EChange) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) Nonnull(javax.annotation.Nonnull)

Example 4 with IRegistrationHook

use of com.helger.phoss.smp.smlhook.IRegistrationHook in project phoss-smp by phax.

the class SMPServiceGroupManagerMongoDB method createSMPServiceGroup.

@Nonnull
public SMPServiceGroup createSMPServiceGroup(@Nonnull @Nonempty final String sOwnerID, @Nonnull final IParticipantIdentifier aParticipantID, @Nullable final String sExtension, final boolean bCreateInSML) throws SMPServerException {
    ValueEnforcer.notEmpty(sOwnerID, "OwnerID");
    ValueEnforcer.notNull(aParticipantID, "ParticipantID");
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("createSMPServiceGroup (" + sOwnerID + ", " + aParticipantID.getURIEncoded() + ", " + (StringHelper.hasText(sExtension) ? "with extension" : "without extension") + ", " + bCreateInSML + ")");
    final SMPServiceGroup aSMPServiceGroup = new SMPServiceGroup(sOwnerID, aParticipantID, sExtension);
    // It's a new service group - throws exception in case of an error
    final IRegistrationHook aHook = RegistrationHookFactory.getInstance();
    if (bCreateInSML)
        try {
            aHook.createServiceGroup(aParticipantID);
        } catch (final RegistrationHookException ex) {
            throw new SMPSMLException("Failed to create '" + aParticipantID.getURIEncoded() + "' in SML", ex);
        }
    try {
        if (!getCollection().insertOne(toBson(aSMPServiceGroup)).wasAcknowledged())
            throw new IllegalStateException("Failed to insert into MongoDB Collection");
    } catch (final RuntimeException ex) {
        // An error occurred - remove from SML again
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("createSMPServiceGroup - failure in storing");
        if (bCreateInSML)
            try {
                aHook.undoCreateServiceGroup(aParticipantID);
            } catch (final RegistrationHookException ex2) {
                LOGGER.error("Failed to undoCreateServiceGroup (" + aParticipantID.getURIEncoded() + ")", ex2);
            }
        throw ex;
    }
    AuditHelper.onAuditCreateSuccess(SMPServiceGroup.OT, aSMPServiceGroup.getID(), sOwnerID, aParticipantID.getURIEncoded(), sExtension, Boolean.valueOf(bCreateInSML));
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("createSMPServiceGroup - success");
    m_aCBs.forEach(x -> x.onSMPServiceGroupCreated(aSMPServiceGroup, bCreateInSML));
    return aSMPServiceGroup;
}
Also used : IRegistrationHook(com.helger.phoss.smp.smlhook.IRegistrationHook) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) Nonnull(javax.annotation.Nonnull)

Example 5 with IRegistrationHook

use of com.helger.phoss.smp.smlhook.IRegistrationHook in project phoss-smp by phax.

the class SMPServiceGroupManagerXML method createSMPServiceGroup.

@Nonnull
public SMPServiceGroup createSMPServiceGroup(@Nonnull @Nonempty final String sOwnerID, @Nonnull final IParticipantIdentifier aParticipantID, @Nullable final String sExtension, final boolean bCreateInSML) throws SMPServerException {
    ValueEnforcer.notEmpty(sOwnerID, "OwnerID");
    ValueEnforcer.notNull(aParticipantID, "ParticipantID");
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("createSMPServiceGroup (" + sOwnerID + ", " + aParticipantID.getURIEncoded() + ", " + (StringHelper.hasText(sExtension) ? "with extension" : "without extension") + ", " + bCreateInSML + ")");
    final SMPServiceGroup aSMPServiceGroup = new SMPServiceGroup(sOwnerID, aParticipantID, sExtension);
    // It's a new service group - throws exception in case of an error
    final IRegistrationHook aHook = RegistrationHookFactory.getInstance();
    if (bCreateInSML)
        try {
            aHook.createServiceGroup(aParticipantID);
        } catch (final RegistrationHookException ex) {
            throw new SMPSMLException("Failed to create '" + aParticipantID.getURIEncoded() + "' in SML", ex);
        }
    m_aRWLock.writeLock().lock();
    try {
        internalCreateItem(aSMPServiceGroup);
    } catch (final RuntimeException ex) {
        // An error occurred - remove from SML again
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("createSMPServiceGroup - failure in storing");
        if (bCreateInSML)
            try {
                aHook.undoCreateServiceGroup(aParticipantID);
            } catch (final RegistrationHookException ex2) {
                LOGGER.error("Failed to undoCreateServiceGroup (" + aParticipantID.getURIEncoded() + ")", ex2);
            }
        throw ex;
    } finally {
        m_aRWLock.writeLock().unlock();
    }
    AuditHelper.onAuditCreateSuccess(SMPServiceGroup.OT, aSMPServiceGroup.getID(), sOwnerID, aParticipantID.getURIEncoded(), sExtension, Boolean.valueOf(bCreateInSML));
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("createSMPServiceGroup - success");
    m_aCBs.forEach(x -> x.onSMPServiceGroupCreated(aSMPServiceGroup, bCreateInSML));
    return aSMPServiceGroup;
}
Also used : IRegistrationHook(com.helger.phoss.smp.smlhook.IRegistrationHook) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) Nonnull(javax.annotation.Nonnull)

Aggregations

ISMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup)6 SMPSMLException (com.helger.phoss.smp.exception.SMPSMLException)6 IRegistrationHook (com.helger.phoss.smp.smlhook.IRegistrationHook)6 RegistrationHookException (com.helger.phoss.smp.smlhook.RegistrationHookException)6 Nonnull (javax.annotation.Nonnull)6 SMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup)5 SMPNotFoundException (com.helger.phoss.smp.exception.SMPNotFoundException)4 MutableBoolean (com.helger.commons.mutable.MutableBoolean)2 ESuccess (com.helger.commons.state.ESuccess)2 Wrapper (com.helger.commons.wrapper.Wrapper)2 ConstantPreparedStatementDataProvider (com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider)2 DBExecutor (com.helger.db.jdbc.executor.DBExecutor)2 ISMPRedirectManager (com.helger.phoss.smp.domain.redirect.ISMPRedirectManager)2 ISMPServiceInformationManager (com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformationManager)2 SMPInternalErrorException (com.helger.phoss.smp.exception.SMPInternalErrorException)2 SMPServerException (com.helger.phoss.smp.exception.SMPServerException)2 EChange (com.helger.commons.state.EChange)1 ISMPRedirect (com.helger.phoss.smp.domain.redirect.ISMPRedirect)1 ISMPServiceInformation (com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformation)1 DeleteResult (com.mongodb.client.result.DeleteResult)1