001package org.apache.archiva.metadata.repository; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.slf4j.Logger; 023import org.slf4j.LoggerFactory; 024import java.lang.AutoCloseable; 025 026/** 027 * The repository session provides a single interface to accessing Archiva repositories. It provides access to three 028 * resources: 029 * <ul> 030 * <li>{@link MetadataRepository} - the metadata content repository for read/write access, in its current state (no 031 * remote resources will be retrieved in the process</li> 032 * <li>{@link MetadataResolver} - access to resolve metadata content, accommodating metadata not yet stored or up to 033 * date in the content repository (i.e. virtualised repositories, remote proxied content, or metadata in a different 034 * model format in the repository storage)</li> 035 * <li>{@link org.apache.archiva.metadata.repository.storage.RepositoryStorage} - access to the physical storage of a 036 * repository and the source artifacts and project models</li> 037 * </ul> 038 */ 039public class RepositorySession 040 implements AutoCloseable 041{ 042 private final MetadataRepository repository; 043 044 private final MetadataResolver resolver; 045 046 private boolean dirty; 047 048 private Logger log = LoggerFactory.getLogger( getClass() ); 049 050 // FIXME: include storage here too - perhaps a factory based on repository ID, or one per type to retrieve and 051 // operate on a given repo within the storage API 052 053 public RepositorySession( MetadataRepository metadataRepository, MetadataResolver resolver ) 054 { 055 this.repository = metadataRepository; 056 this.resolver = resolver; 057 } 058 059 public MetadataRepository getRepository() 060 { 061 return repository; 062 } 063 064 public MetadataResolver getResolver() 065 { 066 return resolver; 067 } 068 069 public void save() 070 { 071 repository.save(); 072 073 dirty = false; 074 } 075 076 public void revert() 077 { 078 repository.revert(); 079 dirty = false; 080 } 081 082 /** 083 * Close the session. Required to be called for all open sessions to ensure resources are properly released. 084 * If the session has been marked as dirty, it will be saved. This may save partial changes in the case of a typical 085 * <code>try { ... } finally { ... }</code> approach - if this is a problem, ensure you revert changes when an 086 * exception occurs. 087 * <b>can throw RuntimeException</b> 088 */ 089 @Override 090 public void close() 091 { 092 try 093 { 094 if ( dirty ) 095 { 096 save(); 097 } 098 } 099 finally 100 { 101 try 102 { 103 repository.close(); 104 } 105 catch ( MetadataRepositoryException e ) 106 { 107 throw new RuntimeException( e.getMessage(), e ); 108 } 109 } 110 } 111 112 /** 113 * ignore RuntimeException when closing repository 114 * @since 1.4-M4 115 */ 116 public void closeQuietly() 117 { 118 try 119 { 120 this.close(); 121 } 122 catch ( RuntimeException e ) 123 { 124 log.warn( "ignore Runtime exception while closing: {}", e.getMessage(), e ); 125 } 126 } 127 128 129 public void markDirty() 130 { 131 this.dirty = true; 132 } 133}