Skip to content
Browse files

the ActiveJDBC Micro extension, initial release

  • Loading branch information...
1 parent 9b2d2e8 commit 6bb89149e8d60c17824074275112c95f38d2f9a8 @florinpatrascu committed Mar 10, 2013
View
128 activejdbc_m/README.md
@@ -7,25 +7,24 @@ Extending Micro with support for the [Activejdbc](http://code.google.com/p/activ
# check if the `${MICRO_HOME}` environment variable is defined
# then:
- $ cd st
+ $ cd activejdbc_m
$ ant clean; ant dist
### Installation
-The integration with Activejdbc (AJ) is still experimental, we're still trying to optimize the installation procedure. Compared with other extensions, installing AJ is not yet straightforward, please stay tuned for updates, meanwhile you can follow the steps described throughout this document.
+The integration with the ActiveJDBC (AJ) library is robust enough to be used for serious db heavy lifting but we're still trying to optimize the installation procedure.
-Copy (or create symbolic links) the `activejdbc_m.yml` file to your application extensions folder. The `extensions` folder will contain at least the following:
+Copy (or create symbolic links) the `activejdbc_m.yml` file and the `activejdbc_m` folder to your application's extensions folder. The `extensions` folder will contain at least the following:
extensions/
├── activejdbc_m.yml
+ ├── activejdbc_m/
└── ...
-Copy all the libraries files distributed under the `lib` folder of the `activejdbc_m` extension to your web app `WEB-INF/lib` folder. This step is required until we solve a classloader issue.
+Copy the `build_models.xml` from the distribution folder to your `WEB-INF` folder and rename it to `build.xml`. This file will help you instrument your class files; details below.
-Copy the `build_models.xml` from the extension folder to your `WEB-INF` folder and rename it to `build.xml`. This file is required for instrumenting your class files. Details below.
+### ActiveJDBC and the Micro web applications
-### Activejdbc and the Micro web applications
-
-This version is using a database connection pool, with the help of: [BoneCP](http://jolbox.com/). A future release will allow you to connect to a datasource using JNDI as well.
+This version is using a database connection pool, with the help of: [BoneCP](http://jolbox.com/). A future release will allow you to connect your web application to a datasource using JNDI.
Create a database connection configuration file: `WEB-INF/config/db.yml`, and describe your connectors, example:
@@ -50,129 +49,22 @@ Create a database connection configuration file: `WEB-INF/config/db.yml`, and de
password:
pool: 1
-Micro will select the connector that correspond to its running mode.
+Micro will select the connector corresponding to its running mode.
Edit the `application.bsh` startup controller and require the `activejdbc_m` extension, example:
site.ExtensionsManager
.require("i18N")
.require("activejdbc_m");
-Create a new folder in your app path: `WEB-INF/models`, and add or create your model classes there. You will have to instrument your models before using them. ActiveJDBC requires instrumentation of class files after they are compiled. You can read more about this by following this link: [What is instrumentation?](https://code.google.com/p/activejdbc/wiki/Instrumentation)
+Create a new folder in your app path: `WEB-INF/models`, and add or create your model classes there. You will have to instrument your models before using them. ActiveJDBC requires instrumentation of class files after they are compiled. You can read more about this procedure by following this link: [What is instrumentation?](https://code.google.com/p/activejdbc/wiki/Instrumentation)
With our setup you can compile and instrument your models as simple as this:
$ cd WEB-INF
$ ant
-If you are familiar with ActiveJDBC, then you can also use your instrumented models as a `.jar` file deployed to your `WEB-INF/lib` folder.
-
-Stop the web app before instrumenting your classes. A future release of Micro will use an agent that will reload the classes for you without restarting the entire app.
-
-### Use the `activejdbc_m` extension in your web application.
-Provided your database connectivity is properly configured and that your models are instrumented and available in the classpath, you can now start using the `activejdbc_m` µ extension. There are countless ways of using Activejdbc in a µ web application, even without this extension, so it is totally up to you. Before going further let's check we have all the gears in place. This is the structure of our web application:
-
- .
- ├── README.md
- ├── WEB-INF
- │   ├── articles_development.h2.db
- │   ├── articles_production.h2.db
- │   ├── articles_test.h2.db
- │   ├── build.xml
- │   ├── classes
- │   │   └── ehcache.xml
- │   ├── config
- │   │   ├── application.bsh
- │   │   ├── db.yml
- │   │   ├── extensions
- │   │   │   ├── activejdbc_m.yml
- │   │   │   └── i18N.yml
- │   │   ├── locales
- │   │   │   ├── messages_en.properties
- │   │   │   └── messages_fr.properties
- │   │   ├── micro-config.yml
- │   │   └── routes.yml
- │   ├── controllers
- │   │   ├── CreateArticle.bsh
- │   │   ├── DeleteAll.bsh
- │   │   ├── FindArticles.bsh
- │   │   └── filters
- │   │   ├── After.bsh
- │   │   └── Before.bsh
- │   ├── lib
- │   │   ├── LICENSE
- │   │   ├── activejdbc-1.4.7-SNAPSHOT.jar
- │   │   ├── activejdbc-instrumentation-1.4.7-SNAPSHOT.jar
- │   │   ├── activejdbc_m-0.1.1.jar
- │   │   ├── bonecp-0.7.1.RELEASE.jar
- │   │   ├── guava-11.0.2.jar
- │   │   ├── h2-1.3.170.jar
- │   │   ├── javalite-common-1.4.7-SNAPSHOT.jar
- │   │   └── javassist-3.17.1-GA.jar
- │   ├── models
- │   │   └── Article.java
- │   ├── views
- │   │   ├── content
- │   │   │   ├── config
- │   │   │   │   ├── delete_all.yml
- │   │   │   │   ├── index.yml
- │   │   │   │   └── new.yml
- │   │   │   ├── delete_all.html
- │   │   │   ├── index.html
- │   │   │   └── new.html
- │   │   ├── partials
- │   │   │   └── footer.html
- │   │   └── templates
- │   │   ├── 404.html
- │   │   ├── 500.html
- │   │   └── default.html
- │   └── web.xml
- ├── favicon.ico
- ├── images
- ├── jetty.xml
- ├── js
- ├── run.sh
- └── styles
- └── main.css
-
-
-Let's say we have a database containing a simple table called: `articles`.
-
- > select * from articles;
- +-----------+-----------------------+----------+
- | id | title | author |
- +-----------+-----------------------+----------+
- 1 Micro and Activejdbc florin
- 2 µExtensions in practice florin
-
-We'll use scripting Controllers for simplicity; Beanshell, in this case.
-
-First let's instrument our classes:
-
- $ cd WEB-INF
- $ ant
-
- Buildfile: WEB-INF/build.xml
-
- compile:
- [javac] Compiling 1 source file to WEB-INF/classes
-
- instrument:
- [java] **************************** START INSTRUMENTATION **************
- [java] Directory: classes
- [java] Found model: models.Article
- [java] Instrumented class: models.Article in directory: WEB-INF/classes/
- [java] **************************** END INSTRUMENTATION ****************
-
- BUILD SUCCESSFUL
- Total time: 0 seconds
-
-Now we can start the application by executing the `run.sh` script from the root of the app (or `micro start`):
-
- $ ./run.sh
-
-
-You can find this demo on Github:
+You can find a full Hotel Booking demo web application on Github, in the `micro-examples` repository: [Micro-AJ](https://github.com/florinpatrascu/micro-examples/tree/master/micro-aj).
### License
**Apache License 2**, see the `LICENSE` file in this folder.
View
29 activejdbc_m/build.xml
@@ -146,45 +146,32 @@
<!-- ====================================================================== -->
<target name="compile_test_models">
<mkdir dir="${build.dir}"/>
- <delete dir="${build.dir}/test/models"/>
- <mkdir dir="${build.dir}/test/models"/>
+ <delete dir="${build.dir}/test/classes/models"/>
+ <delete dir="${build.dir}/test/classes/controllers"/>
+ <mkdir dir="${build.dir}/test/classes/models"/>
+ <mkdir dir="${build.dir}/test/classes/controllers"/>
- <javac srcdir="${test.dir}/files/WEB-INF/models"
- destdir="${build.dir}/test/models"
+ <javac destdir="${build.dir}/test/classes"
debug="${compiler.debug}"
deprecation="${compiler.generate.no.warnings}"
encoding="utf8"
includeantruntime="false"
optimize="${compiler.optimize}">
-
+ <src path="${test.dir}/files/WEB-INF/models::${test.dir}/files/WEB-INF/controllers"/>
<classpath refid="classpath_compile"/>
</javac>
</target>
<target name="instrument_test_models">
<java classname="org.javalite.instrumentation.Main">
- <sysproperty key="outputDirectory" value="build/test/models"/>
-
+ <sysproperty key="outputDirectory" value="${build.dir}/test/classes"/>
<classpath>
- <pathelement location="${build.dir}/test/models"/>
<fileset dir="${build.dist}">
<include name="**/*.jar"/>
</fileset>
+ <pathelement location="${build.dir}/test/classes"/>
</classpath>
</java>
- <copy todir="${build.dir}/test/classes" >
- <fileset dir="build/test/models" includes="**"/>
- </copy>
- <!--
-
-
- <jar jarfile="${build.dist}/models.jar" basedir="${build.dir}/test/models">
- <manifest>
- <attribute name="Built-Date" value="${TODAY}"/>
- </manifest>
- </jar>
- -->
-
</target>
<target name="test" depends="dist, compile_test, compile_test_models, instrument_test_models">
View
89 activejdbc_m/build_models.xml
@@ -0,0 +1,89 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ ~ This script is used for fast instrumentation of the project's models as well as
+ ~ for compiling micro controllers
+ -->
+
+<project default="instrument" basedir=".">
+ <property environment="env"/>
+ <property name="micro.dir" value="${env.MICRO_HOME}"/>
+ <property name="out.dir" value="classes"/>
+ <property name="lib.dir" value="config/extensions/activejdbc_m/lib"/>
+
+ <tstamp>
+ <format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss"/>
+ </tstamp>
+
+ <path id="instrument_classpath">
+ <pathelement location="${out.dir}"/>
+ <path location="${lib.dir}/activejdbc-1.4.7-SNAPSHOT.jar"/>
+ <path location="${lib.dir}/activejdbc-instrumentation-1.4.7-SNAPSHOT.jar"/>
+ <path location="${lib.dir}/javassist-3.17.1-GA.jar"/>
+
+ <!--
+ <path location="${user.home}/.m2/repository/org/javalite/activejdbc-instrumentation/1.4.5/activejdbc-instrumentation-1.4.5.jar"/>
+ <path location="${user.home}/.m2/repository/javassist/javassist/3.8.0.GA/javassist-3.8.0.GA.jar"/>
+ <path location="${user.home}/.m2/repository/org/javalite/activejdbc/1.4.5/activejdbc-1.4.5.jar"/>
+ -->
+ </path>
+
+ <!-- Compiler options -->
+ <property name="compiler.debug" value="on"/>
+ <property name="compiler.optimize" value="on"/>
+ <property name="compiler.generate.no.warnings" value="off"/>
+ <property name="compiler.encoding" value="utf-8"/>
+ <property name="compiler.args" value=""/>
+ <property name="compiler.source" value="1.6"/>
+ <property name="compiler.target" value="1.6"/>
+
+
+ <target name="compile" description="compile and instrument user models">
+ <delete dir="${out.dir}/models"/>
+ <delete dir="${out.dir}/controllers"/>
+
+ <javac destdir="classes"
+ debug="${compiler.debug}"
+ deprecation="${compiler.generate.no.warnings}"
+ encoding="utf8"
+ includeantruntime="false"
+ optimize="${compiler.optimize}">
+
+ <src path="models:controllers"/>
+
+ <classpath refid="instrument_classpath"/>
+ <classpath>
+ <fileset dir="${lib.dir}">
+ <include name="**/*.jar"/>
+ </fileset>
+ <fileset dir="lib">
+ <include name="**/*.jar"/>
+ </fileset>
+ <fileset dir="${micro.dir}/lib/">
+ <include name="**/*.jar"/>
+ </fileset>
+ <fileset dir="${micro.dir}/dist">
+ <include name="**/*.jar"/>
+ </fileset>
+ </classpath>
+
+ </javac>
+
+ </target>
+
+ <target name="instrument" depends="compile">
+ <java classname="org.javalite.instrumentation.Main">
+ <sysproperty key="outputDirectory" value="${out.dir}"/>
+ <classpath refid="instrument_classpath"/>
+ </java>
+
+ <!-- optional:
+ <jar jarfile="lib/models.jar" basedir="${out.dir}">
+ <manifest>
+ <attribute name="Built-Date" value="${TODAY}"/>
+ </manifest>
+ </jar>
+ -->
+ </target>
+
+</project>
View
BIN activejdbc_m/lib/activejdbc_m-0.1.1.jar
Binary file not shown.
View
BIN activejdbc_m/lib/h2-1.3.170.jar
Binary file not shown.
View
32 activejdbc_m/test/files/WEB-INF/controllers/FindArticles.bsh
@@ -1,32 +0,0 @@
-/*
- * Copyright (c)2013 Florin T.Pătraşcu
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-import activejdbc.*;
-import ca.simplegames.micro.test.models.Article;
-
-AJ = site.get("activejdbc_m");
-
-try{
-
- AJ.before();
- articles = Article.findAll();
- context.put("totalArticles", articles.size());
- context.put("article", articles.get(0).toMap());
-}catch(e){
- e.printStackTrace();
- AJ.onException();
-}finally{
- AJ.after();
-}
View
38 activejdbc_m/test/files/WEB-INF/controllers/FindArticles.java
@@ -0,0 +1,38 @@
+package controllers;
+
+import ca.simplegames.micro.Controller;
+import ca.simplegames.micro.MicroContext;
+import ca.simplegames.micro.SiteContext;
+import ca.simplegames.micro.controllers.ControllerException;
+import ca.simplegames.micro.extensions.ActivejdbcExtension;
+import models.Article;
+
+import java.io.FileNotFoundException;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
+ * @since $Revision$ (created: 2013-03-10 3:48 PM)
+ */
+public class FindArticles implements Controller {
+ @Override
+ public void execute(MicroContext context, Map map) throws ControllerException, FileNotFoundException {
+ SiteContext site = context.getSiteContext();
+ ActivejdbcExtension ajExtension = (ActivejdbcExtension) site.get("activejdbc_m");
+ if(ajExtension!=null){
+ try {
+ ajExtension.before();
+ List<Article> articles = Article.findAll();
+ context.put("totalArticles", articles.size());
+ context.put("article", articles.get(0).toMap());
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ ajExtension.after();
+ }
+ }else{
+ throw new RuntimeException("The ActiveJDBC extension is not enabled");
+ }
+ }
+}
View
3 ...implegames/micro/test/models/Article.java → ..._m/test/files/WEB-INF/models/Article.java
@@ -1,4 +1,5 @@
-package ca.simplegames.micro.test.models;/*
+package models;
+/*
* Copyright (c)2013 Florin T.Pătraşcu
*
* Licensed under the Apache License, Version 2.0 (the "License");
View
2 activejdbc_m/test/files/WEB-INF/views/content/config/index.yml
@@ -1,5 +1,5 @@
controllers:
- controller:
- name: FindArticles.bsh
+ name: controllers.FindArticles
options:
top: 10
View
BIN activejdbc_m/test/test.h2.db
Binary file not shown.

0 comments on commit 6bb8914

Please sign in to comment.
Something went wrong with that request. Please try again.