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 protected static final String CUMULUS4J_PERSISTENCE_PROPERTIES = "cumulus4j-persistence.properties"; 043 044 /** 045 * <p> 046 * Open an {@link InputStream} reading the file {@value #CUMULUS4J_BACKEND_PROPERTIES}. 047 * </p> 048 * <p> 049 * <b>Important:</b> You must close this <code>InputStream</code>!!! 050 * </p> 051 * @return an {@link InputStream} (never <code>null</code>). 052 * @see #getCumulus4jBackendProperties() 053 */ 054 protected static InputStream openCumulus4jBackendProperties() 055 { 056 InputStream in = ResourceHelper.class.getResourceAsStream(CUMULUS4J_BACKEND_PROPERTIES); 057 if (in == null) 058 throw new IllegalStateException("Resource does not exist (or cannot be openend): " + CUMULUS4J_BACKEND_PROPERTIES); 059 060 return in; 061 } 062 063 protected static InputStream tryOpenCumulus4jPersistenceProperties() 064 { 065 InputStream in = ResourceHelper.class.getResourceAsStream(CUMULUS4J_PERSISTENCE_PROPERTIES); 066 return in; 067 } 068 069 /** 070 * <p> 071 * Get the backend-properties (from file {@value #CUMULUS4J_BACKEND_PROPERTIES}). 072 * </p> 073 * <p> 074 * The backend-properties are loaded from the file {@value #CUMULUS4J_BACKEND_PROPERTIES} 075 * and used for the configuration of the backend-{@link PersistenceManagerFactory}. This file only 076 * contains connection-independent settings (i.e. things that should never change). 077 * The connection-dependent settings are copied from the frontend-PMF's configuration. 078 * </p> 079 * <p> 080 * Additionally, all properties in the frontend-PMF's configuration starting with 081 * "cumulus4j.datanucleus." or "cumulus4j.javax." are forwarded to the backend-PMF 082 * (without the "cumulus4j."-prefix, of course) <b>overriding</b> the default settings. 083 * </p> 084 * @return a {@link Map} containing all settings from the backend-properties; never <code>null</code>. 085 */ 086 public static Map<String, Object> getCumulus4jBackendProperties() 087 { 088 Properties properties = new Properties(); 089 try { 090 InputStream in = openCumulus4jBackendProperties(); 091 properties.load(in); 092 in.close(); 093 } catch (IOException e) { // should never happen as it is a resource => rethrow as RuntimeException 094 throw new RuntimeException(e); 095 } 096 097 return getPropertiesAsMap(properties); 098 } 099 100 public static Map<String, Object> getCumulus4jPersistenceProperties() 101 { 102 Properties properties = new Properties(); 103 try { 104 InputStream in = tryOpenCumulus4jPersistenceProperties(); 105 if (in != null) { 106 properties.load(in); 107 in.close(); 108 } 109 } catch (IOException e) { // should never happen as it is a resource => rethrow as RuntimeException 110 throw new RuntimeException(e); 111 } 112 113 return getPropertiesAsMap(properties); 114 } 115 116 protected static Map<String, Object> getPropertiesAsMap(Properties properties) { 117 Map<String, Object> result = new HashMap<String, Object>(properties.size()); 118 for (Map.Entry<?, ?> me : properties.entrySet()) 119 result.put(String.valueOf(me.getKey()).toLowerCase(Locale.ENGLISH), String.valueOf(me.getValue())); 120 121 return result; 122 } 123 }