leosutic 2002/06/29 07:47:48 Modified: microcontainer/src/xdocs index.xml microcontainer/src/xdocs/stylesheets project.xml Added: microcontainer/src/xdocs tutorial.xml ShowDatabases.java Log: Initial check-in of the "Extremely Quick and Easy" tutorial. Revision Changes Path 1.3 +107 -22 jakarta-avalon-excalibur/microcontainer/src/xdocs/index.xml Index: index.xml =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/microcontainer/src/xdocs/index.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- index.xml 27 Jun 2002 14:34:05 -0000 1.2 +++ index.xml 29 Jun 2002 14:47:48 -0000 1.3 @@ -1,30 +1,115 @@ <?xml version="1.0"?> <document> <properties> - <title>Excalibur I/O Extensions - Overview</title> - <author email="[EMAIL PROTECTED]">Avalon Documentation Team</author> + <title>Overview</title> + <author email="mailto:[EMAIL PROTECTED]">Leo Sutic</author> </properties> <body> - <section name="Introduction"> - <p>Avalon Excalibur's Input/Output utilities are in package - <code>org.apache.avalon.excalibur.io</code>.</p> - - <subsection name="Copy methods"> - <p>Excalibur's IO package contains a comprehensive set of routines for - copying bytes and chars. Routines exist for copying from: - <code>String</code>, <code>byte[]</code>, <code>Reader</code> and - <code>InputStream</code>, to:<code>String</code>, <code>byte[]</code>, - <code>Writer</code> and <code>OutputStream</code>. - </p> - </subsection> - - <subsection name="Useful java.io.FileFilters"> - <p> - Excalibur includes a number of FileFilters that you can use for your - own purposes. This fills a gap in the Java runtime because the - interface was specified but no implementations were given. - </p> - </subsection> + <section name="What is MicroContainer?"> + <p> + MicroContainer's purpose is to provide a way to use Avalon + components without having to commit to the Avalon architecture. + </p> + + <p> + It does so by: + </p> + + <ul> + <li> + <p> + <b>Using standard Java idioms whenever possible.</b> Use of Avalon components + should be as similar as possible to the use of standard Java classes. + </p> + </li> + <li> + <p> + <b>Removing the need for XML assembly and/or deployment descriptors.</b> Having + those means that you are forced to include XML parsing ability and need to + integrate the configuration with your own existing architecture. + </p> + </li> + <li> + <p> + <b>Removing the need to center the application around the component container.</b> + Most Avalon applications are centered around a component container, such as Fortress, + Phoenix or the Excalibur Component Manager (Cocoon). By removing that need, it + is easier to just pick an Avalon component and have it work with your existing + architecture. <i>A MicroContainer will fit anywhere.</i> + </p> + </li> + </ul> + </section> + + <section name="Getting Started"> + <p> + Does the above sound like something you'd like to use? Great! + </p> + + <ul> + <li> + <p> + You can download + the latest development JAR from <a href="http://gump.covalent.net/jars/latest/jakarta-avalon-excalibur/"> + http://gump.covalent.net/jars/latest/jakarta-avalon-excalibur/</a>. Scroll down and you'll see a + file named <code>excalibur-microcontainer-YYYYMMDD.jar</code>, with YYYYMMDD being the date the + jar was built. + </p> + </li> + <li> + <p> + If you do not have it already, you should grab a copy of <a href="http://gump.covalent.net/jars/latest/jakarta-avalon/avalon-framework.jar">avalon-framework.jar</a>. + </p> + </li> + <li> + <p> + If you want to try the DataSource sample of the <a href="tutorial.html">tutorial</a>, you should go to: + <a href="http://gump.covalent.net/jars/latest/jakarta-avalon-excalibur/"> + http://gump.covalent.net/jars/latest/jakarta-avalon-excalibur/</a> and download + <code>excalibur-datasource-YYYYMMDD.jar</code>, <code>excalibur-pool-YYYYMMDD.jar</code> and <code>excalibur-collections-YYYYMMDD.jar</code>. + As with the MicroContainer JAR, the YYYYMMDD will be the date the JAR was built. + </p> + </li> + </ul> + </section> + + <section name="Project Status"> + <p> + MicroContainer has not yet had an official release. What this means for you is: + </p> + + <ul> + <li> + <p> + <b>The API may change.</b> While every effort will be made to keep API changes to + a minimum and to always provide backwards binary compatibility, some times + a developer must admit that he was going about it the wrong way and needs + to restructure parts of the project. + </p> + </li> + <li> + <p> + <b>The documentation isn't quite complete.</b> + </p> + </li> + <li> + <p> + <b>You should hide your use of MicroContainer behind factory methods.</b> + See the <a href="tutorial.html">tutorial</a> for an example of how to do + this. If you do so, API changes will impact you minimally should they + happen. + </p> + </li> + </ul> + </section> + + <section name="Needing Help?"> + <p> + So it stubbornly refuses to work, does it? Defiantly throwing Exceptions all over + your code? MicroContainer, and all other Avalon projects are discussed in the + <a href="http://jakarta.apache.org/site/mail.html">The Avalon User List</a>. + Subscribe to it and post your question there. + </p> </section> </body> </document> 1.1 jakarta-avalon-excalibur/microcontainer/src/xdocs/tutorial.xml Index: tutorial.xml =================================================================== <?xml version="1.0"?> <document> <properties> <title>Extremely Quick and Easy Tutorial</title> <author email="mailto:[EMAIL PROTECTED]">Leo Sutic</author> </properties> <body> <section name="Extremely Quick and Easy Tutorial"> <p> Welcome to the <b>Extremely Quick and Easy Tutorial</b>. This is what it consists of: </p> <ul> <li> <p> First, we'll make sure you have all the required JAR files. </p> </li> <li> <p> Second, we'll set up your CLASSPATH correctly. </p> </li> <li> <p> After that, a short discussion of what makes an Avalon component different from a regular Java class, and what MicroContainer does. </p> </li> <li> <p> Fourth, an example that uses the Excalibur DataSource to list all databases in a MySQL installation. </p> </li> <li> <p> And finally, we'll take a look at the factory that produces a MicroContainer for the DataSource. </p> </li> </ul> </section> <section name="Getting the JAR Files"> <p> You need the following JAR files: </p> <ul> <li> <p> <b>excalibur-microcontainer</b>: You can download the latest development JAR from <a href="http://gump.covalent.net/jars/latest/jakarta-avalon-excalibur/"> http://gump.covalent.net/jars/latest/jakarta-avalon-excalibur/</a>. Scroll down and you'll see a file named <code>excalibur-microcontainer-YYYYMMDD.jar</code>, with YYYYMMDD being the date the jar was built. </p> </li> <li> <p> <b>avalon-framework</b>: If you do not have it already, you should grab a copy of <a href="http://gump.covalent.net/jars/latest/jakarta-avalon/avalon-framework.jar">avalon-framework.jar</a>. </p> </li> <li> <p> <b>excailbur-datasource</b>: Go to: <a href="http://gump.covalent.net/jars/latest/jakarta-avalon-excalibur/"> http://gump.covalent.net/jars/latest/jakarta-avalon-excalibur/</a> and download</p> <ul> <li> <p> <code>excalibur-datasource-YYYYMMDD.jar</code> </p> </li> <li> <p> <code>excalibur-pool-YYYYMMDD.jar</code> </p> </li> <li> <p> <code>excalibur-collections-YYYYMMDD.jar</code> </p> </li> </ul> <p> As with the MicroContainer JAR, the YYYYMMDD will be the date the JAR was built. </p> </li> <li> <p> You also need a JAR with drivers for your database. If you, like me, use MySQL, you can find drivers at <a href="http://mmmysql.sourceforge.net/">http://mmmysql.sourceforge.net/</a>. </p> <ul> <li> <p> Click on the <b>download</b> link and select <code>mm.mysql-2.0.9-you-must-unjar-me.jar</code>. Note the version number - the other downloads are source distributions. </p> </li> <li> <p> Unjar the file you just downloaded and find the file <code>mm.mysql-2.0.9-bin.jar</code>. This is the file containing DB drivers for MySQL. </p> </li> </ul> </li> </ul> </section> <section name="Setting up your CLASSPATH"> <p> Now that you have all JAR files, create a directory for the tutorial and copy the files into it. </p> <source><![CDATA[ C:\microcontainertutorial>dir Volume in drive C has no label. Volume Serial Number is 6C66-7072 Directory of C:\microcontainertutorial 2002-06-29 16:26 <DIR> . 2002-06-29 16:26 <DIR> .. 2002-06-29 16:26 59 369 avalon-framework.jar 2002-06-29 16:26 33 563 excalibur-collections-20020628.jar 2002-06-29 16:26 42 775 excalibur-datasource-20020628.jar 2002-06-29 16:25 29 623 excalibur-microcontainer-20020628.jar 2002-06-29 16:26 44 453 excalibur-pool-20020628.jar 2002-01-19 10:55 121 015 mm.mysql-2.0.9-bin.jar 7 File(s) 330 798 bytes 2 Dir(s) 11 774 013 440 bytes free C:\microcontainertutorial>]]></source> <p>You need to include all the above jars in your classpath.</p> <source><![CDATA[ SET CLASSPATH=avalon-framework.jar;excalibur-collections-20020628.jar SET CLASSPATH=%CLASSPATH%;excalibur-datasource-20020628.jar SET CLASSPATH=%CLASSPATH%;excalibur-microcontainer-20020628.jar SET CLASSPATH=%CLASSPATH%;excalibur-pool-20020628.jar;mm.mysql-2.0.9-bin.jar SET CLASSPATH=%CLASSPATH%;. ]]></source> <p> Note that the last SET command includes the current directory in the classpath. </p> </section> <section name="What Makes an Avalon Component so Special?"> <p> In five words: <b>It expects to be managed.</b> </p> <p> When you use a regular Java class, you would typically call its constructor and then start using it. Basically, as soon as you have an instance of the class, it is good to go. </p> <p> Avalon components are different. For example, if the component class implements the <code>org.apache.avalon.framework.activity.Initializable</code> interface, it will have an <code>initialize()</code> method that <b>must</b> be called before the component is ready to be used. Another difference is that all components have a no-argument constructor. Those components that have configuration parameters will implement the <code>org.apache.avalon.framework.configuration.Configurable</code> interface, which defines a single method: <code>configure( Configuration config );</code>. The component then expects to recieve all configuration parameters via that method. </p> <p> This is why a <b>container</b> is needed. The container is responsible for calling the right methods with the correct parameters, and in general manage the component the way it expects to be managed. </p> <p> MicroContainer's goal is to provide such a container that can easily be used in any kind of program, and to have minimal impact on your existing code. In particular, MicroContainer is special because <b>it only manages a single component</b>. All the other containers manage several components. MicroContainer manages one. The other containers solve the use case where one component uses another by linking them up inside the container - MicroContainer allows you to link several MicroContainers together for the same purpose. </p> </section> <section name="Accessing a MySQL Database with MicroContainer"> <p> Now that we have a good idea of what makes Avalon components special, let's get down to practicalities. In particular, how would one use MicroContainer in combination with the Excalibur DataSource to access a MySQL database? </p> <p> You can download the sample <a href="ShowDatabases.java">here</a>. Starting with the <code>import</code> declarations, here's the sample code: </p> <source><![CDATA[import java.sql.Connection; import java.sql.Statement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.StringTokenizer; import org.apache.avalon.excalibur.datasource.DataSourceComponent; import org.apache.avalon.excalibur.datasource.JdbcDataSource; import org.apache.excalibur.microcontainer.MicroContainer; import org.apache.excalibur.microcontainer.util.DataSourceFactory; /** * Sample code to show all databases in a MySQL installation. */ public class ShowDatabases {]]></source> <p> For now, we will use the <code>org.apache.excalibur.microcontainer.util.DataSourceFactory</code> to create a MicroContainer for a <code>org.apache.avalon.excalibur.datasource.JdbcDataSource</code>. This is just to quickly get something that can run. As soon as this version runs, we will dig into the <code>DataSourceFactory</code> class and see how to create a MicroContainer. </p> <p> Proceeding with the sample code, here's the top of the <code>main</code> method: </p> <source><![CDATA[ /** * Main method. */ public static void main( String[] args ) throws Exception { String driver = "org.gjt.mm.mysql.Driver"; String dbUrl = "jdbc:mysql://localhost/"; String query = "SHOW DATABASES;"; String userName = "sa"; String password = ""; System.out.println( "Attempting to create a DataSourceComponent with the following parameters:" ); System.out.println( " Driver: " + driver ); System.out.println( " DB URL: " + dbUrl ); System.out.println( "User name: " + userName ); System.out.println( " Password: " + (password != null ? "(hidden)" : "(not specified)") );]]></source> <p> Some fairly basic initialization followed by a status message. You should edit the parameters to fit your MySQL configuration. </p> <p> Now we come to the creation of a DataSourceComponent. </p> <source><![CDATA[ DataSourceComponent ds = DataSourceFactory.getDataSource( driver, // JDBC Driver dbUrl, // Database URL userName, // user name password, // user's password null, // Connection keep-alive command (none) 1, // Minimum number of connections in pool. 3, // Maximum number of connections in pool. -1, // Connection timeout (disabled) true, // Auto commit false, // Using oracle (only relevant when supplying a keep-alive command) null); // Override name of connection class (null == use default).]]></source> <p> A bit of pure magic hidden inside <code>DataSourceFactory.getDataSource()</code> for now. The method takes creation parameters for the DataSource, and returns a MicroContainer that implements the <code>DataSourceComponent</code>. This is important to remember: <b>The object returned by the <code>getDataSource</code> method is a MicroContainer, and not an instance of the Avalon component.</b> Via dynamic proxies, the MicroContainer will implement all interfaces that the component does, thus enabling it to be used just as a component, but keep in mind that it is a proxy. </p> <p> Finally, the code to get a connection, execute a query, and close everything down. </p> <source><![CDATA[ Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = ds.getConnection(); stmt = conn.createStatement(); System.out.println( "\nExecuting query: " + query + "\n" ); rs = stmt.executeQuery( query ); System.out.println( "The following databases were found:" ); while (rs.next()) { System.out.println( rs.getObject( 1 ) ); } } finally { if( rs != null ) { rs.close(); } if( stmt != null ) { stmt.close(); } if( conn != null ) { conn.close(); } MicroContainer.release(ds); } } }]]></source> <p> The above is some fairly straight JDBC access. Do, however, note the <code>MicroContainer.release(ds);</code> at the bottom. As I said above, Avalon components expect to be managed during their initialization phase. They also expect to be managed when shutting down. That's why all MicroContainers must be explicitly released. </p> <p> At this point, you should compile and test the code with </p> <source>javac *.java</source> <p> and </p> <source>java ShowDatabases</source> <p> If it works, it should result in output similar to this: </p> <source><![CDATA[Attempting to create a DataSourceComponent with the following parameters: Driver: org.gjt.mm.mysql.Driver DB URL: jdbc:mysql://localhost/ User name: sa Password: (hidden) Executing query: SHOW DATABASES; The following databases were found: mysql sample test ]]> </source> </section> <section name="The DataSourceFactory.getDataSource() Method"> <p> The problem of creating a suitable MicroContainer for a DataSource boils down to this: Since the JdbcDataSource class (which we will use) implements <code>Configurable</code>, it expects configuration parameters in the form of a <code>Configuration</code> object, and we must create a suitable such object for it. All other aspects of management can be handled by the MicroContainer. </p> <p> Looking at the Javadoc for <code>org.apache.avalon.excalibur.datasource.JdbcDataSource</code> we see that the components expects a configuration like this: </p> <source><![CDATA[ <configuration> <pool-controller min="5" max="10" connection-class="my.overrided.ConnectionClass"> <keep-alive disable="false">select 1</keep-alive> </pool-controller> <auto-commit>true</auto-commit> <driver>com.database.jdbc.JdbcDriver</driver> <dburl>jdbc:driver://host/mydb</dburl> <user>username</user> <password>password</password> </configuration> ]]></source> <p> One thing that is apparent from the configuration above is that it uses the value part of the configurations to pass parameters and not the attributes. That is, instead of having <code><user name="username"/></code>, it has <code><user>username</user></code>. So we'll create utility method to create such configuration elements first. </p> <source><![CDATA[ import org.apache.avalon.excalibur.datasource.DataSourceComponent; import org.apache.avalon.excalibur.datasource.JdbcDataSource; import org.apache.avalon.framework.configuration.DefaultConfiguration; import org.apache.excalibur.microcontainer.MicroContainer; import org.apache.excalibur.microcontainer.MicroContainerException; public class DataSourceFactory { private static final DefaultConfiguration configurationWithValue( String name, String value ) { DefaultConfiguration child = new DefaultConfiguration( name, "" ); child.setValue( value ); return child; } ]]></source> <p> The method will create a <code>DefaultConfiguration</code>, sets its value and return it. For example, to create <code><user>username</user></code>, we would call it thus: </p> <source><![CDATA[ configurationWithValue( "user", "username" ) ]]> </source> <p> Right then, here's the parts that creates the configuration object: </p> <source><![CDATA[ public static DataSourceComponent getDataSource( String driver, String dbUrl, String user, String password, String keepAliveCommand, int poolMin, int poolMax, long timeout, boolean autoCommit, boolean useOracle, String connectionClass ) throws MicroContainerException { DefaultConfiguration config = new DefaultConfiguration( "", "" ); config.addChild( configurationWithValue( "driver", driver ) ); config.addChild( configurationWithValue( "dburl", dbUrl ) ); config.addChild( configurationWithValue( "user", user ) ); config.addChild( configurationWithValue( "password", password ) ); config.addChild( configurationWithValue( "auto-commit", String.valueOf( autoCommit ) ) ); DefaultConfiguration controller = new DefaultConfiguration( "pool-controller", "" ); controller.setAttribute( "min", String.valueOf( poolMin ) ); controller.setAttribute( "max", String.valueOf( poolMax ) ); controller.setAttribute( "timeout", String.valueOf( timeout ) ); controller.setAttribute( "oradb", String.valueOf( useOracle ) ); if( connectionClass != null ) { controller.setAttribute( "connection-class", connectionClass ); } DefaultConfiguration keepAlive = new DefaultConfiguration( "keep-alive", "" ); if( keepAliveCommand != null ) { keepAlive.setValue( keepAliveCommand ); } else { keepAlive.setAttribute( "disable", "true" ); } controller.addChild( keepAlive ); config.addChild( controller ); ]]> </source> <p> You are probably recoiling in shock and horror at the above. After all, MicroContainer was supposed to reduce complexity, wasn't it? Well, creating a configuration instance programmatically is dirty. There's just not many other ways to do it. Second, the <code>JdbcDataSource</code> class has a fairly massive configuration with lots of parameters. </p> <p> (If there were any other component with simpler configuration and that yet was as easy to write a sample program for, I would have used it here instead.) </p> <p> The DataSource component we wanted to use required a configuration. We have one now, and can thus go on to create the actual MicroContainer: </p> <source><![CDATA[ return (DataSourceComponent) new MicroContainer( JdbcDataSource.class ).configuration( config ).create(); ]]> </source> <p> And that was it. Note that the <code>MicroContainer</code> class follows the named parameter idiom. </p> </section> <section name="Needing Help?"> <p> So it stubbornly refuses to work, does it? Defiantly throwing Exceptions all over your code? MicroContainer, and all other Avalon projects are discussed in the <a href="http://jakarta.apache.org/site/mail.html">The Avalon User List</a>. Subscribe to it and post your question there. </p> </section> </body> </document> 1.1 jakarta-avalon-excalibur/microcontainer/src/xdocs/ShowDatabases.java Index: ShowDatabases.java =================================================================== import java.sql.Connection; import java.sql.Statement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.StringTokenizer; import org.apache.avalon.excalibur.datasource.DataSourceComponent; import org.apache.avalon.excalibur.datasource.JdbcDataSource; import org.apache.excalibur.microcontainer.MicroContainer; import org.apache.excalibur.microcontainer.util.DataSourceFactory; /** * Sample code to show all databases in a MySQL installation. * * @author <a href="mailto:[EMAIL PROTECTED]">Leo Sutic</a> */ public class ShowDatabases { /** * Main method. */ public static void main( String[] args ) throws Exception { String driver = "org.gjt.mm.mysql.Driver"; String dbUrl = "jdbc:mysql://localhost/"; String query = "SHOW DATABASES;"; String userName = "sa"; String password = ""; System.out.println( "Attempting to create a DataSourceComponent with the following parameters:" ); System.out.println( " Driver: " + driver ); System.out.println( " DB URL: " + dbUrl ); System.out.println( "User name: " + userName ); System.out.println( " Password: " + (password != null ? "(hidden)" : "(not specified)") ); DataSourceComponent ds = DataSourceFactory.getDataSource( driver, // JDBC Driver dbUrl, // Database URL userName, // user name password, // user's password null, // Connection keep-alive command (none) 1, // Minimum number of connections in pool. 3, // Maximum number of connections in pool. -1, // Connection timeout (disabled) true, // Auto commit false, // Using oracle (only relevant when supplying a keep-alive command) null); // Override name of connection class (null == use default). Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = ds.getConnection(); stmt = conn.createStatement(); System.out.println( "\nExecuting query: " + query + "\n" ); rs = stmt.executeQuery( query ); System.out.println( "The following databases were found:" ); while (rs.next()) { System.out.println( rs.getObject( 1 ) ); } } finally { if( rs != null ) { rs.close(); } if( stmt != null ) { stmt.close(); } if( conn != null ) { conn.close(); } MicroContainer.release(ds); } } } 1.2 +18 -13 jakarta-avalon-excalibur/microcontainer/src/xdocs/stylesheets/project.xml Index: project.xml =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/microcontainer/src/xdocs/stylesheets/project.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- project.xml 27 Jun 2002 14:35:32 -0000 1.1 +++ project.xml 29 Jun 2002 14:47:48 -0000 1.2 @@ -1,16 +1,21 @@ <?xml version="1.0" encoding="UTF-8"?> <project name="Excalibur MicroContainer" href="http://jakarta.apache.org/avalon/excalibur/microcontainer"> - - <title>Excalibur MicroContainer</title> - <body> - <item href="@AVALON_BASE@" name="Back to Avalon"/> - <item href="/../index.html" name="Back to Excalibur"/> - - <menu name="About"> - <item name="Overview" href="/index.html"/> - <item name="Excalibur Home" href="http://jakarta.apache.org/avalon/excalibur/index.html"/> - <item name="Download" href="http://jakarta.apache.org/builds/jakarta-avalon-excalibur/release/microcontainer"/> - <item name="API Docs" href="/api/index.html"/> - </menu> - </body> + + <title>Excalibur MicroContainer</title> + <body> + <item href="@AVALON_BASE@" name="Back to Avalon"/> + <item href="/../index.html" name="Back to Excalibur"/> + + <menu name="About"> + <item name="Overview" href="/index.html"/> + <item name="Excalibur Home" href="http://jakarta.apache.org/avalon/excalibur/index.html"/> + <item name="Download" href="http://jakarta.apache.org/builds/jakarta-avalon-excalibur/release/microcontainer"/> + <item name="API Docs" href="/api/index.html"/> + <item name="Extremely Quick and Easy Tutorial" href="/tutorial.html"/> + </menu> + + <menu name="Developers"> + <item name="Things to Keep in Mind" href="/keepinmind.html"/> + </menu> + </body> </project>
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>