This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
SisuGuice.java
133 lines (121 loc) · 4.34 KB
/
SisuGuice.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
/*******************************************************************************
* 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.sonatype.guice.bean.containers;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Iterator;
import java.util.Map.Entry;
import javax.inject.Inject;
import org.eclipse.sisu.inject.BeanLocator;
import org.eclipse.sisu.inject.Logs;
import org.eclipse.sisu.wire.WireModule;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.util.Providers;
@Deprecated
public final class SisuGuice
{
private static final ThreadLocal<BeanLocator> LOCATOR = new InheritableThreadLocal<BeanLocator>();
private static volatile BeanLocator latest;
private SisuGuice()
{
}
@Inject
public static void setBeanLocator( final BeanLocator locator )
{
if ( null != locator )
{
LOCATOR.set( locator );
}
else
{
LOCATOR.remove();
}
latest = locator;
}
public static BeanLocator getBeanLocator()
{
final BeanLocator locator = LOCATOR.get();
return null != locator ? locator : latest;
}
public static <T> T lookup( final Key<T> key )
{
final BeanLocator locator = getBeanLocator();
if ( null != locator )
{
final Iterator<? extends Entry<?, T>> i = locator.locate( key ).iterator();
if ( i.hasNext() )
{
return i.next().getValue();
}
}
else
{
Logs.trace( "No BeanLocator found for thread {}", Thread.currentThread(), null );
}
return null;
}
public static void inject( final Object that )
{
final BeanLocator locator = getBeanLocator();
if ( null != locator )
{
Guice.createInjector( new WireModule( new Module()
{
public void configure( final Binder binder )
{
binder.bind( BeanLocator.class ).toProvider( Providers.of( locator ) );
binder.requestInjection( that );
}
} ) );
}
else
{
Logs.trace( "No BeanLocator found for thread {}", Thread.currentThread(), null );
}
}
public static Injector enhance( final Injector injector )
{
final Class<?>[] api = { Injector.class };
return (Injector) Proxy.newProxyInstance( api[0].getClassLoader(), api, new InvocationHandler()
{
@SuppressWarnings( { "rawtypes", "unchecked" } )
public Object invoke( final Object proxy, final Method method, final Object[] args )
throws Throwable
{
final String methodName = method.getName();
if ( "getInstance".equals( methodName ) )
{
final Key key = args[0] instanceof Key ? (Key) args[0] : Key.get( (Class) args[0] );
final Iterator<Entry> i = injector.getInstance( BeanLocator.class ).locate( key ).iterator();
return i.hasNext() ? i.next().getValue() : null;
}
if ( "injectMembers".equals( methodName ) )
{
Guice.createInjector( new WireModule( new Module()
{
public void configure( final Binder binder )
{
binder.bind( BeanLocator.class ).toProvider( injector.getProvider( BeanLocator.class ) );
binder.requestInjection( args[0] );
}
} ) );
return null;
}
return method.invoke( injector, args );
}
} );
}
}