This project has retired. For details please refer to its Attic page.
RepositorySession xref
View Javadoc
1   package org.apache.archiva.metadata.repository;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import java.lang.AutoCloseable;
25  
26  /**
27   * The repository session provides a single interface to accessing Archiva repositories. It provides access to three
28   * resources:
29   * <ul>
30   * <li>{@link MetadataRepository} - the metadata content repository for read/write access, in its current state (no
31   * remote resources will be retrieved in the process</li>
32   * <li>{@link MetadataResolver} - access to resolve metadata content, accommodating metadata not yet stored or up to
33   * date in the content repository (i.e. virtualised repositories, remote proxied content, or metadata in a different
34   * model format in the repository storage)</li>
35   * <li>{@link org.apache.archiva.metadata.repository.storage.RepositoryStorage} - access to the physical storage of a
36   * repository and the source artifacts and project models</li>
37   * </ul>
38   */
39  public class RepositorySession
40      implements AutoCloseable
41  {
42      private final MetadataRepository repository;
43  
44      private final MetadataResolver resolver;
45  
46      private boolean dirty;
47  
48      private Logger log = LoggerFactory.getLogger( getClass() );
49  
50      // FIXME: include storage here too - perhaps a factory based on repository ID, or one per type to retrieve and
51      //        operate on a given repo within the storage API
52  
53      public RepositorySession( MetadataRepository metadataRepository, MetadataResolver resolver )
54      {
55          this.repository = metadataRepository;
56          this.resolver = resolver;
57      }
58  
59      public MetadataRepository getRepository()
60      {
61          return repository;
62      }
63  
64      public MetadataResolver getResolver()
65      {
66          return resolver;
67      }
68  
69      public void save()
70      {
71          repository.save();
72  
73          dirty = false;
74      }
75  
76      public void revert()
77      {
78          repository.revert();
79          dirty = false;
80      }
81  
82      /**
83       * Close the session. Required to be called for all open sessions to ensure resources are properly released.
84       * If the session has been marked as dirty, it will be saved. This may save partial changes in the case of a typical
85       * <code>try { ... } finally { ... }</code> approach - if this is a problem, ensure you revert changes when an
86       * exception occurs.
87       * <b>can throw RuntimeException</b>
88       */
89      @Override
90      public void close()
91      {
92          try
93          {
94              if ( dirty )
95              {
96                  save();
97              }
98          }
99          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 }