001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.kernel;
020:
021: import org.apache.openjpa.meta.ClassMetaData;
022: import org.apache.openjpa.util.RuntimeExceptionTranslator;
023:
024: ///////////////////////////////////////////////////////////////
025: // NOTE: when adding a public API method, be sure to add it to
026: // JDO and JPA facades!
027: ///////////////////////////////////////////////////////////////
028:
029: /**
030: * Delegating sequence that can also perform exception translation for use
031: * in facades.
032: *
033: * @author Abe White
034: * @nojavadoc
035: */
036: public class DelegatingSeq implements Seq {
037:
038: private final Seq _seq;
039: private final DelegatingSeq _del;
040: private final RuntimeExceptionTranslator _trans;
041:
042: /**
043: * Constructor; supply delegate.
044: */
045: public DelegatingSeq(Seq seq) {
046: this (seq, null);
047: }
048:
049: /**
050: * Constructor; supply delegate and exception translator.
051: */
052: public DelegatingSeq(Seq seq, RuntimeExceptionTranslator trans) {
053: _seq = seq;
054: if (seq instanceof DelegatingSeq)
055: _del = (DelegatingSeq) seq;
056: else
057: _del = null;
058: _trans = trans;
059: }
060:
061: /**
062: * Return the direct delegate.
063: */
064: public Seq getDelegate() {
065: return _seq;
066: }
067:
068: /**
069: * Return the native delegate.
070: */
071: public Seq getInnermostDelegate() {
072: return (_del == null) ? _seq : _del.getInnermostDelegate();
073: }
074:
075: public int hashCode() {
076: return getInnermostDelegate().hashCode();
077: }
078:
079: public boolean equals(Object other) {
080: if (other == this )
081: return true;
082: if (other instanceof DelegatingSeq)
083: other = ((DelegatingSeq) other).getInnermostDelegate();
084: return getInnermostDelegate().equals(other);
085: }
086:
087: /**
088: * Translate the OpenJPA exception.
089: */
090: protected RuntimeException translate(RuntimeException re) {
091: return (_trans == null) ? re : _trans.translate(re);
092: }
093:
094: public void setType(int type) {
095: try {
096: _seq.setType(type);
097: } catch (RuntimeException re) {
098: throw translate(re);
099: }
100: }
101:
102: public Object next(StoreContext ctx, ClassMetaData meta) {
103: try {
104: return _seq.next(ctx, meta);
105: } catch (RuntimeException re) {
106: throw translate(re);
107: }
108: }
109:
110: public Object current(StoreContext ctx, ClassMetaData meta) {
111: try {
112: return _seq.current(ctx, meta);
113: } catch (RuntimeException re) {
114: throw translate(re);
115: }
116: }
117:
118: public void allocate(int additional, StoreContext ctx,
119: ClassMetaData meta) {
120: try {
121: _seq.allocate(additional, ctx, meta);
122: } catch (RuntimeException re) {
123: throw translate(re);
124: }
125: }
126:
127: public void close() {
128: try {
129: _seq.close();
130: } catch (RuntimeException re) {
131: throw translate(re);
132: }
133: }
134: }
|