-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathLifecycleManager.java
159 lines (140 loc) · 4.64 KB
/
LifecycleManager.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
/*******************************************************************************
* Copyright (c) 2010-present Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
*******************************************************************************/
package org.eclipse.sisu.bean;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* {@link BeanManager} that manages JSR250 beans and schedules lifecycle events.
*/
public final class LifecycleManager
extends BeanScheduler
implements BeanManager
{
// ----------------------------------------------------------------------
// Implementation fields
// ----------------------------------------------------------------------
private final LifecycleBuilder builder = new LifecycleBuilder();
private final Map<Class<?>, BeanLifecycle> lifecycles = //
new ConcurrentHashMap<Class<?>, BeanLifecycle>( 16, 0.75f, 1 );
private final Deque<Object> stoppableBeans = new ArrayDeque<Object>();
// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
public boolean manage( final Class<?> clazz )
{
return buildLifecycle( clazz );
}
public PropertyBinding manage( final BeanProperty<?> property )
{
return null; // no custom property bindings
}
public boolean manage( final Object bean )
{
final BeanLifecycle lifecycle = lifecycleFor( bean );
if ( lifecycle.isStoppable() )
{
pushStoppable( bean );
}
if ( lifecycle.isStartable() )
{
schedule( bean );
}
return true;
}
public boolean unmanage( final Object bean )
{
if ( removeStoppable( bean ) )
{
lifecycleFor( bean ).stop( bean );
}
return true;
}
public boolean unmanage()
{
for ( Object bean; ( bean = popStoppable() ) != null; )
{
lifecycleFor( bean ).stop( bean );
}
return true;
}
// ----------------------------------------------------------------------
// Customized methods
// ----------------------------------------------------------------------
@Override
protected void activate( final Object bean )
{
lifecycleFor( bean ).start( bean );
}
// ----------------------------------------------------------------------
// Implementation methods
// ----------------------------------------------------------------------
/**
* Attempts to build a JSR250 lifecycle for the given bean type.
*
* @param clazz The bean type
* @return {@code true} if the bean defines a lifecycle; otherwise {@code false}
*/
private boolean buildLifecycle( final Class<?> clazz )
{
BeanLifecycle lifecycle = lifecycles.get( clazz );
if ( null == lifecycle )
{
lifecycle = builder.build( clazz );
lifecycles.put( clazz, lifecycle );
}
return lifecycle != BeanLifecycle.NO_OP;
}
/**
* Looks up the JSR250 lifecycle previously built for this bean.
*
* @param bean The bean instance
* @return Lifecycle for the bean
*/
private BeanLifecycle lifecycleFor( final Object bean )
{
if ( null != bean )
{
// check the class hierarchy, just in case the bean instance has been proxied/enhanced
for ( Class<?> c = bean.getClass(); null != c && c != Object.class; c = c.getSuperclass() )
{
final BeanLifecycle lifecycle = lifecycles.get( c );
if ( null != lifecycle )
{
return lifecycle;
}
}
}
return BeanLifecycle.NO_OP;
}
private void pushStoppable( final Object bean )
{
synchronized ( stoppableBeans )
{
stoppableBeans.addLast( bean );
}
}
private boolean removeStoppable( final Object bean )
{
synchronized ( stoppableBeans )
{
return stoppableBeans.remove( bean );
}
}
private Object popStoppable()
{
synchronized ( stoppableBeans )
{
return stoppableBeans.pollLast();
}
}
}