| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.jitr; |
| 18 | |
|
| 19 | |
import org.apache.log4j.Logger; |
| 20 | |
import org.jitr.annotation.JitrConfiguration; |
| 21 | |
import org.jitr.container.JettyContainer; |
| 22 | |
import org.jitr.core.ConfigurationModel; |
| 23 | |
import org.jitr.core.Container; |
| 24 | |
import org.jitr.core.Defaults; |
| 25 | |
import org.jitr.core.JitrException; |
| 26 | |
import org.jitr.core.OperationalMode; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public final class JitrConfigurationFactory { |
| 35 | |
|
| 36 | 1 | private static enum From { |
| 37 | 1 | SYSTEM_PROPERTY("system property"), ANNOTATION("annotation"), DEFAULT("default"), OTHER; |
| 38 | |
|
| 39 | |
private final String str; |
| 40 | |
|
| 41 | 1 | private From() { |
| 42 | 1 | str = null; |
| 43 | 1 | } |
| 44 | |
|
| 45 | 3 | private From(final String str) { |
| 46 | 3 | this.str = str; |
| 47 | 3 | } |
| 48 | |
|
| 49 | |
@Override |
| 50 | |
public String toString() { |
| 51 | 51 | return str == null ? super.toString() : str; |
| 52 | |
} |
| 53 | |
} |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public static final String SP_KEY_PREFIX = "jitr."; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public static final String BASE_URI_SP_KEY = SP_KEY_PREFIX + "baseUri"; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public static final String CONTAINER_CLASS_SP_KEY = SP_KEY_PREFIX + "containerClass"; |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public static final String MODE_SP_KEY = SP_KEY_PREFIX + "mode"; |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
public static final String PORT_SP_KEY = SP_KEY_PREFIX + "port"; |
| 95 | |
|
| 96 | 1 | private static final Logger LOG = Logger.getLogger(JitrConfigurationFactory.class); |
| 97 | |
|
| 98 | 0 | private JitrConfigurationFactory() { |
| 99 | |
|
| 100 | 0 | } |
| 101 | |
|
| 102 | |
public static ConfigurationModel createConfigurationFromAnnotation(final JitrConfiguration annot) { |
| 103 | |
|
| 104 | 7 | final Class<?> containerClass = determineContainerClass(annot); |
| 105 | 7 | final String containerWorkPath = determineContainerWorkPath(annot); |
| 106 | 7 | final String contextPath = determineContextPath(annot); |
| 107 | 7 | final OperationalMode mode = determineOperationalMode(annot); |
| 108 | 7 | final int port = determinePort(annot); |
| 109 | 7 | final String warPath = determineWarPath(annot); |
| 110 | |
|
| 111 | |
|
| 112 | 7 | final String baseUri = determineBaseUri(port, contextPath); |
| 113 | |
|
| 114 | 7 | ConfigurationModel config = |
| 115 | |
new ConfigurationModel(baseUri, containerClass, containerWorkPath, contextPath, |
| 116 | |
mode, port, warPath); |
| 117 | |
|
| 118 | 7 | return config; |
| 119 | |
} |
| 120 | |
|
| 121 | |
static String determineBaseUri(final int port, final String contextPath) { |
| 122 | |
|
| 123 | |
final From from; |
| 124 | |
final String baseUri; |
| 125 | |
|
| 126 | |
|
| 127 | 9 | final String baseUriSp = System.getProperty(BASE_URI_SP_KEY); |
| 128 | 9 | if (baseUriSp != null) { |
| 129 | |
|
| 130 | 1 | baseUri = baseUriSp; |
| 131 | 1 | from = From.SYSTEM_PROPERTY; |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
else { |
| 136 | 8 | baseUri = "http://localhost:" + port + contextPath; |
| 137 | 8 | from = From.OTHER; |
| 138 | |
} |
| 139 | |
|
| 140 | 9 | logSettingConfigValue("base URI", from, baseUri); |
| 141 | |
|
| 142 | 9 | return baseUri; |
| 143 | |
} |
| 144 | |
|
| 145 | |
static Class<?> determineContainerClass(final JitrConfiguration annot) { |
| 146 | |
|
| 147 | |
final From from; |
| 148 | |
final Class<?> containerClass; |
| 149 | |
|
| 150 | |
|
| 151 | 12 | final String containerClassSp = System.getProperty(CONTAINER_CLASS_SP_KEY); |
| 152 | 12 | if (containerClassSp != null) { |
| 153 | |
try { |
| 154 | 3 | containerClass = Class.forName(containerClassSp); |
| 155 | 2 | from = From.SYSTEM_PROPERTY; |
| 156 | 1 | } catch (final ClassNotFoundException cnfe) { |
| 157 | 1 | throw new JitrException( |
| 158 | |
"Could not load specified container class (" + containerClassSp |
| 159 | |
+ "). Make sure you use the fully qualified class name.", cnfe); |
| 160 | 2 | } |
| 161 | |
} |
| 162 | |
|
| 163 | |
|
| 164 | 9 | else if (annot != null) { |
| 165 | 6 | if (annot.containerClass() != Object.class) { |
| 166 | 3 | containerClass = annot.containerClass(); |
| 167 | |
} else { |
| 168 | 3 | containerClass = JettyContainer.class; |
| 169 | |
} |
| 170 | |
|
| 171 | 6 | from = From.ANNOTATION; |
| 172 | |
} else { |
| 173 | 3 | containerClass = JettyContainer.class; |
| 174 | 3 | from = From.DEFAULT; |
| 175 | |
} |
| 176 | |
|
| 177 | |
|
| 178 | 11 | if (!Container.class.isAssignableFrom(containerClass)) { |
| 179 | 1 | throw new JitrException("Container class is not of type Container: " |
| 180 | |
+ containerClass.toString()); |
| 181 | |
} |
| 182 | |
|
| 183 | 10 | logSettingConfigValue("container class", from, containerClass.toString()); |
| 184 | |
|
| 185 | 10 | return containerClass; |
| 186 | |
} |
| 187 | |
|
| 188 | |
static String determineContainerWorkPath(final JitrConfiguration annot) { |
| 189 | |
|
| 190 | |
final From from; |
| 191 | |
final String containerWorkPath; |
| 192 | |
|
| 193 | |
|
| 194 | 9 | if (annot == null) { |
| 195 | 3 | containerWorkPath = Defaults.DEFAULT_CONTAINER_WORK_PATH; |
| 196 | 3 | from = From.DEFAULT; |
| 197 | |
} else { |
| 198 | 6 | containerWorkPath = annot.containerWorkPath(); |
| 199 | 6 | from = From.ANNOTATION; |
| 200 | |
} |
| 201 | |
|
| 202 | 9 | logSettingConfigValue("container work path", from, containerWorkPath); |
| 203 | |
|
| 204 | 9 | return containerWorkPath; |
| 205 | |
} |
| 206 | |
|
| 207 | |
static String determineContextPath(final JitrConfiguration annot) { |
| 208 | |
|
| 209 | |
final From from; |
| 210 | |
final String contextPath; |
| 211 | |
|
| 212 | |
|
| 213 | 9 | if (annot == null) { |
| 214 | 3 | contextPath = Defaults.DEFAULT_CONTEXT_PATH; |
| 215 | 3 | from = From.DEFAULT; |
| 216 | |
} else { |
| 217 | 6 | contextPath = annot.contextPath(); |
| 218 | 6 | from = From.ANNOTATION; |
| 219 | |
} |
| 220 | |
|
| 221 | 9 | logSettingConfigValue("context path", from, contextPath); |
| 222 | |
|
| 223 | 9 | return contextPath; |
| 224 | |
} |
| 225 | |
|
| 226 | |
static OperationalMode determineOperationalMode(final JitrConfiguration annot) { |
| 227 | |
|
| 228 | |
final From from; |
| 229 | |
final OperationalMode mode; |
| 230 | |
|
| 231 | |
|
| 232 | 10 | final String modeSp = System.getProperty(MODE_SP_KEY); |
| 233 | 10 | if (modeSp != null) { |
| 234 | 1 | mode = OperationalMode.valueOf(modeSp); |
| 235 | 1 | from = From.SYSTEM_PROPERTY; |
| 236 | |
} |
| 237 | |
|
| 238 | |
|
| 239 | 9 | else if (annot != null) { |
| 240 | 6 | mode = annot.mode(); |
| 241 | 6 | from = From.ANNOTATION; |
| 242 | |
} else { |
| 243 | 3 | mode = Defaults.DEFAULT_MODE; |
| 244 | 3 | from = From.DEFAULT; |
| 245 | |
} |
| 246 | |
|
| 247 | 10 | logSettingConfigValue("operational mode", from, mode.toString()); |
| 248 | |
|
| 249 | 10 | return mode; |
| 250 | |
} |
| 251 | |
|
| 252 | |
static int determinePort(final JitrConfiguration annot) { |
| 253 | |
|
| 254 | |
final From from; |
| 255 | |
final int port; |
| 256 | |
|
| 257 | |
|
| 258 | 11 | final String portSp = System.getProperty(PORT_SP_KEY); |
| 259 | 11 | if (portSp != null) { |
| 260 | 2 | port = Integer.parseInt(portSp); |
| 261 | 1 | from = From.SYSTEM_PROPERTY; |
| 262 | |
} |
| 263 | |
|
| 264 | |
|
| 265 | 9 | else if (annot != null && annot.port() != Defaults.DEFAULT_PORT) { |
| 266 | |
|
| 267 | 2 | if (annot.port() <= 0) { |
| 268 | 0 | throw new JitrException("Configured Jitr port must be greater than 0."); |
| 269 | |
} |
| 270 | |
|
| 271 | 2 | port = annot.port(); |
| 272 | 2 | from = From.ANNOTATION; |
| 273 | |
} |
| 274 | |
|
| 275 | |
|
| 276 | |
else { |
| 277 | 7 | port = JitrUtils.getRandomUnusedPort(); |
| 278 | 7 | from = From.OTHER; |
| 279 | |
} |
| 280 | |
|
| 281 | 10 | logSettingConfigValue("port", from, Integer.toString(port)); |
| 282 | |
|
| 283 | 10 | return port; |
| 284 | |
} |
| 285 | |
|
| 286 | |
static String determineWarPath(final JitrConfiguration annot) { |
| 287 | |
|
| 288 | |
final From from; |
| 289 | |
final String warPath; |
| 290 | |
|
| 291 | |
|
| 292 | 9 | if (annot == null) { |
| 293 | 3 | warPath = Defaults.DEFAULT_WAR_PATH; |
| 294 | 3 | from = From.DEFAULT; |
| 295 | |
} else { |
| 296 | 6 | warPath = annot.warPath(); |
| 297 | 6 | from = From.ANNOTATION; |
| 298 | |
} |
| 299 | |
|
| 300 | 9 | logSettingConfigValue("war path", from, warPath); |
| 301 | |
|
| 302 | 9 | return warPath; |
| 303 | |
} |
| 304 | |
|
| 305 | |
private static void logSettingConfigValue(final String key, final From from, final String value) { |
| 306 | |
|
| 307 | 66 | if (!LOG.isInfoEnabled()) { |
| 308 | 0 | return; |
| 309 | |
} |
| 310 | |
|
| 311 | 66 | if (from == From.DEFAULT) { |
| 312 | 15 | LOG.info(key + " (" + from + "): " + value); |
| 313 | 51 | } else if (from != From.OTHER) { |
| 314 | 36 | LOG.info(key + " (from " + from + "): " + value); |
| 315 | |
} else { |
| 316 | 15 | LOG.info(key + ": " + value); |
| 317 | |
} |
| 318 | 66 | } |
| 319 | |
} |