001/**
002 * Copyright (C) 2006-2025 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.cli;
017
018import static java.util.Optional.ofNullable;
019
020import org.apache.catalina.connector.Connector;
021import org.apache.catalina.core.StandardServer;
022import org.apache.catalina.webresources.StandardRoot;
023import org.apache.coyote.http11.Http11NioProtocol;
024import org.apache.meecrowave.Meecrowave;
025import org.apache.meecrowave.runner.Cli;
026
027import lombok.extern.slf4j.Slf4j;
028
029// utility to use into the studio, mainly to workaround JVM URL stream handler limitations
030@Slf4j
031public class EnhancedCli extends Cli implements AutoCloseable {
032
033    private final String[] args;
034
035    private volatile Meecrowave instance;
036
037    public EnhancedCli(final String[] args) {
038        super(args);
039        this.args = args;
040    }
041
042    @Override
043    public void run() {
044        try {
045            try (final Meecrowave meecrowave = new Meecrowave(create(args)) {
046
047                @Override
048                protected Connector createConnector() {
049                    return new Connector(CustomPefixHttp11NioProtocol.class.getName());
050                }
051            }) {
052                this.instance = meecrowave;
053
054                meecrowave.start();
055                meecrowave.deployClasspath(new Meecrowave.DeploymentMeta("", null, stdCtx -> {
056                    stdCtx.setResources(new StandardRoot() {
057
058                        @Override
059                        protected void registerURLStreamHandlerFactory() {
060                            // no-op: not supported into OSGi since there is already one and it must set a
061                            // single time
062                        }
063                    });
064                }, null));
065
066                doWait(meecrowave, null);
067            }
068        } catch (final Exception e) {
069            log.error("[Meecrowave#run]", e);
070            throw new IllegalStateException(e);
071        }
072    }
073
074    @Override
075    public void close() {
076        ofNullable(instance).ifPresent(mw -> StandardServer.class.cast(mw.getTomcat().getServer()).stopAwait());
077    }
078
079    public static class CustomPefixHttp11NioProtocol extends Http11NioProtocol {
080
081        @Override
082        protected String getNamePrefix() {
083            return "talend-component-kit-" + super.getNamePrefix();
084        }
085    }
086}