📄 jndiutils.java
字号:
/*
* Copyright(C) 2008, NTT AT Co., Ltd.
* Project: AWGView
*
* Notes:
* N/A
*
* Record of change:
* Date Version Name Content
* 2008/12/15 1.0 TriNT First create
*/
package jp.co.ntt.awgview.server.jms;
import javax.jms.Destination;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueConnection;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import jp.co.ntt.awgview.server.common.LogWriter;
import jp.co.ntt.awgview.server.common.Setting;
/**
* Class name : JndiUtil <BR>
* Package : jp.co.nttat.awgstar.server.common <BR>
* Description: This class provides various utilities common of JNDI.<BR>
*
* Also contains the class DoneWatch, which contains the following methods: waitTillDone, allDone. <BR>
* Monitor class for asynchronous. Producer signals end of message stream;
* listener calls allDone() to notify consumer that the signal has arrived,
* while consumer calls waitTillDone() to wait for this notification.
*
*
* @author : AI&T
* @version : 1.0
*/
public class JndiUtils {
/*
* Define the System Property "USE_JNDI" true to use JNDI lookup() e.g.
* $JAVA -DUSE_JNDI=true ....
*/
public static final boolean USE_JNDI = Boolean.getBoolean("USE_JNDI");
public static final String FACTORY_INIT = "java.naming.factory.initial";
public static final String NAMING_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
public static final String NAMING_PROVIDER_URL = "java.naming.provider.url";
public static final String CON_FACTORY = "ConnectionFactory";
public static final String QUEUE_CON_FACTORY = "QueueConnectionFactory";
public static final String TOPIC_CON_FACTORY = "TopicConnectionFactory";
private static Context jndiContext = null;
/**
* Creates a JNDI InitialContext object if none exists yet. Then looks up
* the string argument and returns the associated object.
*
* @param name
* the name of the object to be looked up
*
* @return the object bound to <code>name</code>
* @throws javax.naming.NamingException
* if name cannot be found
*/
public static Object jndiLookup(String name) throws NamingException {
Object obj = null;
if (jndiContext == null) {
try {
String jbossHostPort = Setting.getServerAddress();
LogWriter.getSNMPLogger().info("With host/port:" + jbossHostPort);
if ((jbossHostPort != null)
&& (jbossHostPort.trim().length() > 0)) {
System.setProperty(FACTORY_INIT, NAMING_CONTEXT_FACTORY);
System.setProperty(NAMING_PROVIDER_URL, jbossHostPort.trim());
jndiContext = new InitialContext();
} else {
throw new NamingException();
}
} catch (NamingException e) {
LogWriter.getSNMPLogger().error("Could not create JNDI context: "
+ e.toString());
throw e;
}
}
try {
obj = jndiContext.lookup(name);
} catch (NamingException e) {
LogWriter.getSNMPLogger().error("JNDI lookup failed for:" + name + ": " + e.toString());
throw e;
}
return obj;
}
/**
* Returns a ConnectionFactory object. If provider uses JNDI, serves as a
* wrapper around jndiLookup method. If provider does not use JNDI,
* substitute provider-specific code here.
*
* @return a ConnectionFactory object
* @throws javax.naming.NamingException
* (or other exception) if name cannot be found
*/
public static ConnectionFactory getConnectionFactory()
throws NamingException {
return (ConnectionFactory) jndiLookup(CON_FACTORY);
}
/**
* Returns a QueueConnectionFactory object. If provider uses JNDI, serves as
* a wrapper around jndiLookup method. If provider does not use JNDI,
* substitute provider-specific code here.
*
* @return a QueueConnectionFactory object
* @throws javax.naming.NamingException
* (or other exception) if name cannot be found
*/
public static QueueConnectionFactory getQueueConnectionFactory()
throws NamingException {
return (QueueConnectionFactory) jndiLookup(QUEUE_CON_FACTORY);
}
/**
* Returns a QueueConnection object. Provider uses JNDI, serves as a wrapper
* around jndiLookup method.
*
* @return a QueueConnection object
* @throws javax.naming.NamingException
* (or other exception) if name cannot be found
*/
public static QueueConnection getQueueConnection() throws NamingException,
JMSException {
try {
QueueConnectionFactory connectionFactory = (QueueConnectionFactory) jndiLookup(QUEUE_CON_FACTORY);
QueueConnection queueConnection = connectionFactory
.createQueueConnection();
return queueConnection;
} catch (JMSException e) {
LogWriter.getSNMPLogger().error("Can not create TopicConnectionFactory: " + e.toString());
throw e;
} catch (NamingException e) {
LogWriter.getSNMPLogger().error("Can not create TopicConnectionFactory: " + e.toString());
throw e;
}
}
/**
* Returns a Queue object. Provider uses JNDI, serves as a wrapper around
* jndiLookup method.
*
* @param name
* String specifying queue name
* @return Queue
* @throws javax.naming.NamingException
* (or other exception) if name cannot be found
*/
public static Queue getQueue(String name) throws NamingException {
try {
return (Queue) jndiLookup(name);
} catch (NamingException e) {
LogWriter.getSNMPLogger().error("Can not get Queue from JBoss server with name: " + name);
LogWriter.getSNMPLogger().error(e.toString());
throw e;
}
}
/**
* Returns a Queue object. If provider uses JNDI, serves as a wrapper around
* jndiLookup method. If provider does not use JNDI, substitute
* provider-specific code here.
*
* @param name
* String specifying queue name
* @param session
* a QueueSession object
*
* @return a Queue object
* @throws javax.naming.NamingException
* (or other exception) if name cannot be found
*/
public static javax.jms.Queue getQueue(String name, Session session)
throws NamingException, JMSException {
if (USE_JNDI) {
return (javax.jms.Queue) jndiLookup(name);
} else {
return session.createQueue(name);
}
}
/**
* Returns a TopicConnectionFactory object. provider uses JNDI, serves as a
* wrapper around jndiLookup method.
*
* @return a TopicConnectionFactory object
* @throws javax.naming.NamingException
* (or other exception) if name cannot be found
*/
public static TopicConnectionFactory getTopicConnectionFactory()
throws NamingException {
return (TopicConnectionFactory) jndiLookup(TOPIC_CON_FACTORY);
}
/**
* Returns a TopicConnection object. Provider uses JNDI, serves as a wrapper
* around jndiLookup method.
*
* @return a TopicConnection object
* @throws javax.naming.NamingException
* (or other exception) if name cannot be found
*/
public static TopicConnection getTopicConnection() throws NamingException,
JMSException {
try {
TopicConnectionFactory connectionFactory = (TopicConnectionFactory) jndiLookup(TOPIC_CON_FACTORY);
TopicConnection topicConnection = connectionFactory
.createTopicConnection();
return topicConnection;
} catch (NamingException e) {
LogWriter.getSNMPLogger().error("Can not create TopicConnectionFactory: " + e.toString());
throw e;
} catch (JMSException e) {
LogWriter.getSNMPLogger().error("Can not create TopicConnectionFactory: " + e.toString());
throw e;
}
}
/**
* Returns a Topic object. If provider uses JNDI, serves as a wrapper around
* jndiLookup method. If provider does not use JNDI, substitute
* provider-specific code here.
*
* @param name
* String specifying topic name
* @param session
* a TopicSession object
*
* @return a Topic object
* @throws javax.naming.NamingException
* (or other exception) if name cannot be found
*/
public static javax.jms.Topic getTopic(String name, Session session)
throws NamingException, JMSException {
if (USE_JNDI) {
return (javax.jms.Topic) jndiLookup(name);
} else {
return session.createTopic(name);
}
}
/**
* Returns a Topic object. If provider uses JNDI, serves as a wrapper around
* jndiLookup method.
*
* @param name
* String specifying topic name
*
* @return a Topic object
* @throws javax.naming.NamingException
* (or other exception) if name cannot be found
*/
public static Topic getTopic(String name) throws NamingException {
try {
return (Topic) jndiLookup(name);
} catch (NamingException e) {
LogWriter.getSNMPLogger().error("Can not get Topic from JBoss server with name: " + name);
LogWriter.getSNMPLogger().error(e.toString());
throw e;
}
}
/**
* Creates a JNDI InitialContext object if none exists yet.
* Then looks up the string argument and returns the associated object.
*
* @param name
* @return Destination
* @throws NamingException
*/
public static Destination getDestination(String name)
throws NamingException {
try {
return (javax.jms.Destination) jndiLookup(name);
} catch (NamingException e) {
LogWriter.getSNMPLogger().error("Can not get Destination from JBoss server with name: " + name);
LogWriter.getSNMPLogger().error(e.toString());
throw e;
}
}
/**
* Calls System.exit().
*
* @param result
* The exit result; 0 indicates no errors
*/
public static void exit(int result) {
System.exit(result);
}
/**
* Initial Context
*
* @param theContext
* @throws NamingException
*/
public static void init(Context theContext) throws NamingException {
if ((jndiContext = theContext) == null) {
jndiContext = new InitialContext();
}
}
/**
* Retrieves the named object
*
* @param ctx
* @param name
* @return String
* @throws NamingException
*/
public static String lookup(Context ctx, String name)
throws NamingException {
if (ctx == null) {
return null;
}
try {
return (String) ctx.lookup(name);
} catch (NamingException e) {
LogWriter.getSNMPLogger().error(e.toString());
throw e;
}
}
/**
* Binds a name to an object.
*
* @param topicName
* @param topic
* @throws NamingException
*/
public static void bind(String topicName, Topic topic)
throws NamingException {
try {
if (jndiContext == null) {
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
LogWriter.getSNMPLogger().error("Could not create JNDI context: " + e.toString());
throw e;
}
}
jndiContext.bind(topicName, topic);
} catch (NamingException e) {
LogWriter.getSNMPLogger().error("Could not create JNDI context: " + e.toString());
throw e;
}
}
/**
* Monitor class for asynchronous. Producer signals end of message stream;
* listener calls allDone() to notify consumer that the signal has arrived,
* while consumer calls waitTillDone() to wait for this notification.
*/
static public class DoneWatch {
boolean done = false;
/**
* Waits until done is set to true.
*/
public void waitTillDone() {
synchronized (this) {
while (!done) {
try {
this.wait();
} catch (InterruptedException ie) {
}
}
}
}
/**
* Sets done to true.
*/
public void allDone() {
synchronized (this) {
done = true;
this.notify();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -