KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > jms > MantaConnectionFactory


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Nimo.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.api.jms;
47
48 import org.mr.MantaAgent;
49 import org.xml.sax.SAXException JavaDoc;
50 import org.xml.sax.InputSource JavaDoc;
51 import org.w3c.dom.Document JavaDoc;
52 import org.w3c.dom.Element JavaDoc;
53
54
55 import javax.jms.*;
56 import javax.naming.NamingException JavaDoc;
57 import javax.naming.Reference JavaDoc;
58 import javax.naming.Referenceable JavaDoc;
59 import javax.naming.StringRefAddr JavaDoc;
60 import javax.xml.parsers.ParserConfigurationException JavaDoc;
61 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
62 import javax.xml.parsers.DocumentBuilder JavaDoc;
63
64 import java.util.Vector JavaDoc;
65 import java.io.Serializable JavaDoc;
66 import java.io.StringReader JavaDoc;
67 import java.io.IOException JavaDoc;
68
69 /**
70  * A <CODE>ConnectionFactory</CODE> object encapsulates a set of connection
71  * configuration parameters that has been defined by an administrator. A client
72  * uses it to create a connection with a JMS provider.
73  *
74  * <P>
75  * A <CODE>ConnectionFactory</CODE> object is a JMS administered object and
76  * supports concurrent use.
77  *
78  * <P>
79  * JMS administered objects are objects containing configuration information
80  * that are created by an administrator and later used by JMS clients. They
81  * make it practical to administer the JMS API in the enterprise.
82  *
83  * <P>
84  * Although the interfaces for administered objects do not explicitly depend on
85  * the Java Naming and Directory Interface (JNDI) API, the JMS API establishes
86  * the convention that JMS clients find administered objects by looking them up
87  * in a JNDI namespace.
88  *
89  * <P>
90  * An administrator can place an administered object anywhere in a namespace.
91  * The JMS API does not define a naming policy.
92  *
93  * <P>
94  * It is expected that JMS providers will provide the tools an administrator
95  * needs to create and configure administered objects in a JNDI namespace. JMS
96  * provider implementations of administered objects should be both <CODE>
97  * javax.jndi.Referenceable</CODE> and <CODE>java.io.Serializable</CODE> so
98  * that they can be stored in all JNDI naming contexts. In addition, it is
99  * recommended that these implementations follow the JavaBeans <SUP>
100  * <FONT SIZE="-2">TM</FONT></SUP> design patterns.
101  *
102  * <P>
103  * This strategy provides several benefits:
104  *
105  * <UL>
106  * <LI>It hides provider-specific details from JMS clients.
107  * <LI>It abstracts administrative information into objects in the Java
108  * programming language ("Java objects") that are easily organized and
109  * administered from a common management console.
110  * <LI>Since there will be JNDI providers for all popular naming services,
111  * this means that JMS providers can deliver one implementation of administered
112  * objects that will run everywhere.
113  * </UL>
114  *
115  * <P>
116  * An administered object should not hold on to any remote resources. Its
117  * lookup should not use remote resources other than those used by the JNDI API
118  * itself.
119  *
120  * <P>
121  * Clients should think of administered objects as local Java objects. Looking
122  * them up should not have any hidden side effects or use surprising amounts of
123  * local resources.
124  *
125  * @author Nimo
126  */

