001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.jndi.tests.javax.naming;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.net.URL;
023: import java.util.Enumeration;
024: import java.util.Hashtable;
025: import java.util.Properties;
026:
027: import javax.naming.NamingException;
028:
029: import junit.framework.TestCase;
030: import org.apache.harmony.jndi.tests.javax.naming.util.Log;
031:
032: public class InitialContextLibTest extends TestCase {
033: private static final Log log = new Log(InitialContextLibTest.class);
034:
035: private static final String jndiProp = "jndi.properties";
036:
037: public void testConstructor_Lib() throws NamingException,
038: IOException {
039: //Comment this test case out because this test case
040: //needs complex configuration about jndi properties.
041:
042: // log.setMethod("testConstructor_Lib");
043: // InitialContext context = new InitialContext();
044: // Hashtable props = context.getEnvironment();
045: // Hashtable expected = readAllProps(null);
046: // assertEquals(expected, props);
047: }
048:
049: void printHashtable(Hashtable<?, ?> env) {
050: // TO DO: Need to remove
051: Enumeration<?> keys = env.keys();
052: while (keys.hasMoreElements()) {
053: Object key = keys.nextElement();
054: log.log(key + "=" + env.get(key));
055: }
056: }
057:
058: static Properties readAllProps(Hashtable<?, ?> env)
059: throws IOException {
060: // env param
061: Properties props = new Properties();
062: if (env != null) {
063: props = mergProps(props, env);
064: }
065:
066: // sys prop
067: props = mergSysProps(props, System.getProperties());
068:
069: // application resource
070: // ClassLoader classLoader = ClassLoader.getSystemClassLoader();
071: ClassLoader classLoader = Thread.currentThread()
072: .getContextClassLoader();
073: Enumeration<?> resources = classLoader.getResources(jndiProp);
074: while (resources.hasMoreElements()) {
075: URL url = (URL) resources.nextElement();
076: InputStream fis = url.openStream();
077: Properties resource = new Properties();
078: resource.load(fis);
079: fis.close();
080: props = mergProps(props, resource);
081: }
082:
083: // lib resource
084: /*
085: * try { File file = new File(System.getProperty("java.home"), "lib");
086: * file = new File(file, jndiProp); FileInputStream fis = new
087: * FileInputStream(file); Properties resource = new Properties();
088: * resource.load(fis); fis.close(); props = mergProps(props, resource); }
089: * catch (FileNotFoundException e) { System.out.println(e.toString()); }
090: */
091: return props;
092: }
093:
094: static Properties mergProps(Properties props, Hashtable<?, ?> env) {
095: Properties resource = new Properties();
096: resource.putAll(props);
097:
098: Hashtable<String, String> items = getItemsType();
099: Enumeration<?> keys = env.keys();
100: while (keys.hasMoreElements()) {
101: Object key = keys.nextElement();
102: String type = items.get(key);
103: Object oldObj = resource.get(key);
104: Object newObj = env.get(key);
105: if (type == null) {
106: resource.put(key, newObj);
107: continue;
108: }
109:
110: if (type.equals("F")) {
111: if ((oldObj == null) && (newObj != null)) {
112: resource.put(key, env.get(key));
113: }
114: } else if (type.equals("C")) {
115: if ((oldObj != null) && (newObj != null)) {
116: resource.put(key, (String) oldObj + ":"
117: + (String) newObj);
118: } else if ((oldObj == null) && (newObj != null)) {
119: resource.put(key, newObj);
120: }
121: }
122: }
123:
124: return resource;
125: }
126:
127: static Properties mergSysProps(Properties props, Hashtable<?, ?> env) {
128: Properties resource = new Properties();
129: resource.putAll(props);
130:
131: Hashtable<String, String> items = getSystemItemsType();
132: Enumeration<String> keys = items.keys();
133: while (keys.hasMoreElements()) {
134: Object key = keys.nextElement();
135: String type = items.get(key);
136: Object oldObj = resource.get(key);
137: Object newObj = env.get(key);
138:
139: if (type.equals("F")) {
140: if ((oldObj == null) && (newObj != null)) {
141: resource.put(key, env.get(key));
142: }
143: } else if (type.equals("C")) {
144: if ((oldObj == null) && (newObj != null)) {
145: resource.put(key, newObj);
146: }
147: }
148: }
149: return resource;
150: }
151:
152: static Hashtable<String, String> getItemsType() {
153: Hashtable<String, String> hashtable = new Hashtable<String, String>();
154: hashtable.put("java.naming.factory.initial", "F");
155: hashtable.put("java.naming.provider.url", "F");
156: hashtable.put("java.naming.factory.control", "C");
157: hashtable.put("java.naming.applet", "F");
158: hashtable.put("java.naming.authoritative", "F");
159: hashtable.put("java.naming.batchsize", "F");
160: hashtable.put("java.naming.dns.url", "F");
161: hashtable.put("java.naming.factory.object", "C");
162: hashtable.put("java.naming.factory.state", "C");
163: hashtable.put("java.naming.factory.url.pkgs", "C");
164: hashtable.put("java.naming.language", "F");
165: hashtable.put("java.naming.referral", "F");
166: hashtable.put("java.naming.security.authentication", "F");
167: hashtable.put("java.naming.security.credentials", "F");
168: hashtable.put("java.naming.security.principal", "F");
169: hashtable.put("java.naming.security.protocol", "F");
170: return hashtable;
171: }
172:
173: static Hashtable<String, String> getSystemItemsType() {
174: Hashtable<String, String> hashtable = new Hashtable<String, String>();
175: hashtable.put("java.naming.factory.initial", "F");
176: hashtable.put("java.naming.provider.url", "F");
177: hashtable.put("java.naming.factory.control", "C");
178: // hashtable.put("java.naming.applet", "F");
179: // hashtable.put("java.naming.authoritative", "F");
180: // hashtable.put("java.naming.batchsize", "F");
181: hashtable.put("java.naming.dns.url", "F");
182: hashtable.put("java.naming.factory.object", "C");
183: hashtable.put("java.naming.factory.state", "C");
184: hashtable.put("java.naming.factory.url.pkgs", "C");
185: // hashtable.put("java.naming.language", "F");
186: // hashtable.put("java.naming.referral", "F");
187: // hashtable.put("java.naming.security.authentication", "F");
188: // hashtable.put("java.naming.security.credentials", "F");
189: // hashtable.put("java.naming.security.principal", "F");
190: // hashtable.put("java.naming.security.protocol", "F");
191: return hashtable;
192: }
193:
194: }
|