001/*
002 * Copyright (c) 2014 Maxim Yunusov
003 *    Licensed under the Apache License, Version 2.0 (the "License");
004 *    you may not use this file except in compliance with the License.
005 *    You may obtain a copy of the License at
006 *
007 *        http://www.apache.org/licenses/LICENSE-2.0
008 *
009 *    Unless required by applicable law or agreed to in writing, software
010 *    distributed under the License is distributed on an "AS IS" BASIS,
011 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 *    See the License for the specific language governing permissions and
013 *    limitations under the License.
014 */
015
016package org.maxur.perfmodel.backend.service.impl;
017
018import org.glassfish.grizzly.Grizzly;
019import org.glassfish.grizzly.http.server.HttpServer;
020import org.glassfish.grizzly.http.server.StaticHttpHandler;
021import org.glassfish.hk2.api.ServiceLocator;
022import org.glassfish.jersey.server.ResourceConfig;
023import org.maxur.perfmodel.backend.service.WebServer;
024
025import javax.inject.Inject;
026import javax.inject.Named;
027import javax.ws.rs.ext.Provider;
028import java.net.URI;
029
030import static org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer;
031
032@Provider
033public class WebServerGrizzlyImpl extends WebServer {
034
035    private HttpServer httpServer;
036
037    @Inject
038    private ResourceConfig config;
039
040    @Inject
041    private ServiceLocator locator;
042
043    @SuppressWarnings("unused")
044    @Named("webapp.folderName")
045    private String webappFolderName;
046
047    @SuppressWarnings("unused")
048    @Named("api.url")
049    private URI apiUri;
050
051
052    @Override
053    protected void launch() {
054        httpServer = createHttpServer(apiUri, config, locator);
055        httpServer.getServerConfiguration().addHttpHandler(
056                new StaticHttpHandler(webappFolderName),
057                WEB_APP_URL
058        );
059    }
060
061    @Override
062    protected void shutdown() {
063        httpServer.shutdownNow();
064    }
065
066    @Override
067    protected String version() {
068        return "Grizzly Http Server " + Grizzly.getDotedVersion();
069    }
070
071    @Override
072    public boolean isStarted() {
073        return httpServer.isStarted();
074    }
075}