Apache OFBiz minilang source code

Source: Internet
Author: User
Tags protected constructor tagname
Parent class of all elements in minilang -- minilangelement

Minilang is a descriptive language based on XML ". All elements, including nodes and attributes, are inherited from this class. It contains three attributes:


  • Linenumber: indicates the line number in which the minilang source code (usually Java) is parsed, mainly to facilitate Logging
  • Tagname: The Tag Name of the current element. It is mainly used for logging.
  • Simplemethod: simplemethod is a large "transfer object", which implements all the execution methods supported by minilang. Its function is similar to servicedispatcher in serviceengine.
This class does not have much functional Code except outputtracemessage. It is used to record tracing log messages. The two attributes mentioned above (linenumber/tagname) are mainly used here.
Corresponding to the implementation of the service engine -- simpleserviceengine in the previous analysis on ofbizservice engine, we talked about a execution engine related to minilang, which is actually the simpleserviceengine here. It inherits from genericasyncengine (the semantics inherited here is not a-a relationship, mainly code reuse, because genericasyncengine implements runasync, other sub-engines do not need to implement this interface ).
Inheritance relationship diagram of simpleserviceengine:

Simpleserviceengine implements two abstract run methods in the parent class:


These two methods do not contain the main implementation logic. The real logic is defined in the private method: serviceinvoker, which is finally executed through the static runsimpleservice method of simplemethod.
Beanscript engine implementation-simplemethodbsfengine is an adapter for the IBM beanscripting library. It is used to call beanscript in minilang. It implements the following methods:

Here, apply and call are not implemented (throwing an exception). It maintains a context object internally to store bsfdeclaredbean, and the context object is used to provide another context for simplemethod execution. The following two methods are used to maintain the context object:
  • Declarebean
  • Undeclarebean
The main logic of method execution is implemented in eval. It finds the simplemethod object through the method parameters, constructs the context object for execution, and calls the exec execution of the simplemethod object.
Simplemethodsimplemethod, the real performer of minilang, inherits from minilangelement, indicating that it is also an element in minilang. Simplemethod contains a lot of code related to task execution:

Of course, most of these methods deal with the overload of minilang applications. We can see that they can be applied to simple methods, events, and services. The real execution method is the exec method.
Of course, there are still some static help methods. For example, readoperations returns the set of methodoperation instances represented by all the child elements of the current element, is interpreted and executed by Java ).
Verification checker -- minilangvalidate because minilang uses XML to describe the "language" of the task, it does not have many compilation check mechanisms in programming languages. Therefore, it uses some auxiliary methods to check: this includes verifying attributes, verifying subelements, testing elements are empty, setting default values if null, testing a property is a constant value, strict mode or loose mode, and error handling.
Minilang's help class-minilangutil is a minilang help class. It provides methods such as calling simplemethod, type conversion/retrieval, and setting minilang content.
Field Verification and converter -- simplemapprocessorsimplemapprocessor obtains the field information from the context object, and then verifies and converts it (of course, the real verification and conversion logic is not implemented by him, it is only used to trigger the action), and then put the verified fields into the target set. Minilang will define many mapprocessor like this, while simplemapprocessor is mainly used to obtain all these mapprocessor (internal acquisition, not public) and then run them:

Simplemapprocessor supports two types of cache for all mapprocessors. One is the key cache based on the string xmlresource, and the other is the key cache based on the urlxmlurl. All runsimplemapprocessor first obtains the specific mapprocessor in the mapprocessor set, and then calls its exec method to perform verification and conversion operations (which will be introduced later ).
The basic class of the operation description -- simplemapoperationminilang provides many operations, and all these operations are inherited from the abstract class simplemapoperation:


The simplemapoperation constructor receives two parameters:
  • Element: an XML Element
  • Simplemapprocess: simplemapprocess class instance
