View Javadoc
1   package fr.ifremer.coser.result.repository.echobase;
2   
3   /*
4    * #%L
5    * Coser :: Business
6    * %%
7    * Copyright (C) 2010 - 2014 Ifremer, Codelutin, Chemit Tony
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.base.Preconditions;
26  import com.google.common.collect.Sets;
27  import fr.ifremer.coser.bean.EchoBaseProject;
28  import fr.ifremer.coser.result.ResultRepositoryInitializationException;
29  import fr.ifremer.coser.result.repository.ResultRepositoryProvider;
30  import org.apache.commons.io.filefilter.AbstractFileFilter;
31  import org.apache.commons.io.filefilter.AndFileFilter;
32  import org.apache.commons.io.filefilter.FileFilterUtils;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import java.io.File;
37  import java.io.FileFilter;
38  import java.io.IOException;
39  import java.util.Set;
40  
41  /**
42   * Created on 3/5/14.
43   *
44   * @author Tony Chemit <chemit@codelutin.com>
45   * @since 1.5
46   */
47  public class EchoBaseResultRepositoryProvider implements ResultRepositoryProvider<EchoBaseResultRepository> {
48  
49      /** Logger. */
50      private static final Log log = LogFactory.getLog(EchoBaseResultRepositoryProvider.class);
51  
52      /**
53       * Directory where are stored all echobase projects.
54       */
55      protected final File basedir;
56  
57      public EchoBaseResultRepositoryProvider(File basedir) {
58          Preconditions.checkNotNull(basedir);
59          this.basedir = basedir;
60      }
61  
62      @Override
63      public EchoBaseResultRepositoryType getRepositoryType() {
64          return EchoBaseResultRepositoryType.INSTANCE;
65      }
66  
67      @Override
68      public Set<EchoBaseResultRepository> loadRepositories() {
69  
70          if (log.isInfoEnabled()) {
71              log.info(String.format("Scan for projects from basedir: %s", basedir));
72          }
73          Set<EchoBaseResultRepository> result = Sets.newHashSet();
74  
75          AndFileFilter projectFilter = new AndFileFilter();
76          projectFilter.addFileFilter(FileFilterUtils.directoryFileFilter());
77          projectFilter.addFileFilter(new AbstractFileFilter() {
78  
79              @Override
80              public boolean accept(File pathname) {
81                  return new File(pathname, EchoBaseProject.METADATA_FILE).exists();
82              }
83          });
84  
85          File[] projects = basedir.listFiles((FileFilter) projectFilter);
86          if (projects != null) {
87              for (File projectDirectory : projects) {
88  
89                  EchoBaseProject project = new EchoBaseProject(projectDirectory);
90  
91                  if (log.isDebugEnabled()) {
92                      log.debug(String.format("Detected result: %s", project.getName()));
93                  }
94                  try {
95                      project.load();
96                  } catch (IOException e) {
97                      throw new ResultRepositoryInitializationException(this, "Could not load project file", e);
98                  }
99                  result.add(new EchoBaseResultRepository(project));
100             }
101         }
102         if (log.isInfoEnabled()) {
103             log.info(String.format("Found %s result repository(ies) from basedir: %s", result.size(), basedir));
104         }
105         return result;
106     }
107 }