1 /* 2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.properties.impl.config.ds; 17 18 import java.io.Serializable; 19 20 import de.smartics.properties.api.core.domain.PropertiesContext; 21 22 /** 23 * Responsible to read configuration properties to connect to a data source 24 * stored via JNDI. 25 */ 26 public class DataSourceConfigurationLoader implements Serializable 27 { 28 // ********************************* Fields ********************************* 29 30 // --- constants ------------------------------------------------------------ 31 32 /** 33 * The class version identifier. 34 * <p> 35 * The value of this constant is {@value}. 36 * </p> 37 */ 38 private static final long serialVersionUID = 1L; 39 40 /** 41 * The location within the classpath to find the configuration file to load. 42 */ 43 public static final String CLASSPATH_LOCATION = 44 PropertiesContext.META_INF_HOME + "/datasource.properties"; 45 46 // --- members -------------------------------------------------------------- 47 48 // ****************************** Initializer ******************************* 49 50 // ****************************** Constructors ****************************** 51 52 /** 53 * Default constructor. 54 */ 55 public DataSourceConfigurationLoader() 56 { 57 } 58 59 // ****************************** Inner Classes ***************************** 60 61 // ********************************* Methods ******************************** 62 63 // --- init ----------------------------------------------------------------- 64 65 // --- get&set -------------------------------------------------------------- 66 67 // --- business ------------------------------------------------------------- 68 69 /** 70 * Loads the data source configuration. 71 * 72 * @return the data source configuration. 73 */ 74 public DataSourceConfiguration load() 75 { 76 try 77 { 78 try 79 { 80 final DataSourceConfigurationPropertiesLoader propertiesLoader = 81 new DataSourceConfigurationPropertiesLoader(); 82 final DataSourceConfiguration config = propertiesLoader.load(); 83 return config; 84 } 85 catch (final IllegalStateException e) 86 { 87 final DataSourceConfigurationJndiLoader jndiLoader = 88 new DataSourceConfigurationJndiLoader(); 89 final DataSourceConfiguration config = jndiLoader.load(); 90 return config; 91 } 92 } 93 catch (final IllegalStateException e) 94 { 95 throw new IllegalStateException( 96 String.format("Cannot configure the access to a data source: %s", 97 e.getMessage(), e)); 98 } 99 } 100 101 // --- object basics -------------------------------------------------------- 102 103 }