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: * $Header:$
018: */
019: package org.apache.beehive.controls.system.jndi.impl;
020:
021: import java.util.Hashtable;
022: import javax.naming.InitialContext;
023: import javax.naming.NamingException;
024:
025: import org.apache.beehive.controls.api.ControlException;
026: import org.apache.beehive.controls.api.bean.ControlImplementation;
027: import org.apache.beehive.controls.api.context.ControlBeanContext;
028: import org.apache.beehive.controls.api.context.Context;
029: import org.apache.beehive.controls.system.jndi.JndiControl;
030:
031: /**
032: * Implementation of the jndi control.
033: */
034: @ControlImplementation
035: public class JndiControlImpl implements JndiControl,
036: java.io.Serializable {
037:
038: /**
039: * @see JndiControl#getResource(String, Class)
040: */
041: public Object getResource(String resource, Class resourceClass)
042: throws ControlException {
043:
044: try {
045: InitialContext cntxt = getInitialContext();
046: Object obj = cntxt.lookup(resource);
047: if (resourceClass != null
048: && !(resourceClass.isInstance(obj)))
049: throw new ControlException("JNDI resource '" + resource
050: + "' is not an instance of '"
051: + resourceClass.getName() + "'");
052: else
053: return obj;
054: } catch (NamingException e) {
055: throw new ControlException("Cannot load JNDI resource '"
056: + resource + "'", e);
057: }
058: }
059:
060: /**
061: * Get the initial context.
062: *
063: * @return the initial context.
064: */
065: public InitialContext getInitialContext() throws ControlException {
066: if (_initialContext != null) {
067: return _initialContext;
068: }
069: Properties props = (Properties) _context
070: .getControlPropertySet(Properties.class);
071: String url = nullIfEmpty(props.url());
072: String factory = nullIfEmpty(props.factory());
073: if (url == null && factory == null) {
074: try {
075: return new InitialContext();
076: } catch (NamingException e) {
077: throw new ControlException(
078: "Cannot get default JNDI initial context", e);
079: }
080:
081: }
082: if (url == null || factory == null) {
083: throw new ControlException(
084: "Both the provider-url and jndi factory need to be provided");
085:
086: }
087: Hashtable<String, String> env = new Hashtable<String, String>();
088: env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, factory);
089: env.put(javax.naming.Context.PROVIDER_URL, url);
090:
091: String username = nullIfEmpty(props.jndiSecurityPrincipal());
092: if (username != null)
093: env.put(javax.naming.Context.SECURITY_PRINCIPAL, username);
094:
095: String password = nullIfEmpty(props.jndiSecurityCredentials());
096: if (password != null)
097: env
098: .put(javax.naming.Context.SECURITY_CREDENTIALS,
099: password);
100:
101: try {
102: return _initialContext = new InitialContext(env);
103: } catch (NamingException e) {
104: throw new ControlException(
105: "Cannot get JNDI initial context at provider '"
106: + url + "' with factory '" + factory + "'",
107: e);
108: }
109: }
110:
111: /**
112: * Return null if the given string is null or an empty string.
113: *
114: * @param str a string.
115: * @return null or the string.
116: */
117: protected String nullIfEmpty(String str) {
118: return (str == null || str.trim().length() == 0) ? null : str;
119: }
120:
121: @Context
122: ControlBeanContext _context;
123:
124: /**
125: * The initial context.
126: */
127: private transient InitialContext _initialContext;
128: }
|