001 /*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 *
020 */
021 package org.apache.qpid.config;
022
023 import org.apache.qpid.config.ConnectionFactoryInitialiser;
024 import org.apache.qpid.config.ConnectorConfig;
025 import org.apache.qpid.client.JMSAMQException;
026
027 import javax.jms.ConnectionFactory;
028 import javax.jms.JMSException;
029 import javax.management.MBeanServerConnection;
030 import javax.management.ObjectName;
031 import javax.management.MBeanException;
032 import javax.naming.InitialContext;
033 import javax.naming.NamingException;
034 import javax.naming.NameNotFoundException;
035 import java.util.Hashtable;
036
037 public class JBossConnectionFactoryInitialiser implements ConnectionFactoryInitialiser
038 {
039 public ConnectionFactory getFactory(ConnectorConfig config) throws JMSException
040 {
041 ConnectionFactory cf = null;
042 InitialContext ic = null;
043 Hashtable ht = new Hashtable();
044 ht.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
045 String jbossHost = System.getProperty("jboss.host", "eqd-lxamq01");
046 String jbossPort = System.getProperty("jboss.port", "1099");
047 ht.put(InitialContext.PROVIDER_URL, "jnp://" + jbossHost + ":" + jbossPort);
048 ht.put(InitialContext.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
049
050 try
051 {
052 ic = new InitialContext(ht);
053 if (!doesDestinationExist("topictest.messages", ic))
054 {
055 deployTopic("topictest.messages", ic);
056 }
057 if (!doesDestinationExist("topictest.control", ic))
058 {
059 deployTopic("topictest.control", ic);
060 }
061
062 cf = (ConnectionFactory) ic.lookup("/ConnectionFactory");
063 return cf;
064 }
065 catch (NamingException e)
066 {
067 throw new JMSAMQException("Unable to lookup object: " + e, e);
068 }
069 catch (Exception e)
070 {
071 throw new JMSAMQException("Error creating topic: " + e, e);
072 }
073 }
074
075 private boolean doesDestinationExist(String name, InitialContext ic) throws Exception
076 {
077 try
078 {
079 ic.lookup("/" + name);
080 }
081 catch (NameNotFoundException e)
082 {
083 return false;
084 }
085 return true;
086 }
087
088 private void deployTopic(String name, InitialContext ic) throws Exception
089 {
090 MBeanServerConnection mBeanServer = lookupMBeanServerProxy(ic);
091
092 ObjectName serverObjectName = new ObjectName("jboss.messaging:service=ServerPeer");
093
094 String jndiName = "/" + name;
095 try
096 {
097 mBeanServer.invoke(serverObjectName, "createTopic",
098 new Object[]{name, jndiName},
099 new String[]{"java.lang.String", "java.lang.String"});
100 }
101 catch (MBeanException e)
102 {
103 System.err.println("Error: " + e);
104 System.err.println("Cause: " + e.getCause());
105 }
106 }
107
108 private MBeanServerConnection lookupMBeanServerProxy(InitialContext ic) throws NamingException
109 {
110 return (MBeanServerConnection) ic.lookup("jmx/invoker/RMIAdaptor");
111 }
112 }
|