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.domain.key.rtaware; 17 18 import de.smartics.properties.api.config.domain.key.ApplicationId; 19 import de.smartics.properties.api.config.domain.key.ConfigurationKeyHelper; 20 import de.smartics.properties.api.config.domain.key.EnvironmentId; 21 import de.smartics.properties.impl.config.domain.key.envapp.EnvAppConfigurationKey; 22 import de.smartics.properties.impl.config.domain.key.envapp.EnvAppConfigurationKeyHelper; 23 24 /** 25 * Helper to construct configuration keys by analyzing the runtime environment. 26 */ 27 public final class TenantUserConfigurationKeyHelper implements 28 ConfigurationKeyHelper 29 { 30 // ********************************* Fields ********************************* 31 32 // --- constants ------------------------------------------------------------ 33 34 /** 35 * The class version identifier. 36 */ 37 private static final long serialVersionUID = 1L; 38 39 // --- members -------------------------------------------------------------- 40 41 /** 42 * Constructs the environment and application part. 43 * 44 * @serial 45 */ 46 private final EnvAppConfigurationKeyHelper helper; 47 48 // ****************************** Initializer ******************************* 49 50 // ****************************** Constructors ****************************** 51 52 /** 53 * Convenience constructor preferring manifest information from EARs. 54 */ 55 public TenantUserConfigurationKeyHelper() 56 { 57 this(true); 58 } 59 60 /** 61 * Default constructor. 62 * 63 * @param preferEarManifest the flag to signal that the manifest of a EAR file 64 * (with the extension <code>ear</code>) is preferred if present. 65 */ 66 public TenantUserConfigurationKeyHelper(final boolean preferEarManifest) 67 { 68 this.helper = new EnvAppConfigurationKeyHelper(preferEarManifest); 69 } 70 71 // ****************************** Inner Classes ***************************** 72 73 // ********************************* Methods ******************************** 74 75 // --- init ----------------------------------------------------------------- 76 77 // --- get&set -------------------------------------------------------------- 78 79 // --- business ------------------------------------------------------------- 80 81 /** 82 * Loads the application identifier from the manifest file pointed at by the 83 * given {@code locator} class. 84 * 85 * @param locator the class to locate the manifest file to load. It is the 86 * manifest file of the archive this class is part of. 87 * @return the application identifier. 88 * @throws IllegalStateException if the application identifier cannot be read 89 * from the manifest. 90 * @see #load(EnvironmentId, Class) 91 */ 92 @Override 93 public TenantUserConfigurationKey load(final Class<?> locator) 94 throws IllegalStateException 95 { 96 return load((String) null, locator); 97 } 98 99 /** 100 * Loads the application identifier from the manifest file pointed at by the 101 * given {@code locator} class. 102 * 103 * @param environmentName the name of the environment of use. May be 104 * <code>null</code> if the value of the system property 105 * {@link #SYSTEM_PROPERTY_ENVIRONMENT_NAME} should be used (along 106 * with the node name defined by 107 * {@link #SYSTEM_PROPERTY_ENVIRONMENT_NODE}). If not specified, the 108 * name of the host of this runtime is determined. 109 * @param locator the class to locate the manifest file to load. It is the 110 * manifest file of the archive this class is part of. 111 * @return the application identifier. 112 * @throws IllegalStateException if the application identifier cannot be read 113 * from the manifest. 114 * @see #load(EnvironmentId, Class) 115 */ 116 @Override 117 public TenantUserConfigurationKey load(final String environmentName, 118 final Class<?> locator) throws IllegalStateException 119 { 120 final EnvAppConfigurationKey envKey = helper.load(environmentName, locator); 121 return createKey(envKey); 122 } 123 124 /** 125 * Loads the application identifier from the manifest file pointed at by the 126 * given {@code locator} class. 127 * 128 * @param environmentId the environment part of the configuration key to 129 * construct. 130 * @param locator the class to locate the manifest file to load. It is the 131 * manifest file of the archive this class is part of. 132 * @return the application identifier. 133 * @throws IllegalStateException if the application identifier cannot be read 134 * from the manifest. 135 * @see #load(Class) 136 * @see #load(String, Class) 137 */ 138 @Override 139 public TenantUserConfigurationKey load(final EnvironmentId environmentId, 140 final Class<?> locator) throws IllegalStateException 141 { 142 final EnvAppConfigurationKey envKey = helper.load(environmentId, locator); 143 return createKey(envKey); 144 } 145 146 /** 147 * Loads the configuration key using the given applicationId and determines 148 * the environmentId through system properties or defaults (null for the 149 * environment and hostname for the node). 150 * 151 * @param applicationId the application part of the configuration key to 152 * construct. 153 * @return the configuration key. 154 */ 155 @Override 156 public TenantUserConfigurationKey load(final ApplicationId applicationId) 157 { 158 final EnvAppConfigurationKey envKey = helper.load(applicationId); 159 return createKey(envKey); 160 } 161 162 private static TenantUserConfigurationKey createKey( 163 final EnvAppConfigurationKey envKey) 164 { 165 final RuntimeAdapter adaper = RuntimeAdapterManager.INSTANCE.adapter(); 166 final TenantUserConfigurationKey key = 167 new TenantUserConfigurationKey(envKey.getEnvironmentId(), 168 envKey.getApplicationId(), adaper.getTenantId(), adaper.getUserId()); 169 return key; 170 } 171 172 // --- object basics -------------------------------------------------------- 173 174 }