001/**
002 * Copyright (C) 2006-2020 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.server.front;
017
018import java.util.Date;
019import java.util.Spliterator;
020import java.util.Spliterators;
021import java.util.concurrent.atomic.AtomicReference;
022import java.util.stream.StreamSupport;
023
024import javax.annotation.PostConstruct;
025import javax.enterprise.context.ApplicationScoped;
026import javax.enterprise.inject.Instance;
027import javax.inject.Inject;
028import javax.ws.rs.ApplicationPath;
029import javax.ws.rs.core.Application;
030
031import org.eclipse.microprofile.config.inject.ConfigProperty;
032import org.talend.sdk.component.server.api.EnvironmentResource;
033import org.talend.sdk.component.server.configuration.ComponentServerConfiguration;
034import org.talend.sdk.component.server.front.model.Environment;
035import org.talend.sdk.component.server.service.ComponentManagerService;
036
037@ApplicationScoped
038public class EnvironmentResourceImpl implements EnvironmentResource {
039
040    private final AtomicReference<Environment> environment = new AtomicReference<>();
041
042    @Inject
043    @ConfigProperty(name = "git.build.version")
044    private String version;
045
046    @Inject
047    @ConfigProperty(name = "git.commit.id")
048    private String commit;
049
050    @Inject
051    @ConfigProperty(name = "git.build.time")
052    private String time;
053
054    @Inject
055    private ComponentServerConfiguration configuration;
056
057    @Inject
058    private Instance<Application> applications;
059
060    @Inject
061    private ComponentManagerService service;
062
063    private int latestApiVersion;
064
065    private Date startTime;
066
067    @PostConstruct
068    private void init() {
069        latestApiVersion = StreamSupport
070                .stream(Spliterators.spliteratorUnknownSize(applications.iterator(), Spliterator.IMMUTABLE), false)
071                .filter(a -> a.getClass().isAnnotationPresent(ApplicationPath.class))
072                .map(a -> a.getClass().getAnnotation(ApplicationPath.class).value())
073                .map(path -> path.replace("api/v", ""))
074                .mapToInt(Integer::parseInt)
075                .max()
076                .orElse(1);
077        startTime = new Date();
078    }
079
080    @Override
081    public Environment get() {
082        return new Environment(latestApiVersion, version, commit, time,
083                configuration.getChangeLastUpdatedAtStartup() ? findLastUpdated() : service.findLastUpdated());
084    }
085
086    private Date findLastUpdated() {
087        final Date lastUpdated = service.findLastUpdated();
088        return startTime.after(lastUpdated) ? startTime : lastUpdated;
089    }
090}