You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by kw...@apache.org on 2006/06/14 23:33:42 UTC

svn commit: r414389 - in /incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb: DAS.java DASFactory.java impl/DASFactoryImpl.java impl/DASImpl.java

Author: kwilliams
Date: Wed Jun 14 14:33:42 2006
New Revision: 414389

URL: http://svn.apache.org/viewvc?rev=414389&view=rev
Log:
The new files for T-469

Added:
    incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DAS.java   (with props)
    incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DASFactory.java   (with props)
    incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASFactoryImpl.java   (with props)
    incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASImpl.java   (with props)

Added: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DAS.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DAS.java?rev=414389&view=auto
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DAS.java (added)
+++ incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DAS.java Wed Jun 14 14:33:42 2006
@@ -0,0 +1,89 @@
+package org.apache.tuscany.das.rdb;
+
+import java.io.InputStream;
+import java.sql.Connection;
+
+import org.apache.tuscany.das.rdb.config.Config;
+import org.apache.tuscany.das.rdb.impl.DASFactoryImpl;
+
+import commonj.sdo.DataObject;
+
+/**
+ * A CommandGroup represents a set of {@link Command} and single
+ * {@link ApplyChangesCommand} that are created from a common config file.
+ * 
+ * 
+ */
+public interface DAS {
+
+    public static DASFactory FACTORY = new DASFactoryImpl();
+
+	/**
+	 * The change history is scanned and modifications to the graph of data
+	 * objects are flushed to the database.
+	 * 
+	 * @param root
+	 *            the topmost containing data object
+	 */
+	public void applyChanges(DataObject root);
+  
+
+    /**
+     * Gets the named command from this factory's inventory
+     * 
+     * @param name
+     *            The identifying name of the requested command
+     * @return Returns the identified command
+     */
+    public Command getCommand(String name);
+
+    /**
+     * Set the default connection for all command in the group
+     * 
+     * @param connection
+     */
+    public void setConnection(Connection connection);
+    
+    /**
+     * If the CommandGroup is managing connections then this method
+     * must be called when the client is done with the instance.
+     * 
+     */
+    public void releaseResources();
+    
+    
+    // begin from CommandFactory
+    
+    /**
+     * Creates a Command based on the provided SQL statement
+     * 
+     * @param sql
+     *            The SQL statement
+     * @return returns a Command instance
+     */
+    public Command createCommand(String sql);
+
+    /**
+     * Creates a Command based on the provided SQL statement and configuration
+     * 
+     * @param sql
+     *            The SQL statement
+     * @param mappingModel
+     *            The congiguration as XML file stream
+     * @return returns a COmmand instance
+     */
+    public Command createCommand(String sql, InputStream config);
+    
+    
+
+    /**
+     * Creates a Command based on the provided SQL statement and configuration
+     * 
+     * @param sql
+     *            The SQL statement
+     * @param config
+     *            The congiguration as Config instance
+     * @return returns a COmmand instance
+     */
+    public Command createCommand(String sql, Config config);
+}

Propchange: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DAS.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DAS.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DASFactory.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DASFactory.java?rev=414389&view=auto
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DASFactory.java (added)
+++ incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DASFactory.java Wed Jun 14 14:33:42 2006
@@ -0,0 +1,55 @@
+/**
+ *
+ *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  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.
+ */
+package org.apache.tuscany.das.rdb;
+
+import java.io.InputStream;
+
+import org.apache.tuscany.das.rdb.config.Config;
+
+/**
+ * A CommandGroupFactory produces {@link DAS} instances.
+ * 
+ * 
+ */
+public interface DASFactory {
+
+    /**
+     * Creates a CommandGroup based on the provided config file stream
+     * 
+     * @param configStream
+     *            A stream over a DAS config file
+     * @return returns a CommandGroup instance
+     */
+    public DAS createDAS(InputStream configStream);
+    
+    
+    /**
+     * Creates a CommandGroup based on the provided config
+     * 
+     * @param config
+     *            A DAS config object
+     * @return returns a CommandGroup instance
+     */
+    public DAS createDAS(Config config);
+
+
+	public DAS createDAS();
+    
+ 
+
+}
+

Propchange: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DASFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/DASFactory.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASFactoryImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASFactoryImpl.java?rev=414389&view=auto
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASFactoryImpl.java (added)
+++ incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASFactoryImpl.java Wed Jun 14 14:33:42 2006
@@ -0,0 +1,40 @@
+/**
+ *
+ *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  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.
+ */
+package org.apache.tuscany.das.rdb.impl;
+
+import java.io.InputStream;
+
+import org.apache.tuscany.das.rdb.DAS;
+import org.apache.tuscany.das.rdb.DASFactory;
+import org.apache.tuscany.das.rdb.config.Config;
+
+public class DASFactoryImpl implements DASFactory {
+
+	public DAS createDAS(InputStream configStream) {
+		return new DASImpl(configStream);
+	}
+
+	public DAS createDAS(Config config) {
+		return new DASImpl(config);
+	}
+	
+	public DAS createDAS() {
+		return new DASImpl();
+	}
+
+
+}

