001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jca.cci.object;
018
019import java.io.IOException;
020
021import javax.resource.cci.ConnectionFactory;
022import javax.resource.cci.InteractionSpec;
023import javax.resource.cci.Record;
024import javax.resource.cci.RecordFactory;
025
026import org.springframework.dao.DataAccessException;
027import org.springframework.dao.DataRetrievalFailureException;
028import org.springframework.jca.cci.core.support.CommAreaRecord;
029
030/**
031 * EIS operation object for access to COMMAREA records.
032 * Subclass of the generic MappingRecordOperation class.
033 *
034 * @author Thierry Templier
035 * @since 1.2
036 */
037public abstract class MappingCommAreaOperation extends MappingRecordOperation {
038
039        /**
040         * Create a new MappingCommAreaQuery.
041         * @see #setConnectionFactory
042         * @see #setInteractionSpec
043         */
044        public MappingCommAreaOperation() {
045        }
046
047        /**
048         * Create a new MappingCommAreaQuery.
049         * @param connectionFactory the ConnectionFactory to use to obtain connections
050         * @param interactionSpec specification to configure the interaction
051         */
052        public MappingCommAreaOperation(ConnectionFactory connectionFactory, InteractionSpec interactionSpec) {
053                super(connectionFactory, interactionSpec);
054        }
055
056
057        @Override
058        protected final Record createInputRecord(RecordFactory recordFactory, Object inObject) {
059                try {
060                        return new CommAreaRecord(objectToBytes(inObject));
061                }
062                catch (IOException ex) {
063                        throw new DataRetrievalFailureException("I/O exception during bytes conversion", ex);
064                }
065        }
066
067        @Override
068        protected final Object extractOutputData(Record record) throws DataAccessException {
069                CommAreaRecord commAreaRecord = (CommAreaRecord) record;
070                try {
071                        return bytesToObject(commAreaRecord.toByteArray());
072                }
073                catch (IOException ex) {
074                        throw new DataRetrievalFailureException("I/O exception during bytes conversion", ex);
075                }
076        }
077
078
079        /**
080         * Method used to convert an object into COMMAREA bytes.
081         * @param inObject the input data
082         * @return the COMMAREA's bytes
083         * @throws IOException if thrown by I/O methods
084         * @throws DataAccessException if conversion failed
085         */
086        protected abstract byte[] objectToBytes(Object inObject) throws IOException, DataAccessException;
087
088        /**
089         * Method used to convert the COMMAREA's bytes to an object.
090         * @param bytes the COMMAREA's bytes
091         * @return the output data
092         * @throws IOException if thrown by I/O methods
093         * @throws DataAccessException if conversion failed
094         */
095        protected abstract Object bytesToObject(byte[] bytes) throws IOException, DataAccessException;
096
097}