io.curly.artifact.service.DefaultArtifactCommand.java Source code

Java tutorial

Introduction

Here is the source code for io.curly.artifact.service.DefaultArtifactCommand.java

Source

/*
 *        Copyright 2015 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package io.curly.artifact.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.command.ObservableResult;
import io.curly.artifact.integration.event.CreatedArtifactEvent;
import io.curly.artifact.model.Artifact;
import io.curly.commons.github.User;
import io.curly.commons.logging.annotation.Loggable;
import io.curly.commons.stereotype.Command;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.retry.annotation.Retryable;
import rx.Observable;

import java.util.Collections;
import java.util.Optional;

/**
 * @author Joo Evangelista
 */
@Slf4j
@Command
@Retryable
public class DefaultArtifactCommand implements ArtifactCommand {

    private static final String EXECUTION_ISOLATION = "execution.isolation.strategy";

    private static final String SEMAPHORE = "SEMAPHORE";

    private final ApplicationEventPublisher applicationEventPublisher;

    private final ArtifactService artifactService;

    @Autowired
    public DefaultArtifactCommand(ApplicationEventPublisher applicationEventPublisher,
            ArtifactService artifactService) {
        this.applicationEventPublisher = applicationEventPublisher;
        this.artifactService = artifactService;
    }

    @Override
    @Loggable
    @HystrixCommand(fallbackMethod = "defaultFindAll", ignoreExceptions = IllegalStateException.class, commandProperties = @HystrixProperty(name = EXECUTION_ISOLATION, value = SEMAPHORE))
    public Observable<Optional<Page<Artifact>>> findAllByPage(Pageable pageable) {
        return new ObservableResult<Optional<Page<Artifact>>>() {
            @Override
            public Optional<Page<Artifact>> invoke() {
                return Optional.ofNullable(artifactService.findAll(pageable));
            }
        };
    }

    @Override
    @Loggable
    @HystrixCommand(fallbackMethod = "defaultFindAllOwned", ignoreExceptions = IllegalStateException.class, commandProperties = @HystrixProperty(name = EXECUTION_ISOLATION, value = SEMAPHORE))
    public Observable<Optional<Page<Artifact>>> findAllOwned(Pageable pageable, User user) {
        return new ObservableResult<Optional<Page<Artifact>>>() {
            @Override
            public Optional<Page<Artifact>> invoke() {
                return Optional.ofNullable(artifactService.findAllOwned(pageable, user));
            }
        };
    }

    @Override
    @Loggable
    @HystrixCommand(ignoreExceptions = IllegalArgumentException.class)
    public Observable<Optional<Artifact>> save(Artifact artifact, User user) {
        return new ObservableResult<Optional<Artifact>>() {
            @Override
            public Optional<Artifact> invoke() {
                Optional<Artifact> optional = Optional.ofNullable(artifactService.save(artifact, user));
                if (optional.isPresent()) {
                    applicationEventPublisher.publishEvent(new CreatedArtifactEvent(optional.get()));
                }
                return optional;
            }
        };
    }

    @Override
    @Loggable
    @HystrixCommand(fallbackMethod = "defaultFindOne", ignoreExceptions = IllegalArgumentException.class, commandProperties = @HystrixProperty(name = EXECUTION_ISOLATION, value = SEMAPHORE))
    public Optional<Artifact> findOne(String id) {
        Artifact one = artifactService.findOne(id);
        return Optional.ofNullable(one);
    }

    @Override
    @Loggable
    @HystrixCommand(ignoreExceptions = IllegalArgumentException.class)
    public void delete(String id, User user) {
        artifactService.delete(id, user);
    }

    @Loggable
    @SuppressWarnings("unused")
    @HystrixCommand
    private Optional<Page<Artifact>> defaultFindAll(Pageable pageable) {
        log.warn("Default find all owned triggered on page {}", pageable.getPageNumber());
        return Optional.of(new PageImpl<>(Collections.emptyList()));
    }

    @Loggable
    @SuppressWarnings("unused")
    @HystrixCommand
    private Optional<Page<Artifact>> defaultFindAllOwned(Pageable pageable, User user) {
        log.warn("Default find all owned triggered for user {} on page {}", user.getId(), pageable.getPageNumber());
        return Optional.of(new PageImpl<>(Collections.emptyList()));
    }

    @Loggable
    @SuppressWarnings("unused")
    @HystrixCommand
    private Optional<Artifact> defaultFindOne(String id) {
        log.warn("Default find one fallback triggered for id {}", id);
        return Optional.of(new Artifact(id));
    }
}