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