/* * Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * 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 com.hazelcast.internal.management; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.webapp.WebAppContext; public class JettyServer { private Server server; JettyServer(int port, String sourceDir, String serverXml) throws Exception { buildJetty(port, sourceDir, serverXml); } public void stop() throws Exception { server.stop(); } public void start() throws Exception { server.start(); } public void restart() throws Exception { server.stop(); server.start(); } public boolean isRunning() { if (server == null) { return false; } else { return server.isRunning(); } } private void buildJetty(int port, String sourceDir, String webXmlFile) throws Exception { server = new Server(); SelectChannelConnector connector = new SelectChannelConnector(); connector.setPort(port); server.addConnector(connector); WebAppContext context = new WebAppContext(); context.setResourceBase(sourceDir); context.setDescriptor(sourceDir + "/WEB-INF/" + webXmlFile); context.setLogUrlOnStart(true); context.setContextPath("/"); context.setParentLoaderPriority(true); server.setHandler(context); server.start(); } }