001/*
002 * Copyright 2002-2012 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;
020import javax.resource.cci.ConnectionFactory;
021import javax.resource.cci.InteractionSpec;
022import javax.resource.cci.Record;
023import javax.resource.cci.RecordFactory;
024
025import org.springframework.dao.DataAccessException;
026import org.springframework.dao.DataRetrievalFailureException;
027import org.springframework.jca.cci.core.support.CommAreaRecord;
028
029/**
030 * EIS operation object for access to COMMAREA records.
031 * Subclass of the generic MappingRecordOperation class.
032 *
033 * @author Thierry Templier
034 * @since 1.2
035 */
036public abstract class MappingCommAreaOperation extends MappingRecordOperation {
037
038        /**
039         * Create a new MappingCommAreaQuery.
040         * @see #setConnectionFactory
041         * @see #setInteractionSpec
042         */
043        public MappingCommAreaOperation() {
044        }
045
046        /**
047         * Create a new MappingCommAreaQuery.
048         * @param connectionFactory ConnectionFactory to use to obtain connections
049         * @param interactionSpec specification to configure the interaction
050         */
051        public MappingCommAreaOperation(ConnectionFactory connectionFactory, InteractionSpec interactionSpec) {
052                super(connectionFactory, interactionSpec);
053        }
054
055
056        @Override
057        protected final Record createInputRecord(RecordFactory recordFactory, Object inObject) {
058                try {
059                        return new CommAreaRecord(objectToBytes(inObject));
060                }
061                catch (IOException ex) {
062                        throw new DataRetrievalFailureException("I/O exception during bytes conversion", ex);
063                }
064        }
065
066        @Override
067        protected final Object extractOutputData(Record record) throws DataAccessException {
068                CommAreaRecord commAreaRecord = (CommAreaRecord) record;
069                try {
070                        return bytesToObject(commAreaRecord.toByteArray());
071                }
072                catch (IOException ex) {
073                        throw new DataRetrievalFailureException("I/O exception during bytes conversion", ex);
074                }
075        }
076
077
078        /**
079         * Method used to convert an object into COMMAREA bytes.
080         * @param inObject the input data
081         * @return the COMMAREA's bytes
082         * @throws IOException if thrown by I/O methods
083         * @throws DataAccessException if conversion failed
084         */
085        protected abstract byte[] objectToBytes(Object inObject) throws IOException, DataAccessException;
086
087        /**
088         * Method used to convert the COMMAREA's bytes to an object.
089         * @param bytes the COMMAREA's bytes
090         * @return the output data
091         * @throws IOException if thrown by I/O methods
092         * @throws DataAccessException if conversion failed
093         */
094        protected abstract Object bytesToObject(byte[] bytes) throws IOException, DataAccessException;
095
096}