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.jt400;
018
019 import com.ibm.as400.access.AS400;
020 import com.ibm.as400.access.DataQueue;
021 import org.apache.camel.Exchange;
022 import org.apache.camel.Producer;
023 import org.apache.camel.component.jt400.Jt400DataQueueEndpoint.Format;
024 import org.apache.camel.impl.DefaultProducer;
025
026 /**
027 * {@link Producer} to send data to an AS/400 data queue.
028 */
029 public class Jt400DataQueueProducer extends DefaultProducer {
030
031 private final Jt400DataQueueEndpoint endpoint;
032
033 protected Jt400DataQueueProducer(Jt400DataQueueEndpoint endpoint) {
034 super(endpoint);
035 this.endpoint = endpoint;
036 }
037
038 /**
039 * Sends the {@link Exchange}'s in body to the AS/400 data queue. If the
040 * endpoint's format is set to {@link Format#binary}, the data queue entry's
041 * data will be sent as a <code>byte[]</code>. If the endpoint's format is
042 * set to {@link Format#text}, the data queue entry's data will be sent as a
043 * <code>String</code>.
044 */
045 public void process(Exchange exchange) throws Exception {
046 DataQueue queue = endpoint.getDataQueue();
047 if (endpoint.getFormat() == Format.binary) {
048 queue.write(exchange.getIn().getBody(byte[].class));
049 } else {
050 queue.write(exchange.getIn().getBody(String.class));
051 }
052 }
053
054 @Override
055 protected void doStart() throws Exception {
056 if (!endpoint.getSystem().isConnected()) {
057 log.info("Connecting to " + endpoint);
058 endpoint.getSystem().connectService(AS400.DATAQUEUE);
059 }
060 }
061
062 @Override
063 protected void doStop() throws Exception {
064 if (endpoint.getSystem().isConnected()) {
065 log.info("Disconnecting from " + endpoint);
066 endpoint.getSystem().disconnectAllServices();
067 }
068 }
069
070 }