forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ExecutionContext and show example with ActionPluginProxy
Signed-off-by: Craig Perkins <[email protected]>
- Loading branch information
Showing
4 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
server/src/main/java/org/opensearch/common/util/concurrent/ExecutionContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.common.util.concurrent; | ||
|
||
public class ExecutionContext { | ||
private final ThreadLocal<String> context = new ThreadLocal<>(); | ||
|
||
public void set(String value) { | ||
context.set(value); | ||
} | ||
|
||
public String get() { | ||
return context.get(); | ||
} | ||
|
||
public void clear() { | ||
context.remove(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
server/src/main/java/org/opensearch/plugins/ActionPluginProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugins; | ||
|
||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
|
||
public class ActionPluginProxy implements InvocationHandler { | ||
private final ActionPlugin actionPlugin; | ||
private final ThreadPool threadPool; | ||
|
||
public static ActionPlugin newInstance(ActionPlugin obj, ThreadPool threadPool) { | ||
return (ActionPlugin) Proxy.newProxyInstance( | ||
obj.getClass().getClassLoader(), | ||
new Class<?>[] { ActionPlugin.class }, | ||
new ActionPluginProxy(obj, threadPool) | ||
); | ||
} | ||
|
||
private ActionPluginProxy(ActionPlugin actionPlugin, ThreadPool threadPool) { | ||
this.actionPlugin = actionPlugin; | ||
this.threadPool = threadPool; | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { | ||
Object result; | ||
try { | ||
threadPool.getThreadContext().setExecutionContext(((Plugin) actionPlugin).getClass().getName()); | ||
result = m.invoke(actionPlugin, args); | ||
threadPool.getThreadContext().clearExecutionContext(); | ||
} catch (InvocationTargetException e) { | ||
throw e.getTargetException(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("unexpected invocation exception: " + e.getMessage()); | ||
} | ||
return result; | ||
} | ||
} |