001/** 002 * Copyright (c) 2025-2026, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.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 * <p> 008 * http://www.gnu.org/licenses/lgpl-3.0.txt 009 * <p> 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 dev.tinyflow.core.code; 017 018import dev.tinyflow.core.code.impl.JavascriptRuntimeEngine; 019 020import java.util.ArrayList; 021import java.util.List; 022 023public class CodeRuntimeEngineManager { 024 025 public List<CodeRuntimeEngineProvider> providers = new ArrayList<>(); 026 027 private static class ManagerHolder { 028 private static final CodeRuntimeEngineManager INSTANCE = new CodeRuntimeEngineManager(); 029 } 030 031 private CodeRuntimeEngineManager() { 032 JavascriptRuntimeEngine javascriptRuntimeEngine = new JavascriptRuntimeEngine(); 033 providers.add(engineId -> { 034 if ("js".equals(engineId) || "javascript".equals(engineId)) { 035 return javascriptRuntimeEngine; 036 } 037 return null; 038 }); 039 } 040 041 public static CodeRuntimeEngineManager getInstance() { 042 return ManagerHolder.INSTANCE; 043 } 044 045 public void registerProvider(CodeRuntimeEngineProvider provider) { 046 providers.add(provider); 047 } 048 049 public void removeProvider(CodeRuntimeEngineProvider provider) { 050 providers.remove(provider); 051 } 052 053 public CodeRuntimeEngine getCodeRuntimeEngine(Object engineId) { 054 for (CodeRuntimeEngineProvider provider : providers) { 055 CodeRuntimeEngine codeRuntimeEngine = provider.getCodeRuntimeEngine(engineId); 056 if (codeRuntimeEngine != null) { 057 return codeRuntimeEngine; 058 } 059 } 060 return null; 061 } 062}