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.tutorial.boot; 17 18 import static org.hamcrest.MatcherAssert.assertThat; 19 import static org.hamcrest.Matchers.is; 20 import static org.hamcrest.Matchers.nullValue; 21 22 import java.net.MalformedURLException; 23 import java.net.URL; 24 import java.util.List; 25 26 import org.junit.Before; 27 import org.junit.Test; 28 29 import de.smartics.projectdoc.annotations.DocCategory; 30 import de.smartics.projectdoc.annotations.Document; 31 import de.smartics.projectdoc.annotations.topic.DocChapter; 32 import de.smartics.properties.api.config.app.BootProperties; 33 import de.smartics.properties.api.config.app.ConfigurationPropertiesFactory; 34 import de.smartics.properties.api.config.app.ConfigurationPropertiesFactoryFactory; 35 import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement; 36 import de.smartics.properties.api.config.domain.key.ApplicationId; 37 import de.smartics.properties.api.config.domain.key.ConfigurationKey; 38 import de.smartics.properties.api.config.domain.key.EnvironmentId; 39 import de.smartics.properties.impl.config.domain.key.envapp.EnvAppConfigurationKey; 40 41 /** 42 * This tutorial shows how to use {@link BootProperties}. 43 */ 44 @Document(title = "Using Boot Properties", sortKey = "basics1001") 45 @DocCategory({ "basics" }) 46 // @DocTopic(path="basics", step="1001") 47 public class BootPropertiesTutorial 48 { 49 private ConfigurationPropertiesManagement config; 50 51 @Before 52 public void setUp() 53 { 54 final ConfigurationPropertiesFactory factory = 55 ConfigurationPropertiesFactoryFactory.createDefaultFactory(); 56 57 final ConfigurationKey<?> key = 58 new EnvAppConfigurationKey(new EnvironmentId("test"), new ApplicationId( 59 "de.smartics.sandbox", "test-application", "0.1.0")); 60 config = factory.createManagement(key); 61 } 62 63 /** 64 * <p> 65 * {@link BootProperties} provide a simple interface to control the process of 66 * dealing with properties. Boot properties are read in advance before any 67 * other properties are evaluated. 68 * </p> 69 * <p> 70 * Boot properties are provided by the system as is. User's cannot add custom 71 * properties to it. Boot properties are used to control the process through 72 * configuration and cannot be accessed by user code. 73 * </p> 74 * <p> 75 * In contrast to other properties files <code>boot.properties</code> have to 76 * be placed under the <code>META-INF/smartics-properties</code> folder. 77 * </p> 78 * <p> 79 * Currently there is only one boot property called 80 * {@link BootProperties#additionalPropertiesLocations() 81 * additionalPropertiesLocations} that allows to add additional locations for 82 * properties definitions. One use case for this property is to add a path to 83 * a (maybe removable) device where sensible properties are stored. Or it is 84 * simply a path to a location where operations has convenient access to. 85 * </p> 86 * <p> 87 * The property is specified as shown in the following example. 88 * </p> 89 * 90 * <pre> 91 * smartics.properties.additionalPropertiesLocations=file:/D:/Transfer/external 92 * </pre> 93 * <p> 94 * The values are expected to be valid URLs. The usual constraints on 95 * specifying multiple properties apply (please refer to 96 * {@link de.smartics.properties.tutorial.property.list.ListPropertyTutorial} 97 * for details). 98 * </p> 99 * <p> 100 * Although the boot properties can be accessed, the client code does not see 101 * the actual properties, as you can see at {@$1} in the following 102 * example: 103 * </p> 104 * {@insertCode} 105 */ 106 @DocChapter 107 @Test 108 public void bootProperties() throws MalformedURLException 109 { 110 final BootProperties properties = 111 config.getProperties(BootProperties.class); 112 113 final List<URL> locationUrls = properties.additionalPropertiesLocations(); 114 assertThat(locationUrls, is(nullValue())); // {1} 115 } 116 }