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    import org.slf4j.Logger;
031    import org.slf4j.LoggerFactory;
032    
033    /**
034     * AS/400 Data queue endpoint
035     */
036    public class Jt400DataQueueEndpoint extends DefaultPollingEndpoint {
037    
038        /**
039         * Enumeration of supported data formats
040         */
041        public enum Format {
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 static final transient Logger LOG = LoggerFactory.getLogger(Jt400DataQueueEndpoint.class);
054    
055        private final AS400 system;
056        private final String objectPath;
057        private DataQueue dataqueue;
058        private Format format = Format.text;
059    
060        /**
061         * Creates a new AS/400 data queue endpoint
062         */
063        protected Jt400DataQueueEndpoint(String endpointUri, Jt400Component component) throws CamelException {
064            super(endpointUri, component);
065            try {
066                URI uri = new URI(endpointUri);
067                String[] credentials = uri.getUserInfo().split(":");
068                system = new AS400(uri.getHost(), credentials[0], credentials[1]);
069                objectPath = uri.getPath();
070            } catch (URISyntaxException e) {
071                throw new CamelException("Unable to parse URI for " + endpointUri, e);
072            }
073    
074            try {
075                system.setGuiAvailable(false);
076            } catch (PropertyVetoException e) {
077                LOG.warn("Failed do disable AS/400 prompting in the environment running Camel.", e);
078            }
079        }
080    
081        public void setCcsid(int ccsid) throws PropertyVetoException {
082            this.system.setCcsid(ccsid);
083        }
084    
085        public void setFormat(Format format) {
086            this.format = format;
087        }
088    
089        public Format getFormat() {
090            return format;
091        }
092    
093        public void setGuiAvailable(boolean guiAvailable) throws PropertyVetoException {
094            this.system.setGuiAvailable(guiAvailable);
095        }
096    
097        @Override
098        public PollingConsumer createPollingConsumer() throws Exception {
099            return new Jt400DataQueueConsumer(this);
100        }
101    
102        public Producer createProducer() throws Exception {
103            return new Jt400DataQueueProducer(this);
104        }
105    
106        protected AS400 getSystem() {
107            return system;
108        }
109    
110        protected DataQueue getDataQueue() {
111            if (dataqueue == null) {
112                dataqueue = new DataQueue(system, objectPath);
113            }
114            return dataqueue;
115        }
116    
117        public boolean isSingleton() {
118            return false;
119        }
120    
121    }