Project

General

Profile

Wiki » History » Version 25

Henning Blohm, 28.01.2015 15:24

1 1 Henning Blohm
h1. Intro
2
3 2 Henning Blohm
This project is for the development of [[z2-environment:wiki|z2-environment]] version 3.0.
4 1 Henning Blohm
5 2 Henning Blohm
Version 3.0 is a major refactoring release of z2 with new repository code lines and complete package renaming.
6 1 Henning Blohm
7 2 Henning Blohm
h1. Essential Differences between v2.3 and v3.
8 1 Henning Blohm
9 2 Henning Blohm
* All the typical declarations and package names are no longer *com.zfabrik.<something>* but rather *org.z2env.<something>*
10 13 Henning Blohm
* The <z2 home> layout is completely new and more flexible
11 3 Henning Blohm
* The essential component types that can be _invoked_ from the core is *system state* and *main program*.
12 14 Henning Blohm
* While Jetty is still contained to support Eclipsoid and z2Unit, we support a pre-installed Tomcat as application container (and with TomEE would even get a Java EE profile)
13 1 Henning Blohm
* The core is lighter and more dedicated. 
14 12 Henning Blohm
* There will be substantially less built-in application support like
15
** a local JTA implementation 
16
** No worker suppport without a corresponding add-on.
17 1 Henning Blohm
18 12 Henning Blohm
The main task of v3 is:
19 13 Henning Blohm
* Make the lower parts of z2 more accessible and easier to understand: While z2 previously started System Centric, we now close the gap from the ordinary main program.
20 12 Henning Blohm
* Increase the OSS flavor of z2
21
22 15 Henning Blohm
Obvious TODOs:
23
* Finish Java 8 support
24
* Test TomEE
25
* Fix Eclipsoid templates
26
* Support MVNCR, also for distributions
27 12 Henning Blohm
28 9 Henning Blohm
h1. Draft Intro Documentation
29 1 Henning Blohm
30 2 Henning Blohm
h2. Getting Started
31 1 Henning Blohm
32 2 Henning Blohm
To understand how z2 works, you need to understand at most what are z2 modules and components and what are component repositories and how these concepts interplay. 
33 1 Henning Blohm
34 6 Henning Blohm
Check out [[wiki#Understanding-Z2|understanding-z2]] for that.
35 1 Henning Blohm
36 2 Henning Blohm
It is simplest to start by running a simple Java main program in z2 or a simple Web App. 
37 1 Henning Blohm
38 2 Henning Blohm
Follow this trail:
39 1 Henning Blohm
40 4 Henning Blohm
* [[wiki#Building-z2env-core|Building the core]]
41 7 Henning Blohm
* [[wiki#Running-a-Hello-World-main-program|Running a Hello World main program]]
42 10 Henning Blohm
* [[wiki#Running-a-Hello-World-Web-App|Running a Hello World Web App]]
43 22 Henning Blohm
* [[wiki#Developing-with-Eclipse-or-IntelliJ|Developing with Eclipse or IntelliJ]]
44 1 Henning Blohm
45 22 Henning Blohm
46
47 3 Henning Blohm
h2. Building z2env-core
48 1 Henning Blohm
49 17 Henning Blohm
(For now, we need to build the core).
50
51
h3. Pre-requisites
52
53
* You need Git installed
54
* You need a JDK, at least version 7
55
* You need the ANT build tool
56
57 3 Henning Blohm
First clone http://git.z2-environment.net/z2env.core
58
59
<pre>
60
git clone http://git.z2-environment.net/z2env.core
61
</pre>
62
63 25 Henning Blohm
Change into *org.z2env.core.main* and run 
64 3 Henning Blohm
65
<pre>
66 18 Henning Blohm
ant -Doutput=<z2home> -Dsetup=<setup> 
67 3 Henning Blohm
</pre>
68
69 18 Henning Blohm
where <z2home> is where you want to install z2 (the *z2 home* that is) and <setup> is a choice of pre-configured content, actually component repositories.
70 3 Henning Blohm
71
If you omit *output* it will default to *gen/dist*. If you omit *setup*, no content will be pre-configured will be modified. 
72
73
Check the folder *setups* for available setups of the core build. These can be local (all repos will be cloned locally) or remote (repos will be remoted).
74
75 1 Henning Blohm
In general this build always updates only boot, z.jar, roots. It will not remove anything.
76
77 18 Henning Blohm
78 3 Henning Blohm
Example to install/update a core only into ../../z2env:
79
80
<pre>
81 24 Henning Blohm
ant -Doutput=../../z2env 
82 3 Henning Blohm
</pre>
83
84 1 Henning Blohm
To install/update a web-basic setup (with tomcat and eclipsoid support) with updates from remote repos:
85
86
<pre>
87 3 Henning Blohm
ant -Doutput=../../z2env -Dsetup=web-basic-remote
88
</pre>
89 19 Henning Blohm
90
Check out [[wiki#Understanding-the-ltHOMEgt|Understanding the <HOME>]] to learn more about the generated structure.
91
92 7 Henning Blohm
93
h2. Running a Hello World main program
94
95
For now, we will assume you have a z2 core installed in <HOME>. In its purest form, all the core can actually execute is simple Java main programs. 
96
97
Being in a modular environment, even if we only want to say „Hello World“, we need a module. Let's call it the greeter module. In order to have a main program run by z2, we need to also declare a  component for it, as that is what we tell z2 to run (not a class – mind you – but rather a component in a module). Assuming we use the local component name hello, than the absolut component name is 
98
99
<pre>
100
greeter/hello
101
</pre>
102
103
Finally, as this component actually has implementation code, we need to declare a Java component. By convention this is 
104
105
<pre>
106
greeter/java
107
</pre>
108
109
Now, after installing in <HOME> the place to simply drop a module is under <HOME>/modules. All in all, here is the complete structure to create:
110
111
| <HOME>/modules/greeter/hello.properties | 
112
<pre><code class="ruby">org.z2env.component.type=org.z2env.main
113
component.className=greetings.Main
114
</code></pre> |
115
| <HOME>/modules/greeter/java/z.properties | 
116
<pre><code class="ruby">org.z2env.component.type=org.z2env.java
117
</code></pre> |
118
| <HOME>/modules/greeter/java/src.impl/greetings/Main.java |
119
<pre><code class="java">
120
package greetings;
121
122
public class Main {
123
  public static void main(String[] args) {
124 8 Henning Blohm
    System.out.println("Hello!");
125 7 Henning Blohm
  }
126
}
127
</code></pre> |
128
129
That gives us the two components of module greeter. To run this on the command line, run
130
131
<pre>
132
java -DcomponentName=greeter/hello -jar z.jar
133
</pre>
134
135 1 Henning Blohm
Now, obviously this sample is not worth a modular execution environment. But even from here, you could check your module in with some Git repository that is referenced from the roots and all other users of the same repo would be able to run your component without installing anything.
136 10 Henning Blohm
137
h2. Running a Hello World Web App
138
139 11 Henning Blohm
This introduction explains how to connect a z2 core to a given Tomcat installation and how to develop a simple Web Application.
140 7 Henning Blohm
141 11 Henning Blohm
Furthermore it describes how to use the Dev Repository and the Eclipsoid plugin for the Eclipse or the IntelliJ development environment.
142
143
h3. Pre-Requisites
144
145
* We assume a development workspace in *workspace*.
146
* Have a local Tomcat (7 or higher) installation.*
147
148
h3. Steps we are going to take
149
150
# Create a module with a simple Web app
151
# Make it visible to Z2
152
# Run the Web App in a Tomcat Web Container
153
154
At first get yourself a z2env core with the *web-basic-local* or *web-basic-remote* setup. Make sure it is in your *workspace*. If you follow [[wiki#Building-z2env-core|Building the core]] this means running 
155
156
<pre>
157
ant -Doutput=workspace/z2env -Dsetup=web-basic-remote
158
</pre>
159
160
Using your IDE create a new project *hello* in *workspace* with the following structure:
161
162
163
| java/z.properties | 
164
<pre><code class="ruby">
165
org.z2env.component.type=org.z2env.java
166
java.privateReferences=\
167
	javax.servlet
168
</code></pre> |
169
| web/z.properties |
170
<pre><code class="ruby">
171
org.z2env.component.type=org.z2env.tomcat.webapp
172
webapp.path=/hello
173
</code></pre> |
174
| web/WebContent/index.jsp |
175
<pre><code class="html"><html><body>Hello! Did you know that 7*7=<%=7*7%>?</body></html></code></pre> |
176
| LOCAL | <empty file> |
177
178 20 Henning Blohm
Your *workspace* folder now contains two subfolders *z2env* and *hello*.
179
180 11 Henning Blohm
So far, that's it.  We will touch on the LOCAL file later. 
181
182
When we run Z2 knowing this Web Application, it will attempt to start a Tomcat Web Container that will serve the application's content. To do that, it needs to find one. 
183
184
In order to tell Z2 where to look, you can either set an environment variable *CATALINA_HOME* (you might have already – it's the Tomcat standard) or specify an environment variable *catalina.home*. 
185
186
To be on the safe side and assuming the location is */home/me/tomcat*, we open a terminal, change into *workspace/z2env* and run
187
188
<pre>
189
java -Dcatalina.home=/home/me/tomcat -jar z.jar hello/web
190
</pre>
191
192
Note, unlike for the main program, the command line will not return. Instead you can enter „q“ to quit or „s“ to synchronize. But first let's open a browser and check out 
193
194
<pre>
195
http://localhost:8080/hello
196
</pre>
197
198
Now on to that LOCAL file. Unlike for the Hello World main program, this time it was to so-called Dev Repo that made our module available to z2. 
199
200
The dev repo is a key component to the development approach with z2. It checks for all modules that have a LOCAL file and that are stored some levels deep, relative to the parent folder of the z2 home (by default), which not so coincidentally is our workspace.
201
202
Try this: 
203
204
# Remove LOCAL
205
# Enter „s“ for synchronization. 
206
# Check the URL above (should say „not found“)
207
# Create a LOCAL file again
208
# Synchronize
209
# Check again.
210 1 Henning Blohm
211 11 Henning Blohm
Similarly, from now on, changes made to the hello module will be picked up, whenever you synchronize.
212 1 Henning Blohm
213 22 Henning Blohm
h2. Developing with Eclipse or IntelliJ
214 1 Henning Blohm
215 21 Henning Blohm
In this section we will add some code to the hello Web app and see how to develop using an integrated development environment like the Eclipse IDE or Jetbrains' IntelliJ IDEA.
216 1 Henning Blohm
217 21 Henning Blohm
h3. Pre-Requisite
218 1 Henning Blohm
219 21 Henning Blohm
* Have an Eclipse or IntelliJ installation
220
* Install the Eclipsoid plugin from http://z2-environment.net/eclipsoid/update/site.xml or by searching for "z2" in the IntelliJ Plugin Repository respectively
221
222
h3. Steps we are going to take
223
224
# Complete the development workspace setup
225
# Add a simple Controller
226
227
h4. In Eclipse
228
229
Use the workspace *workspace*. Create a Java project *hello* where we created the *hello* module. That is, we use the existing module as project content. You should now have an Eclipse development workspace that has one Java project named "hello". Right-click on the project and choose "Transform into z2 Project". A small "Z" icon should not decorate the project icon. 
230
231
Try "Arm Z2 Projects" and "Disarm Z2 Projects" from the context menu. You should see a LOCAL file showing up and disappearing as well as the "Z" decoration showing a green halo and going back to normal. 
232
233
Go into Window/Preferences, pick "z2-Environment". Change the port to 8081, set the user to "z*" and the password to "z". Done.
234
235
h4. In IntelliJ
236
237
Start a new Project with root folder *workspace*. Press the "Manage z2-Modules" button in the toolbar. Import the *hello* module into your IntelliJ project. Open the Settings dialog, pick "z2-Environment". Change the port to 8081, set the user to "z*" and the password to "z". Done.
238
239
h4. IDE Indepently
240
241
Start z2 as in the previous section but make sure the development support is enabled. Use this command line:
242
243
<pre>
244
java -Dcatalina.home=/home/me/tomcat -jar z.jar org.z2env.dev.shared/ready hello/web
245
</pre>
246
247
(possibly you want to turn this into a shell script)
248
249 23 Henning Blohm
*Note:* The _system state_ *org.z2env.dev.shared/ready* will pull up all necessary development support components. Make sure this is part of your to-be-run list.
250
251
252 21 Henning Blohm
Regardless whether you are using Eclipse or IntelliJ, press the "Resolve Classpath from Z2" button in the toolbar. This will make sure that all required dependencies for Z2 modules, as far as Java code is concerned, will be resolved for your workspace. 
253 1 Henning Blohm
254 21 Henning Blohm
Now add a class *SimpleController* and change the following files:
255
256
257 22 Henning Blohm
| java/src.impl/hello/SimpleController.java | 
258 21 Henning Blohm
<pre><code class="java">
259
package hello;
260
261
import javax.servlet.*;
262
import javax.servlet.http.*;
263
import java.io.IOException;
264
265
public class SimpleController extends HttpServlet {
266
267
    @Override
268
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
269
        String input = req.getParameter("input");
270
        if (input!=null && input.trim().length()>0) {
271
            req.setAttribute("output","You said: "+input);
272
        }
273
        req.getRequestDispatcher("/").forward(req,resp);
274
    }
275
}
276
</code></pre> |
277
| java/z.properties | 
278
<pre><code class="ruby">
279
org.z2env.component.type = org.z2env.java
280
281
java.privateReferences=\
282
  javax.servlet,\
283
  javax.servlet.jsp.jstl
284
</code></pre> |
285
| web/WebContent/WEB-INF/web.xml | 
286
<pre><code class="xml">
287
<?xml version="1.0" encoding="UTF-8"?>
288
<web-app id="hello" version="2.5"
289
		xmlns="http://java.sun.com/xml/ns/javaee"
290
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
291
		xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
292
293
    <servlet>
294
        <servlet-name>controller</servlet-name>
295
        <servlet-class>hello.SimpleController</servlet-class>
296
    </servlet>
297
    
298
    <servlet-mapping>
299
        <servlet-name>controller</servlet-name>
300
        <url-pattern>/do</url-pattern>
301
    </servlet-mapping>    
302
</web-app>
303
</code></pre> |
304
| web/WebContent/index.jsp |
305
<pre><code class="html">
306
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core_1_1" %>
307
<html>
308
<body>
309
  <c:if test="${not empty output}">
310
    <p><c:out value="${output}"/></p>
311
  </c:if>
312
  <form method="POST" action="<c:url value='/do'/>">
313
    Input: <input type="text" name="input"><input type="submit" value="go!">
314
  </form>
315
</body>
316
</html>
317
</code></pre>|
318
319
Make sure the *hello* module is armed (right-click) by having a LOCAL file.
320
321
Now synchronize your running z2 by either:
322
323
* Pressing the "Sync with z2-Environment" button in the toolbar
324
* Pressing ALT+y in your IDE
325
* Entering "s" on the command line running z2.
326
327
Go to http://localhost:8080/hello and check it out.
328
329
330
h2. Going Multi-Module
331 12 Henning Blohm
332
TBD
333
334
h2. Going System-Centric
335
336
TBD
337
338
Getting more real by adding a remote repo as system repo.
339
340
Develop by checking out modules and using the Dev Repo.
341 3 Henning Blohm
342 1 Henning Blohm
343 3 Henning Blohm
h2. Understanding the <HOME>
344 1 Henning Blohm
345 18 Henning Blohm
We call an installation of a z2env core a *<home>*. The file structure of the *<home>* consists of few but important locations
346 3 Henning Blohm
347 18 Henning Blohm
h3. Layout
348
349 3 Henning Blohm
| z.jar | The actual core implementation. This is precompiled.|
350
| boot/ | A component repository adding to the core and provides the most basic capabilities such as compiling Java code and running a main program. |
351
| roots/ | A file system component repository that is always registered by the core. This is typically filled with other repository components linking to remote or local component repositories. |
352 1 Henning Blohm
| modules/ | A file system repository declared under roots/ that is useful to locally augment a core installation.|
353
354
We use the modules repository for example for command line experiments and simple hacks. It is not as useful for regular development operations as is the dev repo.
355 18 Henning Blohm
356
h3. Folders and Properties
357
358
When running z2 from the command line, some logic is applied to derive where stuff is and some defaults apply:
359
360
|_. What |_. System property |_. Defaults |
361
| Z2 home - the installation location | org.z2env.home | If no system property is set, the environment variable Z2_HOME will be checked. If that is not set, the z2 home location will be derived from the location of the z.jar file |
362
| Dev Repo workspaces - where the Dev Repo checks for local modules | org.z2env.dev.local.workspaces | Defaults to ${z2.home}/.. assuming modules will be next to the home or one level deeper |
363
364 5 Henning Blohm
365
h2. Understanding Z2
366
367
Most of the concepts that make up z2 can be derived from the desire to efficiently develop standard Java Web Applications that are defined in a central system definition. Add to that the wish to avoid all that build complexity and the desire for a practically usable modularization approach.
368
369
Once you loose the ability to tweak some build script or configuration to describe how to package a Web app or some libraries, you need some structural hint and simple declarations providing sufficient information.
370
371
Secondy, assuming you want to apply changes at runtime, you need some way of determining what to unload at runtime, given some changed file. Again: Structural information needs to present that maps to runtime state. 
372
373
Finally, as we want some support for modular application development anyway, why not make modules our essential structure. As we have not only code but also Web Applications, Data Sources,  Background Jobs and much more to consider, we add components with type into the mix. Voila.
374
375
*Component Repositories* provides modules and components to z2. A component is prefixed by the module name:
376
377
<pre>
378
<global component name> = <module name>/<local component name>
379
</pre>
380
381
In fact, the typical component repository has a file system style structure that is made up exactly like that. 
382
383
Structure from repo root:
384
385
| /<module>/<cmp>.properties | A component <cmp> in module <module> that has no resources on its own.|
386
| /<module>/<cmp>/z.properties | A component <cmp> in module <module> that may have additional resources on its own.|
387
388
Note that some components, like Web applications and typically Java components have file resources, such as HTML files or Java source code files, while other components, such as main program declarations or data source declarations, so not.
389
390
There are some conventions around this naming scheme. In particular, by convention, whenever a component requires Java implementation, z2 will first look for a component named *java* of the same module. As for example in the Hello World example.
391
392
To give you an idea what this model translates to at runtime consider this: At runtime, the static component is turned into a Resource instance by a component type implementation (that, in fact, is provided by another, built-in component type). 
393
394
!uml1.png!
395
396
Dependencies, for example those between Java components, translate into dependencies between resources that are observed during invalidation of resources. This introduces invalidation and synchronization.
397
398 16 Henning Blohm
h3. A crucial concept of z2 is that of a synchronization. 
399 5 Henning Blohm
400
For fast development roundtrips and in some cases for general configuration updates, it is desirable to not require a complete restart of a Java Virtual Machine process but rather just to change runtime state accordingly. 
401
402
In z2 a synchronization describes the following process:
403
404
# Check for changes in component repositories
405
# Based on changes found, identify components affected and invalidate associated runtime resources incl. dependent resources.
406
# Try to attain target states
407
408 1 Henning Blohm
That is, whenever you do a change and trigger a synchronization from your IDE, this is exactly what happens: Identify what needs to be thrown away, make sure to remove dependent stuff as well,  bring up what needs to be running.
409 16 Henning Blohm
410
h3. A second crucial concept of z2 is that of *System Centric Development*
411
412
When working on a software system with many access points and high re-use on a service level, as is typical for business systems, staying quick integration with a team's changes is key. Component repositories can be fed from remotely accessible version control systems such as Subversion and Git. When connecting to a central, team-shared repository, every synchronization will make sure that you are up-to-date with the real source of truth. 
413
414
Using the dev repo, you can still isolate yourself from changes on part of the system that you are actively working on. After your modifications are completed, rebased, and locally tested, getting a clean and consistent state is just one synchronization away.
415
416
h3. The third and final concept is that of a compile-free environment
417
418
It is within the nature of the synchronization concept, that anything required to happen to have your software available for execution must have happened after synchronization completed. Most preparation steps are executed at first use. This includes the compilation of Java code. Z2 uses the Eclipse Java Compiler for that purpose.
419 9 Henning Blohm
420
h2. The Component Model
421
422
See [[z2-environment:Z2_Modules_intro]]
423
424
h2. Java Components
425
426
See [[z2-environment:Z2_Java_components_intro]]