|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AdministeredObjectsLocator.java | - | 46.7% | 100% | 55.6% |
|
1 | /** | |
2 | * JTRunner is free software; you can redistribute it and/or modify it under the | |
3 | * terms of the GNU General Public License as published by the Free Software | |
4 | * Foundation; either version 2, or (at your option) any later version. | |
5 | */ | |
6 | ||
7 | package jtr.jms; | |
8 | ||
9 | import java.util.Hashtable; | |
10 | ||
11 | import javax.naming.Context; | |
12 | import javax.naming.NamingException; | |
13 | import javax.naming.InitialContext; | |
14 | ||
15 | import javax.jms.ConnectionFactory; | |
16 | import javax.jms.Destination; | |
17 | ||
18 | import jtr.enterprise.LocatorException; | |
19 | import org.apache.log4j.Logger; | |
20 | ||
21 | /** | |
22 | * This static class exposes methods useful for retrieving JMS administered | |
23 | * objects.<br> | |
24 | * They include: | |
25 | * <br> | |
26 | * 1. <code>ConnectionFactory</code><br> | |
27 | * 2. <code>QueueConnectionFactory</code><br> | |
28 | * 3. <code>TopicConnectionFactory</code><br> | |
29 | * 4. <code>Destination</code><br> | |
30 | * 5. <code>Queue</code><br> | |
31 | * 6. <code>Topic</code><br> | |
32 | * The retrieval is based on the information provided with the | |
33 | * <code>enterprise</code> jtr-configuration element.<br> | |
34 | * | |
35 | * @author Francesco Russo (frusso@dev.java.net) | |
36 | * @version 4.0 | |
37 | * @since 1.1 | |
38 | */ | |
39 | public class AdministeredObjectsLocator { | |
40 | ||
41 | /** | |
42 | * Retrieves a <code>ConnectionFactory</code> using the provided JNDI | |
43 | * name.<br> | |
44 | * The initial context is created according to the input | |
45 | * <code>environment</code>: it represents the set of properties | |
46 | * specified into the <code>enterprise</code> element of the | |
47 | * <code>jtr.xml</code> configuration file the current | |
48 | * <code>IRunner</code> concrete implementation refers to.<br> | |
49 | * <b>Note:<b> this method returns <code>ConnectionFactory</code> | |
50 | * instances, so you should cast them to whatever you might actually need (<code>QueueConnectionFactory, TopicConnectionFactory</code>). | |
51 | * | |
52 | * @param jndiName | |
53 | * The actual name of the JMS administered object | |
54 | * @param environment | |
55 | * The set of properties required for creating the initial | |
56 | * context | |
57 | * @throws LocatorException | |
58 | * Something went wrong while retrieving the JMS administered | |
59 | * object | |
60 | * @return ConnectionFactory The concrete <code>ConnectionFactory</code> | |
61 | */ | |
62 | 14 | public static ConnectionFactory getConnectionFactory(String jndiName, Hashtable environment) throws LocatorException { |
63 | 14 | try { |
64 | 14 | Context ictx = getInitialContext(environment); |
65 | 14 | return (ConnectionFactory) ictx.lookup(jndiName); |
66 | } catch (NamingException ne) { | |
67 | 0 | logger.error("Unable to locate resource " + jndiName + ". Check configuration or JNDI name.", ne); |
68 | 0 | throw new LocatorException("Unable to locate resource " + jndiName + ". Check configuration or JNDI name.", ne); |
69 | } catch (ClassCastException cce) { | |
70 | 0 | logger.error("Unable to cast resource " + jndiName + " to javax.jms.ConnectionFactory.", cce); |
71 | 0 | throw new LocatorException("Unable to cast resource " + jndiName + " to javax.jms.ConnectionFactory.", cce); |
72 | } | |
73 | } | |
74 | ||
75 | /** | |
76 | * Retrieves a <code>Destination</code> using the provided JNDI name.<br> | |
77 | * The initial context is created according to the input | |
78 | * <code>environment</code>: it represents the set of properties | |
79 | * specified into the <code>enterprise</code> element of the | |
80 | * <code>jtr.xml</code> configuration file the current | |
81 | * <code>IRunner</code> concrete implementation refers to.<br> | |
82 | * <b>Note:<b> this method returns <code>Destination</code> instances, so | |
83 | * you should cast them to whatever you might actually need (<code>Queue, Topic</code>). | |
84 | * | |
85 | * @param jndiName | |
86 | * The actual name of the JMS administered object | |
87 | * @param environment | |
88 | * The set of properties required for creating the initial | |
89 | * context | |
90 | * @throws LocatorException | |
91 | * Something went wrong while retrieving the JMS administered | |
92 | * object | |
93 | * @return Destination The concrete <code>Destination</code> | |
94 | */ | |
95 | 14 | public static Destination getDestination(String jndiName, Hashtable environment) throws LocatorException { |
96 | 14 | try { |
97 | 14 | Context ictx = getInitialContext(environment); |
98 | 14 | return (Destination) ictx.lookup(jndiName); |
99 | } catch (NamingException ne) { | |
100 | 0 | logger.error("Unable to locate resource " + jndiName + ". Check configuration or JNDI name.", ne); |
101 | 0 | throw new LocatorException("Unable to locate resource " + jndiName + ". Check configuration or JNDI name.", ne); |
102 | } catch (ClassCastException cce) { | |
103 | 0 | logger.error("Unable to cast resource " + jndiName + " to javax.jms.Destination.", cce); |
104 | 0 | throw new LocatorException("Unable to cast resource " + jndiName + " to javax.jms.Destination.", cce); |
105 | } | |
106 | } | |
107 | ||
108 | 28 | private static Context getInitialContext(Hashtable env) throws NamingException { |
109 | 28 | return new InitialContext(env); |
110 | } | |
111 | ||
112 | private static Logger logger = Logger.getLogger(AdministeredObjectsLocator.class); | |
113 | } |
|