Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Application |
|
| 0.0;0 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to you under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | * | |
17 | * $Id: Application.java 464373 2006-10-16 04:21:54Z rahul $ | |
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 | 0 | public Application() { |
43 | 0 | System.err.println("Application constructor()"); |
44 | 0 | } |
45 | ||
46 | ||
47 | // ------------------------------------------------------ Manifest Constants | |
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 | // ------------------------------------------------------ Instance Variables | |
57 | ||
58 | ||
59 | /** | |
60 | * <p>The list of <code>DataSource</code>s available via JNDI for this | |
61 | * web application.</p> | |
62 | */ | |
63 | 0 | private SelectItem[] dataSources = null; |
64 | ||
65 | ||
66 | // ---------------------------------------------------------- Public Methods | |
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 | 0 | System.err.println("Application getDataSources()"); |
76 | 0 | if (dataSources == null) { |
77 | 0 | List<SelectItem> list = new ArrayList<SelectItem>(); |
78 | 0 | internal(list); |
79 | try { | |
80 | 0 | InitialContext context = new InitialContext(); |
81 | 0 | append(context, PREFIX, list); |
82 | 0 | } catch (Exception e) { |
83 | 0 | throw new FacesException(e); |
84 | 0 | } |
85 | 0 | dataSources = (SelectItem[]) list.toArray(new SelectItem[list.size()]); |
86 | } | |
87 | 0 | System.err.println("Application getDataSources() --> " + dataSources.length); |
88 | 0 | return dataSources; |
89 | ||
90 | } | |
91 | ||
92 | ||
93 | // --------------------------------------------------------- Private Methods | |
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 | 0 | ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
108 | 0 | if (cl == null) { |
109 | 0 | cl = Application.class.getClassLoader(); |
110 | } | |
111 | try { | |
112 | 0 | NamingEnumeration<NameClassPair> entries = context.list(prefix); |
113 | 0 | while (entries.hasMore()) { |
114 | 0 | NameClassPair entry = entries.next(); |
115 | 0 | Class clazz = cl.loadClass(entry.getClassName()); |
116 | 0 | if (DataSource.class.isAssignableFrom(clazz)) { |
117 | 0 | list.add(new SelectItem(prefix + "/" + entry.getName(), |
118 | (prefix + "/" + entry.getName()).substring(PREFIX.length()))); | |
119 | 0 | } else if (Context.class.isAssignableFrom(clazz)) { |
120 | 0 | Context search = (Context) context.lookup(entry.getName()); |
121 | 0 | append(search, prefix + "/" + entry.getName(), list); |
122 | } | |
123 | 0 | } |
124 | 0 | } catch (Exception e) { |
125 | 0 | throw new FacesException(e); |
126 | 0 | } |
127 | ||
128 | 0 | } |
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 | 0 | FacesContext context = FacesContext.getCurrentInstance(); |
139 | // FIXME - localize the label | |
140 | 0 | list.add(new SelectItem("", "(Internal)")); |
141 | ||
142 | 0 | } |
143 | ||
144 | ||
145 | } |