1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.impl.config.ds;
17
18 import java.io.Serializable;
19
20 import javax.naming.Context;
21 import javax.naming.InitialContext;
22
23 import de.smartics.properties.api.config.ds.DataSourceConfiguration;
24 import de.smartics.properties.api.core.app.JndiContext;
25
26
27
28
29
30 public class DataSourceConfigurationJndiLoader implements Serializable
31 {
32
33
34
35
36
37
38
39
40
41
42 private static final long serialVersionUID = 1L;
43
44
45
46
47
48
49
50
51
52
53 public DataSourceConfigurationJndiLoader()
54 {
55 }
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public DataSourceConfiguration load() throws IllegalStateException
74 {
75 try
76 {
77 final Context context = new InitialContext();
78 final JndiContext lookup = JndiContext.boot(context);
79 final String jndiName = lookup.lookup(DataSourceConfiguration.JNDI_NAME);
80
81 if (jndiName == null)
82 {
83 throw new IllegalStateException(String.format(
84 "Cannot find '%s' in the JNDI context to configure the"
85 + " access to a data source.",
86 DataSourceConfiguration.JNDI_NAME));
87 }
88
89 final boolean createTable =
90 lookup.lookupBoolean(DataSourceConfiguration.CREATE_TABLE);
91 final boolean dropTable =
92 lookup.lookupBoolean(DataSourceConfiguration.DROP_TABLE);
93
94 final DataSourceConfigurationBuilder builder =
95 new DataSourceConfigurationBuilder();
96 builder.setConfigSourceId(lookup.getSourceId());
97 builder.setJndiName(jndiName);
98 builder.setCreateTable(createTable);
99 builder.setDropTable(dropTable);
100
101 final DataSourceConfiguration config = builder.build();
102 return config;
103 }
104 catch (final Exception e)
105 {
106 throw new IllegalStateException(
107 String.format("Cannot access JNDI context to configure the access to"
108 + " a data source."), e);
109 }
110 }
111
112
113
114 }