Broadcast on Broadcast off
The Documentation for Project Zero has moved. Please update your bookmarks to: http://www.projectzero.org/documentation/
Table of
Contents...
Hide

Project Zero Developer’s Guide

Concepts and components
Basic concepts overview
Event processing
Writing Java handlers
Writing Groovy handlers
Firing events
Global Context
Global Context reference
Application directory layout
Virtualized directories
Assemble
PHP
Features and configuration
Configuration
Debugging
Dependencies
Packaging
Application classpath
Logging and tracing
RESTful resources
RESTful documentation
File serving
Response rendering
Validators and validation
HTTP error handling
Calling a remote resource
Using the Connection API
Sending an email using EmailConnection
Configuring destinations
Configuring protocols
Configuring connection handlers
Creating a connection handler
Creating a custom protocol transport
Simple logging connection handlers
Protocol reference
Client programming with Dojo
Runtime options
Deployment modifications
HTTP configuration
SSL configuration
Proxy configuration
Extending the CLI
Security considerations
Authentication
OpenID authentication
Extending security
Security tokens
CSRF prevention support
Extending token support
Leveraging TAI
User service
File based user service
LDAP user service
Extending user service
Security Utilities
Leveraging XOREncoder
Extensions
Atom support
RSS support
JSON support
XMLEncoder
REST to SOAP extension
URIUtils
Developer Web tools
Database setup tools
Configuring data access
Common query patterns
Advanced query patterns
Update patterns
Local database transactions
Extending data access
Configuration vendor differences
PHP data access
Resource model
Configuring ZRM
Resource model declaration
Programmatic model API
HTTP REST API
A ZRM mini tutorial
Active content filtering support
Default filters
Custom filters
Runtime management
Management commands
Zero socket opener
Other extension modules
Amazon E-commerce service
Flickr service
WeatherZero forecast service
Wikipedia service
Reference
Zero command line interface
JavaDoc - Public API
JavaDoc - Public SPI
JavaDoc - All Classes

 

Configuring data access

Data access has use cases that range from very simple to very complex; many developers pine for an API that is flexible enough to handle complex situations without making simple tasks cumbersome. "Make the common things easy and the hard things possible." The zero.data.Manager and zero.data.groovy.Manager classes provide an API that solves many common use cases while still allowing for very specific customizations.

The following sections of this article provide information about data access with zero.data:

tip There are two Manager implementations: one for Groovy (zero.data.groovy.Manager) and one for Java (zero.data.Manager). The Groovy implementation provides additional methods that make use of the dynamic nature of Groovy.

The following sections of this article provide information about configuring data access:

Adding zero.data to your application

If you are using data zero query within a Zero application, you need to resolve a dependency in order to install the required jar files. After creating your application, modify the config/ivy.xml file in your application to add the following line in the dependencies section:

<dependency org="zero" name="zero.data" rev="1.0+"/>

After adding the dependency, issue the resolve command using either Ant or Eclipse.

You can also pull in the jar files you need for database access. For example, adding the following to config/ivy.xml pulls in the Derby client jar file:

<dependency name="derbyclient" org="org.apache.derby" rev="10.3+"/>

Configuring zero.data.Manager and zero.data.groovy.Manager in Project Zero

zero.data.Manager, and its groovier friend, zero.data.groovy.Manager, can be initialized with either a javax.sql.DataSource or a java.sql.Connection as a constructor argument. If you are using the Project Zero zero.config configuration file, Manager.create(String) is a convenience method to create a new instance, allowing you to avoid any reference to JDBC APIs.

The following zero.config file snippet shows the configuration of two databases accessible from a Project Zero application:

# This config is for Apache Derby databases:
/config/db/reviewDB = {
    "class" : "org.apache.derby.jdbc.EmbeddedDataSource",
    "serverName" : "localhost",
    "portNumber" : 1527
    "databaseName" : "./db/review",
    "createDatabase" : "create",
    "password" : "<xor>Lz4sLCgwLTs="
}

# This config is for MySQL databases:
/config/db/votingDB = {
    "class" : "com.mysql.jdbc.jdbc2.optional.MysqlDataSource",
    "serverName" : "db.example.org",
    "portNumber" : 3306
    "databaseName" : "voting",
    "createDatabase" : "create",
    "password" : "<xor>Lz4sLCgwLTs="
}

Obfuscating passwords

The second example above, /config/db/votingDB, illustrates how to use the XOR encoded password to obfuscate the password. For more details on how to generate a password with this encoding, consult the XOR Encoder documentation. Taking this measure prevents the password from being shown in plain text. Although not secure with encryption or hash, this form of encoding simply keeps honest people honest.

Obtain a data manager

Two databases, reviewDB and votingDB, are configured in the zero.config example above. The former is a local network Derby client and the latter is a remote MySQL database. You can configure as many databases as your application needs.

With databases configured at /config/db/{database name}, an application developer can obtain a zero.data.Manager or zero.data.groovy.Manager as follows:

// Java code:
import zero.data.Manager;
...
Manager voteDB = Manager.create("votingDB");
List<Map<String, Object>> results = voteDB.queryList("SELECT * FROM votes WHERE numVotes > ?", 10);

// Groovy code:
import zero.data.groovy.Manager
...
def voteDB = zero.data.Manager.create('votingDB')
def numVotes = 10
def results = voteDB.queryList("SELECT * FROM votes WHERE numVotes > $numVotes")

r14 - 16 Jan 2008 - 19:12:07 - paynel
Syndicate this site RSS ATOM
Copyright 2007 © IBM Corporation | Privacy | Terms of Use | About this site