Package io.permazen.parse
Class ParseSession
- java.lang.Object
-
- io.permazen.Session
-
- io.permazen.parse.ParseSession
-
- Direct Known Subclasses:
CliSession
public class ParseSession extends Session
ASession
with support for parsing Java expressions.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ParseSession.Action
Callback interface used byParseSession.performParseSessionAction()
andParseSession.performParseSessionActionWithCurrentTransaction()
.-
Nested classes/interfaces inherited from class io.permazen.Session
Session.HasTransactionOptions, Session.RetryableAction, Session.TransactionalAction
-
-
Field Summary
Fields Modifier and Type Field Description static String
PARSE_FUNCTIONS_DESCRIPTOR_RESOURCE
Classpath XML file resource describing availableFunction
s: "META-INF/permazen/parse-function-implementations.xml".-
Fields inherited from class io.permazen.Session
DEFAULT_INITIAL_RETRY_DELAY, DEFAULT_MAX_RETRIES, DEFAULT_MAXIMUM_RETRY_DELAY, log
-
-
Constructor Summary
Constructors Constructor Description ParseSession(Database db)
Constructor forSessionMode.CORE_API
mode.ParseSession(KVDatabase kvdb)
Constructor forSessionMode.KEY_VALUE
mode.ParseSession(Permazen jdb)
Constructor forSessionMode.PERMAZEN
mode.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SortedMap<String,Function>
getFunctions()
Get theFunction
s registered with this instance.Parser<? extends Node>
getIdentifierParser()
Get the current standalone identifier parser.Set<String>
getImports()
Get currently configured Java imports.SortedMap<String,Value>
getVars()
Get all variables set on this instance.void
loadFunctionsFromClasspath()
Scan the classpath forFunction
s and register them.boolean
performParseSessionAction(ParseSession.Action action)
Perform the given action in the context of this session.boolean
performParseSessionActionWithCurrentTransaction(ParseSession.Action action)
Associate the currentJTransaction
with this instance, if not already associated, while performing the given action.void
registerFunction(Function function)
Register the givenFunction
.String
relativizeClassName(Class<?> klass)
Relativize the given class's name, so that it is as short as possible given the configured imports.Class<?>
resolveClass(String name, boolean allowPrimitive)
Resolve a class name against this instance's currently configured class imports.void
setIdentifierParser(Parser<? extends Node> identifierParser)
Set the standalone identifier parser.-
Methods inherited from class io.permazen.Session
getDatabase, getDatabaseDescription, getInitialRetryDelay, getJTransaction, getKVDatabase, getKVTransaction, getMaximumRetryDelay, getMaxRetries, getMode, getNameIndex, getPermazen, getSchemaModel, getSchemaVersion, getTransaction, getValidationMode, isAllowNewSchema, isReadOnly, isTransactionOpen, performSessionAction, performSessionActionWithCurrentTransaction, reportException, setAllowNewSchema, setDatabaseDescription, setInitialRetryDelay, setMaximumRetryDelay, setMaxRetries, setMode, setReadOnly, setRollbackOnly, setSchemaModel, setSchemaVersion, setValidationMode
-
-
-
-
Field Detail
-
PARSE_FUNCTIONS_DESCRIPTOR_RESOURCE
public static final String PARSE_FUNCTIONS_DESCRIPTOR_RESOURCE
Classpath XML file resource describing availableFunction
s: "META-INF/permazen/parse-function-implementations.xml".Example:
<parse-function-implementations> <parse-function-implementation class="com.example.MyFunction"/> </parse-function-implementations>
Instances must have a public constructor taking either zero parameters or one
ParseSession
parameter.
-
-
Constructor Detail
-
ParseSession
public ParseSession(KVDatabase kvdb)
Constructor forSessionMode.KEY_VALUE
mode.- Parameters:
kvdb
- key/value database- Throws:
IllegalArgumentException
- ifkvdb
is null
-
ParseSession
public ParseSession(Database db)
Constructor forSessionMode.CORE_API
mode.- Parameters:
db
- core database- Throws:
IllegalArgumentException
- ifdb
is null
-
ParseSession
public ParseSession(Permazen jdb)
Constructor forSessionMode.PERMAZEN
mode.- Parameters:
jdb
- database- Throws:
IllegalArgumentException
- ifjdb
is null
-
-
Method Detail
-
getImports
public Set<String> getImports()
Get currently configured Java imports.Each entry should of the form
foo.bar.Name
orfoo.bar.*
.- Returns:
- configured imports
-
getFunctions
public SortedMap<String,Function> getFunctions()
Get theFunction
s registered with this instance.- Returns:
- registered functions indexed by name
-
getVars
public SortedMap<String,Value> getVars()
Get all variables set on this instance.- Returns:
- variables indexed by name
-
loadFunctionsFromClasspath
public void loadFunctionsFromClasspath()
Scan the classpath forFunction
s and register them.- See Also:
PARSE_FUNCTIONS_DESCRIPTOR_RESOURCE
-
registerFunction
public void registerFunction(Function function)
- Parameters:
function
- new function- Throws:
IllegalArgumentException
- iffunction
is null
-
getIdentifierParser
public Parser<? extends Node> getIdentifierParser()
Get the current standalone identifier parser.- Returns:
- current identifier parser, if any, otherwise null
-
setIdentifierParser
public void setIdentifierParser(Parser<? extends Node> identifierParser)
Set the standalone identifier parser.The configured identifier parser, if any, is invoked on standalone identifiers. This allows for configurable behavior with respect to resolving such identifiers.
Typically when installing a new parser, the previous parser (if any) is set as its delegate.
- Parameters:
identifierParser
- parser for standalone identifiers, or null for none
-
resolveClass
public Class<?> resolveClass(String name, boolean allowPrimitive)
Resolve a class name against this instance's currently configured class imports.- Parameters:
name
- class name as it would appear in the Java languageallowPrimitive
- whether to allow primitive types likeint
- Returns:
- resolved class
- Throws:
IllegalArgumentException
- ifname
cannot be resolved
-
relativizeClassName
public String relativizeClassName(Class<?> klass)
Relativize the given class's name, so that it is as short as possible given the configured imports. For example, for classString
this will returnString
, but for classArrayList
this will returnjava.util.ArrayList
unlessjava.util.*
has been imported.- Parameters:
klass
- class whose name to relativize- Returns:
- relativized class name
- Throws:
IllegalArgumentException
- ifklass
is null
-
performParseSessionAction
public boolean performParseSessionAction(ParseSession.Action action)
Perform the given action in the context of this session.This is a
ParseSession
-specific overload ofSession.performSessionAction()
; see that method for details.- Parameters:
action
- action to perform, possibly within a transaction- Returns:
- true if
action
completed successfully, false if a transaction could not be created oraction
threw an exception - Throws:
IllegalArgumentException
- ifaction
is null
-
performParseSessionActionWithCurrentTransaction
public boolean performParseSessionActionWithCurrentTransaction(ParseSession.Action action)
Associate the currentJTransaction
with this instance, if not already associated, while performing the given action.This is a
ParseSession
-specific overload ofSession.performSessionActionWithCurrentTransaction()
; see that method for details.- Parameters:
action
- action to perform- Returns:
- true if
action
completed successfully, false ifaction
threw an exception - Throws:
IllegalStateException
- if there is a different open transaction already associated with this instanceIllegalStateException
- if this instance is not in modeSessionMode.PERMAZEN
IllegalArgumentException
- ifaction
is null
-
-