Struts DataSource Manager



Home

Features

Documentation

Installation

Using

FAQ

Download

Links

License

Contacts

Configuration

Base configuration

1) First of all you load the plug-in through the struts-config.xml. Just put this XML piece:


<struts-config>

....

<plug-in className="net.sourceforge.sdsmanager.SDSManagerPlugIn">

<set-property property="sdsmanager-config" value="/WEB-INF/sdsmanager-config.xml" />

</plug-in>

</struts-config>


Obviously you must set the "value" attribute of the "set-property" element accordingly to your configuration file path.

2) Now you must configure the DataSource you need. Here is an example:

<!DOCTYPE sdsmanager-config PUBLIC "-//Antonio Petrelli//DTD Struts DataSource Manager//EN" "http://sdsmanager.sourceforge.net/dtds/sdsmanager-config.dtd">


<sdsmanager-config>

<data-sources>

<data-source type="org.apache.commons.dbcp.BasicDataSource">

<set-property property="driverClassName" value="org.firebirdsql.jdbc.FBDriver"/>

<set-property property="url" value="jdbc:firebirdsql://localhost//home/antonio/MyDB.fdb"/>

<set-property property="username" value="username"/>

<set-property property="password" value="password"/>

<set-property property="autoCommit" value="true"/>

<set-property property="pingQuery" value="SELECT COUNT(*) FROM MYTABLE"/>

</data-source>

</data-sources>

</sdsmanager-config>


As you can see, it is a normal DataSource configuration, just as you did inside the "struts-config.xml" file. For this reason, if you have a DataSource configured in "struts-config.xml", you have only to cut and paste the "data-sources" piece inside the new XML file.

For documentation about basic DataSource configuration, see the docs in Struts ;-)


Advanced configuration

Now the good news. How many times did you ask yourself "Why do I have to get a DataSource inside an Action? Action is Controller and DataSource is Model, it is an overlapping of competence, right?"

It's time to change. Now you can configure a DataSource so that it will be used inside business delegates. OK, probably there is space for improvement, but now this is all I need in my projects.

1) Create a business delegate that implements the "net.sourceforge.sdsmanager.business.DataSourceUser" interface.

There are two methods in this interface:

public void setDataSource(DataSource ds);

For setting the default DataSource. In fact it will be never called, but it can be useful, so I put it :-P

public void setDataSource(String key, DataSource ds);

For setting a keyed DataSource. The key is the same specified in the XML file or (if not specified) it's the default key used in Struts, i.e.:

Globals.DATA_SOURCE_KEY

2) Be sure that your delegate is a singleton, with a method:

public static void getInstance() {...}

that returns the only instance of the delegate itself.

3) Configure the delegate in the "sdsmanager-config.xml" file this way:

<!DOCTYPE sdsmanager-config PUBLIC "-//Antonio Petrelli//DTD Struts DataSource Manager//EN" "http://sdsmanager.sourceforge.net/dtds/sdsmanager-config.dtd">


<sdsmanager-config>

<data-sources>

<data-source key="mykey" type="org.apache.commons.dbcp.BasicDataSource">

<set-property ....

</data-source>

</data-sources>

<data-source-mappings>

<data-source-mapping key="mykey">

<user type="net.sourceforge.sdsmanager.test.business.TestDelegate" pattern="singleton" />

</data-source-mapping>

</data-source-mappings>

</sdsmanager-config>


This way you assign the DataSource keyed with "mykey" to the singleton "TestDelegate".

Notice that for now only the pattern "singleton" is supported.

You can also avoid to specify the "key" attribute if you use an unkeyed DataSource. In this case, the unkeyed DataSource will be passed to the delegate.


Reloading the configuration

Suppose that you modify the DataSource configuration (either base or advanced, or both), for example by using some administration tool, or simply a bunch of Actions and JSP pages that can modify the "sdsmanager-config.xml".

You can reload the configuration without reloading the entire application in this ways:

1) By calling the "net.sourceforge.sdsmanager.action.ReloadAllAction".

Obviously you have to configure this action, for example this way:

<action path="/reloadAllDataSources"

type="net.sourceforge.sdsmanager.action.ReloadAllAction">

<forward name="success" path="/test.jsp" redirect="false" />

<forward name="failure" path="/error.jsp" redirect="false" />

</action>


Remember to configure the local forwards "success" and "failure"!

2) By calling the "reloadAll" method of "net.sourceforge.sdsmanager.SDSManagerPool" singleton class.

(You have to call "getInstance()" to get the single instance).

3) You can reload only a specific SDSManager. An instance of SDSManager will be created for each module. You can get the SDSManager instances bound to a configuration file (e.g. sdsmanager-config.xml) by calling the method "getManagerCollectionByPath" in SDSManagerPool and then, for each item in the returned collection (of class SDSManager), call the "reload" method.