/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. Camunda licenses this file to you under the Apache License,
 * Version 2.0; 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 org.camunda.bpm.engine.rest.util;

import static org.camunda.bpm.engine.rest.util.EngineUtil.getProcessEngineProvider;

import java.util.function.Consumer;
import java.util.function.Function;

import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngineConfiguration;
import org.camunda.bpm.engine.impl.telemetry.TelemetryRegistry;
import org.camunda.bpm.engine.impl.telemetry.dto.LicenseKeyData;
import org.camunda.bpm.engine.rest.spi.ProcessEngineProvider;

public class WebApplicationUtil {

  public static void setApplicationServer(String serverInfo) {
    if (serverInfo != null && !serverInfo.isEmpty() ) {
      addToTelemetryRegistry(t -> t.setApplicationServer(serverInfo), t -> t.getApplicationServer() == null);
    }
  }

  public static void setLicenseKey(LicenseKeyData licenseKeyData) {
    if (licenseKeyData != null) {
      addToTelemetryRegistry(t -> t.setLicenseKey(licenseKeyData), t -> true);
    }
  }

  protected static void addToTelemetryRegistry(Consumer<TelemetryRegistry> addTelemetryData, Function<TelemetryRegistry, Boolean> addTelemetryDataFilter) {
    ProcessEngineProvider processEngineProvider = getProcessEngineProvider();
    for (String engineName : processEngineProvider.getProcessEngineNames()) {
      TelemetryRegistry telemetryRegistry = getTelemetryRegistry(processEngineProvider, engineName);
      if (telemetryRegistry != null && addTelemetryDataFilter.apply(telemetryRegistry)) {
          addTelemetryData.accept(telemetryRegistry);
      }
    }
  }

  protected static TelemetryRegistry getTelemetryRegistry(ProcessEngineProvider processEngineProvider, String engineName) {
    ProcessEngine processEngine = processEngineProvider.getProcessEngine(engineName);
    ProcessEngineConfiguration configuration = processEngine.getProcessEngineConfiguration();
    return configuration.getTelemetryRegistry();
  }
}

@@ -933,81 +933,8 @@
 r;%0A%0A
-import java.util.function.Consumer;%0Aimport java.util.function.Function;%0A%0A
 impo
@@ -1388,215 +1388,348 @@
     
-addToTelemetryRegistry(t -%3E t.setApplicationServer(serverInfo), t -%3E t.getApplicationServer() == null);%0A    %7D%0A  %7D%0A%0A  public static void setLicenseKey(LicenseKeyData licenseKeyData) %7B%0A
+ProcessEngineProvider processEngineProvider = getProcessEngineProvider();%0A      for (String engineName : processEngineProvider.getProcessEngineNames()) %7B%0A        TelemetryRegistry telemetryRegistry = getTelemetryRegistry(processEngineProvider, engineName);%0A 
     
+   
 if (
-licenseKeyData !
+telemetryRegistry != null && telemetryRegistry.getApplicationServer() =
 = nu
@@ -1740,22 +1740,23 @@
 %7B%0A      
-addToT
+      t
 elemetry
@@ -1767,58 +1767,60 @@
 stry
-(t -%3E t.setLicenseKey(licenseKeyData), t -%3E true);
+.setApplicationServer(serverInfo);%0A        %7D%0A      %7D
 %0A   
@@ -1826,32 +1826,29 @@
   %7D%0A  %7D%0A%0A  p
-rotected
+ublic
  static void
@@ -1852,144 +1852,93 @@
 oid 
-addToTelemetryRegistry(Consumer%3CTelemetryRegistry%3E addTelemetryData, Function%3CTelemetryRegistry, Boolean%3E addTelemetryDataFilter
+setLicenseKey(LicenseKeyData licenseKeyData) %7B%0A    if (licenseKeyData != null
 ) %7B%0A    
 Proc
@@ -1925,32 +1925,34 @@
  != null) %7B%0A    
+  
 ProcessEnginePro
@@ -2009,24 +2009,26 @@
 ider();%0A    
+ 
+ 
 for (String 
@@ -2085,24 +2085,26 @@
 eNames()) %7B%0A
+  
       Teleme
@@ -2194,24 +2194,26 @@
 ame);%0A      
+  
 if (telemetr
@@ -2233,59 +2233,8 @@
 null
- && addTelemetryDataFilter.apply(telemetryRegistry)
 ) %7B%0A
@@ -2247,20 +2247,19 @@
     
-addT
+  t
 elemetry
 Data
@@ -2258,39 +2258,58 @@
 etry
-Data.accept(telemetryRegistry);
+Registry.setLicenseKey(licenseKeyData);;%0A        %7D
 %0A   

make `WebApplicationUtil` JDK7 compliant for easier backporting