001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.smpp;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Processor;
021    import org.apache.camel.impl.DefaultConsumer;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.jsmpp.bean.AlertNotification;
025    import org.jsmpp.bean.BindType;
026    import org.jsmpp.bean.DeliverSm;
027    import org.jsmpp.bean.NumberingPlanIndicator;
028    import org.jsmpp.bean.TypeOfNumber;
029    import org.jsmpp.session.BindParameter;
030    import org.jsmpp.session.MessageReceiverListener;
031    import org.jsmpp.session.SMPPSession;
032    
033    /**
034     * An implementation of @{link Consumer} which use the SMPP protocol
035     * 
036     * @version $Revision: 888296 $
037     * @author muellerc
038     */
039    public class SmppConsumer extends DefaultConsumer {
040    
041        private static final transient Log LOG = LogFactory.getLog(SmppConsumer.class);
042    
043        private SmppConfiguration configuration;
044        private SMPPSession session;
045    
046        /**
047         * The constructor which gets a smpp endpoint, a smpp configuration and a processor
048         */
049        public SmppConsumer(SmppEndpoint endpoint, SmppConfiguration configuration, Processor processor) {
050            super(endpoint, processor);
051            this.configuration = configuration;
052        }
053    
054        @Override
055        protected void doStart() throws Exception {
056            if (LOG.isDebugEnabled()) {
057                LOG.debug("Connecting to: " + getEndpoint().getConnectionString() + "...");
058            }
059    
060            super.doStart();
061    
062            session = createSMPPSession();
063            session.setEnquireLinkTimer(this.configuration.getEnquireLinkTimer());
064            session.setTransactionTimer(this.configuration.getTransactionTimer());
065            session.setMessageReceiverListener(new MessageReceiverListener() {
066                public void onAcceptAlertNotification(AlertNotification alertNotification) {
067                    if (LOG.isDebugEnabled()) {
068                        LOG.debug("Received an alertNotification " + alertNotification);
069                    }
070    
071                    try {
072                        Exchange exchange = getEndpoint().createOnAcceptAlertNotificationExchange(alertNotification);
073    
074                        LOG.trace("Processing the new smpp exchange...");
075                        getProcessor().process(exchange);
076                        LOG.trace("Processed the new smpp exchange");
077                    } catch (Exception e) {
078                        getExceptionHandler().handleException(e);
079                    }
080                }
081    
082                public void onAcceptDeliverSm(DeliverSm deliverSm) {
083                    if (LOG.isDebugEnabled()) {
084                        LOG.debug("Received a deliverSm " + deliverSm);
085                    }
086    
087                    try {
088                        Exchange exchange = getEndpoint().createOnAcceptDeliverSmExchange(deliverSm);
089    
090                        LOG.trace("processing the new smpp exchange...");
091                        getProcessor().process(exchange);
092                        LOG.trace("processed the new smpp exchange");
093                    } catch (Exception e) {
094                        getExceptionHandler().handleException(e);
095                    }
096                }
097    
098            });
099    
100            session.connectAndBind(
101                    this.configuration.getHost(),
102                    this.configuration.getPort(),
103                    new BindParameter(
104                            BindType.BIND_RX,
105                            this.configuration.getSystemId(),
106                            this.configuration.getPassword(),
107                            this.configuration.getSystemType(),
108                            TypeOfNumber.UNKNOWN,
109                            NumberingPlanIndicator.UNKNOWN,
110                            ""));
111    
112            LOG.info("Connected to: " + getEndpoint().getConnectionString());
113        }
114    
115        /**
116         * Factory method to easily instantiate a mock SMPPSession
117         * 
118         * @return the SMPPSession
119         */
120        SMPPSession createSMPPSession() {
121            return new SMPPSession();
122        }
123    
124        @Override
125        protected void doStop() throws Exception {
126            LOG.debug("Disconnecting from: " + getEndpoint().getConnectionString() + "...");
127    
128            super.doStop();
129    
130            if (session != null) {
131                session.close();
132                session = null;
133            }
134    
135            LOG.info("Disconnected from: " + getEndpoint().getConnectionString());
136        }
137    
138        @Override
139        public String toString() {
140            return "SmppConsumer[" + getEndpoint().getConnectionString() + "]";
141        }
142    
143        @Override
144        public SmppEndpoint getEndpoint() {
145            return (SmppEndpoint) super.getEndpoint();
146        }
147    
148        /**
149         * Returns the smpp configuration
150         * 
151         * @return the configuration
152         */
153        public SmppConfiguration getConfiguration() {
154            return configuration;
155        }
156    
157    }