127 public class MantaConnectionFactory implements Serializable JavaDoc, Referenceable JavaDoc,
128                                                  ConnectionFactory {
129     /**
130      * generated <code>serialVersionUID</code>
131      */

132     private static final long serialVersionUID = -3363600666809978286L;
133     String JavaDoc name;
134
135     /**
136      * Constructors:
137      * These don't really do anything. There's no special setup
138      * needed for a MantaConnectionFactory.
139      *
140      */

141     public MantaConnectionFactory() {
142             this(MantaConnection.JMS_AGENT_NAME);
143     }
144     public MantaConnectionFactory(String JavaDoc s) {
145         this.name = s;
146     }
147
148     /**
149      * Creates a connection with the default user identity. The connection is
150      * created in stopped mode. No messages will be delivered until the <code>Connection.start</code>
151      * method is explicitly called.
152      *
153      * @return a newly created connection
154      *
155      * @exception JMSException
156      * if the JMS provider fails to create the connection due to
157      * some internal error.
158      * @exception JMSSecurityException
159      * if client authentication fails due to an invalid user
160      * name or password.
161      * @since 1.1
162      */

163     public Connection createConnection() throws JMSException{
164
165         return createConnection("unknown","unknown");
166     }//createConnection
167

168     /**
169      * Creates a connection with the specified user identity. The connection is
170      * created in stopped mode. No messages will be delivered until the <code>Connection.start</code>
171      * method is explicitly called.
172      *
173      * @param userName
174      * the caller's user name
175      * @param password
176      * the caller's password
177      *
178      * @return a newly created connection
179      *
180      * @exception JMSException
181      * if the JMS provider fails to create the connection due to
182      * some internal error.
183      * @exception JMSSecurityException
184      * if client authentication fails due to an invalid user
185      * name or password.
186      * @since 1.1
187      */

188     public Connection createConnection(String JavaDoc userName, String JavaDoc password) throws JMSException{
189
190         MantaConnection cc = new MantaConnection(this,userName,password);
191         addNewConnection(cc);
192         return cc;
193     }//createConnection
194

195
196
197     /*
198      * This method is used to add a new connection to the repository of
199      * connections created in this factory.
200      */

201     void addNewConnection (Connection connection) {
202         createdConnections.addElement(connection);
203     }
204
205     /*
206      * this method is used to remove the connections from the repository.
207      * upon closing, the connections remove themselves from the repository.
208      */

209     void removeConnection (Connection connection) {
210         createdConnections.removeElement(connection);
211     }
212
213     public String JavaDoc toString()
214     {
215         return name;
216     }
217
218     /**
219      * Used to return a reference to the MantaAgent running on this computer.
220      * @return MantaAgent instance.
221      */

222     MantaAgent getChannel()
223     {
224        MantaAgent agent = MantaAgent.getInstance();
225        agent.init();
226        return agent;
227     }
228
229
230    /**
231     * Since this is an object that might be registered in the JNDI, it should
232     * implement the Referenceable interface.
233     */

234    public Reference JavaDoc getReference() throws NamingException JavaDoc {
235
236       return new Reference JavaDoc (
237             getClass().getName(),
238             new StringRefAddr JavaDoc("name",this.name),
239             MantaConnectionFactoryFactory.class.getName(),
240             null
241       );
242    }
243
244    //XA stuff:
245

246
247
248    // Holds the connections created by this factory.
249
Vector JavaDoc createdConnections = new Vector JavaDoc();
250
251
252     /**
253      * This method is used by an application to set the configuration by itself, without using
254      * a configuration file and supply it as a VM parameter.
255      *
256      * @param element
257      * Configuration XML given as a DOM element
258      */

259     public void setConfiguration(Element JavaDoc element){
260         MantaAgent.setConfiguration(element);
261     }
262
263     /**
264      * This method is used by an application to set the configuration by itself, without using
265      * a configuration file and supply it as a VM parameter.
266      *
267      * @param strConfiguration
268      * Configuration XML given as a String
269      * @throws ParserConfigurationException if a DocumentBuilder cannot be created which satisfies the configuration requested
270      * @throws SAXException if any parse errors occur
271      * @throws IOException if any IO errors occur
272      */

273     public void setConfiguration(String JavaDoc strConfiguration) throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
274         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
275         factory.setIgnoringElementContentWhitespace(true);
276         DocumentBuilder JavaDoc xmlBuilder= factory.newDocumentBuilder();
277         Document JavaDoc xmlDoc = xmlBuilder.parse(new InputSource JavaDoc(new StringReader JavaDoc(strConfiguration)));
278         Element JavaDoc configurationElement = xmlDoc.getDocumentElement();
279         MantaAgent.setConfiguration(configurationElement);
280     }
281 }
282
Popular Tags