Some parameters, such as failmessage and failproperty, are also initialized in the constructor.
In addition, addmessage is provided, which is used in combination with failmessage and failproperty to provide more comprehensive error information. Another important method, exec, is marked as abstract to implement the subclass.
String Parameter Synthesis Processor-simplemapprocess is the synthesis processor of the parameters in minilang. It contains a readoperations method used to traverse all the subnodes of the current node, match the node names of these subnodes, and generate various simplemapoperation instances, then add it to the set.
This class also provides an exec method, which traverses all the processors and has to run their own exec methods on each of them one by one to complete corresponding processing.
Base class that provides comparative operations-many operations in basecompareminilang are comparative operations, while basecompare is the abstract base class that executes comparative operations. It is obvious that it inherits from simplemapoperation.
In the constructor, it extracts three attributes from the parameter element:
  • Operator: comparison operator
  • Type: the type of the object to be compared.
  • Format: Format
There are also three methods:

  • Dorealcompare is a static method used for comparison. It depends on other implementations of OFBiz and returns the comparison result of the bool value.
  • Docompare calls dorealcompare to obtain the comparison result. If the result is false, the addmessage method of simplemapoperation is called to add the failure information.
  • Exec: NULL implementation. This is semantic-compliant because it is only compareoperation.
Value comparator -- compare provides the ability to compare input parameters. In the constructor, it extracts the Value Attribute of the element and serves as one of the objects to be compared.
This class implements the exec method as follows:
Obtain the value of fieldname from the inmap (set of all input parameters required for minilang execution) of the method parameter (fieldname is extracted from simplemapprocess currently processing elements) as another comparison object, and then call the basecompare docompare for comparison.
Field comparator -- comparefield the value of a parameter provided by compare is compared with the value of an element defined by minilang. The value of a parameter provided by comparefield is compared with the parameter value of a field defined by minilang. This is a bit difficult to understand:
Compare:
public Compare(Element element, SimpleMapProcess simpleMapProcess) {        super(element, simpleMapProcess);        this.value = element.getAttribute("value");    }    @Override    public void exec(Map<String, Object> inMap, Map<String, Object> results, List<Object> messages, Locale locale, ClassLoader loader) {        Object fieldValue = inMap.get(fieldName);        doCompare(fieldValue, value, messages, locale, loader, true);    }

Comparefield:
public CompareField(Element element, SimpleMapProcess simpleMapProcess) {        super(element, simpleMapProcess);        this.compareName = element.getAttribute("field");    }    @Override    public void exec(Map<String, Object> inMap, Map<String, Object> results, List<Object> messages, Locale locale, ClassLoader loader) {        Object compareValue = inMap.get(compareName);        Object fieldValue = inMap.get(fieldName);        doCompare(fieldValue, compareValue, messages, locale, loader, false);    }

Minilang's copy operation-copy class allows you to copy a parameter from the input parameter list to the output parameter list. In the constructor, obtain the target key to be copied to the element, whether to replace the key, and if it is null, SET related configurations. The exec method mainly implements the copy action.
Non-empty field detection-notempty this class is used to detect whether the current field is not empty (null or empty string), if it is empty, set the error message. Example:
<not-empty>            <fail-property resource="AccountingUiLabels" property="AccountingCardNumberMissing"/>        </not-empty>

Minilang type converter-convertconvert class is used to convert a parameter in the input parameter set and put it into the output parameter set. In the constructor, it first obtains the "to-field" attribute of the element to be processed, and then obtains the type to be converted. In exec, get the object to be converted, convert it, and put it into the output parameter set
Regexpregexp, the regular expression validators of minilang, is used for regular expression verification. It obtains the expr attribute from the processing element and converts the current field to string in the exec method, then, perform regular matching on it.
Validatemethodminilang, The minilang verification method caller, supports the verification of the specified verification method called by the string, as shown in:
<validate-method method="isAnyCard" class="org.ofbiz.base.util.UtilValidate">            <fail-property resource="AccountingUiLabels" property="AccountingCardNumberIncorrect"/>        </validate-method>

In the constructor, it first obtains the method attribute and Class Attribute of the element. The exec method obtains the value of this field, converts it to the string type, obtains the classloader of the current thread, loads the class, and obtains this method (it is worth mentioning that, only static methods are supported here ). This is because the instance of this class is not passed in when this method is called Using Reflection.
The string input parameter generator makepolicingoperationminilang allows you to generate a specific string value based on the configuration as an input parameter. Here there are several different types of string generation operations involved:

