-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
DelegateInvocationHandler.java
155 lines (139 loc) · 5.39 KB
/
DelegateInvocationHandler.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
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.ha;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Objects;
import org.neo4j.graphdb.TransactionFailureException;
import org.neo4j.graphdb.TransientDatabaseFailureException;
import org.neo4j.kernel.impl.util.LazySingleReference;
/**
* InvocationHandler for dynamic proxies that delegate calls to a given backing implementation. This is mostly
* used to present a single object to others, while being able to switch implementation at runtime.
*
* {@link #cement()}: acquire a proxy that will have its delegate assigned the next call to
* {@link #setDelegate(Object)}. This is useful if one {@link DelegateInvocationHandler} depends on
* another which will have its delegate set later than this one.
*/
public class DelegateInvocationHandler<T> implements InvocationHandler
{
private volatile T delegate;
// A concrete version of delegate, where a user can request to cement this delegate so that it gets concrete
// the next call to setDelegate and will never change since.
private final LazySingleReference<T> concrete;
public DelegateInvocationHandler( final Class<T> interfaceClass )
{
concrete = new LazySingleReference<T>()
{
@SuppressWarnings( "unchecked" )
@Override
protected T create()
{
return (T) Proxy.newProxyInstance( DelegateInvocationHandler.class.getClassLoader(),
new Class[] {interfaceClass}, new Concrete<>() );
}
};
}
/**
* Updates the delegate for this handler, also {@link #harden() hardens} instances
* {@link #cement() cemented} from the last call to {@link #setDelegate(Object)}.
* This call will also dereference the {@link DelegateInvocationHandler.Concrete},
* such that future calls to {@link #harden()} cannot affect any reference received
* from {@link #cement()} prior to this call.
* @param delegate the new delegate to set.
*/
public void setDelegate( T delegate )
{
this.delegate = Objects.requireNonNull( delegate, "delegate should not be null" );
harden();
concrete.invalidate();
}
/**
* Updates {@link #cement() cemented} delegates with the current delegate, making it concrete.
* Callers of {@link #cement()} in between this call and the previous call to {@link #setDelegate(Object)}
* will see the current delegate.
*/
@SuppressWarnings( "unchecked" )
void harden()
{
((Concrete<T>) Proxy.getInvocationHandler( concrete.get() )).set( delegate );
}
@Override
public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
{
if ( delegate == null )
{
throw new TransactionFailureException( "Instance state changed after this transaction started." );
}
return proxyInvoke( delegate, method, args );
}
private static Object proxyInvoke( Object delegate, Method method, Object[] args )
throws Throwable
{
try
{
return method.invoke( delegate, args );
}
catch ( InvocationTargetException e )
{
throw e.getCause();
}
}
/**
* Cements this delegate, i.e. returns an instance which will have its delegate assigned and hardened
* later on so that it never will change after that point.
*/
public T cement()
{
return concrete.get();
}
@Override
public String toString()
{
return "Delegate[" + delegate + "]";
}
private static class Concrete<T> implements InvocationHandler
{
private volatile T delegate;
void set( T delegate )
{
this.delegate = delegate;
}
@Override
public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
{
if ( delegate == null )
{
throw new TransientDatabaseFailureException(
"Instance state is not valid. There is no master currently available. Possible causes " +
"include unavailability of a majority of the cluster members or network failure " +
"that caused this instance to be partitioned away from the cluster" );
}
return proxyInvoke( delegate, method, args );
}
@Override
public String toString()
{
return "Concrete[" + delegate + "]";
}
}
}