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.net.URI;
020    import java.net.URISyntaxException;
021    import java.util.Arrays;
022    import java.util.Map;
023    
024    import javax.naming.OperationNotSupportedException;
025    
026    import com.ibm.as400.access.AS400;
027    import org.apache.camel.CamelContext;
028    import org.apache.camel.CamelException;
029    import org.apache.camel.Consumer;
030    import org.apache.camel.Processor;
031    import org.apache.camel.Producer;
032    import org.apache.camel.impl.DefaultEndpoint;
033    
034    public class Jt400PgmEndpoint extends DefaultEndpoint {
035    
036        private String programToExecute;
037    
038        private Integer[] outputFieldsIdxArray;
039        private Integer[] outputFieldsLengthArray;
040    
041        private AS400 iSeries;
042    
043        /**
044         * Creates a new AS/400 PGM CALL endpoint
045         */
046        protected Jt400PgmEndpoint(String endpointUri, Jt400Component component) throws CamelException {
047            super(endpointUri, component);
048            try {
049                URI uri = new URI(endpointUri);
050                String[] credentials = uri.getUserInfo().split(":");
051                iSeries = new AS400(uri.getHost(), credentials[0], credentials[1]);
052                programToExecute = uri.getPath();
053            } catch (URISyntaxException e) {
054                throw new CamelException("Unable to parse URI for " + endpointUri, e);
055            }
056        }
057    
058        public Jt400PgmEndpoint(String endpointUri, String programToExecute, Map<String, Object> parameters,
059                                CamelContext camelContext) {
060            super(endpointUri, camelContext);
061            this.programToExecute = programToExecute;
062        }
063    
064        public Producer createProducer() throws Exception {
065            return new Jt400PgmProducer(this);
066        }
067    
068        public Consumer createConsumer(Processor processor) throws Exception {
069            throw new OperationNotSupportedException();
070        }
071    
072        public boolean isSingleton() {
073            return false;
074        }
075    
076        @Override
077        public void stop() throws Exception {
078            super.stop();
079            if (iSeries != null) {
080                iSeries.disconnectAllServices();
081            }
082        }
083    
084        public boolean isFieldIdxForOuput(int idx) {
085            return Arrays.binarySearch(outputFieldsIdxArray, idx) >= 0;
086        }
087    
088        public int getOutputFieldLength(int idx) {
089            return outputFieldsLengthArray[idx];
090        }
091    
092        // getters and setters
093        public String getProgramToExecute() {
094            return programToExecute;
095        }
096    
097        public AS400 getiSeries() {
098            return iSeries;
099        }
100    
101        public void setOutputFieldsIdx(String outputFieldsIdx) {
102            if (outputFieldsIdx != null) {
103                String[] outputArray = outputFieldsIdx.split(",");
104                outputFieldsIdxArray = new Integer[outputArray.length];
105                for (int i = 0; i < outputArray.length; i++) {
106                    String str = outputArray[i];
107                    outputFieldsIdxArray[i] = Integer.parseInt(str);
108                }
109            }
110        }
111    
112        public void setFieldsLength(String fieldsLength) {
113            if (fieldsLength != null) {
114                String[] outputArray = fieldsLength.split(",");
115                outputFieldsLengthArray = new Integer[outputArray.length];
116                for (int i = 0; i < outputArray.length; i++) {
117                    String str = outputArray[i];
118                    outputFieldsLengthArray[i] = Integer.parseInt(str);
119                }
120            }
121        }
122    
123    }