KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > jms > JMSModule


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Emil Ong
28  */

29
30 package com.caucho.quercus.lib.jms;
31
32 import com.caucho.quercus.QuercusModuleException;
33 import com.caucho.quercus.annotation.ClassImplementation;
34 import com.caucho.quercus.env.Env;
35 import com.caucho.quercus.env.StringValue;
36 import com.caucho.quercus.module.AbstractQuercusModule;
37 import com.caucho.util.L10N;
38
39 import javax.jms.*;
40 import javax.naming.Context JavaDoc;
41 import javax.naming.InitialContext JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * JMS functions
48  */

49 @ClassImplementation
50 public class JMSModule extends AbstractQuercusModule
51 {
52   private static final Logger JavaDoc log
53     = Logger.getLogger(JMSModule.class.getName());
54
55   private static final L10N L = new L10N(JMSModule.class);
56
57   private static final HashMap JavaDoc<String JavaDoc,StringValue> _iniMap
58     = new HashMap JavaDoc<String JavaDoc,StringValue>();
59
60   private ConnectionFactory _connectionFactory = null;
61
62   /**
63    * Returns the default quercus.ini values.
64    */

65   public Map JavaDoc<String JavaDoc,StringValue> getDefaultIni()
66   {
67     return _iniMap;
68   }
69
70   static JMSQueue message_get_queue(Env env, String JavaDoc queueName,
71                                     ConnectionFactory connectionFactory)
72   {
73     if (connectionFactory == null)
74       connectionFactory = getConnectionFactory(env);
75
76     if (connectionFactory == null) {
77       env.warning(L.l("No connection factory"));
78       return null;
79     }
80
81     try {
82       Destination queue = null;
83
84       if (queueName != null && ! queueName.equals(""))
85     queue = (Destination) new InitialContext JavaDoc().lookup("java:comp/env/" + queueName);
86       
87       return new JMSQueue(connectionFactory, queue);
88     } catch (Exception JavaDoc e) {
89       env.warning(e);
90
91       return null;
92     }
93   }
94
95   private static ConnectionFactory getConnectionFactory(Env env)
96   {
97     StringValue factoryName = env.getIni("jms.connection_factory");
98
99     if (factoryName == null)
100       log.fine("jms.connection_factory not set");
101
102     try {
103       Context JavaDoc context = (Context JavaDoc) new InitialContext JavaDoc().lookup("java:comp/env");
104
105       ConnectionFactory connectionFactory =
106         (ConnectionFactory) context.lookup(factoryName.toString());
107
108       if (connectionFactory == null)
109         log.warning("Couldn't find factory " + factoryName.toString());
110
111       return connectionFactory;
112     } catch (RuntimeException JavaDoc e) {
113       throw e;
114     } catch (Exception JavaDoc e) {
115       throw new QuercusModuleException(e);
116     }
117   }
118
119   static {
120     addIni(_iniMap, "jms.connection_factory",
121                     "jms/ConnectionFactory", PHP_INI_SYSTEM);
122   }
123 }
124
125
Popular Tags