1    package com.instantbank.component.lettersjobmdb.util;
2    
3    import javax.jms.QueueConnectionFactory;
4    import javax.jms.QueueConnection;
5    import javax.jms.QueueSession;
6    import javax.jms.QueueSender;
7    import javax.jms.Queue;
8    import javax.jms.JMSException;
9    import javax.jms.MapMessage;
10   import javax.jms.Message;
11   
12   import javax.naming.Context;
13   import javax.naming.InitialContext;
14   import javax.naming.NamingEnumeration;
15   import javax.naming.NameClassPair;
16   import javax.naming.CompositeName;
17   import javax.naming.NameNotFoundException;
18   
19   import java.util.Hashtable;
20   
21   import com.instantbank.common.utilcomponents.CommonUtil;
22   import com.instantbank.common.utilcomponents.JNDINames;
23   import com.instantbank.common.utilcomponents.Debug;
24   import com.instantbank.common.utilcomponents.LetterTemplateGlobals;
25   
26   /**
27    * Wraper for the message sending process related to letter jobs.
28    *
29    * @author InstantBank (Rodrigo Lopez)
30    * @created October 2002
31    */
32   public class MessageSender {
33   
34     /**
35      * Name of the initial context factory class.
36      */
37     private String initContextFactory = null;
38   
39     /**
40      * Context provider url.
41      */
42     private String contextProviderUrl = null;
43   
44     /**
45      * Queue connection
46      */
47     private QueueConnection qConnection = null;
48   
49     /**
50      * Queue session
51      */
52     private QueueSession qSession = null;
53   
54     /**
55      * Queue sender
56      */
57     private QueueSender qSender = null;
58   
59     /**
60      * Actual queue
61      */
62     private Queue q = null;
63   
64     /**
65      * The ubiquitous debugger object
66      */
67     private Debug debug;
68   
69   
70     /**
71      * MessageSender constructor. It searches the required initial context factory
72      * through JNDI.
73      *
74      * @param server Server name.
75      * @param port Port number
76      * @param queue Actual queue where messages will be posted.
77      * @throws JMSException if any trouble arises constructing the sender
78      */
79     public MessageSender(String server, int port, String queue)
80        throws JMSException {
81       debug = new Debug();
82       debug.setDebugginOn(true);
83       debug.setPreMessage("** MessageSender: ");
84   
85       contextProviderUrl = "t3://" + server + ":" + port;
86       initContextFactory =
87         CommonUtil.getApplicationProperty(JNDINames.WLInitialContextFactory);
88       init(queue);
89   
90     }
91   
92   
93     /**
94      * MessageSender constructor.
95      *
96      * @param contextProviderUrl Context Provider Url.
97      * @param initContextFactory Name of the Initial Context Factory class
98      * @param queue Actual queue where messages will be posted.
99      * @throws JMSException if any trouble arises constructing the sender
100     */
101    public MessageSender(String contextProviderUrl,
102                         String initContextFactory, String queue)
103       throws JMSException {
104      debug = new Debug();
105      debug.setDebugginOn(true);
106      debug.setPreMessage("** MessageSender: ");
107  
108      this.contextProviderUrl = contextProviderUrl;
109      this.initContextFactory = initContextFactory;
110      debug.println("contextProviderUrl = " + contextProviderUrl);
111      init(queue);
112  
113    }
114  
115  
116    /**
117     * Inits the required entities for sending messages.
118     *
119     * @param queue Name of the actual queue where messages are posted.
120     * @throws JMSException If anything goes wrong.
121     */
122    private void init(String queue) throws JMSException {
123      Context ctx = null;
124      Hashtable ht = new Hashtable();
125      QueueConnectionFactory qConnectionFactory = null;
126  
127      try {
128        debug.println("queue = " + queue);
129        debug.println("initCtxFac = " + initContextFactory);
130  
131        ht.put(Context.INITIAL_CONTEXT_FACTORY, initContextFactory);
132  
133        ht.put(Context.PROVIDER_URL, contextProviderUrl);
134  
135        ctx = new InitialContext(ht);
136  
137        //Debug
138        //debugContext(ctx);
139  
140        qConnectionFactory =
141          (QueueConnectionFactory)
142          ctx.lookup(LetterTemplateGlobals.LETTERS_JMS_FACTORY);
143  
144        qConnection = qConnectionFactory.createQueueConnection();
145        qSession =
146          qConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
147  
148        q = (Queue)ctx.lookup(queue);
149  
150        qSender = qSession.createSender(q);
151  
152      }
153      catch(Exception ex) {
154        debug.println(CommonUtil.stackTraceToString(ex));
155        // Release JMS resources in reverse order of their creation.
156        try {
157          qSender.close();
158          qSession.close();
159          qConnection.close();
160          throw new JMSException("Problems constructing the " + queue + " queue");
161        }
162        catch(Exception jex) {
163          debug.println(CommonUtil.stackTraceToString(jex));
164        }
165      }
166    }
167  
168  
169    /**
170     * Builds a letter job message as expected by the LettersJobMessageEJB.
171     *
172     * @param companyId Company Id
173     * @param temporalPath Full path of a working (scratch) directory.
174     * @param userId User Id
175     * @param jobId Job Id
176     * @param contextProvider Context Provider Url
177     * @param initContextFactory Name of the Initial Context Factory class
178     * @param maxZipSize Description of the Parameter
179     * @return Description of the Return Value
180     * @throws JMSException Description of the Exception
181     */
182    public Message buildLetterJobMessage(
183                                         String companyId, String temporalPath,   //String rtf2foConfDir,
184    Long userId, Long jobId, String contextProvider,
185                                         String initContextFactory, String maxZipSize)
186       throws JMSException {
187  
188      MapMessage msg = qSession.createMapMessage();
189      msg.setString("CompanyId", companyId);
190      msg.setString("WorkDir", temporalPath);
191      //msg.setString("Rtf2foConfDir",rtf2foConfDir);
192      msg.setString("ContextProviderUrl", contextProvider);
193      msg.setString("InitContextFactory", initContextFactory);
194      msg.setObject("UserId", userId);  //Long
195      msg.setObject("JobKey", jobId);  //Long
196      msg.setString("MaxZipSize", maxZipSize);
197  
198      return msg;
199    }
200  
201  
202    /**
203     * Builds a message as expected by the LettersJobFtpMdb.
204     *
205     * @param companyId Description of the Parameter
206     * @param userId Description of the Parameter
207     * @param filePath Description of the Parameter
208     * @param jobLogId Description of the Parameter
209     * @param locId1 Description of the Parameter
210     * @param locId2 Description of the Parameter
211     * @return Description of the Return Value
212     * @throws JMSException Description of the Exception
213     */
214    public Message buildFtpMessage(String companyId, Long userId,
215                                   String filePath, long jobLogId, long locId1, long locId2)
216       throws JMSException {
217      MapMessage ftpMsg = qSession.createMapMessage();
218      ftpMsg.setString("CompanyId", companyId);
219      ftpMsg.setObject("UserId", userId);
220      ftpMsg.setString("FilePath", filePath);
221      ftpMsg.setLong("JobLogId", jobLogId);
222      ftpMsg.setLong("FtpPrimLoc", locId1);
223      ftpMsg.setLong("FtpAltLoc", locId2);
224      return ftpMsg;
225    }
226  
227  
228    /**
229     * Sends a message
230     *
231     * @param msg Description of the Parameter
232     * @throws JMSException Description of the Exception
233     */
234    public void send(Message msg) throws JMSException {
235      qSender.send(msg);
236    }
237  
238  
239    /**
240     * Release the message sending resources
241     */
242    public void release() {
243      try {
244        if(qSender != null) {
245          qSender.close();
246        }
247  
248        if(qSession != null) {
249          qSession.close();
250        }
251  
252        if(qConnection != null) {
253          qConnection.close();
254        }
255      }
256      catch(Exception jex) {
257        debug.println(CommonUtil.stackTraceToString(jex));
258      }
259    }
260  
261  
262    /**
263     * Getter method for contextProviderUrl
264     *
265     * @return The contextProviderUrl value
266     */
267    public String getContextProviderUrl() {
268      return this.contextProviderUrl;
269    }
270  
271  
272    /**
273     * Getter method for initContextFactory
274     *
275     * @return The initContextFactory value
276     */
277    public String getInitContextFactory() {
278      return this.initContextFactory;
279    }
280  
281  
282    /**
283     * Debug method allowing to explore some standard
284     * context in the JNDI tree.
285     *
286     * @param ctx Description of the Parameter
287     */
288    private void debugContext(Context ctx) {
289      try {
290        String[] names = {"", "java:comp/env", "java:comp/env/jms",
291          "java:comp/env/ejb", "java:comp/env/url"};
292        for(int i = 0; i < names.length; i++) {
293          String name = names[i];
294          debug.println("====" + name + "===");
295          NamingEnumeration ne;
296          try {
297            ne = ctx.list(new CompositeName(name));
298          }
299          catch(NameNotFoundException nex) {
300            debug.println("Never mind. The name \"" + name + "\" could not be resolved");
301            continue;
302          }
303  
304          while(ne.hasMore()) {
305            NameClassPair ncp = (NameClassPair)ne.next();
306            debug.println(ncp.getName() + " .=. " + ncp.getClassName());
307          }
308        }
309      }
310      catch(Exception ex) {
311        debug.println(CommonUtil.stackTraceToString(ex));
312      }
313    }
314  }
315