KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * BundleContents.java
3  *
4  * Created on 23. listopad 2003, 16:16
5  */

6
7 package SOFA.SOFAnet.Repository;
8
9 import java.io.*;
10 import java.util.*;
11 import SOFA.SOFAnode.TR.ComponentInfo;
12
13 /**
14  * Persistent storage of bundle contents (lists of components).
15  * Implemented as directory of XML files.
16  *
17  * @author Ladislav Sobr
18  */

19 public class BundleContents extends StorageBase
20 {
21   private CompBundleMap compBundleMap;
22   
23   /** Creates a new instance of BundleContents */
24   public BundleContents(File baseDir)
25   {
26     super(baseDir, "content", true); //in memory only at the startup, than removed!
27
init();
28     createCompBundleMap();
29   }
30   
31   protected StorageItem newItem(String JavaDoc name, File file)
32   {
33     String JavaDoc bundleName = BundleInfo.fileNameToBundleName(name);
34     if (bundleName == null) return null;
35     else return new BundleContent(bundleName, file);
36   }
37   
38   /**
39    * Created component-bundle map that is used to quick look up of the bundle that contains concrete component
40    */

41   public void createCompBundleMap()
42   {
43     compBundleMap = new CompBundleMap();
44     Iterator it = map.values().iterator();
45     while (it.hasNext())
46     {
47       BundleContent bundleContent = (BundleContent)it.next();
48       compBundleMap.addBundleContent(bundleContent);
49       bundleContent.setLoaded(false); //remove from memory
50
}
51   }
52   
53   /**
54    * Returns component-bundle map
55    *
56    * @return component-bundle map
57    */

58   public CompBundleMap getCompBundleMap()
59   {
60     return compBundleMap;
61   }
62   
63   /**
64    * Returns content of the specified bundle
65    * The function assures that the returned BundleContent is loaded in the memory.
66    *
67    * @param bundleName name of bundle
68    * @return BundleContent object for the specified bundle; null if not present
69    */

70   public BundleContent getBundleContent(String JavaDoc bundleName)
71   {
72     BundleContent bundleContent = (BundleContent)map.get(bundleName);
73     if (bundleContent != null && !bundleContent.isLoaded()) bundleContent.loadFromStorage();
74     return bundleContent;
75   }
76   
77   /**
78    * Deletes bundle content of the specified bundle.
79    *
80    * @param bundleName name of bundle
81    * @return true, if bundle content was found (and deleted)
82    */

83   public boolean deleteBundleContent(String JavaDoc bundleName)
84   {
85     synchronized (map)
86     {
87       BundleContent bundleContent = (BundleContent)map.remove(bundleName);
88       if (bundleContent != null)
89       {
90         bundleContent.deleteFromStorage();
91         compBundleMap.deleteBundle(bundleName);
92         return true;
93       }
94       else return false;
95     }
96   }
97   
98   /**
99    * Adds bundle content for the specified bundle.
100    *
101    * @param bundleName name of bundle
102    * @param components array of the components that the bundle consist of
103    * @return bundle content, if bundle content was added (bundleName was correct and there was not already the one with the same name), null otherwise
104    */

105   public BundleContent addBundleContent(String JavaDoc bundleName, ComponentInfo[] components)
106   {
107     synchronized (map)
108     {
109       if (map.get(bundleName) != null) return null;
110
111       String JavaDoc fileName = BundleInfo.bundleNameToFileName(bundleName);
112       if (fileName == null) return null;
113       BundleContent bundleContent = new BundleContent(bundleName, new File(baseDir, fileName + ".content"));
114       List compList = bundleContent.getComponents();
115       for (int i = 0; i < components.length; i++)
116       {
117         compList.add(components[i]);
118       }
119
120       compBundleMap.addBundleContent(bundleContent);
121       bundleContent.saveToStorage();
122       map.put(bundleName, bundleContent);
123       return bundleContent;
124     }
125   }
126 }
127
Popular Tags