KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * BundleOffers.java
3  *
4  * Created on 19. bøezen 2004, 20:53
5  */

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

19 public class BundleOffers extends StorageBase
20 {
21   private CompBundleMap compBundleMap;
22   
23   /** Creates a new instance of BundleOffers */
24   public BundleOffers(File baseDir)
25   {
26     super(baseDir, "offer", 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 BundleOffer(bundleName, file);
36   }
37   
38   /**
39    * Created component-bundle map that is used to quick look up of the bundle offer 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       BundleOffer bundleOffer = (BundleOffer)it.next();
48       compBundleMap.addBundleOffer(bundleOffer);
49       bundleOffer.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 specified bundle offer
65    * The function assures that the returned BundleOffer is loaded in the memory.
66    *
67    * @param bundleName name of bundle offer
68    * @return BundleOffer object for the specified bundle; null if not present
69    */

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

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

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