Makepolicingoperation is an abstract base class, which only defines an abstract method: exec.
  • Propertyparameters: property-based generation Parameters
  • Infieldoper: Generate parameters based on the input parameter list (inmap)
  • Constantoper: APPEND "String constant" to generate Parameters
The container of the string generator-makeinstringmakeinstring is the container of the makepolicingoperation above. In the constructor, it obtains the field attribute of the current element and serves as the key of a parameter. Obtain all the child elements of the current element, and judge whether the node name generates different makepolicingoperation and puts it into a collection.
In the exec method, traverse all makepolicingoperation, execute their exec methods respectively, and obtain the results they return.
The real map Processor -- mapprocessor previously mentioned that simplemapprocessor is actually a total processor, and the real processor is mapprocessor. The so-called processing includes two aspects:
  • Makeings: corresponds to the "make-in-string" subnode in minilang.
  • Simplemapprocess: corresponds to the "process" subnode in minilang.
Example:
<simple-map-processor name="newAffilPostalPurpose">    <make-in-string field="contactMechPurposeTypeId"><constant>BILLING_LOCATION</constant></make-in-string>    <process field="contactMechId"><copy/><not-empty>    <fail-property resource="PartyUiLabels" property="PartyContactMechIdMissing"/></not-empty></process>    <process field="contactMechPurposeTypeId"><copy/></process>  </simple-map-processor>

Yes, we have been talking about implementation classes related to these two aspects before.
Mapprocessor uses two sets to place the above two processors. In the constructor, it first traverses the two subnodes mentioned above to initialize these two sets.
Unexpectedly, exec will traverse the instances of the Two processors to process the parameters in the map.
The base class of the element entity-methodoperation we entered the legal "Syntax" section recognized by minilang. They are much more. Let's take a look at the integration relationship diagram of the package:

Methodoperation is the abstract base class of all classes in these packages. Methodoperation inherits from minilangelement. It has a protected constructor and requires two parameters:
  • Element: the current element.
  • Simplemethod: simplemethod class instance
It is mainly used to initialize minilangelement.
It defines an abstract method exec, which receives a context object. Returns a bool value to indicate whether the script execution needs to continue.
This class defines a factory interface for creating methodoperation. It mainly contains the interface method for creating methodoperation.
It is worth mentioning that the methodoperation class also defines a deprecatedoperation annotation to mark obsolete methodoperation.
Entity-related entities in minilang are undoubtedly represented by delegator for all entity-related operations. This is described in previous entityengine-related articles. To operate entity in minilang, You need to translate those syntaxes into the Java code that delegator operates on them. These are encapsulated in entityopspackage. Each implementation class contains a static internal class that implements the factory interface defined in methodoperation to create a specific factory.
In addition, the tostring method of each implementation class restores the string expression of the current node in minilang.
Judgment entity related to the IF judgment condition in minilang
  • Ifnotempty: corresponds to the IF-not-empty node in minilang, used to determine whether the current field is empty. In the constructor, it will parse the operations of all its subnodes, and if there is an else element node, it will parse all the subnode operations under the else element at the same time. In the exec method, first determine if all operations under if are not empty. Otherwise, if the operations under the else node are not empty, execute these operations.
  • Ifinstanceof: determines whether an object is an instance of a class. The processing process is similar to ifnotempty.
  • Checkpermission: Check permissions. in the previous article, we briefly talked about the permission Design of OFBiz, which has multiple levels of permission check. Because minilang can implement service, ECA, and Seca in OFBiz, the check syntax for permissions must also be embedded. This class defines an internal entity class: permissioninfo. It is used to extract permission and action from the checkpermission node. The haspermission method is defined to determine whether the user has the entity operation permission or whether the user has passed the authentication authorization. Call haspermission In the exec method of checkpermission for judgment.
