001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.store.resource;
019    
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.util.HashMap;
023    import java.util.Locale;
024    import java.util.Map;
025    import java.util.Properties;
026    
027    import javax.jdo.PersistenceManagerFactory;
028    
029    /**
030     * Helper class to load resources.
031     *
032     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
033     */
034    public class ResourceHelper
035    {
036            /**
037             * File name of the backend-properties.
038             * @see #getCumulus4jBackendProperties()
039             */
040            protected static final String CUMULUS4J_BACKEND_PROPERTIES = "cumulus4j-backend.properties";
041    
042            /**
043             * <p>
044             * Open an {@link InputStream} reading the file {@value #CUMULUS4J_BACKEND_PROPERTIES}.
045             * </p>
046             * <p>
047             * <b>Important:</b> You must close this <code>InputStream</code>!!!
048             * </p>
049             * @return an {@link InputStream} (never <code>null</code>).
050             * @see #getCumulus4jBackendProperties()
051             */
052            protected static InputStream openCumulus4jBackendProperties()
053            {
054                    InputStream in = ResourceHelper.class.getResourceAsStream(CUMULUS4J_BACKEND_PROPERTIES);
055                    if (in == null)
056                            throw new IllegalStateException("Resource does not exist (or cannot be openend): " + CUMULUS4J_BACKEND_PROPERTIES);
057    
058                    return in;
059            }
060    
061            /**
062             * <p>
063             * Get the backend-properties (from file {@value #CUMULUS4J_BACKEND_PROPERTIES}).
064             * </p>
065             * <p>
066             * The backend-properties are loaded from the file {@value #CUMULUS4J_BACKEND_PROPERTIES}
067             * and used for the configuration of the backend-{@link PersistenceManagerFactory}. This file only
068             * contains connection-independent settings (i.e. things that should never change).
069             * The connection-dependent settings are copied from the frontend-PMF's configuration.
070             * </p>
071             * <p>
072             * Additionally, all properties in the frontend-PMF's configuration starting with
073             * "cumulus4j.datanucleus." or "cumulus4j.javax." are forwarded to the backend-PMF
074             * (without the "cumulus4j."-prefix, of course) <b>overriding</b> the default settings.
075             * </p>
076             * @return a {@link Map} containing all settings from the backend-properties; never <code>null</code>.
077             */
078            public static Map<String, Object> getCumulus4jBackendProperties()
079            {
080                    Properties properties = new Properties();
081                    try {
082                            InputStream in = openCumulus4jBackendProperties();
083                            properties.load(in);
084                            in.close();
085                    } catch (IOException e) { // should never happen as it is a resource => rethrow as RuntimeException
086                            throw new RuntimeException(e);
087                    }
088    
089                    Map<String, Object> result = new HashMap<String, Object>(properties.size());
090                    for (Map.Entry<?, ?> me : properties.entrySet())
091                            result.put(String.valueOf(me.getKey()).toLowerCase(Locale.ENGLISH), String.valueOf(me.getValue()));
092    
093                    return result;
094            }
095    }