-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1477e42
commit 923b3e9
Showing
5 changed files
with
94 additions
and
11 deletions.
There are no files selected for viewing
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
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,62 @@ | ||
/* | ||
Copyright 2012-2024 Michael Pozhidaev <[email protected]> | ||
This file is part of LUWRAIN. | ||
LUWRAIN is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU General Public | ||
License as published by the Free Software Foundation; either | ||
version 3 of the License, or (at your option) any later version. | ||
LUWRAIN 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 | ||
General Public License for more details. | ||
*/ | ||
|
||
package org.luwrain.script.core; | ||
|
||
import java.util.*; | ||
|
||
import org.graalvm.polyglot.*; | ||
import org.graalvm.polyglot.proxy.*; | ||
|
||
import org.luwrain.core.*; | ||
import org.luwrain.popups.*; | ||
|
||
import static org.luwrain.core.NullCheck.*; | ||
import static org.luwrain.script.ScriptUtils.*; | ||
|
||
public class PropertiesObj | ||
{ | ||
protected Properties properties; | ||
|
||
public PropertiesObj(Properties props) | ||
{ | ||
notNull(properties, "properties"); | ||
this.properties = properties; | ||
} | ||
|
||
@HostAccess.Export | ||
public final ProxyExecutable getProperty = this::getPropertyImpl; | ||
private Object getPropertyImpl(Value[] args) | ||
{ | ||
if (!notNullAndLen(args, 1) || !args[0].isString() || args[0].asString().trim().isEmpty()) | ||
throw new IllegalArgumentException("getProperty() takes exactly one non-empty string argument"); | ||
return properties.getProperty(args[0].asString().trim()); | ||
} | ||
|
||
@HostAccess.Export | ||
public final ProxyExecutable setProperty = this::setPropertyImpl; | ||
private Object setPropertyImpl(Value[] args) | ||
{ | ||
if (args == null || args.length != 2) | ||
throw new IllegalArgumentException("setProperty() takes two argument"); | ||
if (args[0] == null || args[0].isNull() || ! args[0].asString().trim().isEmpty()) | ||
throw new IllegalArgumentException("setProperty() takes a non-empty string as the first argument"); | ||
if (args[1] != null && !args[1].isNull() && !args[1].isString()) | ||
throw new IllegalArgumentException("setProperty() takes null or a string as the second argument"); | ||
properties.setProperty(args[0].asString().trim(), (args[1] == null || args[1].isNull())?null:args[1].asString()); | ||
return this; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
Copyright 2012-2022 Michael Pozhidaev <[email protected]> | ||
Copyright 2012-2024 Michael Pozhidaev <[email protected]> | ||
This file is part of LUWRAIN. | ||
|
@@ -17,24 +17,28 @@ | |
package org.luwrain.script.hooks; | ||
|
||
import java.util.concurrent.atomic.*; | ||
import org.apache.logging.log4j.*; | ||
|
||
import org.luwrain.core.*; | ||
import org.luwrain.script.*; | ||
|
||
import static org.luwrain.core.NullCheck.*; | ||
|
||
public class ProviderHook | ||
{ | ||
static protected final Logger log = LogManager.getLogger(); | ||
protected final HookContainer hookContainer; | ||
|
||
public ProviderHook(HookContainer hookContainer) | ||
{ | ||
NullCheck.notNull(hookContainer, "hookContainer"); | ||
notNull(hookContainer, "hookContainer"); | ||
this.hookContainer = hookContainer; | ||
} | ||
|
||
public Object run(String hookName, Object[] args) | ||
{ | ||
NullCheck.notEmpty(hookName, "hookName"); | ||
NullCheck.notNullItems(args, "args"); | ||
notEmpty(hookName, "hookName"); | ||
notNullItems(args, "args"); | ||
final AtomicReference<Object> res = new AtomicReference<>(); | ||
hookContainer.runHooks(hookName, (hook)->{ | ||
try { | ||
|
@@ -44,16 +48,21 @@ public Object run(String hookName, Object[] args) | |
res.set(obj); | ||
return Luwrain.HookResult.BREAK; | ||
} | ||
catch(RuntimeException e) | ||
catch(Throwable ex) | ||
{ | ||
res.set(e); | ||
log.catching(ex); | ||
res.set(ex); | ||
return Luwrain.HookResult.BREAK; | ||
} | ||
}); | ||
if (res.get() == null) | ||
return null; | ||
if (res.get() instanceof RuntimeException) | ||
throw (RuntimeException)res.get(); | ||
if (res.get() instanceof Throwable t) | ||
{ | ||
if (res.get() instanceof RuntimeException r) | ||
throw r; | ||
throw new RuntimeException(t); | ||
} | ||
return res.get(); | ||
} | ||
} |