001/* 002 * Copyright 2019 DuraSpace, Inc. 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 * http://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.fcrepo.migration.handlers.ocfl; 018 019import edu.wisc.library.ocfl.api.MutableOcflRepository; 020import org.fcrepo.storage.ocfl.OcflObjectSession; 021import org.fcrepo.storage.ocfl.OcflObjectSessionFactory; 022import org.slf4j.Logger; 023import org.slf4j.LoggerFactory; 024 025import java.nio.file.Path; 026import java.util.UUID; 027 028/** 029 * Factory for PlainOcflObjectSessions 030 * 031 * @author pwinckles 032 */ 033public class PlainOcflObjectSessionFactory implements OcflObjectSessionFactory { 034 035 private static final Logger LOG = LoggerFactory.getLogger(PlainOcflObjectSessionFactory.class); 036 037 private final MutableOcflRepository ocflRepo; 038 private final Path stagingRoot; 039 private final String defaultVersionMessage; 040 private final String defaultVersionUserName; 041 private final String defaultVersionUserAddress; 042 043 private boolean closed = false; 044 045 /** 046 * @param ocflRepo the OCFL client 047 * @param stagingRoot the root staging directory 048 * @param defaultVersionMessage OCFL version message 049 * @param defaultVersionUserName OCFL version user 050 * @param defaultVersionUserAddress OCFL version user address 051 */ 052 public PlainOcflObjectSessionFactory(final MutableOcflRepository ocflRepo, 053 final Path stagingRoot, 054 final String defaultVersionMessage, 055 final String defaultVersionUserName, 056 final String defaultVersionUserAddress) { 057 this.ocflRepo = ocflRepo; 058 this.stagingRoot = stagingRoot; 059 this.defaultVersionMessage = defaultVersionMessage; 060 this.defaultVersionUserName = defaultVersionUserName; 061 this.defaultVersionUserAddress = defaultVersionUserAddress; 062 } 063 064 @Override 065 public OcflObjectSession newSession(final String ocflObjectId) { 066 enforceOpen(); 067 068 final var sessionId = UUID.randomUUID().toString(); 069 final var session = new PlainOcflObjectSession( 070 sessionId, 071 ocflRepo, 072 ocflObjectId, 073 stagingRoot.resolve(sessionId) 074 ); 075 076 session.versionAuthor(defaultVersionUserName, defaultVersionUserAddress); 077 session.versionMessage(defaultVersionMessage); 078 return session; 079 } 080 081 @Override 082 public void close() { 083 if (!closed) { 084 closed = true; 085 ocflRepo.close(); 086 } 087 } 088 089 private void enforceOpen() { 090 if (closed) { 091 throw new IllegalStateException("The session factory is closed!"); 092 } 093 } 094 095}