org.apache.accumulo.server.AbstractServer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of accumulo-server-base Show documentation
Show all versions of accumulo-server-base Show documentation
A common base library for Apache Accumulo servers.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.apache.accumulo.server;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.accumulo.core.Constants;
import org.apache.accumulo.core.classloader.ClassLoaderUtil;
import org.apache.accumulo.core.conf.AccumuloConfiguration;
import org.apache.accumulo.core.metrics.MetricsUtil;
import org.apache.accumulo.core.trace.TraceUtil;
import org.apache.accumulo.server.security.SecurityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AbstractServer implements AutoCloseable, Runnable {
private final ServerContext context;
protected final String applicationName;
private final String hostname;
private final Logger log;
protected AbstractServer(String appName, ServerOpts opts, String[] args) {
this.log = LoggerFactory.getLogger(getClass().getName());
this.applicationName = appName;
opts.parseArgs(appName, args);
this.hostname = Objects.requireNonNull(opts.getAddress());
var siteConfig = opts.getSiteConfiguration();
SecurityUtil.serverLogin(siteConfig);
context = new ServerContext(siteConfig);
log.info("Version " + Constants.VERSION);
log.info("Instance " + context.getInstanceID());
context.init(appName);
ClassLoaderUtil.initContextFactory(context.getConfiguration());
TraceUtil.initializeTracer(context.getConfiguration());
if (context.getSaslParams() != null) {
// Server-side "client" check to make sure we're logged in as a user we expect to be
context.enforceKerberosLogin();
}
}
/**
* Run this server in a main thread
*/
public void runServer() throws Exception {
final AtomicReference err = new AtomicReference<>();
Thread service = new Thread(TraceUtil.wrap(this), applicationName);
service.setUncaughtExceptionHandler((thread, exception) -> err.set(exception));
service.start();
service.join();
Throwable thrown = err.get();
if (thrown != null) {
if (thrown instanceof Error) {
throw (Error) thrown;
}
if (thrown instanceof Exception) {
throw (Exception) thrown;
}
throw new RuntimeException("Weird throwable type thrown", thrown);
}
}
public String getHostname() {
return hostname;
}
public ServerContext getContext() {
return context;
}
public AccumuloConfiguration getConfiguration() {
return getContext().getConfiguration();
}
@Override
public void close() {
MetricsUtil.close();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy