Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
JythonModuleLoader.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2014 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.python;
20 
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.FilenameFilter;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Scanner;
29 import java.util.Set;
30 import java.util.logging.Level;
31 import java.util.regex.Matcher;
32 import org.openide.DialogDisplayer;
33 import org.openide.NotifyDescriptor;
34 import org.openide.modules.InstalledFileLocator;
35 import org.openide.util.NbBundle;
36 import org.python.util.PythonInterpreter;
41 
46 public final class JythonModuleLoader {
47 
48  private static final Logger logger = Logger.getLogger(JythonModuleLoader.class.getName());
49 
56  public static List<IngestModuleFactory> getIngestModuleFactories() {
58  }
59 
66  public static List<GeneralReportModule> getGeneralReportModules() {
68  }
69 
70  private static <T> List<T> getInterfaceImplementations(LineFilter filter, Class<T> interfaceClass) {
71  List<T> objects = new ArrayList<>();
72  Set<File> pythonModuleDirs = new HashSet<>();
73  PythonInterpreter interpreter = new PythonInterpreter();
74 
75  // add python modules from 'autospy/build/cluster/InternalPythonModules' folder
76  // which are copied from 'autopsy/*/release/InternalPythonModules' folders.
77  for (File f : InstalledFileLocator.getDefault().locateAll("InternalPythonModules", JythonModuleLoader.class.getPackage().getName(), false)) { //NON-NLS
78  Collections.addAll(pythonModuleDirs, f.listFiles());
79  }
80  // add python modules from 'testuserdir/python_modules' folder
81  Collections.addAll(pythonModuleDirs, new File(PlatformUtil.getUserPythonModulesPath()).listFiles());
82 
83  for (File file : pythonModuleDirs) {
84  if (file.isDirectory()) {
85  File[] pythonScripts = file.listFiles(new PythonScriptFileFilter());
86  for (File script : pythonScripts) {
87  try {
88  Scanner fileScanner = new Scanner(script);
89  while (fileScanner.hasNextLine()) {
90  String line = fileScanner.nextLine();
91  if (line.startsWith("class ") && filter.accept(line)) { //NON-NLS
92  String className = line.substring(6, line.indexOf("("));
93  try {
94  objects.add(createObjectFromScript(interpreter, script, className, interfaceClass));
95  } catch (Exception ex) {
96  logger.log(Level.SEVERE, String.format("Failed to load %s from %s", className, script.getAbsolutePath()), ex); //NON-NLS
97  // NOTE: using ex.toString() because the current version is always returning null for ex.getMessage().
98  DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
99  NbBundle.getMessage(JythonModuleLoader.class, "JythonModuleLoader.errorMessages.failedToLoadModule", className, ex.toString()),
100  NotifyDescriptor.ERROR_MESSAGE));
101  }
102  }
103  }
104  } catch (FileNotFoundException ex) {
105  logger.log(Level.SEVERE, String.format("Failed to open %s", script.getAbsolutePath()), ex); //NON-NLS
106  DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
107  NbBundle.getMessage(JythonModuleLoader.class, "JythonModuleLoader.errorMessages.failedToOpenModule", script.getAbsolutePath()),
108  NotifyDescriptor.ERROR_MESSAGE));
109  }
110  }
111  }
112  }
113  return objects;
114  }
115 
116  private static <T> T createObjectFromScript(PythonInterpreter interpreter, File script, String className, Class<T> interfaceClass) {
117  // Add the directory where the Python script resides to the Python
118  // module search path to allow the script to use other scripts bundled
119  // with it.
120  interpreter.exec("import sys"); //NON-NLS
121  String path = Matcher.quoteReplacement(script.getParent());
122  interpreter.exec("sys.path.append('" + path + "')"); //NON-NLS
123  String moduleName = script.getName().replaceAll("\\.py$", ""); //NON-NLS
124 
125  // reload the module so that the changes made to it can be loaded.
126  interpreter.exec("import " + moduleName); //NON-NLS
127  interpreter.exec("reload(" + moduleName + ")"); //NON-NLS
128 
129  // Importing the appropriate class from the Py Script which contains multiple classes.
130  interpreter.exec("from " + moduleName + " import " + className); //NON-NLS
131  interpreter.exec("obj = " + className + "()"); //NON-NLS
132 
133  T obj = interpreter.get("obj", interfaceClass); //NON-NLS
134 
135  // Remove the directory where the Python script resides from the Python
136  // module search path.
137  interpreter.exec("sys.path.remove('" + path + "')"); //NON-NLS
138 
139  return obj;
140  }
141 
142  private static class PythonScriptFileFilter implements FilenameFilter {
143 
144  @Override
145  public boolean accept(File dir, String name) {
146  return name.endsWith(".py"); //NON-NLS
147  } //NON-NLS
148  }
149 
150  private static interface LineFilter {
151 
152  boolean accept(String line);
153  }
154 
155  private static class IngestModuleFactoryDefFilter implements LineFilter {
156 
157  @Override
158  public boolean accept(String line) {
159  return (line.contains("IngestModuleFactoryAdapter") || line.contains("IngestModuleFactory")); //NON-NLS
160  }
161  }
162 
163  private static class GeneralReportModuleDefFilter implements LineFilter {
164 
165  @Override
166  public boolean accept(String line) {
167  return (line.contains("GeneralReportModuleAdapter") || line.contains("GeneralReportModule")); //NON-NLS
168  }
169  }
170 }
static< T > List< T > getInterfaceImplementations(LineFilter filter, Class< T > interfaceClass)
static List< IngestModuleFactory > getIngestModuleFactories()
static List< GeneralReportModule > getGeneralReportModules()
static< T > T createObjectFromScript(PythonInterpreter interpreter, File script, String className, Class< T > interfaceClass)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161

Copyright © 2012-2016 Basis Technology. Generated on: Tue Oct 25 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.