Commits

Anonymous committed 968af1a

Ported over from sf.net, GRRRR

git-svn-id: http://svn.opensymphony.com/svn/propertyset/trunk@88bf3cbcdd-1c1a-0410-9a68-d6f521e3fa7b

  • Participants
  • Parent commits e1d5d39

Comments (0)

Files changed (6)

File src/java/com/opensymphony/module/propertyset/hibernate/DefaultHibernateConfigurationProvider.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.hibernate;
+
+import net.sf.hibernate.HibernateException;
+import net.sf.hibernate.SessionFactory;
+import net.sf.hibernate.cfg.Configuration;
+
+import java.util.Iterator;
+import java.util.Map;
+
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: Mike
+ * Date: Jul 26, 2003
+ * Time: 5:05:55 PM
+ * To change this template use Options | File Templates.
+ */
+public class DefaultHibernateConfigurationProvider implements HibernateConfigurationProvider {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private Configuration configuration;
+    private HibernatePropertySetDAO propertySetDAO;
+    private SessionFactory sessionFactory;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public Configuration getConfiguration() {
+        return configuration;
+    }
+
+    public HibernatePropertySetDAO getPropertySetDAO() {
+        if (propertySetDAO == null) {
+            propertySetDAO = new HibernatePropertySetDAOImpl(sessionFactory);
+        }
+
+        return propertySetDAO;
+    }
+
+    public void setupConfiguration(Map configurationProperties) {
+        // loaded hibernate config
+        try {
+            configuration = new Configuration().addClass(PropertySetItem.class);
+
+            Iterator itr = configurationProperties.keySet().iterator();
+
+            while (itr.hasNext()) {
+                String key = (String) itr.next();
+
+                if (key.startsWith("hibernate")) {
+                    configuration.setProperty(key, (String) configurationProperties.get(key));
+                }
+            }
+
+            this.sessionFactory = configuration.buildSessionFactory();
+        } catch (HibernateException e) {
+        }
+    }
+}

File src/java/com/opensymphony/module/propertyset/hibernate/HibernateConfigurationProvider.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.hibernate;
+
+import net.sf.hibernate.cfg.Configuration;
+
+import java.util.Map;
+
+
+/**
+ * Use this class to provide your own configurations to the PropertySet hibernate providers.
+ * <p>
+ * Simply implement this interface and return a Hibernate Configuration object.
+ * <p>
+ * This is setup by using the configuration.provider.class property, with the classname
+ * of your implementation.
+ */
+public interface HibernateConfigurationProvider {
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    /**
+ * Get a Hibernate configuration object
+ */
+    public Configuration getConfiguration();
+
+    HibernatePropertySetDAO getPropertySetDAO();
+
+    /**
+ * Setup a Hibernate configuration object with the given properties.
+ *
+ * This will always be called before getConfiguration().
+ */
+    void setupConfiguration(Map properties);
+}

File src/java/com/opensymphony/module/propertyset/hibernate/HibernatePropertySet.java

 import com.opensymphony.module.propertyset.AbstractPropertySet;
 import com.opensymphony.module.propertyset.PropertyException;
 
-import net.sf.hibernate.Hibernate;
-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 net.sf.hibernate.type.Type;
+import com.opensymphony.util.ClassLoaderUtil;
 
-import java.io.Serializable;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
-import java.sql.SQLException;
-
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
 import java.util.Map;
 
 
  *  <li><b>entityName</b> - String that holds the name of this entity type</li>
  * </ul>
  *
- * if "<b>sessionFactory</b> - hibornate sessionFactory" is not passed as an arg then init will use: <br />
+ * if "<b>sessionFactory</b> - hibernate sessionFactory" is not passed as an arg then init will use: <br />
  *  <b>hibernate.*</b> - config params needed to create a hibernate sessionFactory in the propertyset config xml.
  * <br />
  * This can be any of the configs avail from hibernate.
  * @version $Revision$
  */
 public class HibernatePropertySet extends AbstractPropertySet {
+    //~ Static fields/initializers /////////////////////////////////////////////
+
+    protected static Log log = LogFactory.getLog(HibernatePropertySet.class.getName());
+
     //~ Instance fields ////////////////////////////////////////////////////////
 
+    private HibernateConfigurationProvider configProvider;
     private Long entityId;
-    private SessionFactory sessionFactory;
     private String entityName;
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
     public Collection getKeys(String prefix, int type) throws PropertyException {
-        Session session = null;
-        Query query = null;
-        List list = null;
-
-        try {
-            session = this.sessionFactory.openSession();
-
-            if ((prefix != null) && (type > 0)) {
-                query = session.getNamedQuery("all_keys_with_type_like");
-                query.setString("like", prefix + '%');
-                query.setInteger("type", type);
-            } else if (prefix != null) {
-                query = session.getNamedQuery("all_keys_like");
-                query.setString("like", prefix + '%');
-            } else if (type > 0) {
-                query = session.getNamedQuery("all_keys_with_type");
-                query.setInteger("type", type);
-            } else {
-                query = session.getNamedQuery("all_keys");
-            }
-
-            query.setString("entityName", entityName);
-            query.setLong("entityId", entityId.longValue());
-
-            list = query.list();
-        } catch (HibernateException e) {
-            list = Collections.EMPTY_LIST;
-        } finally {
-            try {
-                if (session != null) {
-                    session.flush();
-                    session.close();
-                }
-            } catch (Exception e) {
-            }
-        }
-
-        return list;
+        return configProvider.getPropertySetDAO().getKeys(entityName, entityId, prefix, type);
     }
 
     public int getType(String key) throws PropertyException {
         super.init(config, args);
         this.entityId = (Long) args.get("entityId");
         this.entityName = (String) args.get("entityName");
-        this.sessionFactory = (SessionFactory) args.get("sessionFactory");
 
-        if (this.sessionFactory == null) {
-            // loaded hibernate config
-            try {
-                Configuration cfg = new Configuration().addClass(PropertySetItem.class);
-                Iterator itr = config.keySet().iterator();
+        // first let's see if we got given a configuration provider to use already
+        configProvider = (HibernateConfigurationProvider) args.get("configurationProvider");
 
-                while (itr.hasNext()) {
-                    String key = (String) itr.next();
+        if (configProvider == null) // if we did not get given one in the args, we need to set a config provider up
+         {
+            // lets see if we need to use a configurationProvider from a class
+            String configProviderClass = (String) config.get("configuration.provider.class");
 
-                    if (key.startsWith("hibernate")) {
-                        cfg.setProperty(key, (String) args.get(key));
-                    }
+            if (configProviderClass != null) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Setting up property set provider of class: " + configProviderClass);
                 }
 
-                this.sessionFactory = cfg.buildSessionFactory();
-            } catch (HibernateException e) {
+                try {
+                    configProvider = (HibernateConfigurationProvider) ClassLoaderUtil.loadClass(configProviderClass, this.getClass()).newInstance();
+                } catch (Exception e) {
+                    log.error("Unable to load configuration provider class: " + configProviderClass, e);
+
+                    return;
+                }
+            } else {
+                if (log.isDebugEnabled()) {
+                    log.debug("Setting up property set with DefaultHibernateConfigurationProvider");
+                }
+
+                configProvider = new DefaultHibernateConfigurationProvider();
+            }
+
+            configProvider.setupConfiguration(config);
+        } else {
+            if (log.isDebugEnabled()) {
+                log.debug("Setting up property set with hibernate provider passed in args.");
             }
         }
     }
 
     public void remove(String key) throws PropertyException {
-        Session session = null;
-
-        try {
-            session = this.sessionFactory.openSession();
-            session.delete(findByKey(key));
-            session.flush();
-        } catch (HibernateException e) {
-            throw new PropertyException("Could not remove key '" + key + "': " + e.getMessage());
-        } finally {
-            try {
-                if (session != null) {
-                    if (!session.connection().getAutoCommit()) {
-                        session.connection().commit();
-                    }
-
-                    session.close();
-                }
-            } catch (Exception e) {
-            }
-        }
+        configProvider.getPropertySetDAO().remove(entityName, entityId, key);
     }
 
     protected void setImpl(int type, String key, Object value) throws PropertyException {
-        Session session = null;
         PropertySetItem item = new PropertySetItem(entityName, entityId.longValue(), key);
 
         switch (type) {
             throw new PropertyException("type " + type + " not supported");
         }
 
-        try {
-            item.setType(type);
-            session = this.sessionFactory.openSession();
-
-            Serializable foo = session.save(item);
-            session.flush();
-        } catch (HibernateException he) {
-            throw new PropertyException("Could not save key '" + key + "':" + he.getMessage());
-        } finally {
-            try {
-                if (session != null) {
-                    if (!session.connection().getAutoCommit()) {
-                        session.connection().commit();
-                    }
-
-                    session.close();
-                }
-            } catch (Exception e) {
-            }
-        }
+        item.setType(type);
+
+        configProvider.getPropertySetDAO().setImpl(item);
     }
 
     protected Object get(int type, String key) throws PropertyException {
     }
 
     private PropertySetItem findByKey(String key) throws PropertyException {
-        Session session = null;
-        PropertySetItem item = null;
-
-        try {
-            session = this.sessionFactory.openSession();
-            item = (PropertySetItem) session.load(PropertySetItem.class, new PropertySetItem(entityName, entityId.longValue(), key));
-            session.flush();
-        } catch (HibernateException e) {
-            throw new PropertyException("Could not find key '" + key + "': " + e.getMessage());
-        } finally {
-            try {
-                if (session != null) {
-                    session.close();
-                }
-            } catch (Exception e) {
-            }
-        }
+        PropertySetItem item = configProvider.getPropertySetDAO().findByKey(entityName, entityId, key);
 
         if (item != null) {
             return item;

File src/java/com/opensymphony/module/propertyset/hibernate/HibernatePropertySetDAO.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.hibernate;
+
+import java.util.Collection;
+
+
+/**
+ * DOCUMENT ME!
+ *
+ * @author $author$
+ * @version $Revision$
+ */
+public interface HibernatePropertySetDAO {
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    void setImpl(PropertySetItem item);
+
+    Collection getKeys(String entityName, Long entityId, String prefix, int type);
+
+    PropertySetItem findByKey(String entityName, Long entityId, String key);
+
+    void remove(String entityName, Long entityId, String key);
+}

File src/java/com/opensymphony/module/propertyset/hibernate/HibernatePropertySetDAOImpl.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.hibernate;
+
+import com.opensymphony.module.propertyset.PropertyException;
+
+import net.sf.hibernate.HibernateException;
+import net.sf.hibernate.Query;
+import net.sf.hibernate.Session;
+import net.sf.hibernate.SessionFactory;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+
+/**
+ * DOCUMENT ME!
+ *
+ * @author $author$
+ * @version $Revision$
+ */
+public class HibernatePropertySetDAOImpl implements HibernatePropertySetDAO {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private SessionFactory sessionFactory;
+
+    //~ Constructors ///////////////////////////////////////////////////////////
+
+    public HibernatePropertySetDAOImpl(SessionFactory sessionFactory) {
+        this.sessionFactory = sessionFactory;
+    }
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void setImpl(PropertySetItem item) {
+        Session session = null;
+
+        try {
+            session = this.sessionFactory.openSession();
+            session.save(item);
+            session.flush();
+        } catch (HibernateException he) {
+            throw new PropertyException("Could not save key '" + item.getKey() + "':" + he.getMessage());
+        } finally {
+            try {
+                if (session != null) {
+                    if (!session.connection().getAutoCommit()) {
+                        session.connection().commit();
+                    }
+
+                    session.close();
+                }
+            } catch (Exception e) {
+            }
+        }
+    }
+
+    public Collection getKeys(String entityName, Long entityId, String prefix, int type) {
+        Session session = null;
+        List list = null;
+
+        try {
+            session = this.sessionFactory.openSession();
+            list = HibernatePropertySetDAOUtils.getKeysImpl(session, entityName, entityId, prefix, type);
+        } catch (HibernateException e) {
+            list = Collections.EMPTY_LIST;
+        } finally {
+            try {
+                if (session != null) {
+                    session.flush();
+                    session.close();
+                }
+            } catch (Exception e) {
+            }
+        }
+
+        return list;
+    }
+
+    public PropertySetItem findByKey(String entityName, Long entityId, String key) {
+        Session session = null;
+        PropertySetItem item = null;
+
+        try {
+            session = this.sessionFactory.openSession();
+            item = HibernatePropertySetDAOUtils.getItem(session, entityName, entityId, key);
+            session.flush();
+        } catch (HibernateException e) {
+            throw new PropertyException("Could not find key '" + key + "': " + e.getMessage());
+        } finally {
+            try {
+                if (session != null) {
+                    session.close();
+                }
+            } catch (Exception e) {
+            }
+        }
+
+        return item;
+    }
+
+    public void remove(String entityName, Long entityId, String key) {
+        Session session = null;
+
+        try {
+            session = this.sessionFactory.openSession();
+            session.delete(HibernatePropertySetDAOUtils.getItem(session, entityName, entityId, key));
+            session.flush();
+        } catch (HibernateException e) {
+            throw new PropertyException("Could not remove key '" + key + "': " + e.getMessage());
+        } finally {
+            try {
+                if (session != null) {
+                    if (!session.connection().getAutoCommit()) {
+                        session.connection().commit();
+                    }
+
+                    session.close();
+                }
+            } catch (Exception e) {
+            }
+        }
+    }
+}

File src/java/com/opensymphony/module/propertyset/hibernate/HibernatePropertySetDAOUtils.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.hibernate;
+
+import net.sf.hibernate.HibernateException;
+import net.sf.hibernate.Query;
+import net.sf.hibernate.Session;
+
+import java.util.List;
+
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: Mike
+ * Date: Jul 26, 2003
+ * Time: 5:46:58 PM
+ * To change this template use Options | File Templates.
+ */
+public class HibernatePropertySetDAOUtils {
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public static PropertySetItem getItem(Session session, String entityName, Long entityId, String key) throws HibernateException {
+        return (PropertySetItem) session.load(PropertySetItem.class, new PropertySetItem(entityName, entityId.longValue(), key));
+    }
+
+    /**
+     * This is the body of the getKeys() method, so that you can reuse it wrapped by your own session management.
+     */
+    public static List getKeysImpl(Session session, String entityName, Long entityId, String prefix, int type) throws HibernateException {
+        Query query;
+
+        if ((prefix != null) && (type > 0)) {
+            query = session.getNamedQuery("all_keys_with_type_like");
+            query.setString("like", prefix + '%');
+            query.setInteger("type", type);
+        } else if (prefix != null) {
+            query = session.getNamedQuery("all_keys_like");
+            query.setString("like", prefix + '%');
+        } else if (type > 0) {
+            query = session.getNamedQuery("all_keys_with_type");
+            query.setInteger("type", type);
+        } else {
+            query = session.getNamedQuery("all_keys");
+        }
+
+        query.setString("entityName", entityName);
+        query.setLong("entityId", entityId.longValue());
+
+        return query.list();
+    }
+}