Skip to content

Commit c348a97

Browse files
committed
Refactor internals move shutdown hook into DBeanScope
1 parent 4c625cc commit c348a97

File tree

4 files changed

+41
-112
lines changed

4 files changed

+41
-112
lines changed

inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java

Lines changed: 1 addition & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,7 @@ public BeanScope build() {
116116
for (BeanContextFactory factory : factoryOrder.factories()) {
117117
factory.build(builder);
118118
}
119-
120-
BeanScope beanScope = builder.build();
121-
if (shutdownHook) {
122-
return new ShutdownAwareBeanScope(beanScope);
123-
}
124-
return beanScope;
119+
return builder.build(shutdownHook);
125120
}
126121

127122
/**
@@ -137,106 +132,6 @@ private Class<?> suppliedType(Class<?> suppliedClass) {
137132
}
138133
}
139134

140-
/**
141-
* Internal shutdown hook.
142-
*/
143-
private static class Hook extends Thread {
144-
145-
private final ShutdownAwareBeanScope context;
146-
147-
Hook(ShutdownAwareBeanScope context) {
148-
this.context = context;
149-
}
150-
151-
@Override
152-
public void run() {
153-
context.shutdown();
154-
}
155-
}
156-
157-
/**
158-
* Proxy that handles shutdown hook registration and de-registration.
159-
*/
160-
private static class ShutdownAwareBeanScope implements BeanScope {
161-
162-
private final ReentrantLock lock = new ReentrantLock();
163-
private final BeanScope beanScope;
164-
private final Hook hook;
165-
private boolean shutdown;
166-
167-
ShutdownAwareBeanScope(BeanScope beanScope) {
168-
this.beanScope = beanScope;
169-
this.hook = new Hook(this);
170-
Runtime.getRuntime().addShutdownHook(hook);
171-
}
172-
173-
@Override
174-
public RequestScopeBuilder newRequestScope() {
175-
return beanScope.newRequestScope();
176-
}
177-
178-
@Override
179-
public <T> RequestScopeMatch<T> requestProvider(Class<T> type, String name) {
180-
return beanScope.requestProvider(type, name);
181-
}
182-
183-
@Override
184-
public <T> T get(Class<T> beanClass) {
185-
return beanScope.get(beanClass);
186-
}
187-
188-
@Override
189-
public <T> T get(Class<T> beanClass, String name) {
190-
return beanScope.get(beanClass, name);
191-
}
192-
193-
@Override
194-
public List<Object> listByAnnotation(Class<?> annotation) {
195-
return beanScope.listByAnnotation(annotation);
196-
}
197-
198-
@Override
199-
public <T> List<T> list(Class<T> interfaceType) {
200-
return beanScope.list(interfaceType);
201-
}
202-
203-
@Override
204-
public <T> List<T> listByPriority(Class<T> interfaceType) {
205-
return beanScope.listByPriority(interfaceType);
206-
}
207-
208-
@Override
209-
public <T> List<T> listByPriority(Class<T> interfaceType, Class<? extends Annotation> priority) {
210-
return beanScope.listByPriority(interfaceType, priority);
211-
}
212-
213-
@Override
214-
public void close() {
215-
lock.lock();
216-
try {
217-
if (!shutdown) {
218-
Runtime.getRuntime().removeShutdownHook(hook);
219-
}
220-
beanScope.close();
221-
} finally {
222-
lock.unlock();
223-
}
224-
}
225-
226-
/**
227-
* Close via shutdown hook.
228-
*/
229-
void shutdown() {
230-
lock.lock();
231-
try {
232-
shutdown = true;
233-
close();
234-
} finally {
235-
lock.unlock();
236-
}
237-
}
238-
}
239-
240135
/**
241136
* Helper to order the BeanContextFactory based on dependsOn.
242137
*/

inject/src/main/java/io/avaje/inject/spi/Builder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,5 @@ static Builder newBuilder(List<SuppliedBean> suppliedBeans, List<EnrichBean> enr
158158
/**
159159
* Build and return the bean context.
160160
*/
161-
BeanScope build();
161+
BeanScope build(boolean withShutdownHook);
162162
}

inject/src/main/java/io/avaje/inject/spi/DBeanScope.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ class DBeanScope implements BeanScope {
2121
private final List<BeanLifecycle> lifecycleList;
2222
private final DBeanMap beans;
2323
private final Map<String, RequestScopeMatch<?>> reqScopeProviders;
24-
24+
private final ShutdownHook shutdownHook;
25+
private boolean shutdown;
2526
private boolean closed;
2627

27-
DBeanScope(List<BeanLifecycle> lifecycleList, DBeanMap beans, Map<String, RequestScopeMatch<?>> reqScopeProviders) {
28+
DBeanScope(boolean withShutdownHook, List<BeanLifecycle> lifecycleList, DBeanMap beans, Map<String, RequestScopeMatch<?>> reqScopeProviders) {
2829
this.lifecycleList = lifecycleList;
2930
this.beans = beans;
3031
this.reqScopeProviders = reqScopeProviders;
32+
if (withShutdownHook) {
33+
this.shutdownHook = new ShutdownHook(this);
34+
Runtime.getRuntime().addShutdownHook(shutdownHook);
35+
} else {
36+
this.shutdownHook = null;
37+
}
3138
}
3239

3340
@Override
@@ -68,7 +75,7 @@ public <T> List<T> listByPriority(Class<T> interfaceType, Class<? extends Annota
6875
return list.size() > 1 ? sortByPriority(list, priorityAnnotation) : list;
6976
}
7077

71-
private <T> List<T> sortByPriority(List<T> list, final Class<? extends Annotation> priorityAnnotation) {
78+
private <T> List<T> sortByPriority(List<T> list, final Class<? extends Annotation> priorityAnnotation) {
7279
boolean priorityUsed = false;
7380
List<SortBean<T>> tempList = new ArrayList<>(list.size());
7481
for (T bean : list) {
@@ -113,6 +120,9 @@ DBeanScope start() {
113120
public void close() {
114121
lock.lock();
115122
try {
123+
if (shutdownHook != null && !shutdown) {
124+
Runtime.getRuntime().removeShutdownHook(shutdownHook);
125+
}
116126
if (!closed) {
117127
// we only allow one call to preDestroy
118128
closed = true;
@@ -126,6 +136,30 @@ public void close() {
126136
}
127137
}
128138

139+
private void shutdown() {
140+
lock.lock();
141+
try {
142+
shutdown = true;
143+
close();
144+
} finally {
145+
lock.unlock();
146+
}
147+
}
148+
149+
private static class ShutdownHook extends Thread {
150+
private final DBeanScope scope;
151+
152+
ShutdownHook(DBeanScope scope) {
153+
this.scope = scope;
154+
}
155+
156+
@Override
157+
public void run() {
158+
scope.shutdown();
159+
}
160+
}
161+
162+
129163
private static class SortBean<T> implements Comparable<SortBean<T>> {
130164

131165
private final T bean;

inject/src/main/java/io/avaje/inject/spi/DBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ private void runInjectors() {
195195
}
196196
}
197197

198-
public BeanScope build() {
198+
public BeanScope build(boolean withShutdownHook) {
199199
runInjectors();
200-
return new DBeanScope(lifecycleList, beanMap, reqScopeProviders).start();
200+
return new DBeanScope(withShutdownHook, lifecycleList, beanMap, reqScopeProviders).start();
201201
}
202202
}

0 commit comments

Comments
 (0)