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 java.beans.PropertyVetoException;
020    import java.net.URI;
021    import java.net.URISyntaxException;
022    
023    import com.ibm.as400.access.AS400;
024    import com.ibm.as400.access.DataQueue;
025    
026    import org.apache.camel.CamelException;
027    import org.apache.camel.Exchange;
028    import org.apache.camel.PollingConsumer;
029    import org.apache.camel.Producer;
030    import org.apache.camel.impl.DefaultPollingEndpoint;
031    
032    /**
033     * AS/400 Data queue endpoint
034     */
035    public class Jt400DataQueueEndpoint extends DefaultPollingEndpoint<Exchange> {
036    
037        /**
038         * Enumeration of supported data formats
039         */
040        public enum Format {
041    
042            /**
043             * Using <code>String</code> for transferring data
044             */
045            text,
046    
047            /**
048             * Using <code>byte[]</code> for transferring data
049             */
050            binary;
051        }
052    
053        private final AS400 system;
054        private final String objectPath;
055        private DataQueue dataqueue;
056        private Format format = Format.text;
057    
058        /**
059         * Creates a new AS/400 data queue endpoint
060         */
061        protected Jt400DataQueueEndpoint(String endpointUri, Jt400Component component) throws CamelException {
062            super(endpointUri, component);
063            try {
064                URI uri = new URI(endpointUri);
065                String[] credentials = uri.getUserInfo().split(":");
066                system = new AS400(uri.getHost(), credentials[0], credentials[1]);
067                objectPath = uri.getPath();
068            } catch (URISyntaxException e) {
069                throw new CamelException("Unable to parse URI for " + endpointUri, e);
070            }
071        }
072    
073        public void setCcsid(int ccsid) throws PropertyVetoException {
074            this.system.setCcsid(ccsid);
075        }
076    
077        public void setFormat(Format format) {
078            this.format = format;
079        }
080    
081        public Format getFormat() {
082            return format;
083        }
084    
085        @Override
086        public PollingConsumer<Exchange> createPollingConsumer() throws Exception {
087            return new Jt400DataQueueConsumer(this);
088        }
089    
090        public Producer<Exchange> createProducer() throws Exception {
091            return new Jt400DataQueueProducer(this);
092        }
093    
094        protected AS400 getSystem() {
095            return system;
096        }
097    
098        protected DataQueue getDataQueue() {
099            if (dataqueue == null) {
100                dataqueue = new DataQueue(system, objectPath);
101            }
102            return dataqueue;
103        }
104    
105        public boolean isSingleton() {
106            return false;
107        }
108    
109    }