KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * LocalInfos.java
3  *
4  * Created on 19. bøezen 2004, 0:00
5  */

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

18 public class LocalInfos extends StorageBase
19 {
20   
21   /** Creates a new instance of LocalInfos */
22   public LocalInfos(File baseDir)
23   {
24     super(baseDir, "local", true);
25     init();
26     fixCrashInconsistency();
27   }
28   
29   /**
30    * The function corrects local info that can be corrupted by crash of the system.
31    * Because one licence copy is taken from "local licence" when the bundle is loaded in the memory,
32    * this copy must be returned to local licence, if the "in memory" flag is found during inicialization
33    * of this storage.
34    */

35   protected void fixCrashInconsistency()
36   {
37     synchronized (map)
38     {
39       Iterator it = map.values().iterator();
40       while (it.hasNext())
41       {
42         LocalInfo localInfo = (LocalInfo)it.next();
43         Licence licence = localInfo.getLicence();
44         if (localInfo.isInMemory() && licence.withCopies())
45         {
46           localInfo.setInMemoryFlag(false);
47           licence.increaseNumberOfCopies(1);
48           localInfo.saveToStorage();
49         }
50       }
51     }
52   }
53   
54   protected StorageItem newItem(String JavaDoc name, File file)
55   {
56     String JavaDoc bundleName = BundleInfo.fileNameToBundleName(name);
57     if (bundleName == null) return null;
58     return new LocalInfo(bundleName, file);
59   }
60   
61   /**
62    * Returns local info of specified bundle.
63    * The function assures that the returned LocalInfo is loaded in the memory.
64    *
65    * @param bundleName name of bundle we want the local info for
66    * @return requested local info; null if not present
67    */

68   public LocalInfo getLocalInfo(String JavaDoc bundleName)
69   {
70     return (LocalInfo)map.get(bundleName);
71   }
72
73   /**
74    * Deletes local info .
75    *
76    * @param bundleName name of bundle of which we want to delete local info
77    * @return true, if local info was found (and deleted)
78    */

79   public boolean deleteLocalInfo(String JavaDoc bundleName)
80   {
81     synchronized (map)
82     {
83       LocalInfo localInfo = (LocalInfo)map.remove(bundleName);
84       if (localInfo != null)
85       {
86         localInfo.deleteFromStorage();
87         return true;
88       }
89       else return false;
90     }
91   }
92
93   /**
94    * Adds local info for specified bundle.
95    * <p>
96    * For description of all parameters see LocalInfo class
97    *
98    * @return local info, if local info was added (bundleName was correct and there was not already the one with the same name), null otherwise
99    */

100   public LocalInfo addLocalInfo(String JavaDoc bundleName,
101                                 Licence licence,
102                                 int state,
103                                 ShareGroups smShareGroups,
104                                 NodeNameFilter smNodeFilter,
105                                 boolean smEquality,
106                                 String JavaDoc scManager)
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       LocalInfo localInfo = new LocalInfo(bundleName, new File(baseDir, fileName + ".local"));
115       localInfo.setLicence(licence);
116       localInfo.setState(state);
117       localInfo.setSMShareGroups(smShareGroups);
118       localInfo.setSMNodeFilter(smNodeFilter);
119       localInfo.setSMEquality(smEquality);
120       if (localInfo.isShareManager()) localInfo.setSMClients(new NodeNameList());
121       localInfo.setSCManager(scManager);
122       localInfo.saveToStorage();
123       map.put(bundleName, localInfo);
124       return localInfo;
125     }
126   }
127   
128 }
129
Popular Tags