/**
* Copyright (C) 2009-2013 FoundationDB, LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.foundationdb.server.service;
import java.util.concurrent.atomic.AtomicReference;
import com.foundationdb.server.error.ServiceNotStartedException;
import com.foundationdb.server.service.servicemanager.DelegatingServiceManager;
import com.foundationdb.server.service.session.Session;
public final class ServiceManagerImpl extends DelegatingServiceManager
{
private static final AtomicReference<ServiceManager> instance = new AtomicReference<>(null);
public static void setServiceManager(ServiceManager newInstance) {
if (newInstance == null) {
instance.set(null);
} else if (!instance.compareAndSet(null, newInstance)) {
throw new RuntimeException(
"Tried to install a ServiceManager, but one was already set");
}
}
public ServiceManagerImpl() {}
/**
* Gets the active ServiceManager; you can then use the returned instance to get any service you want.
* @return the active ServiceManager
* @deprecated for new code, please just use dependency injection
*/
@Deprecated
public static ServiceManager get() {
return installed();
}
/**
* Convenience for {@code ServiceManagerImpl.get().getSessionService().createSession()}
* @return a new Session
*/
public static Session newSession() {
return installed().getSessionService().createSession();
}
// DelegatingServiceManager override
@Override
protected ServiceManager delegate() {
return installed();
}
// for use by this class
private static ServiceManager installed() {
ServiceManager sm = instance.get();
if (sm == null) {
throw new ServiceNotStartedException("services haven't been started");
}
return sm;
}
}