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