001/* 002 * Copyright 2023 Anyware Services 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.ametys.web.analytics; 017 018import java.util.HashMap; 019import java.util.Map; 020import java.util.Objects; 021import java.util.stream.Collectors; 022 023import org.apache.avalon.framework.component.Component; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027 028import org.ametys.runtime.i18n.I18nizableText; 029import org.ametys.runtime.model.Enumerator; 030 031/** 032 * {@link Enumerator} for the web analytics provider enumerator. 033 */ 034public class WebAnalyticsProviderEnumerator implements Enumerator<String>, Serviceable, Component 035{ 036 /** Avalon role */ 037 public static final String ROLE = WebAnalyticsProviderEnumerator.class.getName(); 038 039 /** The web analytics component extension point */ 040 protected WebAnalyticsProviderExtensionPoint _webAnalyticsComponentEP; 041 042 /** The service manager */ 043 protected ServiceManager _manager; 044 045 @Override 046 public void service(ServiceManager manager) throws ServiceException 047 { 048 _manager = manager; 049 } 050 051 private WebAnalyticsProviderExtensionPoint _getWebAnalyticsProviderExtensionPoint() 052 { 053 if (_webAnalyticsComponentEP == null) 054 { 055 try 056 { 057 _webAnalyticsComponentEP = (WebAnalyticsProviderExtensionPoint) _manager.lookup(WebAnalyticsProviderExtensionPoint.ROLE); 058 } 059 catch (ServiceException e) 060 { 061 // Can be called during application startup 062 return null; 063 } 064 } 065 return _webAnalyticsComponentEP; 066 } 067 068 @Override 069 public I18nizableText getEntry(String value) throws Exception 070 { 071 WebAnalyticsProviderExtensionPoint webAnalyticsProviderEP = _getWebAnalyticsProviderExtensionPoint(); 072 if (webAnalyticsProviderEP != null && webAnalyticsProviderEP.hasExtension(value)) 073 { 074 return webAnalyticsProviderEP.getExtension(value).getLabel(); 075 } 076 else 077 { 078 // Can be called during application startup 079 return new I18nizableText(value); 080 } 081 } 082 083 @Override 084 public Map<String, I18nizableText> getTypedEntries() throws Exception 085 { 086 WebAnalyticsProviderExtensionPoint webAnalyticsProviderEP = _getWebAnalyticsProviderExtensionPoint(); 087 if (webAnalyticsProviderEP == null) 088 { 089 return Map.of(); 090 } 091 092 Map<String, I18nizableText> entries = new HashMap<>(); 093 entries.put(WebAnalyticsProviderExtensionPoint.NO_PROVIDER_ID, new I18nizableText("plugin.web", "PLUGINS_WEB_ANALYTICS_PROVIDER_ENUMERATOR_NONE")); 094 095 entries.putAll(webAnalyticsProviderEP.getExtensionsIds() 096 .stream() 097 .map(id -> webAnalyticsProviderEP.getExtension(id)) 098 .filter(Objects::nonNull) 099 .collect( 100 Collectors.toMap( 101 WebAnalyticsProvider::getId, 102 WebAnalyticsProvider::getLabel 103 ) 104 )); 105 106 return entries; 107 } 108}