KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Repository > StorageBase


1 /*
2  * StorageBase.java
3  *
4  * Created on 22. listopad 2003, 19:38
5  */

6
7 package SOFA.SOFAnet.Repository;
8
9 import java.lang.*;
10 import java.io.*;
11 import java.util.*;
12 import SOFA.SOFAnet.Core.Reporter;
13
14 /**
15  * Base class for storages implemented as directories with files.
16  *
17  * @author Ladislav Sobr
18  */

19 abstract public class StorageBase
20 {
21   public Map map;
22   protected File baseDir;
23   protected String JavaDoc extension;
24   protected boolean loadOnInit;
25   
26   
27   /**
28    * Creates a new instance of StorageBase
29    *
30    * @param baseDir directory with storage
31    * @param extension extension of files that belongs to storage
32    * @param loadOnInit if true, the content of all items in repository is loaded on init (otherwise items are created in memory but not fully loaded)
33    */

34   public StorageBase(File baseDir, String JavaDoc extension, boolean loadOnInit)
35   {
36     this.baseDir = baseDir;
37     this.extension = extension;
38     this.loadOnInit = loadOnInit;
39     map = Collections.synchronizedMap(new HashMap());
40   }
41   
42   abstract protected StorageItem newItem(String JavaDoc name, File file);
43   
44   /**
45    * Initialization of storage.
46    */

47   public void init()
48   {
49     File [] files = baseDir.listFiles();
50     if (files == null)
51     {
52       Reporter.error("Cannot open directory '" + baseDir + "'");
53       return;
54     }
55     
56     for (int i = 0; i < files.length; i++)
57     {
58       if (!files[i].isDirectory())
59       {
60         String JavaDoc name = files[i].getName();
61         if (name.endsWith("." + extension))
62         {
63           name = name.substring(0, name.length() - extension.length() - 1);
64           StorageItem item = newItem(name, files[i].getAbsoluteFile());
65           if (item != null)
66           {
67             boolean toBeDeleted = false;
68             if (loadOnInit)
69             {
70               item.loadFromStorage();
71               toBeDeleted = item.toBeDeleted();
72             }
73             if (toBeDeleted) item.deleteFromStorage();
74             else map.put(item.getName(), item);
75           }
76         }
77       }
78     }
79   }
80
81 }
82
Popular Tags