This project has retired. For details please refer to its Attic page.
MavenIndexContext xref
View Javadoc
1   package org.apache.archiva.indexer.maven;
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.apache.archiva.common.filelock.DefaultFileLockManager;
23  import org.apache.archiva.common.filelock.FileLockManager;
24  import org.apache.archiva.indexer.ArchivaIndexingContext;
25  import org.apache.archiva.repository.Repository;
26  import org.apache.archiva.repository.storage.FilesystemStorage;
27  import org.apache.archiva.repository.storage.StorageAsset;
28  import org.apache.maven.index.context.IndexingContext;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import java.io.IOException;
33  import java.net.URI;
34  import java.nio.file.Files;
35  import java.nio.file.NoSuchFileException;
36  import java.nio.file.Path;
37  import java.sql.Date;
38  import java.time.ZonedDateTime;
39  import java.util.Set;
40  import java.util.concurrent.atomic.AtomicBoolean;
41  
42  /**
43   * Maven implementation of index context
44   */
45  public class MavenIndexContext implements ArchivaIndexingContext {
46  
47      private static final Logger log = LoggerFactory.getLogger(ArchivaIndexingContext.class);
48  
49  
50      private AtomicBoolean openStatus = new AtomicBoolean(false);
51      private IndexingContext delegate;
52      private Repository repository;
53      private StorageAsset dir = null;
54  
55      protected MavenIndexContext(Repository repository, IndexingContext delegate) {
56          this.delegate = delegate;
57          this.repository = repository;
58          this.openStatus.set(true);
59  
60      }
61  
62      @Override
63      public String getId() {
64          return delegate.getId();
65      }
66  
67      @Override
68      public Repository getRepository() {
69          return repository;
70      }
71  
72      @Override
73      public StorageAsset getPath() {
74          if (dir==null) {
75              StorageAsset repositoryDirAsset = repository.getAsset("");
76              Path repositoryDir = repositoryDirAsset.getFilePath().toAbsolutePath();
77              Path indexDir = delegate.getIndexDirectoryFile().toPath();
78              if (indexDir.startsWith(repositoryDir)) {
79                  dir = repository.getAsset(repositoryDir.relativize(indexDir).toString());
80              } else {
81                  try {
82                      FilesystemStorage/FilesystemStorage.html#FilesystemStorage">FilesystemStorage storage = new FilesystemStorage(indexDir, new DefaultFileLockManager());
83                      dir = storage.getAsset("");
84                  } catch (IOException e) {
85                      log.error("Error occured while creating storage for index dir");
86                  }
87              }
88          }
89          return dir;
90      }
91  
92      @Override
93      public boolean isEmpty() throws IOException {
94          return Files.list(delegate.getIndexDirectoryFile().toPath()).count()==0;
95      }
96  
97      @Override
98      public void commit() throws IOException {
99          delegate.commit();
100     }
101 
102     @Override
103     public void rollback() throws IOException {
104         delegate.rollback();
105     }
106 
107     @Override
108     public void optimize() throws IOException {
109         delegate.optimize();
110     }
111 
112     @Override
113     public void close(boolean deleteFiles) throws IOException {
114         if (openStatus.compareAndSet(true,false)) {
115             try {
116                 delegate.close(deleteFiles);
117             } catch (NoSuchFileException e) {
118                 // Ignore missing directory
119             }
120         }
121     }
122 
123     @Override
124     public void close() throws IOException {
125         if (openStatus.compareAndSet(true,false)) {
126             try {
127                 delegate.close(false);
128             } catch (NoSuchFileException e) {
129                 // Ignore missing directory
130             }
131         }
132     }
133 
134     @Override
135     public boolean isOpen() {
136         return openStatus.get();
137     }
138 
139     @Override
140     public void purge() throws IOException {
141         delegate.purge();
142     }
143 
144     @Override
145     public boolean supports(Class<?> clazz) {
146         return IndexingContext.class.equals(clazz);
147     }
148 
149     @SuppressWarnings( "unchecked" )
150     @Override
151     public <T> T getBaseContext(Class<T> clazz) throws UnsupportedOperationException {
152         if (IndexingContext.class.equals(clazz)) {
153             return (T) delegate;
154         } else {
155             throw new UnsupportedOperationException("The class "+clazz+" is not supported by the maven indexer");
156         }
157     }
158 
159     @Override
160     public Set<String> getGroups() throws IOException {
161         return delegate.getAllGroups();
162     }
163 
164     @Override
165     public void updateTimestamp(boolean save) throws IOException {
166         delegate.updateTimestamp(save);
167     }
168 
169     @Override
170     public void updateTimestamp(boolean save, ZonedDateTime time) throws IOException {
171         delegate.updateTimestamp(save, Date.from(time.toInstant()));
172     }
173 
174 
175 }