001/* 002 * (C) Copyright 2020 Nuxeo (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * Kevin Leturc <kleturc@nuxeo.com> 018 */ 019 020package org.nuxeo.runtime.capabilities; 021 022import static java.util.stream.Collectors.collectingAndThen; 023import static java.util.stream.Collectors.toMap; 024import static org.apache.commons.lang3.StringUtils.isNotBlank; 025import static org.nuxeo.common.Environment.DISTRIBUTION_HOTFIX; 026import static org.nuxeo.common.Environment.DISTRIBUTION_NAME; 027import static org.nuxeo.common.Environment.DISTRIBUTION_SERVER; 028import static org.nuxeo.common.Environment.DISTRIBUTION_VERSION; 029 030import java.util.HashMap; 031import java.util.LinkedHashMap; 032import java.util.Map; 033import java.util.function.Supplier; 034 035import org.nuxeo.runtime.api.Framework; 036import org.nuxeo.runtime.model.ComponentContext; 037import org.nuxeo.runtime.model.ComponentManager; 038import org.nuxeo.runtime.model.ComponentStartOrders; 039import org.nuxeo.runtime.model.DefaultComponent; 040 041/** 042 * @since 11.5 043 */ 044public class CapabilitiesServiceImpl extends DefaultComponent implements CapabilitiesService { 045 046 public static final String CAPABILITY_SERVER = "server"; 047 048 protected final Map<String, Supplier<Map<String, Object>>> capabilitiesSuppliers = new HashMap<>(); 049 050 @Override 051 public void activate(ComponentContext context) { 052 super.activate(context); 053 new ComponentManager.Listener() { 054 @Override 055 public void beforeStart(ComponentManager mgr, boolean isResume) { 056 capabilitiesSuppliers.clear(); 057 } 058 }.install(); 059 } 060 061 @Override 062 public int getApplicationStartedOrder() { 063 // very early as other services depend on us 064 return ComponentStartOrders.CAPABILITIES; 065 } 066 067 @Override 068 public void start(ComponentContext context) { 069 registerCapabilities(CAPABILITY_SERVER, this::getServerCapabilities); 070 } 071 072 protected Map<String, Object> getServerCapabilities() { 073 var serverCapabilities = new LinkedHashMap<String, Object>(); 074 serverCapabilities.put("distributionName", Framework.getProperty(DISTRIBUTION_NAME)); 075 serverCapabilities.put("distributionVersion", Framework.getProperty(DISTRIBUTION_VERSION)); 076 serverCapabilities.put("distributionServer", Framework.getProperty(DISTRIBUTION_SERVER)); 077 var hotfixVersion = Framework.getProperty(DISTRIBUTION_HOTFIX); 078 if (isNotBlank(hotfixVersion)) { 079 serverCapabilities.put("hotfixVersion", hotfixVersion); 080 } 081 return serverCapabilities; 082 } 083 084 // ------------------------ 085 // Service implementations 086 // ------------------------ 087 088 @Override 089 public void registerCapabilities(String name, Map<String, Object> map) { 090 registerCapabilities(name, () -> map); 091 } 092 093 @Override 094 public void registerCapabilities(String name, Supplier<Map<String, Object>> supplier) { 095 capabilitiesSuppliers.put(name, supplier); 096 } 097 098 @Override 099 public Capabilities getCapabilities() { 100 return capabilitiesSuppliers.entrySet() 101 .stream() 102 .collect(collectingAndThen(toMap(Map.Entry::getKey, e -> e.getValue().get()), 103 Capabilities::new)); 104 } 105}