1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.impl.config.domain.key.envapp;
17
18 import static org.apache.commons.lang.StringUtils.defaultIfBlank;
19 import de.smartics.properties.api.config.domain.key.ApplicationId;
20 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
21 import de.smartics.properties.api.config.domain.key.ConfigurationKeyFactory;
22 import de.smartics.properties.api.config.domain.key.EnvironmentId;
23
24
25
26
27 final class EnvAppConfigurationKeyFactory implements
28 ConfigurationKeyFactory<EnvAppConfigurationKey>
29 {
30
31
32
33
34
35
36
37 private static final EnvAppConfigurationKey DEFAULT =
38 new EnvAppConfigurationKey(EnvironmentId.ANY_ENV, ApplicationId.ANY_APP);
39
40
41
42
43
44
45
46
47
48
49 EnvAppConfigurationKeyFactory()
50 {
51 }
52
53
54
55
56
57
58
59
60
61
62
63 @Override
64 public EnvAppConfigurationKey createDefaultKey()
65 {
66 return DEFAULT;
67 }
68
69 @Override
70 public ConfigurationKey<?> createKey(final ApplicationId applicationId)
71 {
72 final ConfigurationKey<?> key =
73 new EnvAppConfigurationKey(EnvironmentId.ANY_ENV, applicationId);
74 return key;
75 }
76
77 @Override
78 public ConfigurationKey<?> createUniqueKey(final String id)
79 {
80 return new EnvAppConfigurationKey(new EnvironmentId(id),
81 ApplicationId.ANY_APP);
82 }
83
84 @Override
85 public ConfigurationKey<?> createKey(final ConfigurationKey<?> key,
86 final ApplicationId appId)
87 {
88 final ConfigurationKey<?> newKey =
89 new EnvAppConfigurationKey((EnvAppConfigurationKey) key, appId);
90 return newKey;
91 }
92
93 @Override
94 public ConfigurationKey<?> createKeyFromString(final String key)
95 throws IllegalArgumentException
96 {
97
98 if (key == null)
99 {
100 throw new IllegalArgumentException(String.format(
101 "Cannot parse key '%s', since key is null", key));
102
103 }
104 final String[] keyArray = key.split("/", -1);
105 final int count = keyArray.length;
106 final int expectedCount = 2;
107 if (count != expectedCount)
108 {
109 throw new IllegalArgumentException(String.format(
110 "Cannot parse key '%s', since %s slash-separated elements are expected,"
111 + " but found only %s.", key, expectedCount, count));
112 }
113
114 final EnvironmentId environmentId =
115 EnvironmentId.valueOf(defaultIfBlank(keyArray[0], null));
116 final ApplicationId applicationId =
117 ApplicationId.valueOf(defaultIfBlank(keyArray[1], null));
118
119 final ConfigurationKey<?> newKey =
120 new EnvAppConfigurationKey(environmentId, applicationId);
121 return newKey;
122 }
123
124 @Override
125 public ConfigurationKey<?> createStaticKey(final ConfigurationKey<?> key)
126 {
127 return new EnvAppConfigurationKey((EnvAppConfigurationKey) key,
128 ((EnvAppConfigurationKey) key).getApplicationId());
129 }
130
131
132
133 }