001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.adapter.mail;
023:
024: import javax.resource.spi.ResourceAdapter;
025: import javax.resource.spi.BootstrapContext;
026: import javax.resource.spi.ResourceAdapterInternalException;
027: import javax.resource.spi.ActivationSpec;
028: import javax.resource.spi.work.WorkManager;
029: import javax.resource.spi.work.WorkException;
030: import javax.resource.spi.endpoint.MessageEndpointFactory;
031: import javax.resource.ResourceException;
032: import javax.transaction.xa.XAResource;
033:
034: import org.jboss.resource.adapter.mail.inflow.MailActivation;
035: import org.jboss.resource.adapter.mail.inflow.MailActivationSpec;
036: import org.jboss.resource.adapter.mail.inflow.NewMsgsWorker;
037: import org.jboss.logging.Logger;
038: import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
039:
040: /**
041: * @author Scott.Stark@jboss.org
042: * @version $Revision: 57189 $
043: */
044: public class MailResourceAdapter implements ResourceAdapter {
045: private static Logger log = Logger
046: .getLogger(MailResourceAdapter.class);
047:
048: private BootstrapContext ctx;
049: /** The activations by activation spec */
050: private ConcurrentReaderHashMap activations = new ConcurrentReaderHashMap();
051: /** */
052: private NewMsgsWorker newMsgsWorker;
053:
054: /**
055: * Get the work manager
056: *
057: * @return the work manager
058: */
059: public WorkManager getWorkManager() {
060: return ctx.getWorkManager();
061: }
062:
063: // --- Begin ResourceAdapter interface methods
064: public void start(BootstrapContext ctx)
065: throws ResourceAdapterInternalException {
066: log.debug("start");
067: this .ctx = ctx;
068: WorkManager mgr = ctx.getWorkManager();
069: newMsgsWorker = new NewMsgsWorker(mgr);
070: try {
071: mgr.scheduleWork(newMsgsWorker);
072: } catch (WorkException e) {
073: throw new ResourceAdapterInternalException(e);
074: }
075: }
076:
077: public void stop() {
078: log.debug("stop");
079: newMsgsWorker.release();
080: }
081:
082: public void endpointActivation(
083: MessageEndpointFactory endpointFactory, ActivationSpec spec)
084: throws ResourceException {
085: log.debug("endpointActivation, spec=" + spec);
086: MailActivationSpec mailSpec = (MailActivationSpec) spec;
087: MailActivation activation = new MailActivation(this ,
088: endpointFactory, mailSpec);
089: try {
090: newMsgsWorker.watch(activation);
091: } catch (InterruptedException e) {
092: throw new ResourceException(
093: "Failed to schedule new msg check", e);
094: }
095: activations.put(spec, activation);
096: }
097:
098: public void endpointDeactivation(
099: MessageEndpointFactory endpointFactory, ActivationSpec spec) {
100: log.debug("endpointDeactivation, spec=" + spec);
101: MailActivation activation = (MailActivation) activations
102: .remove(spec);
103: if (activation != null)
104: activation.release();
105: }
106:
107: public XAResource[] getXAResources(ActivationSpec[] specs)
108: throws ResourceException {
109: return new XAResource[0];
110: }
111: // --- End ResourceAdapter interface methods
112:
113: }
|