1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.config.support;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import de.smartics.properties.api.config.domain.key.ApplicationId;
22 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
23 import de.smartics.properties.api.config.domain.key.EnvironmentId;
24 import de.smartics.util.lang.NullArgumentException;
25
26
27
28
29 class KeySetBuilder
30 {
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public KeySetBuilder()
45 {
46 }
47
48
49
50
51
52
53
54
55
56
57
58 public List<ConfigurationKey> createKeySet(final ConfigurationKey key)
59 {
60 final List<ConfigurationKey> list = new ArrayList<ConfigurationKey>();
61 list.add(key);
62
63 final EnvironmentId envId = key.getEnvironmentId();
64 final ApplicationId appId = key.getApplicationId();
65
66 addAppKeys(list, envId, appId);
67
68 if (envId.getNode() != null)
69 {
70 final EnvironmentId defaultEnv = new EnvironmentId(envId.getName());
71 if (!ApplicationId.ANY_APP.equals(appId))
72 {
73 if (appId.getVersion() != null)
74 {
75 final ConfigurationKey defaultKey =
76 new ConfigurationKey(defaultEnv, appId);
77 list.add(defaultKey);
78 }
79
80 addAppKeys(list, defaultEnv, appId);
81 }
82 else
83 {
84 list.add(new ConfigurationKey(defaultEnv, ApplicationId.ANY_APP));
85 }
86 }
87
88 addAppKeyNoEnv(list, appId);
89
90 final ConfigurationKey defaultKey =
91 new ConfigurationKey(EnvironmentId.ANY_ENV, ApplicationId.ANY_APP);
92 list.add(defaultKey);
93
94 return list;
95 }
96
97 private void addAppKeys(final List<ConfigurationKey> list,
98 final EnvironmentId envId, final ApplicationId appId)
99 throws IllegalArgumentException, NullArgumentException
100 {
101 if (appId.getVersion() != null)
102 {
103 final ApplicationId defaultAppId =
104 new ApplicationId(appId.getGroupId(), appId.getArtifactId(), null);
105 final ConfigurationKey defaultKey =
106 new ConfigurationKey(envId, defaultAppId);
107 list.add(defaultKey);
108 }
109
110 if (appId.getArtifactId() != null)
111 {
112 final ApplicationId defaultAppId =
113 new ApplicationId(appId.getGroupId(), null, null);
114 final ConfigurationKey defaultKey =
115 new ConfigurationKey(envId, defaultAppId);
116 list.add(defaultKey);
117 }
118
119 if (appId.getGroupId() != null)
120 {
121 final ConfigurationKey defaultKey =
122 new ConfigurationKey(envId, ApplicationId.ANY_APP);
123 list.add(defaultKey);
124 }
125 }
126
127 private void addAppKeyNoEnv(final List<ConfigurationKey> list,
128 final ApplicationId appId) throws NullArgumentException,
129 IllegalArgumentException
130 {
131 if (appId.getVersion() != null)
132 {
133 final ConfigurationKey defaultAppKey =
134 new ConfigurationKey(EnvironmentId.ANY_ENV, appId);
135 list.add(defaultAppKey);
136 }
137
138 if (appId.getArtifactId() != null)
139 {
140 final ApplicationId noEnvAppId =
141 new ApplicationId(appId.getGroupId(), appId.getArtifactId(), null);
142 final ConfigurationKey defaultAppKey =
143 new ConfigurationKey(EnvironmentId.ANY_ENV, noEnvAppId);
144 list.add(defaultAppKey);
145 }
146
147 if (appId.getGroupId() != null)
148 {
149 final ApplicationId noEnvAppId =
150 new ApplicationId(appId.getGroupId(), null, null);
151 final ConfigurationKey defaultAppKey =
152 new ConfigurationKey(EnvironmentId.ANY_ENV, noEnvAppId);
153 list.add(defaultAppKey);
154 }
155 }
156
157
158
159 }