001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance 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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.fcrepo.kernel.api;
020
021import java.time.Duration;
022import java.time.Instant;
023
024import org.fcrepo.kernel.api.identifiers.FedoraId;
025
026/**
027 * A read-only tx that never expires and cannot be committed.
028 *
029 * @author pwinckles
030 */
031public class ReadOnlyTransaction implements Transaction {
032
033    // This is not a perfect way to create a singleton, but it should be fine for its purposes as it does
034    // not really need to be a singleton.
035    public static final ReadOnlyTransaction INSTANCE = new ReadOnlyTransaction();
036
037    public static final String READ_ONLY_TX_ID = "read-only";
038
039    private ReadOnlyTransaction() {
040        // empty
041    }
042
043    @Override
044    public void commit() {
045        // no-op
046    }
047
048    @Override
049    public void commitIfShortLived() {
050        // no-op
051    }
052
053    @Override
054    public boolean isCommitted() {
055        return false;
056    }
057
058    @Override
059    public void rollback() {
060        // no-op
061    }
062
063    @Override
064    public void fail() {
065        // no-op
066    }
067
068    @Override
069    public boolean isRolledBack() {
070        return false;
071    }
072
073    @Override
074    public boolean isOpenLongRunning() {
075        return false;
076    }
077
078    @Override
079    public boolean isOpen() {
080        return true;
081    }
082
083    @Override
084    public void ensureCommitting() {
085        // no-op
086    }
087
088    @Override
089    public boolean isReadOnly() {
090        return true;
091    }
092
093    @Override
094    public String getId() {
095        return READ_ONLY_TX_ID;
096    }
097
098    @Override
099    public boolean isShortLived() {
100        return true;
101    }
102
103    @Override
104    public void setShortLived(final boolean shortLived) {
105        // no-op
106    }
107
108    @Override
109    public void expire() {
110        // no-op
111    }
112
113    @Override
114    public boolean hasExpired() {
115        return false;
116    }
117
118    @Override
119    public Instant updateExpiry(final Duration amountToAdd) {
120        return Instant.now().plus(amountToAdd);
121    }
122
123    @Override
124    public Instant getExpires() {
125        return Instant.now().plus(Duration.ofMinutes(1));
126    }
127
128    @Override
129    public void refresh() {
130        // no-op
131    }
132
133    @Override
134    public void lockResource(final FedoraId resourceId) {
135        // no-op
136    }
137
138    @Override
139    public void releaseResourceLocksIfShortLived() {
140        // no-op
141    }
142
143    @Override
144    public void doInTx(final Runnable runnable) {
145        runnable.run();
146    }
147
148    @Override
149    public void setBaseUri(final String baseUri) {
150        // no-op
151    }
152
153    @Override
154    public void setUserAgent(final String userAgent) {
155        // no-op
156    }
157}