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