Condition-related judgment entities in minilang
  • Conditional: interfaces inherited by all condition entities. This interface contains two methods.
    • Checkcondition: Check condition, return bool value result
    • Prettyprint: print. It is mainly used to provide complete error information when executing the assert command.
  • Conditionalfactory: A factory used to create condition entities. This is required partly because minilang can be extended to support additional condition elements (by inheriting the class and then providing custom implementations ). This class is an abstract generic class. In the static constructor, it obtains and caches all the configured conditionalfactory. This class contains a static method and two abstract methods:
    • Makeconditional: obtains a factory from the cache and creates an instance with a certain condition.
    • Createcondition: creation method to be implemented by all Condition
    • Getname: Method for returning the name of a condition to be implemented by all conditions
    • Therefore, each of the following condition implementation classes will create a static internal factory class and inherit the conditionalfactory and the factory (methodoperation semantics) mentioned earlier ).
  • Compare: an abstract class (can also be understood as a help class) that provides comparison of various conditions. Contains a docompare method, which is implemented by multiple internal static classes.
  • Combinedcondition: this class is an abstract class used to judge multiple conditions connected by and/or/not/XOR. It does not inherit methodoperation (it does not implement the previous exec method, so it is not a simple operation), but inherits from minilangelement, but implements the conditional interface at the same time (in general, conditional provides "interfaces" that are narrower than methodoperation, but conditional is only responsible for condition-related comparison operations ). It has a subconditions set, which is initialized in the constructor and stores the condition corresponding to the subnode of the current node. This class does not differentiate specific logical operators, but provides implementations based on different logical operators for conditionalfactory (Multiple Static internal classes ). In each different factory, combinedcondition is instantiated based on its own semantics.
  • Masterif/elseif: These two classes are used together to implement the conditional judgment logic for if/else.
Operating entities related to environment check/setting in minilang
The ventops package contains some events supported by minilang, such as mutual conversion between fields and request parameters, and conversion between fields and session parameters. In the exec method that must be implemented, you must first determine that the current methodtype must be an event.
Call-related operation entities in minilang
Minilang supports calling other components, such as service, bsh, script, and classmethod. These are implemented in the callops package, although there is no association between them. But they all inherit from methodoperation. In addition, each class implements a static internal factory.
Other operating entities in minilang
The otherops package contains several non-functional operations related to computing, tracking, and logging.
Method-related parameter base class -- methodobject
Methodobject is an abstract class that provides the abstraction for passing parameters in multiple minilang. It contains three Abstract METHODS:
  • Get object Value: GetObject
  • Get the Class Object of the type: gettypeclass
  • Get the string representation of the type name: gettypename
And two implementation classes:
  • Fieldobject: Common parameter type of the abstract class
  • Stringobject: abstracts the string type representation.
The Common Object -- fieldobjectminilang of a field contains many field attributes. The model class corresponding to the fields in the program is fieldobject. A key attribute of this class is type. As for methods, the most critical method is GetObject, which obtains its value from the message context object (methodcontext.
String Representation class -- stringobject the instance of this object represents a javastring object, used for passing to minilang for method calling. It has two attributes:
  • The text value of the current XML element, which is located between open/closed labels of XML
  • Value of the "value" attribute of the current XML Element
The key method is GetObject, which Concatenates the value of the value attribute with the text value and returns it as a whole string.
Messageelementminilang, the expression class of the Message Element, can be configured with fail-message for methods. It is used to provide related failure information when an exception occurs in method calls. This class is used to parse and encapsulate this object.
The context object of a method call -- methodcontextmethodcontext is a large object that contains all the object information required for method execution in minilang. Key objects:
  • Delegator: the instance of the delegator, used to represent all data access in OFBiz.
  • Dispatcher: the instance of localdispatcher, used to access all services in OFBiz.
  • Request: the instance of httpservletrequest, indicating the request object of the client.
  • Response: The instance of httpservletresponse, which indicates the response object of the client request.
  • Userlogin: genericvalue instance, which encapsulates user Identity Authentication
In addition, some get/put methods are provided.

Apache OFBiz minilang source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.