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