Author: lresende
Date: Wed Sep 5 14:10:53 2007
New Revision: 573058
URL: http://svn.apache.org/viewvc?rev=573058&view=rev
Log:
Simplifiying customer sample code to show DAS programming model and hide
database initialization code
Added:
incubator/tuscany/java/das/samples/customer/src/main/java/org/apache/tuscany/samples/das/customer/CustomerDatabaseInitializer.java
(with props)
incubator/tuscany/java/das/samples/customer/src/main/resources/CustomersConfig.xml
- copied unchanged from r573010,
incubator/tuscany/java/das/samples/customer/src/main/resources/Customers.xml
incubator/tuscany/java/das/samples/customer/src/main/resources/log4j.properties
(with props)
Removed:
incubator/tuscany/java/das/samples/customer/src/main/java/org/apache/tuscany/samples/das/databaseSetup/DB2Setup.java
incubator/tuscany/java/das/samples/customer/src/main/resources/Customers.xml
Modified:
incubator/tuscany/java/das/samples/customer/src/main/java/org/apache/tuscany/samples/das/customer/CustomerClient.java
Modified:
incubator/tuscany/java/das/samples/customer/src/main/java/org/apache/tuscany/samples/das/customer/CustomerClient.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/das/samples/customer/src/main/java/org/apache/tuscany/samples/das/customer/CustomerClient.java?rev=573058&r1=573057&r2=573058&view=diff
==============================================================================
---
incubator/tuscany/java/das/samples/customer/src/main/java/org/apache/tuscany/samples/das/customer/CustomerClient.java
(original)
+++
incubator/tuscany/java/das/samples/customer/src/main/java/org/apache/tuscany/samples/das/customer/CustomerClient.java
Wed Sep 5 14:10:53 2007
@@ -23,138 +23,58 @@
import org.apache.tuscany.das.rdb.Command;
import org.apache.tuscany.das.rdb.DAS;
-import org.apache.tuscany.das.rdb.config.Config;
-import org.apache.tuscany.das.rdb.util.ConfigUtil;
-import org.apache.tuscany.samples.das.databaseSetup.DB2Setup;
-import org.apache.tuscany.samples.das.databaseSetup.DerbySetup;
-import org.apache.tuscany.samples.das.databaseSetup.MySQLSetup;
import commonj.sdo.DataObject;
public class CustomerClient {
+ private static final String DEFAULT_CUSTOMER_CONFIG =
"CustomersConfig.xml";
+
private DAS das = null;
- public static final String DERBY = "derby";
- public static final String MYSQL = "mysql";
- public static final String DB2 = "db2";
-
- private CustomerClient(){
+ private final String configFile;
+
+ /**
+ * Default constructor
+ *
+ */
+ public CustomerClient(){
+ this.configFile = DEFAULT_CUSTOMER_CONFIG;
}
-
- /**If database is present connect to it and create necessary tables,
procedures, data etc.
- * If database is not present create database and then create the above
elements. Create database
- * is implemented for MySQL and Derby (not for IBM DB2).
- */
-
- protected boolean checkIfDBPresent(String dbType, String dbName, String
user, String password){
- try {
- if(dbType.equals(DERBY)){
- new DerbySetup(dbName+"-"+user+"-"+password);
- }
-
- if(dbType.equals(DB2)){
- new DB2Setup(dbName+"-"+user+"-"+password);
- }
-
- if(dbType.equals(MYSQL)){
- new MySQLSetup(dbName+"-"+user+"-"+password);
- }
-
- } catch(Exception e){
- e.printStackTrace();
+
+ /**
+ * Constructor receiving the das config file to be used
+ * @param configFile DAS configuration file
+ */
+ public CustomerClient(String configFile) {
+ this.configFile = configFile;
+ }
+
+ /**
+ * Helper method to get a stream from the customer config file
+ * @param fileName
+ * @return
+ */
+ protected InputStream getConfig(String fileName) {
+ return getClass().getClassLoader().getResourceAsStream(fileName);
+ }
+
+ /**
+ * Get a reference to DAS, initialize it if necessary
+ * @return DAS reference
+ */
+ protected DAS getDAS() {
+ if (this.das == null ) {
+ this.das = DAS.FACTORY.createDAS(getConfig(this.configFile));
}
-
- return true;
+ return this.das;
}
-
- private void init(String configFile){
- try{
- this.das = DAS.FACTORY.createDAS(getConfig(configFile));
- }catch(Exception e){
- e.printStackTrace();
- }
- }
-
- public static void main(String[] args){
- String configFile = "Customers.xml";//this can be from input
params too as below
-
- if(args != null && args.length >0){
- configFile = args[0];
- }
-
- CustomerClient cclient = new CustomerClient();
-
- Config config =
ConfigUtil.loadConfig(cclient.getClass().getClassLoader().getResourceAsStream(configFile));
-
- String dbType = null;
- String dbURL = null;
- String user = null;
- String password = null;
-
-
if(config.getConnectionInfo().getConnectionProperties().getDriverClass().indexOf(DERBY)
!= -1){
- dbType = DERBY;
- }
-
-
if(config.getConnectionInfo().getConnectionProperties().getDriverClass().indexOf(MYSQL)
!= -1){
- dbType = MYSQL;
- }
-
-
if(config.getConnectionInfo().getConnectionProperties().getDriverClass().indexOf(DB2)
!= -1){
- dbType = DB2;
- }
-
- //get connection info from config
- dbURL =
config.getConnectionInfo().getConnectionProperties().getDatabaseURL();
- user =
config.getConnectionInfo().getConnectionProperties().getUserName();
- password =
config.getConnectionInfo().getConnectionProperties().getPassword();
-
- System.out.println("connection info from
config***************");
- System.out.println("dbName:"+dbURL+" user:"+user+"
password:"+password);
-
System.out.println("******************************************");
- //dt create/connect/create schema
- cclient.checkIfDBPresent(dbType, dbURL, user, password);
-
- //get das handle
- cclient.init(configFile);
-
- //test select
- System.out.println("Result:select all customers");
- printList(cclient.getCustomers());
-
- //test insert
- System.out.println("Result:insert new customer");
- cclient.addCustomer();
- printList(cclient.getCustomers());
-
- //test update
- System.out.println("Result:update first customer");
- cclient.changeFirstCustomerName();
- printList(cclient.getCustomers());
-
- //test delete
- System.out.println("Result:delete last customer");
- cclient.deleteCustomer();
- printList(cclient.getCustomers());
- }
- /**
- * display result
- * @param customers
- */
- public static void printList(List customers){
- for(int i=0; i<customers.size(); i++){
- System.out.println("
ID:"+(((DataObject)customers.get(i)).getInt("ID"))+
- "
LASTNAME:"+(((DataObject)customers.get(i)).getString("LASTNAME"))+
- "
ADDRESS:"+(((DataObject)customers.get(i)).getString("ADDRESS")));
- }
- }
/**
* select
* @return
*/
public final List getCustomers() {
-
- Command read = das.getCommand("AllCustomers");
+ Command read = this.getDAS().getCommand("AllCustomers");
DataObject root = read.executeQuery();
return root.getList("CUSTOMER");
}
@@ -164,7 +84,7 @@
*
*/
public final void addCustomer() {
- Command read = das.getCommand("AllCustomers");
+ Command read = this.getDAS().getCommand("AllCustomers");
DataObject root = read.executeQuery();
DataObject newCustomer = root.createDataObject("CUSTOMER");
@@ -180,7 +100,7 @@
*
*/
public final void deleteCustomer() {
- Command readAll = das.getCommand("AllCustomers");
+ Command readAll = this.getDAS().getCommand("AllCustomers");
DataObject root = readAll.executeQuery();
List allCustomers = root.getList("CUSTOMER");
@@ -199,7 +119,7 @@
*
*/
public final void changeFirstCustomerName() {
- Command readAll = das.getCommand("AllCustomers");
+ Command readAll = this.getDAS().getCommand("AllCustomers");
DataObject root = readAll.executeQuery();
DataObject firstCustomer = root.getDataObject("CUSTOMER[1]");
@@ -207,21 +127,71 @@
das.applyChanges(root);
}
-
+
+ /**
+ * display result
+ * @param customers
+ */
+ public static void printList(List customers){
+ for(int i=0; i<customers.size(); i++){
+ System.out.println("
ID:"+(((DataObject)customers.get(i)).getInt("ID"))+
+ "
LASTNAME:"+(((DataObject)customers.get(i)).getString("LASTNAME"))+
+ "
ADDRESS:"+(((DataObject)customers.get(i)).getString("ADDRESS")));
+ }
+ }
+
/**
* cleanup
*
*/
public void releaseResources() {
das.releaseResources();
- }
-
- /**Utilities
- *
- * @param fileName
- * @return
- */
- private InputStream getConfig(String fileName) {
- return getClass().getClassLoader().getResourceAsStream(fileName);
+ }
+
+ /**
+ * Main customer application
+ */
+ public static void main(String[] args){
+ String customerConfigFile = "CustomersConfig.xml"; //this can be from
input params too as below
+
+ if(args != null && args.length >0){
+ customerConfigFile = args[0];
+ }
+
+
+ //initialize customer database using helper class
+ CustomerDatabaseInitializer dbInitializer = new
CustomerDatabaseInitializer(customerConfigFile);
+ try {
+ dbInitializer.Initialize();
+ } catch (Exception e) {
+ e.printStackTrace();
+ return;
+ }
+
+
+ //perform customer operations using DAS
+ CustomerClient customerClient = new
CustomerClient(customerConfigFile);
+ //test select
+ System.out.println();
+ System.out.println("Result:select all customers");
+ printList(customerClient.getCustomers());
+
+ //test insert
+ System.out.println();
+ System.out.println("Result:insert new customer");
+ customerClient.addCustomer();
+ printList(customerClient.getCustomers());
+
+ //test update
+ System.out.println();
+ System.out.println("Result:update first customer");
+ customerClient.changeFirstCustomerName();
+ printList(customerClient.getCustomers());
+
+ //test delete
+ System.out.println();
+ System.out.println("Result:delete last customer");
+ customerClient.deleteCustomer();
+ printList(customerClient.getCustomers());
}
}
Added:
incubator/tuscany/java/das/samples/customer/src/main/java/org/apache/tuscany/samples/das/customer/CustomerDatabaseInitializer.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/das/samples/customer/src/main/java/org/apache/tuscany/samples/das/customer/CustomerDatabaseInitializer.java?rev=573058&view=auto
==============================================================================
---
incubator/tuscany/java/das/samples/customer/src/main/java/org/apache/tuscany/samples/das/customer/CustomerDatabaseInitializer.java
(added)
+++
incubator/tuscany/java/das/samples/customer/src/main/java/org/apache/tuscany/samples/das/customer/CustomerDatabaseInitializer.java
Wed Sep 5 14:10:53 2007
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.tuscany.samples.das.customer;
+
+import org.apache.tuscany.das.rdb.config.Config;
+import org.apache.tuscany.das.rdb.util.ConfigUtil;
+import org.apache.tuscany.samples.das.databaseSetup.DerbySetup;
+import org.apache.tuscany.samples.das.databaseSetup.MySQLSetup;
+
+public class CustomerDatabaseInitializer {
+ public static final String DERBY = "derby";
+ public static final String MYSQL = "mysql";
+
+ private final Config config;
+
+ private final String dbType;
+ private final String dbURL;
+ private final String user;
+ private final String password;
+
+
+
+ public CustomerDatabaseInitializer(String configFile) {
+ this.config =
ConfigUtil.loadConfig(this.getClass().getClassLoader().getResourceAsStream(configFile));
+
+
if(config.getConnectionInfo().getConnectionProperties().getDriverClass().indexOf(DERBY)
!= -1) {
+ dbType = DERBY;
+ } else
if(config.getConnectionInfo().getConnectionProperties().getDriverClass().indexOf(MYSQL)
!= -1) {
+ dbType = MYSQL;
+ } else {
+ dbType = null;
+ }
+
+ //get connection info from config
+ dbURL =
config.getConnectionInfo().getConnectionProperties().getDatabaseURL();
+ user =
config.getConnectionInfo().getConnectionProperties().getUserName();
+ password =
config.getConnectionInfo().getConnectionProperties().getPassword();
+ }
+
+
+
+ /**
+ * If database is present connect to it and create necessary tables,
procedures, data etc.
+ * If database is not present create database and then create the above
elements. Create database
+ * is implemented for MySQL and Derby.
+ */
+ public void Initialize() {
+
+ //display DB configuration iformation
+ DisplayDatabaseConfiguration();
+
+ //initialize DB
+ try {
+ if(dbType.equals(DERBY)){
+ new DerbySetup(dbURL+"-"+user+"-"+password);
+ }
+
+ if(dbType.equals(MYSQL)){
+ new MySQLSetup(dbURL+"-"+user+"-"+password);
+ }
+
+ } catch(Exception e){
+ throw new RuntimeException("Error initializing database !", e);
+ }
+ }
+
+ public void DisplayDatabaseConfiguration() {
+
+ System.out.println("************* Initializing database
*************");
+ System.out.println("** DB type : " + dbType );
+ System.out.println("** Database : " + dbURL );
+ System.out.println("** User : " + user );
+ System.out.println("** Password : " + password);
+ System.out.println("************************************************");
+ }
+}
Propchange:
incubator/tuscany/java/das/samples/customer/src/main/java/org/apache/tuscany/samples/das/customer/CustomerDatabaseInitializer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/tuscany/java/das/samples/customer/src/main/java/org/apache/tuscany/samples/das/customer/CustomerDatabaseInitializer.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added:
incubator/tuscany/java/das/samples/customer/src/main/resources/log4j.properties
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/das/samples/customer/src/main/resources/log4j.properties?rev=573058&view=auto
==============================================================================
---
incubator/tuscany/java/das/samples/customer/src/main/resources/log4j.properties
(added)
+++
incubator/tuscany/java/das/samples/customer/src/main/resources/log4j.properties
Wed Sep 5 14:10:53 2007
@@ -0,0 +1,36 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you 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.
+#
+# Set root logger level to DEBUG and its only appender to A1.
+log4j.rootLogger=INFO, NULL
+
+# NULL Appender
+log4j.appender.NULL=org.apache.log4j.varia.NullAppender
+
+log4j.appender.NULL.layout=org.apache.log4j.PatternLayout
+log4j.appender.NULL.layout.ConversionPattern=[DAS RDB] - %c{1}.%M (%L) : %m %n
+
+# CONSOLE is set to be a ConsoleAppender.
+log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
+
+# A1 uses PatternLayout.
+log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
+log4j.appender.CONSOLE.layout.ConversionPattern=[DAS RDB] - %c{1}.%M (%L) : %m
%n
+
+
+# Print only messages of level WARN or above in the package com.foo.
+log4j.logger.org.apache.tuscany=NONE
\ No newline at end of file
Propchange:
incubator/tuscany/java/das/samples/customer/src/main/resources/log4j.properties
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/tuscany/java/das/samples/customer/src/main/resources/log4j.properties
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange:
incubator/tuscany/java/das/samples/customer/src/main/resources/log4j.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]