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:
019: package org.apache.jmeter.protocol.jms.client;
020:
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Properties;
024:
025: import javax.naming.Context;
026: import javax.naming.InitialContext;
027: import javax.naming.NamingException;
028: import javax.jms.Topic;
029:
030: import org.apache.jorphan.logging.LoggingManager;
031: import org.apache.log.Logger;
032: import org.apache.jmeter.protocol.jms.sampler.BaseJMSSampler;
033:
034: /**
035: * @author pete
036: *
037: * InitialContextFactory is responsible for getting and instance of the initial
038: * context. It is also responsible for looking up JMS topics and queues.
039: */
040: public class InitialContextFactory {
041:
042: private static java.util.HashMap MAP = new HashMap();
043:
044: static Logger log = LoggingManager.getLoggerForClass();
045:
046: public static synchronized Context lookupContext(String jndi,
047: String url, String useAuth, String user, String pwd) {
048: Context ctx = (Context) MAP.get(jndi + url);
049: if (ctx == null) {
050: Properties props = new Properties();
051: props.setProperty(Context.INITIAL_CONTEXT_FACTORY, jndi);
052: props.setProperty(Context.PROVIDER_URL, url);
053: if (useAuth != null
054: && useAuth.equals(BaseJMSSampler.required)
055: && user != null && pwd != null && user.length() > 0
056: && pwd.length() > 0) {
057: props.setProperty(Context.SECURITY_PRINCIPAL, user);
058: props.setProperty(Context.SECURITY_CREDENTIALS, pwd);
059: log.info("authentication properties set");
060: }
061: try {
062: ctx = new InitialContext(props);
063: log.info("created the JNDI initial context factory");
064: } catch (NamingException e) {
065: log.error("lookupContext:: " + e.getMessage());
066: }
067: if (ctx != null) {
068: MAP.put(jndi + url, ctx);
069: }
070: }
071: return ctx;
072: }
073:
074: /**
075: * Method will lookup a given topic using JNDI.
076: *
077: * @param ctx
078: * @param name
079: * @return the topic or null
080: */
081: public static synchronized Topic lookupTopic(Context ctx,
082: String name) {
083: Topic t = null;
084: if (name != null && ctx != null) {
085: try {
086: t = (Topic) ctx.lookup(name);
087: } catch (NamingException e) {
088: log.error("JNDI error: " + e.getMessage());
089: }
090: } else if (name == null) {
091: log.error("lookupTopic: name was null");
092: } else {
093: log.error("lookupTopic: Context was null");
094: }
095: return t;
096: }
097:
098: /**
099: * clear all the InitialContext objects.
100: */
101: public static void close() {
102: Iterator itr = MAP.keySet().iterator();
103: while (itr.hasNext()) {
104: Context ctx = (Context) MAP.get(itr.next());
105: try {
106: ctx.close();
107: } catch (NamingException e) {
108: log.error(e.getMessage());
109: }
110: }
111: log
112: .info("InitialContextFactory.close() called and Context instances cleaned up");
113: }
114: }
|