KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > GenericConnectionFactory


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.mq;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import javax.jms.JMSException JavaDoc;
28
29 import org.jboss.logging.Logger;
30 import org.jboss.mq.il.ClientILService;
31 import org.jboss.mq.il.ServerIL;
32 import org.jboss.mq.il.ServerILFactory;
33
34 /**
35  * The RMI implementation of the DistributedConnectionFactory object
36  *
37  * @author Hiram Chirino (Cojonudo14@hotmail.com)
38  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
39  * @version $Revision: 37459 $
40  */

41 public class GenericConnectionFactory implements Serializable JavaDoc
42 {
43    // Constants -----------------------------------------------------
44

45    /** The serialVersionUID */
46    private static final long serialVersionUID = 2288420610006129296L;
47
48    /** The log */
49    static Logger log = Logger.getLogger(GenericConnectionFactory.class);
50
51    // Attributes ----------------------------------------------------
52

53    /**
54      * An instance of the ServerIL, once it is setup, we make clones every
55      */

56    private ServerIL server;
57
58    /**
59      * Holds all the information need to connect to the server.
60      */

61    private Properties JavaDoc connectionProperties;
62    
63    // Static --------------------------------------------------------
64

65    // Constructors --------------------------------------------------
66

67    /**
68      * The constructor takes a ServerIL and the Connection Properties
69      * parameters, The connection properties are allways required since they are
70      * used to setup the ClientIL, but the ServerIL can be null if the
71      * connection properties defines a ServerILFactory so that the SeverIL can
72      * be created on the client side. The ServerIL paramter is usefull for IL
73      * such as RMI or the JVM IL since trying to explicity create a connection
74      * to them is not strait forward.
75      *
76      * @param server the serverIL
77      * @param props the connection properties
78      */

79    public GenericConnectionFactory(ServerIL server, Properties JavaDoc props)
80    {
81       this.server = server;
82       this.connectionProperties = props;
83    }
84    
85    // Public --------------------------------------------------------
86

87    /**
88     * For testing
89     */

90    public Properties JavaDoc getProperties()
91    {
92       return connectionProperties;
93    }
94    
95    /**
96     * Initialise the connection
97     *
98     * @param connection the connection to initialise
99     */

100    public void initialise(Connection connection) throws JMSException JavaDoc
101    {
102       String JavaDoc clientID = connectionProperties.getProperty(ServerILFactory.CLIENTID);
103       if (clientID != null)
104          connection.clientID = clientID;
105    }
106    
107    /**
108      * Creates a new instance of the ClientILService
109      *
110      * @param connection the connection
111      * @return the client il
112      * @exception Exception for any error
113      */

114    public ClientILService createClientILService(Connection connection) throws Exception JavaDoc
115    {
116       // This is a good time to setup the PingPeriod
117
String JavaDoc pingPeriod = connectionProperties.getProperty(ServerILFactory.PING_PERIOD_KEY, "" + connection.pingPeriod);
118       connection.pingPeriod = Long.parseLong(pingPeriod);
119
120       // Setup the client connection.
121
String JavaDoc clientILServiceCN = connectionProperties.getProperty(ServerILFactory.CLIENT_IL_SERVICE_KEY);
122       ClientILService service = (ClientILService) Class.forName(clientILServiceCN).newInstance();
123       service.init(connection, connectionProperties);
124
125       if (log.isTraceEnabled())
126          log.trace("Handing out ClientIL: " + clientILServiceCN);
127
128       return service;
129    }
130
131    /**
132      * Creates a new instance of the ServerIL
133      *
134      * @return the server il
135      * @exception JMSException for any error
136      */

137    public ServerIL createServerIL() throws JMSException JavaDoc
138    {
139       try
140       {
141          // The server was not set, so lets try to set it up with
142
// A ServerILFactory
143
if (server == null)
144          {
145             String JavaDoc className = connectionProperties.getProperty(ServerILFactory.SERVER_IL_FACTORY_KEY);
146             ServerILFactory factory = (ServerILFactory) Class.forName(className).newInstance();
147             factory.init(connectionProperties);
148
149             server = factory.getServerIL();
150          }
151
152          // We clone because one ConnectionFactory instance can be
153
// used to produce multiple connections.
154
return server.cloneServerIL();
155       }
156       catch (Exception JavaDoc e)
157       {
158          throw new SpyJMSException("Could not connect to the server", e);
159       }
160    }
161    
162    // Object overrides ----------------------------------------------
163

164    public String JavaDoc toString()
165    {
166       return "GenericConnectionFactory[server=" + server + " connectionProperties=" + connectionProperties + "]";
167    }
168    
169    // Package protected ---------------------------------------------
170

171    // Protected -----------------------------------------------------
172

173    // Private -------------------------------------------------------
174

175    // Inner classes -------------------------------------------------
176
}
Popular Tags