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