Sleuth Kit Java Bindings (JNI)  4.2
Java bindings for using The Sleuth Kit
AbstractContent.java
Go to the documentation of this file.
1 /*
2  * Sleuth Kit Data Model
3  *
4  * Copyright 2011 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.datamodel;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
31 
36 public abstract class AbstractContent implements Content {
37 
38  public final static long UNKNOWN_ID = -1;
39  private final SleuthkitCase db;
40  private long objId;
41  private String name;
42  private Content parent;
43  private String uniquePath;
44  protected long parentId;
45  private volatile boolean hasChildren;
46  private volatile boolean checkedHasChildren;
47  private volatile int childrenCount;
49 
50  protected AbstractContent(SleuthkitCase db, long obj_id, String name) {
51  this.db = db;
52  this.objId = obj_id;
53  this.name = name;
54  this.parentId = UNKNOWN_ID;
55 
56  checkedHasChildren = false;
57  hasChildren = false;
58  childrenCount = -1;
59  }
60 
61  @Override
62  public String getName() {
63  return this.name;
64  }
65 
66  /*
67  * This base implementation simply walks the hierarchy appending its own
68  * name to the result of calling its parent's getUniquePath() method (with
69  * interleaving forward slashes).
70  */
71  @Override
72  public synchronized String getUniquePath() throws TskCoreException {
73  if (uniquePath == null) {
74  uniquePath = "";
75  if (!name.isEmpty()) {
76  uniquePath = "/" + getName();
77  }
78 
79  Content myParent = getParent();
80  if (myParent != null) {
81  uniquePath = myParent.getUniquePath() + uniquePath;
82  }
83  }
84  return uniquePath;
85  }
86 
87  @Override
88  public boolean hasChildren() throws TskCoreException {
89  if (checkedHasChildren == true) {
90  return hasChildren;
91  }
92 
93  hasChildren = this.getSleuthkitCase().getContentHasChildren(this);
94  checkedHasChildren = true;
95 
96  if (!hasChildren) {
97  childrenCount = 0;
98  }
99 
100  return hasChildren;
101  }
102 
103  @Override
104  public int getChildrenCount() throws TskCoreException {
105  if (childrenCount != -1) {
106  return childrenCount;
107  }
108 
109  childrenCount = this.getSleuthkitCase().getContentChildrenCount(this);
110 
111  hasChildren = childrenCount > 0;
112  checkedHasChildren = true;
113 
114  return childrenCount;
115  }
116 
117  @Override
118  public synchronized Content getParent() throws TskCoreException {
119  if (parent == null) {
120  ObjectInfo parentInfo = null;
121  try {
122  parentInfo = db.getParentInfo(this);
123  } catch (TskCoreException ex) {
124  // there is not parent; not an error if we've got a data source
125  return null;
126  }
127  parent = db.getContentById(parentInfo.id);
128  }
129  return parent;
130  }
131 
132  void setParent(Content parent) {
133  this.parent = parent;
134  }
135 
142  void setParentId(long parentId) {
143  this.parentId = parentId;
144  }
145 
146  @Override
147  public long getId() {
148  return this.objId;
149  }
150 
151  // classes should override this if they can be a data source
152  @Override
154  Content myParent = getParent();
155  if (myParent == null) {
156  return null;
157  }
158 
159  return myParent.getDataSource();
160  }
161 
168  return db;
169  }
170 
171  @Override
172  public boolean equals(Object obj) {
173  if (obj == null) {
174  return false;
175  }
176  if (getClass() != obj.getClass()) {
177  return false;
178  }
179  final AbstractContent other = (AbstractContent) obj;
180  if (this.objId != other.objId) {
181  return false;
182  }
183  return true;
184  }
185 
186  @Override
187  public int hashCode() {
188  int hash = 7;
189  hash = 41 * hash + (int) (this.objId ^ (this.objId >>> 32));
190  return hash;
191  }
192 
193  @Override
194  public BlackboardArtifact newArtifact(int artifactTypeID) throws TskCoreException {
195  // don't let them make more than 1 GEN_INFO
196  if (artifactTypeID == ARTIFACT_TYPE.TSK_GEN_INFO.getTypeID()) {
197  return getGenInfoArtifact(true);
198  }
199  return db.newBlackboardArtifact(artifactTypeID, objId);
200  }
201 
202  @Override
204  return newArtifact(type.getTypeID());
205  }
206 
207  @Override
208  public ArrayList<BlackboardArtifact> getArtifacts(String artifactTypeName) throws TskCoreException {
209  return getArtifacts(ARTIFACT_TYPE.fromLabel(artifactTypeName).getTypeID());
210  }
211 
212  @Override
213  public ArrayList<BlackboardArtifact> getArtifacts(int artifactTypeID) throws TskCoreException {
214  if (artifactTypeID == ARTIFACT_TYPE.TSK_GEN_INFO.getTypeID()) {
215  if (genInfoArtifact == null) // don't make one if it doesn't already exist
216  {
217  getGenInfoArtifact(false);
218  }
219 
220  ArrayList<BlackboardArtifact> list = new ArrayList<BlackboardArtifact>();
221  // genInfoArtifact coudl still be null if there isn't an artifact
222  if (genInfoArtifact != null) {
223  list.add(genInfoArtifact);
224  }
225  return list;
226  }
227  return db.getBlackboardArtifacts(artifactTypeID, objId);
228  }
229 
230  @Override
231  public ArrayList<BlackboardArtifact> getArtifacts(BlackboardArtifact.ARTIFACT_TYPE type) throws TskCoreException {
232  return getArtifacts(type.getTypeID());
233  }
234 
235  @Override
237  return getGenInfoArtifact(true);
238  }
239 
240  @Override
242  if (genInfoArtifact != null) {
243  return genInfoArtifact;
244  }
245 
246  // go to db directly to avoid infinite loop
247  ArrayList<BlackboardArtifact> arts = db.getBlackboardArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_GEN_INFO, objId);
248  BlackboardArtifact retArt;
249  if (arts.isEmpty()) {
250  if (create) {
252  } else {
253  return null;
254  }
255  } else {
256  retArt = arts.get(0);
257  }
258  genInfoArtifact = retArt;
259  return retArt;
260  }
261 
262  @Override
263  public ArrayList<BlackboardAttribute> getGenInfoAttributes(ATTRIBUTE_TYPE attr_type) throws TskCoreException {
264  ArrayList<BlackboardAttribute> returnList = new ArrayList<BlackboardAttribute>();
265 
266  if (genInfoArtifact == null) {
267  getGenInfoArtifact(false);
268  if (genInfoArtifact == null) {
269  return returnList;
270  }
271  }
272 
273  for (BlackboardAttribute attribute : genInfoArtifact.getAttributes()) {
274  if (attribute.getAttributeTypeID() == attr_type.getTypeID()) {
275  returnList.add(attribute);
276  }
277  }
278 
279  return returnList;
280  }
281 
282  @Override
283  public ArrayList<BlackboardArtifact> getAllArtifacts() throws TskCoreException {
284  return db.getMatchingArtifacts("WHERE obj_id = " + objId); //NON-NLS
285  }
286 
287  @Override
288  public long getArtifactsCount(String artifactTypeName) throws TskCoreException {
289  return db.getBlackboardArtifactsCount(artifactTypeName, objId);
290  }
291 
292  @Override
293  public long getArtifactsCount(int artifactTypeID) throws TskCoreException {
294  return db.getBlackboardArtifactsCount(artifactTypeID, objId);
295  }
296 
297  @Override
299  return db.getBlackboardArtifactsCount(type, objId);
300  }
301 
302  @Override
303  public long getAllArtifactsCount() throws TskCoreException {
304  return db.getBlackboardArtifactsCount(objId);
305  }
306 
307  @Override
308  public Set<String> getHashSetNames() throws TskCoreException {
309  Set<String> hashNames = new HashSet<String>();
310  ArrayList<BlackboardArtifact> artifacts = getArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_HASHSET_HIT);
311 
312  for (BlackboardArtifact a : artifacts) {
313  List<BlackboardAttribute> attributes = a.getAttributes(ATTRIBUTE_TYPE.TSK_SET_NAME);
314  for (BlackboardAttribute attr : attributes) {
315  hashNames.add(attr.getValueString());
316  }
317  }
318  return Collections.unmodifiableSet(hashNames);
319  }
320 
321  @Override
322  public String toString() {
323  return toString(true);
324  }
325 
326  public String toString(boolean preserveState) {
327  if (preserveState) {
328  return "AbstractContent [\t" + "objId " + String.format("%010d", objId) + "\t" //NON-NLS
329  + "name " + name + "\t" + "parentId " + parentId + "\t" //NON-NLS
330  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
331  + "\t" + "hasChildren " + hasChildren //NON-NLS
332  + "\t" + "childrenCount " + childrenCount //NON-NLS
333  + "uniquePath " + uniquePath + "]\t"; //NON-NLS
334  } else {
335  try {
336  if (getParent() != null) {
337  return "AbstractContent [\t" + "objId " + String.format("%010d", objId) //NON-NLS
338  + "\t" + "name " + name //NON-NLS
339  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
340  + "\t" + "hasChildren " + hasChildren //NON-NLS
341  + "\t" + "childrenCount " + childrenCount //NON-NLS
342  + "\t" + "getUniquePath " + getUniquePath() //NON-NLS
343  + "\t" + "getParent " + getParent().getId() + "]\t"; //NON-NLS
344  } else {
345  return "AbstractContent [\t" + "objId " //NON-NLS
346  + String.format("%010d", objId) + "\t" + "name " + name //NON-NLS
347  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
348  + "\t" + "hasChildren " + hasChildren //NON-NLS
349  + "\t" + "childrenCount " + childrenCount //NON-NLS
350  + "\t" + "uniquePath " + getUniquePath() //NON-NLS
351  + "\t" + "parentId " + parentId + "]\t"; //NON-NLS
352  }
353  } catch (TskCoreException ex) {
354  Logger.getLogger(AbstractContent.class.getName()).log(Level.SEVERE, "Could not find Parent", ex); //NON-NLS
355  return "AbstractContent [\t" + "objId " + String.format("%010d", objId) + "\t" //NON-NLS
356  + "name " + name + "\t" + "parentId " + parentId + "\t" //NON-NLS
357  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
358  + "\t" + "hasChildren " + hasChildren //NON-NLS
359  + "\t" + "childrenCount " + childrenCount //NON-NLS
360  + "uniquePath " + uniquePath + "]\t"; //NON-NLS
361  }
362  }
363  }
364 }
ArrayList< BlackboardArtifact > getBlackboardArtifacts(int artifactTypeID)
ArrayList< BlackboardArtifact > getArtifacts(int artifactTypeID)
ArrayList< BlackboardArtifact > getAllArtifacts()
String toString(boolean preserveState)
ArrayList< BlackboardArtifact > getMatchingArtifacts(String whereClause)
BlackboardArtifact newArtifact(BlackboardArtifact.ARTIFACT_TYPE type)
ArrayList< BlackboardArtifact > getArtifacts(BlackboardArtifact.ARTIFACT_TYPE type)
BlackboardArtifact newBlackboardArtifact(int artifactTypeID, long obj_id)
AbstractContent(SleuthkitCase db, long obj_id, String name)
long getArtifactsCount(ARTIFACT_TYPE type)
BlackboardArtifact newArtifact(int artifactTypeID)
long getArtifactsCount(int artifactTypeID)
ArrayList< BlackboardArtifact > getArtifacts(String artifactTypeName)
BlackboardArtifact getGenInfoArtifact(boolean create)
ArrayList< BlackboardAttribute > getGenInfoAttributes(ATTRIBUTE_TYPE attr_type)
long getArtifactsCount(String artifactTypeName)

Copyright © 2011-2015 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.