Project

General

Profile

Smart props - DONE » History » Version 10

Henning Blohm, 05.03.2017 22:14

1 10 Henning Blohm
h1. Smart Props (#1697)
2 1 Henning Blohm
3 7 Henning Blohm
The goal of this concept is to provide facilities to simplify sharing of component configuration, making use of system and environment variables while still being able to query resolved component configuration efficiently and with minimal resource usage.
4 1 Henning Blohm
5 7 Henning Blohm
h2. Latest Design
6
7
In order to maximize compatibility and flexibility the following design has been chosen:
8
9
Component properties can be marked with a processing style. To do so, the property name is qualified with an appended, colon separated style descriptor. For example,
10
11
<pre>
12
someProp\:JEXL3="This is a JEXL3 string"
13
</pre>
14
15 8 Henning Blohm
marks the property "someProp" as being of style <em>JEXL3</em>. 
16 1 Henning Blohm
17 8 Henning Blohm
<b>Note #1:</b> Colons need to be escaped using a backslash to be part of the name.
18
19
Running into an expression like this, z2 will hand it off to an IComponentDescriptorProcessor for evaluation. In this particular case it would be the included JEXL3 processor.
20
21
<b>Note #2:</b> There is still a uniqueness requirement. The same property name ("someProp" in this case) may not be used with more than one style qualifier.
22
23
h3. The plain style
24
25
There is a defaulting style "plain". That is, declaring a property 
26
27
<pre>x=...</pre>
28
29
is equivalent to 
30
31
<pre>
32
x\:plain=...
33
</pre>
34
35
h3. The JEXL3 style
36
37
A built-in processor is based on the "Apache Commons JEXL library":http://commons.apache.org/proper/commons-jexl.
38
39
JEXL offers a simple and versatile expression language. See "here":http://commons.apache.org/proper/commons-jexl/reference/syntax.html for a syntax reference. 
40
41
The JEXL3 style offers some implicit variables so that you can easily access system properties, environment variables, and other components. 
42
43
Example expressions:
44
45
Set path based on user's home folder:
46
<pre>
47
importPath\:JEXL3=system['user.home']+"/import"
48
</pre>
49
50
Copy a database configuration:
51
<pre>
52
ds.prop.user\:JEXL3=components.getComponent("environment/mainDB").getProperty("ds.prop.password")
53
ds.prop.password\:JEXL3=components.getComponent("environment/mainDB").getProperty("ds.prop.user")
54
ds.prop.url\:JEXL3=components.getComponent("environment/mainDB").getProperty("ds.prop.url")
55
</pre>
56
57
Copy a database configuration:
58
<pre>
59
ds.prop.user\:JEXL3=components.getComponent("environment/mainDB").getProperty("ds.prop.password")
60
ds.prop.password\:JEXL3=components.getComponent("environment/mainDB").getProperty("ds.prop.user")
61
ds.prop.url\:JEXL3=components.getComponent("environment/mainDB").getProperty("ds.prop.url")
62
</pre>
63
64
String with expressions:
65
<pre>
66
message\:JEXL3=`Sorry. We do not serve users called "${system['user.name']}" at ${env.JAVA_HOME}`
67
</pre>
68
69
Conditional expressions:
70
<pre>
71 9 Henning Blohm
osChoice\:JEXL3=system["os.name"].startsWith("Linux")? "We are running on a Linux OS" : "We are running on something else"
72 8 Henning Blohm
</pre>
73
74
75
76 7 Henning Blohm
77
----
78
79
80
h2. Previous Design Draft
81 1 Henning Blohm
82 6 Henning Blohm
What we aim for is an extended _properties_ format that allows declarative inclusion of other component's configuration and the use of some expression language style substitution of variables. In other words, we strive for the use of a template processing language.
83 1 Henning Blohm
84 4 Henning Blohm
The prospective candidate is the "Java Unified Expression Language":https://en.wikipedia.org/wiki/Unified_Expression_Language based on the implementation "JUEL":http://juel.sourceforge.net/
85 1 Henning Blohm
86 6 Henning Blohm
<b>Update:</b> As JUEL seems to be pretty dead and support is not clear, currently Java Expression Language ("JEXL":http://commons.apache.org/proper/commons-jexl/) is the preferred candidate. 
87
88 5 Henning Blohm
Built-in implicit contexts include 
89
90
* system properties, 
91
* environment variables, 
92
* other component properties (_lookup(<name>)_), 
93
* the component name, 
94
* the module name.
95 4 Henning Blohm
96
Example: A repository declaration could look like this:
97
98
<pre>
99
com.zfabrik.component.type=com.zfabrik.svncr
100
101
svncr.url=${system['svn.repoBaseUrl']}${system.branch}/base
102
svncr.user=${system['svn.user']}
103
svncr.passwordr=${system['svn.password']}
104
</pre>
105 1 Henning Blohm
106 6 Henning Blohm
h2. Resolution Flow
107 1 Henning Blohm
108 6 Henning Blohm
In principle, expressions could be resolved early, during local component repository index load, or late, during component descriptor evaluation.
109
110
h3. Early Evaluation
111
112
This approach has the advantage that component queries would already take resolved component properties into account. Using this method, even system state participations or the visibility as component as such could be controlled via expressions.
113
114
We strive for early evaluation. 
115
116
h2. Processing Scope
117
118
Allowing template processing means that we do not only process the value part of component properties, but instead the complete component descriptor as a text fragment. A sufficiently powerful expression language might as well introduce properties as desired.
119 4 Henning Blohm
120
121
h2. Possible extensions
122
123 2 Henning Blohm
To extend this further, component declarations may indicate their template style via some "Shebang":http://de.wikipedia.org/wiki/Shebang, the default being *java.util.Properties*.
124 1 Henning Blohm
125
For example:
126
127
<pre>
128
#!com.zfabrik.components/velocityConfig
129
#
130 3 Henning Blohm
com.zfabrik.component.type=com.zfabrik.java
131 1 Henning Blohm
132
#parse('mymodule/spring_template')
133
134
java.privateReferences=,\
135
  ${java_private_references},\
136
  someotherref
137
</pre>