1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.admin.domain.service;
17
18 import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.ARCHIVE_TYPE;
19 import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.ARTIFACT_ID;
20 import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.CLASSIFIER;
21 import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.GROUP_ID;
22 import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.VERSION;
23
24 import java.io.Serializable;
25
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29
30 import org.apache.commons.lang.StringUtils;
31
32 import de.smartics.properties.api.core.app.JndiContext;
33 import de.smartics.properties.resource.domain.ArtifactId;
34
35
36
37
38 public class ApplicationIdConfigurationJndiLoader implements Serializable
39 {
40
41
42
43
44
45
46
47
48
49
50 private static final long serialVersionUID = 1L;
51
52
53
54
55
56
57
58
59
60
61 public ApplicationIdConfigurationJndiLoader()
62 {
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public ArtifactId load()
81 {
82 try
83 {
84 final Context context = new InitialContext();
85
86 final JndiContext lookup = JndiContext.boot(context);
87 final String sourceId = lookup.getSourceId();
88 final String groupId = lookup.lookup(GROUP_ID);
89 final String artifactId = lookup.lookup(ARTIFACT_ID);
90 final String version = lookup.lookup(VERSION);
91 final String archiveType = lookup.lookup(ARCHIVE_TYPE);
92 final String classifier = readClassifier(lookup);
93 try
94 {
95 final ArtifactId applicationId =
96 new ArtifactId.Builder().withGroupId(groupId).withName(artifactId)
97 .withVersion(version).withArchiveType(archiveType)
98 .withClassifier(classifier).build();
99
100 return applicationId;
101 }
102 catch (final Exception e)
103 {
104 throw new IllegalStateException(String.format(
105 "Cannot define the application identifier from JNDI %s: %s",
106 sourceId, e.getMessage(), e));
107 }
108 }
109 catch (final Exception e)
110 {
111 throw new IllegalStateException(String.format(
112 "Cannot define the application identifier: %s", e.getMessage(), e));
113 }
114 }
115
116 private static String readClassifier(final JndiContext lookup)
117 throws NamingException
118 {
119
120 final String classifier = lookup.lookup(CLASSIFIER);
121 if (StringUtils.isBlank(classifier))
122 {
123 return null;
124 }
125 return classifier;
126 }
127
128
129
130 }