Sleuth Kit Java Bindings (JNI)  4.3
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-2016 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.Set;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
30 
35 public abstract class AbstractContent implements Content {
36 
37  public final static long UNKNOWN_ID = -1;
38  private final SleuthkitCase db;
39  private final long objId;
40  private final String name;
41  private Content parent;
42  private String uniquePath;
43  protected long parentId;
44  private volatile boolean hasChildren;
45  private volatile boolean checkedHasChildren;
46  private volatile int childrenCount;
48 
49  protected AbstractContent(SleuthkitCase db, long obj_id, String name) {
50  this.db = db;
51  this.objId = obj_id;
52  this.name = name;
53  this.parentId = UNKNOWN_ID;
54 
55  checkedHasChildren = false;
56  hasChildren = false;
57  childrenCount = -1;
58  }
59 
60  @Override
61  public String getName() {
62  return this.name;
63  }
64 
65  /*
66  * This base implementation simply walks the hierarchy appending its own
67  * name to the result of calling its parent's getUniquePath() method (with
68  * interleaving forward slashes).
69  */
70  @Override
71  public synchronized String getUniquePath() throws TskCoreException {
72  if (uniquePath == null) {
73  uniquePath = "";
74  if (!name.isEmpty()) {
75  uniquePath = "/" + getName();
76  }
77 
78  Content myParent = getParent();
79  if (myParent != null) {
80  uniquePath = myParent.getUniquePath() + uniquePath;
81  }
82  }
83  return uniquePath;
84  }
85 
86  @Override
87  public boolean hasChildren() throws TskCoreException {
88  if (checkedHasChildren == true) {
89  return hasChildren;
90  }
91 
92  hasChildren = this.getSleuthkitCase().getContentHasChildren(this);
93  checkedHasChildren = true;
94 
95  if (!hasChildren) {
96  childrenCount = 0;
97  }
98 
99  return hasChildren;
100  }
101 
102  @Override
103  public int getChildrenCount() throws TskCoreException {
104  if (childrenCount != -1) {
105  return childrenCount;
106  }
107 
108  childrenCount = this.getSleuthkitCase().getContentChildrenCount(this);
109 
110  hasChildren = childrenCount > 0;
111  checkedHasChildren = true;
112 
113  return childrenCount;
114  }
115 
116  @Override
117  public synchronized Content getParent() throws TskCoreException {
118  if (parent == null) {
119  ObjectInfo parentInfo;
120  try {
121  parentInfo = db.getParentInfo(this);
122  } catch (TskCoreException ex) {
123  // there is not parent; not an error if we've got a data source
124  return null;
125  }
126  parent = db.getContentById(parentInfo.getId());
127  }
128  return parent;
129  }
130 
131  void setParent(Content parent) {
132  this.parent = parent;
133  }
134 
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 
184  try {
185  // New children may have been added to an existing content
186  // object in which case they are not equal.
187  if (this.getChildrenCount() != other.getChildrenCount()) {
188  return false;
189  }
190  } catch (TskCoreException ex) {
191  Logger.getLogger(AbstractContent.class.getName()).log(Level.SEVERE, null, ex);
192  }
193 
194  return true;
195  }
196 
197  @Override
198  public int hashCode() {
199  int hash = 7 + (int) (this.objId ^ (this.objId >>> 32));
200  try {
201  hash = 41 * hash + this.getChildrenCount();
202  } catch (TskCoreException ex) {
203  Logger.getLogger(AbstractContent.class.getName()).log(Level.SEVERE, null, ex);
204  }
205  return hash;
206  }
207 
208  @Override
209  public BlackboardArtifact newArtifact(int artifactTypeID) throws TskCoreException {
210  // don't let them make more than 1 GEN_INFO
211  if (artifactTypeID == ARTIFACT_TYPE.TSK_GEN_INFO.getTypeID()) {
212  return getGenInfoArtifact(true);
213  }
214  return db.newBlackboardArtifact(artifactTypeID, objId);
215  }
216 
217  @Override
219  return newArtifact(type.getTypeID());
220  }
221 
222  @Override
223  public ArrayList<BlackboardArtifact> getArtifacts(String artifactTypeName) throws TskCoreException {
224  return getArtifacts(db.getArtifactType(artifactTypeName).getTypeID());
225  }
226 
227  @Override
228  public ArrayList<BlackboardArtifact> getArtifacts(int artifactTypeID) throws TskCoreException {
229  if (artifactTypeID == ARTIFACT_TYPE.TSK_GEN_INFO.getTypeID()) {
230  if (genInfoArtifact == null) // don't make one if it doesn't already exist
231  {
232  getGenInfoArtifact(false);
233  }
234 
235  ArrayList<BlackboardArtifact> list = new ArrayList<BlackboardArtifact>();
236  // genInfoArtifact coudl still be null if there isn't an artifact
237  if (genInfoArtifact != null) {
238  list.add(genInfoArtifact);
239  }
240  return list;
241  }
242  return db.getBlackboardArtifacts(artifactTypeID, objId);
243  }
244 
245  @Override
246  public ArrayList<BlackboardArtifact> getArtifacts(BlackboardArtifact.ARTIFACT_TYPE type) throws TskCoreException {
247  return getArtifacts(type.getTypeID());
248  }
249 
250  @Override
252  return getGenInfoArtifact(true);
253  }
254 
255  @Override
257  if (genInfoArtifact != null) {
258  return genInfoArtifact;
259  }
260 
261  // go to db directly to avoid infinite loop
262  ArrayList<BlackboardArtifact> arts = db.getBlackboardArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_GEN_INFO, objId);
263  BlackboardArtifact retArt;
264  if (arts.isEmpty()) {
265  if (create) {
267  } else {
268  return null;
269  }
270  } else {
271  retArt = arts.get(0);
272  }
273  genInfoArtifact = retArt;
274  return retArt;
275  }
276 
277  @Override
278  public ArrayList<BlackboardAttribute> getGenInfoAttributes(ATTRIBUTE_TYPE attr_type) throws TskCoreException {
279  ArrayList<BlackboardAttribute> returnList = new ArrayList<BlackboardAttribute>();
280 
281  if (genInfoArtifact == null) {
282  getGenInfoArtifact(false);
283  if (genInfoArtifact == null) {
284  return returnList;
285  }
286  }
287 
288  for (BlackboardAttribute attribute : genInfoArtifact.getAttributes()) {
289  if (attribute.getAttributeType().getTypeID() == attr_type.getTypeID()) {
290  returnList.add(attribute);
291  }
292  }
293 
294  return returnList;
295  }
296 
297  @Override
298  public ArrayList<BlackboardArtifact> getAllArtifacts() throws TskCoreException {
299  return db.getMatchingArtifacts("WHERE obj_id = " + objId); //NON-NLS
300  }
301 
302  @Override
303  public long getArtifactsCount(String artifactTypeName) throws TskCoreException {
304  return db.getBlackboardArtifactsCount(artifactTypeName, objId);
305  }
306 
307  @Override
308  public long getArtifactsCount(int artifactTypeID) throws TskCoreException {
309  return db.getBlackboardArtifactsCount(artifactTypeID, objId);
310  }
311 
312  @Override
314  return db.getBlackboardArtifactsCount(type, objId);
315  }
316 
317  @Override
318  public long getAllArtifactsCount() throws TskCoreException {
319  return db.getBlackboardArtifactsCount(objId);
320  }
321 
322  @Override
323  public Set<String> getHashSetNames() throws TskCoreException {
324  Set<String> hashNames = new HashSet<String>();
325  ArrayList<BlackboardArtifact> artifacts = getArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_HASHSET_HIT);
326 
327  for (BlackboardArtifact a : artifacts) {
329  if (attribute != null) {
330  hashNames.add(attribute.getValueString());
331  }
332  }
333  return Collections.unmodifiableSet(hashNames);
334  }
335 
336  @Override
337  public String toString() {
338  return toString(true);
339  }
340 
341  public String toString(boolean preserveState) {
342  if (preserveState) {
343  return "AbstractContent [\t" + "objId " + String.format("%010d", objId) + "\t" //NON-NLS
344  + "name " + name + "\t" + "parentId " + parentId + "\t" //NON-NLS
345  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
346  + "\t" + "hasChildren " + hasChildren //NON-NLS
347  + "\t" + "childrenCount " + childrenCount //NON-NLS
348  + "uniquePath " + uniquePath + "]\t"; //NON-NLS
349  } else {
350  try {
351  if (getParent() != null) {
352  return "AbstractContent [\t" + "objId " + String.format("%010d", objId) //NON-NLS
353  + "\t" + "name " + name //NON-NLS
354  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
355  + "\t" + "hasChildren " + hasChildren //NON-NLS
356  + "\t" + "childrenCount " + childrenCount //NON-NLS
357  + "\t" + "getUniquePath " + getUniquePath() //NON-NLS
358  + "\t" + "getParent " + getParent().getId() + "]\t"; //NON-NLS
359  } else {
360  return "AbstractContent [\t" + "objId " //NON-NLS
361  + String.format("%010d", objId) + "\t" + "name " + name //NON-NLS
362  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
363  + "\t" + "hasChildren " + hasChildren //NON-NLS
364  + "\t" + "childrenCount " + childrenCount //NON-NLS
365  + "\t" + "uniquePath " + getUniquePath() //NON-NLS
366  + "\t" + "parentId " + parentId + "]\t"; //NON-NLS
367  }
368  } catch (TskCoreException ex) {
369  Logger.getLogger(AbstractContent.class.getName()).log(Level.SEVERE, "Could not find Parent", ex); //NON-NLS
370  return "AbstractContent [\t" + "objId " + String.format("%010d", objId) + "\t" //NON-NLS
371  + "name " + name + "\t" + "parentId " + parentId + "\t" //NON-NLS
372  + "\t" + "checkedHasChildren " + checkedHasChildren //NON-NLS
373  + "\t" + "hasChildren " + hasChildren //NON-NLS
374  + "\t" + "childrenCount " + childrenCount //NON-NLS
375  + "uniquePath " + uniquePath + "]\t"; //NON-NLS
376  }
377  }
378  }
379 }
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)
BlackboardArtifact.Type getArtifactType(String artTypeName)
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.