KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmessaging > JBossMessagingAdmin


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jbossmessaging;
23
24 import javax.management.MBeanServerConnection JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28
29 import org.jboss.logging.Logger;
30 import org.jboss.util.NestedRuntimeException;
31
32 import org.objectweb.jtests.jms.admin.Admin;
33
34 /**
35  * JBossMessagingAdmin.
36  *
37  * @author <a HREF="richard.achmatowicz@jboss.com">Richard Achmatowicz</a>
38  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
39  * @version $Revision: 38213 $
40  */

41 public class JBossMessagingAdmin implements Admin
42 {
43    private Logger log = Logger.getLogger(JBossMessagingAdmin.class);
44
45    private InitialContext JavaDoc initialContext ;
46    private MBeanServerConnection JavaDoc server ;
47
48    protected static final String JavaDoc name ;
49    protected static final ObjectName JavaDoc serverPeer;
50    protected static final ObjectName JavaDoc namingService;
51    
52    static
53    {
54       try
55       {
56      name = JBossMessagingAdmin.class.getName() ;
57          serverPeer = new ObjectName JavaDoc("jboss.messaging:service=ServerPeer");
58          namingService = new ObjectName JavaDoc("jboss:service=Naming");
59       }
60       catch (Exception JavaDoc e)
61       {
62          throw new NestedRuntimeException(e);
63       }
64    }
65    
66    public JBossMessagingAdmin() throws Exception JavaDoc
67    {
68       try {
69       log.info("Initializing...") ;
70
71           // set up the initial naming service context
72
initialContext = new InitialContext JavaDoc() ;
73
74           // set up the MBean server connection
75
String JavaDoc adaptorName = System.getProperty("jbosstest.server.name","jmx/invoker/RMIAdaptor") ;
76           server = (MBeanServerConnection JavaDoc) initialContext.lookup(adaptorName) ;
77        
78       } catch (Exception JavaDoc e) {
79          throw new NestedRuntimeException(e);
80       }
81    }
82
83    public String JavaDoc getName() {
84       return name ;
85    }
86
87    private MBeanServerConnection JavaDoc getServer() {
88        return server ;
89    }
90
91    public InitialContext JavaDoc createInitialContext() throws NamingException JavaDoc {
92       return initialContext ;
93    }
94
95    public void createQueue(String JavaDoc name)
96    {
97       try
98       {
99          MBeanServerConnection JavaDoc server = getServer();
100          try
101          {
102             server.invoke(serverPeer, "createQueue", new Object JavaDoc[] { name, name }, new String JavaDoc[] { String JavaDoc.class.getName(), String JavaDoc.class.getName() } );
103          }
104          catch (Exception JavaDoc ignored)
105          {
106             log.trace("Ignored", ignored);
107          }
108          ObjectName JavaDoc queueName = new ObjectName JavaDoc("jboss.messaging.destination:service=Queue,name=" + name);
109          server.invoke(queueName, "removeAllMessages", null, null);
110       }
111       catch (Exception JavaDoc e)
112       {
113          throw new NestedRuntimeException(e);
114       }
115    }
116
117    public void deleteQueue(String JavaDoc name)
118    {
119       try
120       {
121          MBeanServerConnection JavaDoc server = getServer();
122          ObjectName JavaDoc queueName = new ObjectName JavaDoc("jboss.messaging.destination:service=Queue,name=" + name);
123          server.invoke(queueName, "removeAllMessages", null, null);
124          server.invoke(serverPeer, "destroyQueue", new Object JavaDoc[] { name }, new String JavaDoc[] { String JavaDoc.class.getName() } );
125       }
126       catch (Exception JavaDoc e)
127       {
128          throw new NestedRuntimeException(e);
129       }
130    }
131
132    public void createTopic(String JavaDoc name)
133    {
134       try
135       {
136          MBeanServerConnection JavaDoc server = getServer();
137          try
138          {
139             server.invoke(serverPeer, "createTopic", new Object JavaDoc[] { name, name }, new String JavaDoc[] { String JavaDoc.class.getName(), String JavaDoc.class.getName() } );
140          }
141          catch (Exception JavaDoc ignored)
142          {
143             log.trace("Ignored", ignored);
144          }
145          ObjectName JavaDoc topicName = new ObjectName JavaDoc("jboss.messaging.destination:service=Topic,name=" + name);
146          server.invoke(topicName, "removeAllMessages", null, null);
147       }
148       catch (Exception JavaDoc e)
149       {
150          throw new NestedRuntimeException(e);
151       }
152    }
153
154    public void deleteTopic(String JavaDoc name)
155    {
156       try
157       {
158          MBeanServerConnection JavaDoc server = getServer();
159          ObjectName JavaDoc topicName = new ObjectName JavaDoc("jboss.messaging.destination:service=Topic,name=" + name);
160          server.invoke(topicName, "removeAllMessages", null, null);
161          server.invoke(serverPeer, "destroyTopic", new Object JavaDoc[] { name }, new String JavaDoc[] { String JavaDoc.class.getName() } );
162       }
163       catch (Exception JavaDoc e)
164       {
165          throw new NestedRuntimeException(e);
166       }
167    }
168
169    public void createConnectionFactory(String JavaDoc name)
170    {
171       try
172       {
173          MBeanServerConnection JavaDoc server = getServer();
174          server.invoke(namingService, "createAlias", new Object JavaDoc[] { name, "ConnectionFactory" }, new String JavaDoc[] { String JavaDoc.class.getName(), String JavaDoc.class.getName() } );
175       }
176       catch (Exception JavaDoc e)
177       {
178          throw new NestedRuntimeException(e);
179       }
180    }
181
182    public void deleteConnectionFactory(String JavaDoc name)
183    {
184       try
185       {
186          MBeanServerConnection JavaDoc server = getServer();
187          server.invoke(namingService, "removeAlias", new Object JavaDoc[] { name }, new String JavaDoc[] { String JavaDoc.class.getName() } );
188       }
189       catch (Exception JavaDoc e)
190       {
191          throw new NestedRuntimeException(e);
192       }
193    }
194
195    public void createQueueConnectionFactory(String JavaDoc name)
196    {
197        createConnectionFactory(name) ;
198    }
199
200    public void deleteQueueConnectionFactory(String JavaDoc name)
201    {
202        deleteConnectionFactory(name) ;
203    }
204
205    public void createTopicConnectionFactory(String JavaDoc name)
206    {
207        createConnectionFactory(name) ;
208    }
209
210    public void deleteTopicConnectionFactory(String JavaDoc name)
211    {
212        deleteConnectionFactory(name) ;
213    }
214
215 }
216
Popular Tags