Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.service.internal;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import org.hibernate.integrator.internal.IntegratorServiceImpl;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.integrator.spi.IntegratorService;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.service.BootstrapServiceRegistry;
import org.hibernate.service.Service;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.classloading.internal.ClassLoaderServiceImpl;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.spi.ServiceBinding;
import org.hibernate.service.spi.ServiceException;
import org.hibernate.service.spi.ServiceInitiator;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.spi.Stoppable;
import org.jboss.logging.Logger;
/**
* {@link ServiceRegistry} implementation containing specialized "bootstrap" services, specifically:
*
{@link ClassLoaderService}
*
{@link IntegratorService}
*
*
* @author Steve Ebersole
*/
public class BootstrapServiceRegistryImpl
implements ServiceRegistryImplementor, BootstrapServiceRegistry, ServiceBinding.ServiceLifecycleOwner {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
BootstrapServiceRegistryImpl.class.getName()
);
private static final LinkedHashSet NO_INTEGRATORS = new LinkedHashSet();
private final boolean autoCloseRegistry;
private boolean active = true;
private final ServiceBinding classLoaderServiceBinding;
private final ServiceBinding integratorServiceBinding;
private Set childRegistries;
public BootstrapServiceRegistryImpl() {
this( new ClassLoaderServiceImpl(), NO_INTEGRATORS );
}
public BootstrapServiceRegistryImpl(
ClassLoaderService classLoaderService,
IntegratorService integratorService) {
this( true, classLoaderService, integratorService );
}
public BootstrapServiceRegistryImpl(
boolean autoCloseRegistry,
ClassLoaderService classLoaderService,
IntegratorService integratorService) {
this.autoCloseRegistry = autoCloseRegistry;
this.classLoaderServiceBinding = new ServiceBinding(
this,
ClassLoaderService.class,
classLoaderService
);
this.integratorServiceBinding = new ServiceBinding(
this,
IntegratorService.class,
integratorService
);
}
public BootstrapServiceRegistryImpl(
ClassLoaderService classLoaderService,
LinkedHashSet providedIntegrators) {
this( true, classLoaderService, new IntegratorServiceImpl( providedIntegrators, classLoaderService ) );
}
@Override
public R getService(Class serviceRole) {
final ServiceBinding binding = locateServiceBinding( serviceRole );
return binding == null ? null : binding.getService();
}
@Override
@SuppressWarnings( {"unchecked"})
public ServiceBinding locateServiceBinding(Class serviceRole) {
if ( ClassLoaderService.class.equals( serviceRole ) ) {
return (ServiceBinding) classLoaderServiceBinding;
}
else if ( IntegratorService.class.equals( serviceRole ) ) {
return (ServiceBinding) integratorServiceBinding;
}
return null;
}
@Override
public void destroy() {
if ( !active ) {
return;
}
active = false;
destroy( classLoaderServiceBinding );
destroy( integratorServiceBinding );
}
public boolean isActive() {
return active;
}
private void destroy(ServiceBinding serviceBinding) {
serviceBinding.getLifecycleOwner().stopService( serviceBinding );
}
@Override
public ServiceRegistry getParentServiceRegistry() {
return null;
}
@Override
public R initiateService(ServiceInitiator serviceInitiator) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public void configureService(ServiceBinding binding) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public void injectDependencies(ServiceBinding binding) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public void startService(ServiceBinding binding) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public void stopService(ServiceBinding binding) {
final Service service = binding.getService();
if ( Stoppable.class.isInstance( service ) ) {
try {
( (Stoppable) service ).stop();
}
catch ( Exception e ) {
LOG.unableToStopService( service.getClass(), e.toString() );
}
}
}
@Override
public void registerChild(ServiceRegistryImplementor child) {
if ( childRegistries == null ) {
childRegistries = new HashSet();
}
if ( !childRegistries.add( child ) ) {
LOG.warnf( "Child ServiceRegistry [%s] was already registered; this will end badly later...", child );
}
}
@Override
public void deRegisterChild(ServiceRegistryImplementor child) {
if ( childRegistries == null ) {
throw new IllegalStateException( "No child ServiceRegistry registrations found" );
}
childRegistries.remove( child );
if ( childRegistries.isEmpty() ) {
if ( autoCloseRegistry ) {
LOG.debug( "Implicitly destroying Boot-strap registry on de-registration " + "of all child ServiceRegistries" );
destroy();
}
else {
LOG.debug( "Skipping implicitly destroying Boot-strap registry on de-registration " + "of all child ServiceRegistries" );
}
}
}
}