You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jm...@apache.org on 2004/06/11 04:43:48 UTC
cvs commit: jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl HibernateBasicMessage.java HibernateResources.java HibernateResourcesFactory.java JDBCResources.java JDBCResourcesFactory.java
jmitchell 2004/06/10 19:43:48
Added: resources/src/java/org/apache/commons/resources/impl
HibernateBasicMessage.java HibernateResources.java
HibernateResourcesFactory.java JDBCResources.java
JDBCResourcesFactory.java
Log:
Changes required to support move of JDBC and Hibernate impls.
Revision Changes Path
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/HibernateBasicMessage.java
Index: HibernateBasicMessage.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/HibernateBasicMessage.java,v 1.1 2004/06/11 02:43:48 jmitchell Exp $
* $Revision: 1.1 $
* $Date: 2004/06/11 02:43:48 $
*
* ====================================================================
*
* Copyright 2003-2004 The Apache Software Foundation
*
* 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.commons.resources.impl;
/**
* BasicMessage to allow standard Hibernate
* configuration (no complex keys).
*
* @author James Mitchell
*
*/
public class HibernateBasicMessage extends BasicMessage {
public HibernateBasicMessage() {
super();
}
/**
*
* @param id The id (primary key) for this message.
* @param locale The locale used to retrieve the value for the specified key
* @param key The key for this message.
* @param value The value for this message.
*/
public HibernateBasicMessage(String locale, String key, Object[] values) {
super(key, values);
this.locale = locale;
}
private String locale = null;
/**
* @return Returns the locale.
*/
public String getLocale() {
return locale;
}
/**
* @param locale The locale to set.
*/
public void setLocale(String locale) {
this.locale = locale;
}
public void setKey(String key) {
this.key = key;
}
public void setValue(String value) {
this.values = new String[]{value};
}
public String getValue() {
if (values == null || values.length < 1) {
throw new IllegalStateException("The retrived value for msg " +
this.key + "was null");
}
if (values.length > 1) {
throw new IllegalStateException("There were more than one values " +
"retrived value for msg " +
this.key);
}
return (String)getValues()[0];
}
public boolean equals(Object obj) {
if (obj instanceof HibernateBasicMessage){
if (((HibernateBasicMessage)obj).getKey().equals(this.key) &&
((HibernateBasicMessage)obj).getLocale().equals(this.locale)){
return true;
}
}
return false;
}
public int hashCode() {
return super.hashCode();
}
}
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/HibernateResources.java
Index: HibernateResources.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/HibernateResources.java,v 1.1 2004/06/11 02:43:48 jmitchell Exp $
* $Revision: 1.1 $
* $Date: 2004/06/11 02:43:48 $
*
* ====================================================================
*
* Copyright 2003-2004 The Apache Software Foundation
*
* 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.commons.resources.impl;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.resources.ResourcesException;
/**
* <p>Concrete implementation of
* {@link org.apache.commons.resources.Resources} that wraps a
* Hibernate session and retrieves values for the given
* <code>Locale</code> and have name suffixes reflecting the
* <code>Locale</code> for which the document's messages apply.
* For this specific implementation, resources are looked up in
* a hierarchy of database values in a manner identical to that
* performed by <code>java.util.ResourceBundle.getBundle().</code>.
* </p>
*
* <p>The base URL passed to our constructor must contain the base name
* of a the hibernate configuration file.</p>
*
* @author James Mitchell
* @version $Revision: 1.1 $ $Date: 2004/06/11 02:43:48 $
*/
public class HibernateResources extends CollectionResourcesBase {
/**
* <p>The <code>Log</code> instance for this class.</p>
*/
private static final Log log = LogFactory.getLog(HibernateResources.class);
// ----------------------------------------------------------- Constructors
/**
* <p>Create a new
* {@link org.apache.commons.resources.Resources} instance with the specified
* logical name and base resource URL.</p>
*
* @param name Logical name of the new instance
* @param base Base URL of the Hibernate configuration file.
*/
public HibernateResources(String name, String base) {
super(name, base);
}
// ------------------------------------------------------ Protected Methods
/**
* <p>Return a <code>Map</code> containing the name-value mappings for
* the specified base URL and requested <code>Locale</code>, if there
* are any. If there are no defined mappings for the specified
* <code>Locale</code>, return an empty <code>Map</code> instead.</p>
*
* <p>Concrete subclasses must override this method to perform the
* appropriate lookup. A typical implementation will construct an
* absolute URL based on the specified base URL and <code>Locale</code>,
* retrieve the specified resource file (if any), and parse it into
* a <code>Map</code> structure.</p>
*
* <p>Caching of previously retrieved <code>Map</code>s (if any) should
* be performed by callers of this method. Therefore, this method should
* always attempt to retrieve the specified resource and load it
* appropriately.</p>
*
* @param baseUrl Base URL of the resource files for this
* {@link org.apache.commons.resources.Resources} instance
* @param locale <code>Locale</code> for which name-value mappings
* are requested
*/
protected Map getLocaleMap(String baseUrl, Locale locale) {
if (log.isDebugEnabled()) {
log.debug("Loading database configuration '" + locale + "' resources from base '" +
baseUrl + "'");
}
Properties props = new Properties();
Session session = null;
try {
// TODO - change this to load the specified hibernate config file (baseUrl)
SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
Query q = session.getNamedQuery("QueryByLocale");
q.setString(0, locale.toString());
q.list();
List msgs = q.list();
for (Iterator iter = msgs.iterator(); iter.hasNext();) {
HibernateBasicMessage msg = (HibernateBasicMessage) iter.next();
props.put(msg.getKey(), msg.getValue());
}
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
return props;
}
public void init() throws ResourcesException {
super.init();
}
}
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/HibernateResourcesFactory.java
Index: HibernateResourcesFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/HibernateResourcesFactory.java,v 1.1 2004/06/11 02:43:48 jmitchell Exp $
* $Revision: 1.1 $
* $Date: 2004/06/11 02:43:48 $
*
* ====================================================================
*
* Copyright 2003-2004 The Apache Software Foundation
*
* 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.commons.resources.impl;
import org.apache.commons.resources.Resources;
import org.apache.commons.resources.ResourcesException;
/**
* <p>Concrete implementation of
* {@link org.apache.commons.resources.ResourcesFactory} that creates
* {@link org.apache.commons.resources.Resources} instances that wraps
* a Hibernate session and retrieves values for the given
* <code>Locale</code> and have name suffixes reflecting the
* <code>Locale</code> for which the document's messages apply.
* For this specific implementation, resources are looked up in
* a hierarchy of database values in a manner identical to that
* performed by <code>java.util.ResourceBundle.getBundle().</code>.
*
* @author James Mitchell
* @version $Revision: 1.1 $
*/
public class HibernateResourcesFactory extends ResourcesFactoryBase {
// ------------------------------------------------------ Protected Methods
/**
* <p>Create and return a new {@link org.apache.commons.resources.Resources}
* instance with the specified logical name, after calling its <code>init()</code>
* method and delegating the relevant properties.</p>
*
* @param name Logical name of the {@link org.apache.commons.resources.Resources}
* instance to create
*
* @param config Configuration string for this resource (if any)
*
* @exception ResourcesException if a {@link org.apache.commons.resources.Resources}
* instance of the specified logical name cannot be created.
*/
protected Resources createResources(String name, String config)
throws ResourcesException {
Resources res = new HibernateResources(name, config);
res.setReturnNull(isReturnNull());
res.init();
return (res);
}
}
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/JDBCResources.java
Index: JDBCResources.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/JDBCResources.java,v 1.1 2004/06/11 02:43:48 jmitchell Exp $
* $Revision: 1.1 $
* $Date: 2004/06/11 02:43:48 $
*
* ====================================================================
*
* Copyright 2003-2004 The Apache Software Foundation
*
* 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.commons.resources.impl;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.resources.ResourcesException;
/**
* <p>Concrete implementation of
* {@link org.apache.commons.resources.Resources} that wraps a
* JDBC database connection and retrieves values for the given
* <code>Locale</code> and have name suffixes reflecting the
* <code>Locale</code> for which the document's messages apply.
* For this specific implementation, resources are looked up in
* a hierarchy of database values in a manner identical to that
* performed by <code>java.util.ResourceBundle.getBundle().</code>.
*
* </p>
*
* <p>The base URL passed to our constructor must contain the base name
* of a properties file that holds the JDBC datasource configuration.</p>
*
* <p>
* The expected format of the required properties file might look like this:<br>
* <code><pre>
* jdbc.connect.driver = org.gjt.mm.mysql.Driver
* jdbc.connect.url = jdbc:mysql://localhost/resources
* jdbc.connect.login = resourcesTest
* jdbc.connect.password = resourcesTest
*
* jdbc.sql.db = resources
* jdbc.sql.table = resources
* jdbc.sql.locale.column = locale
* jdbc.sql.key.column = msgKey
* jdbc.sql.val.column = val
*
* org.apache.commons.resource.CACHE = true
*
* </pre></code>
*
* </p>
*
* @author James Mitchell
* @version $Revision: 1.1 $ $Date: 2004/06/11 02:43:48 $
*/
public class JDBCResources extends CollectionResourcesBase {
/**
* <p>The <code>Log</code> instance for this class.</p>
*/
private static final Log log = LogFactory.getLog(JDBCResources.class);
Connection con = null;
// ----------------------------------------------------------- Constructors
/**
* <p>Create a new
* {@link org.apache.commons.resources.Resources} instance with the specified
* logical name and base resource URL.</p>
*
* @param name Logical name of the new instance
* @param base Base URL of the JDBC configuration properties.
*/
public JDBCResources(String name, String base) {
super(name, base);
}
// ------------------------------------------------------ Protected Methods
/**
* <p>Return a <code>Map</code> containing the name-value mappings for
* the specified base URL and requested <code>Locale</code>, if there
* are any. If there are no defined mappings for the specified
* <code>Locale</code>, return an empty <code>Map</code> instead.</p>
*
* <p>Concrete subclasses must override this method to perform the
* appropriate lookup. A typical implementation will construct an
* absolute URL based on the specified base URL and <code>Locale</code>,
* retrieve the specified resource file (if any), and parse it into
* a <code>Map</code> structure.</p>
*
* <p>Caching of previously retrieved <code>Map</code>s (if any) should
* be performed by callers of this method. Therefore, this method should
* always attempt to retrieve the specified resource and load it
* appropriately.</p>
*
* @param baseUrl Base URL of the resource files for this
* {@link org.apache.commons.resources.Resources} instance
* @param locale <code>Locale</code> for which name-value mappings
* are requested
*/
protected Map getLocaleMap(String baseUrl, Locale locale) {
if (log.isDebugEnabled()) {
log.debug("Loading database configuration'" + locale + "' resources from base '" +
baseUrl + "'");
}
Properties props = new Properties();
//getLocaleSuffix(locale) +
String name = baseUrl + ".properties";
InputStream stream = null;
try {
// Open an input stream to the URL for this locale (if any)
if (log.isTraceEnabled()) {
log.trace("Absolute URL is '" + name + "'");
}
URL url = new URL(name);
stream = url.openStream();
// Parse the input stream and populate the name-value mappings map
if (log.isTraceEnabled()) {
log.trace("Parsing input resource");
}
props.load(stream);
} catch (FileNotFoundException e) {
// Log and swallow this exception
if (log.isDebugEnabled()) {
log.debug("No resources for locale '" + locale +
"' from base '" + baseUrl + "'");
}
props.clear();
} catch (IOException e) {
log.warn("IOException loading locale '" + locale +
"' from base '" + baseUrl + "'", e);
props.clear();
} finally {
// Close the input stream that was opened earlier
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
log.error("Error closing stream.", e);
}
stream = null;
}
}
// Return the populated (or empty) properties
Properties properties = new Properties();
try {
properties = loadData(locale, props);
} catch (InstantiationException e) {
log.warn("InstantiationException: locale= '" + locale +
"' base= '" + baseUrl + "'", e);
} catch (IllegalAccessException e) {
log.warn("IllegalAccessException: locale= '" + locale +
"' base= '" + baseUrl + "'", e);
} catch (ClassNotFoundException e) {
log.warn("Specified Driver not found, make sure it is on " +
"the classpath: locale= '" + locale +
"' base= '" + baseUrl + "'", e);
} catch (SQLException e) {
log.warn("SQLException: locale= '" + locale +
"' base= '" + baseUrl + "'", e);
}
return properties;
}
/**
* @param locale <code>Locale</code> for which name-value mappings
* are requested
* @param connectionProps The connection properties used to instantiate
* a JDBC connection.
*/
private Properties loadData(Locale locale, Properties connectionProps)
throws InstantiationException, IllegalAccessException,
ClassNotFoundException, SQLException {
String driver = connectionProps.getProperty("jdbc.connect.driver");
String url = connectionProps.getProperty("jdbc.connect.url");
String user = connectionProps.getProperty("jdbc.connect.login");
String pass = connectionProps.getProperty("jdbc.connect.password");
String db = connectionProps.getProperty("jdbc.sql.db");
String table = connectionProps.getProperty("jdbc.sql.table");
String localeColumn = connectionProps.getProperty("jdbc.sql.locale.column");
String keyColumn = connectionProps.getProperty("jdbc.sql.key.column");
String valColumn = connectionProps.getProperty("jdbc.sql.val.column");
Properties pairs = new Properties();
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url, user, pass);
String query = "SELECT " + keyColumn + ", " + valColumn +
" FROM " + table + " where " + localeColumn + "= '" + locale + "'";
PreparedStatement stmt = con.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
pairs.put(rs.getString(keyColumn), rs.getString(valColumn));
}
return pairs;
}
public void init() throws ResourcesException {
super.init();
}
}
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/JDBCResourcesFactory.java
Index: JDBCResourcesFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/JDBCResourcesFactory.java,v 1.1 2004/06/11 02:43:48 jmitchell Exp $
* $Revision: 1.1 $
* $Date: 2004/06/11 02:43:48 $
*
* ====================================================================
*
* Copyright 2003-2004 The Apache Software Foundation
*
* 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.commons.resources.impl;
import org.apache.commons.resources.Resources;
import org.apache.commons.resources.ResourcesException;
/**
* <p>Concrete implementation of
* {@link org.apache.commons.resources.ResourcesFactory} that creates
* {@link org.apache.commons.resources.Resources} instances that wraps
* a JDBC database connection and retrieves values for the given
* <code>Locale</code> and have name suffixes reflecting the
* <code>Locale</code> for which the document's messages apply.
* For this specific implementation, resources are looked up in
* a hierarchy of database values in a manner identical to that
* performed by <code>java.util.ResourceBundle.getBundle().</code>.
*
* @author James Mitchell
* @version $Revision: 1.1 $
*/
public class JDBCResourcesFactory extends ResourcesFactoryBase {
// ------------------------------------------------------ Protected Methods
/**
* <p>Create and return a new {@link org.apache.commons.resources.Resources}
* instance with the specified logical name, after calling its <code>init()</code>
* method and delegating the relevant properties.</p>
*
* @param name Logical name of the {@link org.apache.commons.resources.Resources}
* instance to create
*
* @param config Configuration string for this resource (if any)
*
* @exception ResourcesException if a {@link org.apache.commons.resources.Resources}
* instance of the specified logical name cannot be created.
*/
protected Resources createResources(String name, String config)
throws ResourcesException {
Resources res = new JDBCResources(name, config);
res.setReturnNull(isReturnNull());
res.init();
return (res);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org