1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.germinus.easyconf;
17
18 import javax.naming.InitialContext;
19 import javax.naming.NamingException;
20 import javax.sql.DataSource;
21
22 import org.apache.commons.configuration.DatabaseConfiguration;
23
24 /***
25 * Represents the URL to a datasource as specified in a properties file
26 * TODO: Add support for ASP applications
27 *
28 * @author Jorge Ferrer
29 * @version $Revision: 1.2 $
30 *
31 */
32 public class DatasourceURL {
33
34 private static final String DATASOURCE_PREFIX = Conventions.DATASOURCE_PREFIX;
35 private static InitialContext ctx = null;
36 private String dataSourceName;
37 private String companyId;
38 private String componentName;
39
40 /***
41 * @param datasourcePath
42 * @param companyId
43 * @param componentName
44 */
45 public DatasourceURL(String datasourcePath, String companyId, String componentName) {
46 this.dataSourceName = datasourcePath.substring(DATASOURCE_PREFIX.length());
47 this.companyId = companyId;
48 this.componentName = componentName;
49 }
50
51 public DataSource getDatasource() {
52 try {
53 return (DataSource)getContext().lookup(dataSourceName);
54 } catch (NamingException e) {
55 throw new ConfigurationException("Error loading datasource " + dataSourceName);
56 }
57 }
58
59 private synchronized InitialContext getContext() throws NamingException {
60 if (ctx == null) {
61 ctx = new InitialContext();
62 }
63 return ctx;
64 }
65
66 protected String getTableName() {
67 return "EasyConfProperties";
68 }
69
70 protected String getComponentColumnName() {
71 return "component";
72 }
73
74 protected String getKeyColumnName() {
75 return "key";
76 }
77
78 protected String getValueColumnName() {
79 return "value";
80 }
81
82 public static boolean isDatasource(String fileName) {
83 return fileName.startsWith(DATASOURCE_PREFIX);
84 }
85
86 public DatabaseConfiguration getConfiguration() {
87 return new DatabaseConfiguration(getDatasource(),
88 getTableName(),
89 getComponentColumnName(),
90 getKeyColumnName(),
91 getValueColumnName(),
92 componentName);
93 }
94
95 }