2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.shale.examples.sqlbrowser;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import javax.faces.FacesException;
25 import javax.faces.context.FacesContext;
26 import javax.faces.model.SelectItem;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NameClassPair;
30 import javax.naming.NamingEnumeration;
31 import javax.sql.DataSource;
32 import org.apache.shale.tiger.managed.Bean;
33 import org.apache.shale.tiger.managed.Scope;
34
35 /***
36 * <p>Application scope managed bean for the SQL Browser application.</p>
37 */
38 @Bean(name="appbean", scope=Scope.APPLICATION)
39 public class Application {
40
41
42 public Application() {
43 System.err.println("Application constructor()");
44 }
45
46
47
48
49
50 /***
51 * <p>The prefix of the JNDI name for all relevant data sources.</p>
52 */
53 private static final String PREFIX = "java:comp/env";
54
55
56
57
58
59 /***
60 * <p>The list of <code>DataSource</code>s available via JNDI for this
61 * web application.</p>
62 */
63 private SelectItem[] dataSources = null;
64
65
66
67
68
69 /***
70 * <p>Return the list of <code>DataSource</code>s available via JNDI
71 * for this web application.</p>
72 */
73 public SelectItem[] getDataSources() {
74
75 System.err.println("Application getDataSources()");
76 if (dataSources == null) {
77 List<SelectItem> list = new ArrayList<SelectItem>();
78 internal(list);
79 try {
80 InitialContext context = new InitialContext();
81 append(context, PREFIX, list);
82 } catch (Exception e) {
83 throw new FacesException(e);
84 }
85 dataSources = (SelectItem[]) list.toArray(new SelectItem[list.size()]);
86 }
87 System.err.println("Application getDataSources() --> " + dataSources.length);
88 return dataSources;
89
90 }
91
92
93
94
95
96 /***
97 * <p>Append any data sources found in the specified JNDI context
98 * to the specified list. If a JNDI subcontext is found, apply this
99 * method recursively.</p>
100 *
101 * @param context JNDI context to be searched
102 * @param prefix Prefix for the fully qualified resource names to be added
103 * @param list List of data sources to which new ones should be added
104 */
105 private void append(Context context, String prefix, List<SelectItem> list) {
106
107 ClassLoader cl = Thread.currentThread().getContextClassLoader();
108 if (cl == null) {
109 cl = Application.class.getClassLoader();
110 }
111 try {
112 NamingEnumeration<NameClassPair> entries = context.list(prefix);
113 while (entries.hasMore()) {
114 NameClassPair entry = entries.next();
115 Class clazz = cl.loadClass(entry.getClassName());
116 if (DataSource.class.isAssignableFrom(clazz)) {
117 list.add(new SelectItem(prefix + "/" + entry.getName(),
118 (prefix + "/" + entry.getName()).substring(PREFIX.length())));
119 } else if (Context.class.isAssignableFrom(clazz)) {
120 Context search = (Context) context.lookup(entry.getName());
121 append(search, prefix + "/" + entry.getName(), list);
122 }
123 }
124 } catch (Exception e) {
125 throw new FacesException(e);
126 }
127
128 }
129
130
131 /***
132 * <p>Append the default internal data source for this application.</p>
133 *
134 * @param list List of <code>SelectItem</code> of available data sources
135 */
136 private void internal(List<SelectItem> list) {
137
138 FacesContext context = FacesContext.getCurrentInstance();
139
140 list.add(new SelectItem("", "(Internal)"));
141
142 }
143
144
145 }