forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request keycloak#3189 from thomasdarimont/issue/KEYCLOAK-3…
…491-revise-scripting-support KEYCLOAK-3491 Revise Scripting Support
- Loading branch information
Showing
18 changed files
with
693 additions
and
158 deletions.
There are no files selected for viewing
23 changes: 22 additions & 1 deletion
23
server-spi/src/main/java/org/keycloak/models/ScriptModel.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
64 changes: 0 additions & 64 deletions
64
server-spi/src/main/java/org/keycloak/scripting/InvocableScript.java
This file was deleted.
Oops, something went wrong.
118 changes: 118 additions & 0 deletions
118
server-spi/src/main/java/org/keycloak/scripting/InvocableScriptAdapter.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,118 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.keycloak.scripting; | ||
|
||
import org.keycloak.models.ScriptModel; | ||
|
||
import javax.script.Invocable; | ||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptException; | ||
|
||
/** | ||
* Wraps a {@link ScriptModel} and makes it {@link Invocable}. | ||
* | ||
* @author <a href="mailto:thomas.darimont@gmail.com">Thomas Darimont</a> | ||
*/ | ||
public class InvocableScriptAdapter implements Invocable { | ||
|
||
/** | ||
* Holds the {@ScriptModel} | ||
*/ | ||
private final ScriptModel scriptModel; | ||
|
||
/** | ||
* Holds the {@link ScriptEngine} instance initialized with the script code. | ||
*/ | ||
private final ScriptEngine scriptEngine; | ||
|
||
/** | ||
* Creates a new {@link InvocableScriptAdapter} instance. | ||
* | ||
* @param scriptModel must not be {@literal null} | ||
* @param scriptEngine must not be {@literal null} | ||
*/ | ||
public InvocableScriptAdapter(ScriptModel scriptModel, ScriptEngine scriptEngine) { | ||
|
||
if (scriptModel == null) { | ||
throw new IllegalArgumentException("scriptModel must not be null"); | ||
} | ||
|
||
if (scriptEngine == null) { | ||
throw new IllegalArgumentException("scriptEngine must not be null"); | ||
} | ||
|
||
this.scriptModel = scriptModel; | ||
this.scriptEngine = loadScriptIntoEngine(scriptModel, scriptEngine); | ||
} | ||
|
||
@Override | ||
public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptExecutionException { | ||
|
||
try { | ||
return getInvocableEngine().invokeMethod(thiz, name, args); | ||
} catch (ScriptException | NoSuchMethodException e) { | ||
throw new ScriptExecutionException(scriptModel, e); | ||
} | ||
} | ||
|
||
@Override | ||
public Object invokeFunction(String name, Object... args) throws ScriptExecutionException { | ||
try { | ||
return getInvocableEngine().invokeFunction(name, args); | ||
} catch (ScriptException | NoSuchMethodException e) { | ||
throw new ScriptExecutionException(scriptModel, e); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T getInterface(Class<T> clazz) { | ||
return getInvocableEngine().getInterface(clazz); | ||
} | ||
|
||
@Override | ||
public <T> T getInterface(Object thiz, Class<T> clazz) { | ||
return getInvocableEngine().getInterface(thiz, clazz); | ||
} | ||
|
||
/** | ||
* Returns {@literal true} if the {@link ScriptEngine} has a definition with the given {@code name}. | ||
* | ||
* @param name | ||
* @return | ||
*/ | ||
public boolean isDefined(String name) { | ||
|
||
Object candidate = scriptEngine.getContext().getAttribute(name); | ||
|
||
return candidate != null; | ||
} | ||
|
||
private ScriptEngine loadScriptIntoEngine(ScriptModel script, ScriptEngine engine) { | ||
|
||
try { | ||
engine.eval(script.getCode()); | ||
} catch (ScriptException se) { | ||
throw new ScriptExecutionException(script, se); | ||
} | ||
|
||
return engine; | ||
} | ||
|
||
private Invocable getInvocableEngine() { | ||
return (Invocable) scriptEngine; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
server-spi/src/main/java/org/keycloak/scripting/Script.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
20 changes: 18 additions & 2 deletions
20
server-spi/src/main/java/org/keycloak/scripting/ScriptBindingsConfigurer.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
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
Oops, something went wrong.