KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > mail > MailResourceAdapter


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.resource.adapter.mail;
23
24 import javax.resource.spi.ResourceAdapter JavaDoc;
25 import javax.resource.spi.BootstrapContext JavaDoc;
26 import javax.resource.spi.ResourceAdapterInternalException JavaDoc;
27 import javax.resource.spi.ActivationSpec JavaDoc;
28 import javax.resource.spi.work.WorkManager JavaDoc;
29 import javax.resource.spi.work.WorkException JavaDoc;
30 import javax.resource.spi.endpoint.MessageEndpointFactory JavaDoc;
31 import javax.resource.ResourceException JavaDoc;
32 import javax.transaction.xa.XAResource JavaDoc;
33
34 import org.jboss.resource.adapter.mail.inflow.MailActivation;
35 import org.jboss.resource.adapter.mail.inflow.MailActivationSpec;
36 import org.jboss.resource.adapter.mail.inflow.NewMsgsWorker;
37 import org.jboss.logging.Logger;
38 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
39
40 /**
41  * @author Scott.Stark@jboss.org
42  * @version $Revision: 37459 $
43  */

44 public class MailResourceAdapter
45    implements ResourceAdapter JavaDoc
46 {
47    private static Logger log = Logger.getLogger(MailResourceAdapter.class);
48
49    private BootstrapContext JavaDoc ctx;
50    /** The activations by activation spec */
51    private ConcurrentReaderHashMap activations = new ConcurrentReaderHashMap();
52    /** */
53    private NewMsgsWorker newMsgsWorker;
54
55    /**
56     * Get the work manager
57     *
58     * @return the work manager
59     */

60    public WorkManager JavaDoc getWorkManager()
61    {
62       return ctx.getWorkManager();
63    }
64
65    // --- Begin ResourceAdapter interface methods
66
public void start(BootstrapContext JavaDoc ctx)
67       throws ResourceAdapterInternalException JavaDoc
68    {
69       log.debug("start");
70       this.ctx = ctx;
71       WorkManager JavaDoc mgr = ctx.getWorkManager();
72       newMsgsWorker = new NewMsgsWorker(mgr);
73       try
74       {
75          mgr.scheduleWork(newMsgsWorker);
76       }
77       catch (WorkException JavaDoc e)
78       {
79          throw new ResourceAdapterInternalException JavaDoc(e);
80       }
81    }
82
83    public void stop()
84    {
85       log.debug("stop");
86       newMsgsWorker.release();
87    }
88
89    public void endpointActivation(MessageEndpointFactory JavaDoc endpointFactory,
90       ActivationSpec JavaDoc spec)
91       throws ResourceException JavaDoc
92    {
93       log.debug("endpointActivation, spec="+spec);
94       MailActivationSpec mailSpec = (MailActivationSpec) spec;
95       MailActivation activation = new MailActivation(this, endpointFactory,
96          mailSpec);
97       try
98       {
99          newMsgsWorker.watch(activation);
100       }
101       catch (InterruptedException JavaDoc e)
102       {
103          throw new ResourceException JavaDoc("Failed to schedule new msg check", e);
104       }
105       activations.put(spec, activation);
106    }
107
108    public void endpointDeactivation(MessageEndpointFactory JavaDoc endpointFactory,
109       ActivationSpec JavaDoc spec)
110    {
111       log.debug("endpointDeactivation, spec="+spec);
112       MailActivation activation = (MailActivation) activations.remove(spec);
113       if (activation != null)
114          activation.release();
115    }
116
117    public XAResource JavaDoc[] getXAResources(ActivationSpec JavaDoc[] specs) throws ResourceException JavaDoc
118    {
119       return new XAResource JavaDoc[0];
120    }
121    // --- End ResourceAdapter interface methods
122

123 }
124
Popular Tags