commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmitch...@apache.org
Subject cvs commit: jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl IBatisResources.java DatabaseBasicMessage.java IBatisResourcesFactory.java
Date Tue, 07 Sep 2004 23:47:48 GMT
jmitchell    2004/09/07 16:47:48

  Added:       resources/src/java/org/apache/commons/resources/impl
                        IBatisResources.java DatabaseBasicMessage.java
                        IBatisResourcesFactory.java
  Log:
  adding iBatis Impl
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/IBatisResources.java
  
  Index: IBatisResources.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/IBatisResources.java,v
1.1 2004/09/07 23:47:48 jmitchell Exp $
   * $Revision: 1.1 $
   * $Date: 2004/09/07 23:47: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.Reader;
  import java.util.Iterator;
  import java.util.List;
  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;
  
  import com.ibatis.common.resources.Resources;
  import com.ibatis.db.sqlmap.SqlMap;
  import com.ibatis.db.sqlmap.XmlSqlMapBuilder;
  
  /**
   * <p>Concrete implementation of 
   * {@link org.apache.commons.resources.Resources} that 
   * retrieves values from a database via the iBatis framework 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/09/07 23:47:48 $
   */
  public class IBatisResources extends CollectionResourcesBase {
  
      /**
       * <p>The <code>Log</code> instance for this class.</p>
       */
      private static final Log log = LogFactory.getLog(IBatisResources.class);
      protected static SqlMap sqlMap;
      // ----------------------------------------------------------- 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 IBatisResources(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();
          
          String category = locale.toString();
          try {
          	if (sqlMap == null)
          		initSqlMap(baseUrl);
          	
          	// TODO change this to NOT be hard coded
  			List msgs = sqlMap.executeQueryForList("getBundleByLocale", category);
      		for (Iterator iter = msgs.iterator(); iter.hasNext();) {
      	          DatabaseBasicMessage msg = (DatabaseBasicMessage) iter.next();
      	          props.put(msg.getKey(), msg.getValue());
      	    }
  			
  		} catch (Exception e) {
  			e.printStackTrace();
  		}
  
          return props;
  
      }
      protected void initSqlMap(String configFile) throws Exception {
          Reader reader = Resources.getResourceAsReader(configFile);
          sqlMap = XmlSqlMapBuilder.buildSqlMap(reader);
          reader.close();
        }
  
      public void init() throws ResourcesException {
          super.init();
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/DatabaseBasicMessage.java
  
  Index: DatabaseBasicMessage.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/DatabaseBasicMessage.java,v
1.1 2004/09/07 23:47:48 jmitchell Exp $
   * $Revision: 1.1 $
   * $Date: 2004/09/07 23:47: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 ORM 
   * configuration (no complex keys).
   * 
   * @author James Mitchell
   *
   */
  public class DatabaseBasicMessage extends BasicMessage {
  
      public DatabaseBasicMessage() {
          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 DatabaseBasicMessage(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 DatabaseBasicMessage){
      		if (((DatabaseBasicMessage)obj).getKey().equals(this.key) && 
      				((DatabaseBasicMessage)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/IBatisResourcesFactory.java
  
  Index: IBatisResourcesFactory.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/IBatisResourcesFactory.java,v
1.1 2004/09/07 23:47:48 jmitchell Exp $
   * $Revision: 1.1 $
   * $Date: 2004/09/07 23:47: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 
   * retrieves values from a database via the iBatis framework 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 IBatisResourcesFactory 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 IBatisResources(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


Mime
View raw message