Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
IngestModuleFactoryLoader.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2014-2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.ingest;
20 
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.TreeMap;
26 import java.util.logging.Level;
27 import org.openide.DialogDisplayer;
28 import org.openide.NotifyDescriptor;
29 import org.openide.util.Lookup;
30 import org.openide.util.NbBundle;
45 
49 final class IngestModuleFactoryLoader {
50 
51  private static final Logger logger = Logger.getLogger(IngestModuleFactoryLoader.class.getName());
52  private static final String SAMPLE_MODULE_FACTORY_CLASS_NAME = SampleIngestModuleFactory.class.getCanonicalName();
53  private static final ArrayList<String> coreModuleOrdering = new ArrayList<String>() {
54  private static final long serialVersionUID = 1L;
55  {
56  // The ordering of the core ingest module factories implemented
57  // using Java is hard-coded.
58  add("org.sleuthkit.autopsy.recentactivity.RecentActivityExtracterModuleFactory"); //NON-NLS
59  add(HashLookupModuleFactory.class.getCanonicalName());
60  add(FileTypeIdModuleFactory.class.getCanonicalName());
61  add(FileExtMismatchDetectorModuleFactory.class.getCanonicalName());
62  add(EmbeddedFileExtractorModuleFactory.class.getCanonicalName());
63  add(PictureAnalyzerIngestModuleFactory.class.getCanonicalName());
64  add("org.sleuthkit.autopsy.keywordsearch.KeywordSearchModuleFactory"); //NON-NLS
65  add("org.sleuthkit.autopsy.thunderbirdparser.EmailParserModuleFactory"); //NON-NLS
66  add(EncryptionDetectionModuleFactory.class.getCanonicalName());
67  add(InterestingItemsIngestModuleFactory.class.getCanonicalName());
68  add(CentralRepoIngestModuleFactory.class.getCanonicalName());
69  add(PhotoRecCarverIngestModuleFactory.class.getCanonicalName());
70  add(VMExtractorIngestModuleFactory.class.getCanonicalName());
71  add(DataSourceIntegrityModuleFactory.class.getCanonicalName());
72  }
73  };
74 
85  static List<IngestModuleFactory> getIngestModuleFactories() {
86  // A hash set of display names and a hash map of class names to
87  // discovered factories are used to de-duplicate and order the
88  // factories.
89  HashSet<String> moduleDisplayNames = new HashSet<>();
90  HashMap<String, IngestModuleFactory> javaFactoriesByClass = new HashMap<>();
91 
92  // Discover the ingest module factories implemented using Java with a
93  // service provider annotation for the IngestModuleFactory interface.
94  for (IngestModuleFactory factory : Lookup.getDefault().lookupAll(IngestModuleFactory.class)) {
95  IngestModuleFactoryLoader.addFactory(factory, moduleDisplayNames, javaFactoriesByClass);
96  }
97 
98  // Discover the ingest module factories implemented using Java with a
99  // service provider annotation for the IngestModuleFactoryAdapter
100  // abstract base class.
101  for (IngestModuleFactory factory : Lookup.getDefault().lookupAll(IngestModuleFactoryAdapter.class)) {
102  if (!javaFactoriesByClass.containsValue(factory)) {
103  IngestModuleFactoryLoader.addFactory(factory, moduleDisplayNames, javaFactoriesByClass);
104  }
105  }
106 
107  // Add the core ingest module factories in the desired order, removing
108  // the core factories from the map so that the map will only contain
109  // non-core modules after this loop.
110  List<IngestModuleFactory> factories = new ArrayList<>();
111  for (String className : coreModuleOrdering) {
112  IngestModuleFactory coreFactory = javaFactoriesByClass.remove(className);
113  if (coreFactory != null) {
114  factories.add(coreFactory);
115  } else {
116  logger.log(Level.SEVERE, "Core factory {0} not loaded", className); //NON-NLS
117  }
118  }
119 
120  // Add any remaining non-core factories discovered. Order with an
121  // alphabetical sort by module display name.
122  TreeMap<String, IngestModuleFactory> javaFactoriesSortedByName = new TreeMap<>();
123  for (IngestModuleFactory factory : javaFactoriesByClass.values()) {
124  javaFactoriesSortedByName.put(factory.getModuleDisplayName(), factory);
125  }
126  factories.addAll(javaFactoriesSortedByName.values());
127 
128  // Add any ingest module factories implemented using Jython. Order is
129  // not guaranteed!
130  for (IngestModuleFactory factory : JythonModuleLoader.getIngestModuleFactories()) {
131  if (!moduleDisplayNames.contains(factory.getModuleDisplayName())) {
132  moduleDisplayNames.add(factory.getModuleDisplayName());
133  factories.add(factory);
134  logger.log(Level.INFO, "Found ingest module factory: name = {0}, version = {1}", new Object[]{factory.getModuleDisplayName(), factory.getModuleVersionNumber()}); //NON-NLS
135  } else {
136  logger.log(Level.WARNING, "Found duplicate ingest module display name (name = {0})", factory.getModuleDisplayName()); //NON-NLS
137  DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
138  NbBundle.getMessage(IngestModuleFactoryLoader.class, "IngestModuleFactoryLoader.errorMessages.duplicateDisplayName", factory.getModuleDisplayName()),
139  NotifyDescriptor.ERROR_MESSAGE));
140  }
141  }
142 
143  return factories;
144  }
145 
146  private static void addFactory(IngestModuleFactory factory, HashSet<String> moduleDisplayNames, HashMap<String, IngestModuleFactory> javaFactoriesByClass) {
147  // Ignore the sample ingest module factories implemented in Java.
148  String className = factory.getClass().getCanonicalName();
149  if (className.equals(IngestModuleFactoryLoader.SAMPLE_MODULE_FACTORY_CLASS_NAME)) {
150  return;
151  }
152 
153  if (!moduleDisplayNames.contains(factory.getModuleDisplayName())) {
154  moduleDisplayNames.add(factory.getModuleDisplayName());
155  javaFactoriesByClass.put(factory.getClass().getCanonicalName(), factory);
156  logger.log(Level.INFO, "Found ingest module factory: name = {0}, version = {1}", new Object[]{factory.getModuleDisplayName(), factory.getModuleVersionNumber()}); //NON-NLS
157  } else {
158  logger.log(Level.WARNING, "Found duplicate ingest module display name (name = {0})", factory.getModuleDisplayName()); //NON-NLS
159  }
160  }
161 
165  private IngestModuleFactoryLoader() {
166  }
167 
168 }

Copyright © 2012-2022 Basis Technology. Generated on: Tue Jun 27 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.