01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: *
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: /**
20: * @author Alexei Y. Zakharov
21: * @version $Revision: 1.1.2.4 $
22: */package org.apache.harmony.jndi.provider.dns;
23:
24: import java.util.Hashtable;
25:
26: import javax.naming.Context;
27: import javax.naming.Name;
28: import javax.naming.NamingException;
29: import javax.naming.spi.ObjectFactory;
30:
31: import org.apache.harmony.jndi.internal.nls.Messages;
32:
33: /**
34: * Factory to create DNS URL contexts.
35: */
36: public class dnsURLContextFactory implements ObjectFactory {
37:
38: /**
39: * Returns new instance of DNS URL context.
40: *
41: * @param obj
42: * either <code>null</code>, URL in string form or array of
43: * URL in string form
44: * @param name
45: * ignored
46: * @param nameCtx
47: * ignored
48: * @param environment
49: * is passed to the context being created
50: * @return created DNS context, an instance of either
51: * <code>dnsURLContext</code> or <code>DNSContext</code> class
52: * @throws IllegalArgumentException
53: * if bad <code>obj</code> is given
54: * @throws NamingException
55: * if such exception was encountered
56: * @see ObjectFactory#getObjectInstance(Object, Name, Context, Hashtable)
57: */
58: @SuppressWarnings("unchecked")
59: public Object getObjectInstance(Object obj, Name name,
60: Context nameCtx, Hashtable<?, ?> environment)
61: throws NamingException {
62: if (obj == null) {
63: return new dnsURLContext(environment);
64: } else if (obj instanceof String) {
65: Hashtable<Object, Object> newEnv = (Hashtable<Object, Object>) environment
66: .clone();
67:
68: newEnv.put(Context.PROVIDER_URL, obj);
69: return new DNSContext(newEnv);
70: } else if (obj instanceof String[]) {
71: Hashtable<Object, Object> newEnv = (Hashtable<Object, Object>) environment
72: .clone();
73: StringBuffer sb = new StringBuffer();
74: String urlArr[] = (String[]) obj;
75:
76: for (int i = 0; i < urlArr.length; i++) {
77: if (i != 0) {
78: sb.append(' ');
79: }
80: sb.append(urlArr[i]);
81: }
82: newEnv.put(Context.PROVIDER_URL, sb.toString());
83: return new DNSContext(newEnv);
84: } else {
85: // jndi.65=obj should be either null, String or array of String
86: throw new IllegalArgumentException(Messages
87: .getString("jndi.65")); //$NON-NLS-1$
88: }
89: }
90:
91: }
|