Propchange: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASFactoryImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASFactoryImpl.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASImpl.java?rev=414389&view=auto
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASImpl.java (added)
+++ incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASImpl.java Wed Jun 14 14:33:42 2006
@@ -0,0 +1,214 @@
+/**
+ *
+ *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  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.
+ */
+package org.apache.tuscany.das.rdb.impl;
+
+import java.io.InputStream;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+
+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 commonj.sdo.DataObject;
+
+/**
+ * An ConfiguredCommandFactory produces instances of Command and
+ * ApplyChangesCommand. This factory is initialized with a configuration that
+ * defines the commands it produces.
+ * 
+ */
+public class DASImpl implements DAS {
+
+    private Config config;
+
+    private Connection connection;
+
+    private Map commands = new HashMap();
+
+    public DASImpl(InputStream stream) {
+        this(ConfigUtil.loadConfig(stream));
+        
+    }
+
+    public DASImpl(Config inConfig) {
+    	this.config = inConfig;
+        
+        Iterator i = config.getCommand().iterator();
+        while (i.hasNext()) {
+            org.apache.tuscany.das.rdb.config.Command commandConfig = (org.apache.tuscany.das.rdb.config.Command) i
+                    .next();
+            String kind = commandConfig.getKind();
+            if (kind.equalsIgnoreCase("select"))                             
+                commands.put(commandConfig.getName(), new ReadCommandImpl(commandConfig.getSQL(), config));
+            else if (kind.equalsIgnoreCase("update"))
+                commands.put(commandConfig.getName(), new UpdateCommandImpl(commandConfig.getSQL()));
+            else if (kind.equalsIgnoreCase("insert"))
+                commands.put(commandConfig.getName(), new InsertCommandImpl(commandConfig.getSQL()));
+            else if (kind.equalsIgnoreCase("delete"))
+                commands.put(commandConfig.getName(), new DeleteCommandImpl(commandConfig.getSQL()));
+            else
+                throw new RuntimeException("Invalid kind of command: " + kind);
+
+        }
+
+    }
+
+    public DASImpl() {
+		// TODO Auto-generated constructor stub
+	}
+
+	/*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.tuscany.das.rdb.CommandGroup#getApplyChangesCommand()
+     */
+    public ApplyChangesCommandImpl getApplyChangesCommand() {
+        ApplyChangesCommandImpl cmd = new ApplyChangesCommandImpl(config, connection);
+        return cmd;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.tuscany.das.rdb.CommandGroup#getCommand(java.lang.String)
+     */
+    public Command getCommand(String name) {
+        if (!commands.containsKey(name))
+            throw new RuntimeException("CommandGroup has no command named: " + name);
+        Command cmd = (Command) commands.get(name);
+        cmd.setConnection(getConnection());
+        return cmd;
+    }
+
+
+    public void setConnection(Connection connection) {
+        this.connection = connection;
+    }
+
+    public Connection getConnection() {
+        if (connection == null)
+            initializeConnection();
+        return connection;
+    }
+
+    private void initializeConnection() {
+
+        String dataSource = config.getDataSource();
+        if (dataSource == null)
+            throw new RuntimeException(
+                    "No connection has been provided and no data source has been specified");
+
+        Connection connection = null;
+
+        InitialContext ctx;
+        try {
+            ctx = new InitialContext();
+        } catch (NamingException e) {
+            throw new RuntimeException(e);
+        }
+        try {
+            // TODO - I think we should rename this getDataSourceURL?
+            DataSource ds = (DataSource) ctx.lookup(dataSource);
+            try {
+                connection = ds.getConnection();
+                connection.setAutoCommit(false);
+                setConnection(connection);
+            } catch (SQLException e) {
+                throw new RuntimeException(e);
+            }
+        } catch (NamingException e) {
+            throw new RuntimeException(e);
+        }
+
+    }
+
+    public void releaseResources() {
+
+        if (managingConnections())
+            closeConnection();
+    }
+
+    private void closeConnection() {
+        if (connection != null)
+            try {
+                connection.close();
+                connection = null;
+            } catch (SQLException e) {
+                throw new RuntimeException(e);
+            }
+    }
+
+    /**
+     * If the config has connection properties then we are "managing" the
+     * connection via DataSource
+     */
+    private boolean managingConnections() {
+
+        if (config.getDataSource() == null)
+            return false;
+        else
+            return true;
+
+    }
+
+    public Command createCommand(String sql) {
+        return baseCreateCommand(sql, this.config);
+    }
+
+    public Command createCommand(String sql, InputStream configStream) {
+        return baseCreateCommand(sql, ConfigUtil.loadConfig(configStream));
+    }
+
+    public Command createCommand(String sql, Config config) {
+        return baseCreateCommand(sql, config);
+    }
+
+    private Command baseCreateCommand(String sql, Config config) {
+
+        sql = sql.trim(); // Remove leading white space
+        char firstChar = Character.toUpperCase(sql.charAt(0));
+        switch (firstChar) {
+        case 'S':
+            return new ReadCommandImpl(sql, config);
+        case 'I':
+            return new InsertCommandImpl(sql);
+        case 'U':
+            return new UpdateCommandImpl(sql);
+        case 'D':
+            return new DeleteCommandImpl(sql);
+        case '{':
+            return new SPCommandImpl(sql, config);
+        default:
+            throw new RuntimeException("SQL => " + sql + " is not valid");
+        }
+
+    }
+
+	public void applyChanges(DataObject root) {
+		getApplyChangesCommand().execute(root);			
+	}
+
+}
\ No newline at end of file

Propchange: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASImpl.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org