001// Generated by delombok at Mon Oct 12 22:59:35 BST 2020
002/*
003 *  Licensed to the Apache Software Foundation (ASF) under one
004 *  or more contributor license agreements.  See the NOTICE file
005 *  distributed with this work for additional information
006 *  regarding copyright ownership.  The ASF licenses this file
007 *  to you under the Apache License, Version 2.0 (the
008 *  "License"); you may not use this file except in compliance
009 *  with the License.  You may obtain a copy of the License at
010 *
011 *        http://www.apache.org/licenses/LICENSE-2.0
012 *
013 *  Unless required by applicable law or agreed to in writing,
014 *  software distributed under the License is distributed on an
015 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 *  KIND, either express or implied.  See the License for the
017 *  specific language governing permissions and limitations
018 *  under the License.
019 */
020package org.apache.isis.extensions.commandreplay.secondary.analyser;
021
022import javax.annotation.PostConstruct;
023import javax.inject.Named;
024import com.google.common.base.Objects;
025import org.springframework.core.annotation.Order;
026import org.springframework.stereotype.Service;
027import org.apache.isis.applib.annotation.OrderPrecedence;
028import org.apache.isis.applib.services.commanddto.conmap.UserDataKeys;
029import org.apache.isis.applib.util.schema.CommandDtoUtils;
030import org.apache.isis.commons.internal.base._Strings;
031import org.apache.isis.core.config.IsisConfiguration;
032import org.apache.isis.extensions.commandlog.impl.jdo.CommandJdo;
033import org.apache.isis.schema.common.v2.InteractionType;
034
035@Service
036@Named("isisExtensionsCommandReplaySecondary.CommandReplayAnalyserException")
037@Order(OrderPrecedence.MIDPOINT)
038public class CommandReplayAnalyserException implements CommandReplayAnalyser {
039    private final IsisConfiguration isisConfiguration;
040    private boolean enabled;
041
042    @PostConstruct
043    public void init() {
044        enabled = isisConfiguration.getExtensions().getCommandReplay().getAnalyser().getResult().isEnabled();
045    }
046
047    @Override
048    public String analyzeReplay(final CommandJdo commandJdo) {
049        if (!enabled) {
050            return null;
051        }
052        final org.apache.isis.schema.cmd.v2.CommandDto dto = commandJdo.getCommandDto();
053        if (dto.getMember().getInteractionType() == InteractionType.PROPERTY_EDIT) {
054            return null;
055        }
056        final java.lang.String primaryException = CommandDtoUtils.getUserData(dto, UserDataKeys.EXCEPTION);
057        if (_Strings.isNullOrEmpty(primaryException)) {
058            return null;
059        }
060        final java.lang.String replayedException = commandJdo.getException();
061        final java.lang.String primaryExceptionTrimmed = trimmed(primaryException);
062        final java.lang.String replayedExceptionTrimmed = trimmed(replayedException);
063        return Objects.equal(primaryExceptionTrimmed, replayedExceptionTrimmed) ? null : String.format("Exceptions differ.  On primary system was \'%s\'", primaryException);
064    }
065
066    private String trimmed(final String str) {
067        return withoutWhitespace(initialPartOfStackTrace(str));
068    }
069
070    // we only look at beginning of the stack trace because the latter part will differ when replayed
071    private String initialPartOfStackTrace(final String str) {
072        final int toInspectOfStackTrace = 500;
073        return str.length() > toInspectOfStackTrace ? str.substring(0, toInspectOfStackTrace) : str;
074    }
075
076    private String withoutWhitespace(final String s) {
077        return s.replaceAll("\\s", "");
078    }
079
080    @java.lang.SuppressWarnings("all")
081    public CommandReplayAnalyserException(final IsisConfiguration isisConfiguration) {
082        this.isisConfiguration = isisConfiguration;
083    }
084}