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: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.openejb.client;
17:
18: import javax.naming.Context;
19: import javax.naming.InitialContext;
20: import javax.naming.NamingException;
21: import java.net.URI;
22: import java.util.Properties;
23:
24: /**
25: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
26: */
27: public class ServiceLocator {
28:
29: private final Context context;
30:
31: public ServiceLocator(URI serverUri) throws NamingException {
32: this (serverUri, null, null, null);
33: }
34:
35: public ServiceLocator(URI serverUri, String username,
36: String password) throws NamingException {
37: this (serverUri, username, password, null);
38: }
39:
40: public ServiceLocator(URI serverUri, String username,
41: String password, String realm) throws NamingException {
42: Properties properties = new Properties();
43: properties
44: .put(Context.INITIAL_CONTEXT_FACTORY,
45: "org.apache.openejb.client.RemoteInitialContextFactory");
46: properties.put(Context.PROVIDER_URL, serverUri.toString());
47: if (username != null && password != null) {
48: properties.put(Context.SECURITY_PRINCIPAL, username);
49: properties.put(Context.SECURITY_CREDENTIALS, password);
50: if (realm != null)
51: properties.put("openejb.authentication.realmName",
52: realm);
53: }
54: this .context = new InitialContext(properties);
55: }
56:
57: public ServiceLocator(Context context) {
58: this .context = context;
59: }
60:
61: public Object lookup(String name) {
62: try {
63: return context.lookup(name);
64: } catch (NamingException e) {
65: throw new IllegalArgumentException(e);
66: } catch (RuntimeException e) {
67: throw new IllegalArgumentException(e);
68: }
69: }
70:
71: }
|