/
BootstrapServiceRegistryImpl.java
161 lines (136 loc) · 5.33 KB
/
BootstrapServiceRegistryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* 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.LinkedHashSet;
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:<ul>
* <li>{@link ClassLoaderService}</li>
* <li>{@link IntegratorService}</li>
* </ul>
*
* @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<Integrator> NO_INTEGRATORS = new LinkedHashSet<Integrator>();
private final ServiceBinding<ClassLoaderService> classLoaderServiceBinding;
private final ServiceBinding<IntegratorService> integratorServiceBinding;
public BootstrapServiceRegistryImpl() {
this( new ClassLoaderServiceImpl(), NO_INTEGRATORS );
}
public BootstrapServiceRegistryImpl(
ClassLoaderService classLoaderService,
IntegratorService integratorService) {
this.classLoaderServiceBinding = new ServiceBinding<ClassLoaderService>(
this,
ClassLoaderService.class,
classLoaderService
);
this.integratorServiceBinding = new ServiceBinding<IntegratorService>(
this,
IntegratorService.class,
integratorService
);
}
public BootstrapServiceRegistryImpl(
ClassLoaderService classLoaderService,
LinkedHashSet<Integrator> providedIntegrators) {
this( classLoaderService, new IntegratorServiceImpl( providedIntegrators, classLoaderService ) );
}
@Override
public <R extends Service> R getService(Class<R> serviceRole) {
final ServiceBinding<R> binding = locateServiceBinding( serviceRole );
return binding == null ? null : binding.getService();
}
@Override
@SuppressWarnings( {"unchecked"})
public <R extends Service> ServiceBinding<R> locateServiceBinding(Class<R> serviceRole) {
if ( ClassLoaderService.class.equals( serviceRole ) ) {
return (ServiceBinding<R>) classLoaderServiceBinding;
}
else if ( IntegratorService.class.equals( serviceRole ) ) {
return (ServiceBinding<R>) integratorServiceBinding;
}
return null;
}
@Override
public void destroy() {
destroy( classLoaderServiceBinding );
destroy( integratorServiceBinding );
}
private void destroy(ServiceBinding serviceBinding) {
serviceBinding.getLifecycleOwner().stopService( serviceBinding );
}
@Override
public ServiceRegistry getParentServiceRegistry() {
return null;
}
@Override
public <R extends Service> R initiateService(ServiceInitiator<R> serviceInitiator) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public <R extends Service> void configureService(ServiceBinding<R> binding) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public <R extends Service> void injectDependencies(ServiceBinding<R> binding) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public <R extends Service> void startService(ServiceBinding<R> binding) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public <R extends Service> void stopService(ServiceBinding<R> binding) {
final Service service = binding.getService();
if ( Stoppable.class.isInstance( service ) ) {
try {
( (Stoppable) service ).stop();
}
catch ( Exception e ) {
LOG.unableToStopService( service.getClass(), e.toString() );
}
}
}
}