All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opentable.server.PreFlight Maven / Gradle / Ivy

Go to download

Wiring for basic Jetty core servlet regardless of MVC or JaxRS choices. Also wires logging, metrics, etc

There is a newer version: 6.0.4
Show newest version
/*
 * 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.opentable.server;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.eclipse.jetty.util.ProcessorUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opentable.logging.CommonLogHolder;
import com.opentable.service.K8sInfo;

public class PreFlight {
    private static final String COMMIT = "X-BasePOM-Git-Commit-Id";

    private static final Logger LOG = LoggerFactory.getLogger(PreFlight.class);

    @Inject
    public PreFlight(K8sInfo k8sInfo) {
        LOG.debug("Setting k8sInfo kubernetes: {}, cluster: {}, namespace:{}",
                k8sInfo.isKubernetes(), k8sInfo.getClusterName().orElse(null), k8sInfo.getNamespace().orElse(null));
        CommonLogHolder.setK8sInfo(k8sInfo);
    }

    public void readManifests() throws IOException {
        final Enumeration urlsEnum = Thread.currentThread().getContextClassLoader().getResources("META-INF/MANIFEST.MF");
        while (urlsEnum.hasMoreElements()) {
            readManifest(urlsEnum.nextElement());
        }
    }

    private void readManifest(final URL url) throws IOException {
        try (InputStream is = url.openStream()) {
            final Manifest mf = new Manifest();
            mf.read(is);
            final Attributes atts = mf.getMainAttributes();
            LOG.debug("Starting up: {} version {} - built from commit {}",
                    atts.getValue(Attributes.Name.IMPLEMENTATION_TITLE),
                    atts.getValue(Attributes.Name.IMPLEMENTATION_VERSION),
                    atts.getValue(COMMIT)
            );
        }
    }

    @PostConstruct
    public void start() {
        try {
            LOG.info("At startup: JVM {} processors, and Jetty {} processors", Runtime.getRuntime().availableProcessors(), ProcessorUtils.availableProcessors());
            readManifests();
        } catch (IOException e) {
            LOG.debug("Error while reading manifest", e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy