001/*
002 * Copyright (c) 2004-2020 Tada AB and other contributors, as listed below.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the The BSD 3-Clause License
006 * which accompanies this distribution, and is available at
007 * http://opensource.org/licenses/BSD-3-Clause
008 *
009 * Contributors:
010 *   Tada AB
011 *   Chapman Flack
012 */
013package org.postgresql.pljava.example;
014
015import java.io.IOException;
016import java.io.InputStream;
017import java.sql.ResultSet;
018import java.sql.SQLException;
019import java.util.Enumeration;
020import java.util.Properties;
021import java.util.logging.Logger;
022
023import org.postgresql.pljava.ObjectPool;
024import org.postgresql.pljava.PooledObject;
025import org.postgresql.pljava.ResultSetProvider;
026import org.postgresql.pljava.SessionManager;
027
028/**
029 * Illustrates use of the {@code ResultSetProvider} interface to return
030 * (key,value) rows from the {@code example.properties} file, also making use
031 * of PL/Java's {@code ObjectPool} facility.
032 * @author Thomas Hallgren
033 */
034public class UsingProperties implements ResultSetProvider.Large, PooledObject {
035    private static Logger s_logger = Logger.getAnonymousLogger();
036
037    public static ResultSetProvider getProperties() throws SQLException {
038        ObjectPool<UsingProperties> pool =
039            SessionManager.current().getObjectPool(UsingProperties.class);
040        return (ResultSetProvider) pool.activateInstance();
041    }
042
043    private final Properties m_properties;
044
045    private final ObjectPool<UsingProperties> m_pool;
046
047    private Enumeration<?> m_propertyIterator;
048
049    public UsingProperties(ObjectPool<UsingProperties> pool) throws IOException
050    {
051        m_pool = pool;
052        m_properties = new Properties();
053
054        s_logger.info("** UsingProperties()");
055        InputStream propStream = this.getClass().getResourceAsStream(
056                "example.properties");
057        if (propStream == null) {
058            s_logger.info("example.properties was null");
059        } else {
060            m_properties.load(propStream);
061            propStream.close();
062            s_logger.info("example.properties has " + m_properties.size()
063                    + " entries");
064        }
065    }
066
067    @Override
068    public void activate() {
069        s_logger.info("** UsingProperties.activate()");
070        m_propertyIterator = m_properties.propertyNames();
071    }
072
073    @Override
074    public boolean assignRowValues(ResultSet receiver, long currentRow)
075            throws SQLException {
076        if (m_propertyIterator == null || !m_propertyIterator.hasMoreElements()) {
077            s_logger.fine("no more rows, returning false");
078            return false;
079        }
080        String propName = (String) m_propertyIterator.nextElement();
081        receiver.updateString(1, propName);
082        receiver.updateString(2, m_properties.getProperty(propName));
083        // s_logger.fine("next row created, returning true");
084        return true;
085    }
086
087    @Override
088    public void close() throws SQLException {
089        m_pool.passivateInstance(this);
090    }
091
092    @Override
093    public void passivate() {
094        s_logger.info("** UsingProperties.passivate()");
095        m_propertyIterator = null;
096    }
097
098    @Override
099    public void remove() {
100        s_logger.info("** UsingProperties.remove()");
101        m_properties.clear();
102    }
103}