Skip to content

Commit

Permalink
Adding script.core.PropertiesObj
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed May 6, 2024
1 parent 1477e42 commit 923b3e9
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/luwrain/app/console/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
final class Entry
{
final String message;
final Throwable ex;
Entry(LogEvent event)
{
this.message = event.getMessage().getFormattedMessage();
this.ex = event.getMessage().getThrowable();
}
}
9 changes: 7 additions & 2 deletions src/main/java/org/luwrain/app/console/MainLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ final class MainLayout extends LayoutBase implements ConsoleArea.InputHandler
return super.onAreaQuery(query);
}
};
setAreaLayout(consoleArea, null);
}

@Override public Result onConsoleInput(ConsoleArea area, String text)
Expand All @@ -92,11 +93,15 @@ private final class Appearance implements ConsoleArea.Appearance<Entry>
{
@Override public void announceItem(Entry entry)
{
if (entry.ex != null)
app.setEventResponse(text(Sounds.ATTENTION, app.getLuwrain().getSpeakableText(entry.message + ": " + entry.ex.getMessage() + " (" + entry.ex.getClass().getSimpleName() + ")", Luwrain.SpeakableTextType.PROGRAMMING))); else
app.setEventResponse(text(Sounds.LIST_ITEM, app.getLuwrain().getSpeakableText(entry.message, Luwrain.SpeakableTextType.PROGRAMMING)));
}
@Override public String getTextAppearance(Entry entry)
@Override public String getTextAppearance(Entry entry)
{
return entry.message;
if (entry.ex != null)
return entry.message + ": " + entry.ex.getMessage() + " (" + entry.ex.getClass().getSimpleName() + ")"; else
return entry.message;
}
}
}
7 changes: 6 additions & 1 deletion src/main/java/org/luwrain/script/Hooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.luwrain.script;

import org.apache.logging.log4j.*;

import org.luwrain.core.*;
import org.luwrain.script.hooks.ChainOfResponsibilityHook;
import org.luwrain.script.hooks.PermissionHook;
Expand All @@ -28,6 +30,8 @@

public final class Hooks
{
static private final Logger log = LogManager.getLogger();

static public final String
ANNOUNCEMENT = "luwrain.announcement",
AREA_CLEAR = "luwrain.area.clear",
Expand Down Expand Up @@ -66,8 +70,9 @@ static public boolean chainOfResponsibilityNoExc(HookContainer container, String
try {
return new ChainOfResponsibilityHook(container).run(hookName, args);
}
catch(Throwable e)
catch(Throwable ex)
{
log.error("The " + hookName + " thrown an exception", ex);
return false;
}
}
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/org/luwrain/script/core/PropertiesObj.java
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;
}
}
25 changes: 17 additions & 8 deletions src/main/java/org/luwrain/script/hooks/ProviderHook.java
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.
Expand All @@ -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 {
Expand All @@ -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();
}
}

0 comments on commit 923b3e9

Please sign in to comment.