KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Search > RMI > RMISearchConfig


1 /*
2  * SearchConfig.java
3  *
4  * Created on 29. duben 2004, 10:42
5  */

6
7 package SOFA.SOFAnet.Search.RMI;
8
9 import SOFA.SOFAnet.Repository.XMLStorageItem;
10 import SOFA.SOFAnet.Repository.Repository;
11 import SOFA.SOFAnet.Repository.NodeNameFilter;
12 import SOFA.SOFAnet.Repository.ShareGroups;
13 import SOFA.SOFAnet.Repository.NodeInfo;
14 import SOFA.SOFAnet.Core.Reporter;
15 import SOFA.Util.XML;
16 import java.io.*;
17 import java.util.*;
18 import org.w3c.dom.*;
19 import org.xml.sax.SAXException JavaDoc;
20
21 /**
22  * Loader of configuration of RMI implementation of search subsystem.
23  *
24  * @author Ladislav Sobr
25  */

26 public class RMISearchConfig
27 {
28   private File cfgFile;
29   private List rmiSearchConnections;
30   private RMISearchConfiguration configuration;
31   
32   /** Creates a new instance of SearchConfig */
33   public RMISearchConfig(Repository rep, List rmiSearchConnections)
34   {
35     cfgFile = new File(rep.getInfoBaseDir(), "search_configuration.xml");
36     this.rmiSearchConnections = rmiSearchConnections;
37     configuration = new RMISearchConfiguration();
38   }
39
40   /**
41    * Loads configuration from the XML file.
42    * <p>
43    * Search configuration XML format:
44    * <p>
45    * <pre>
46    * &lt;search_configuration&gt;
47    * ?&lt;node_filter&gt;
48    * NodeNameFilter XML format
49    * &lt;/node_filter&gt;
50    * &lt;connections&gt;
51    * *&lt;connection node="string"&gt;
52    * ?&lt;share_groups&gt;
53    * ShareGroups XML format
54    * &lt;/share_groups&gt;
55    * &lt;/connection&gt;
56    * &lt;/connections&gt;
57    * &lt;/search_configuration&gt;
58    * </pre>
59    *
60    * @param start true when loading the configuration first time
61    */

62   public void loadFromStorage(boolean start)
63   {
64     configuration.reset();
65     
66     try
67     {
68       Document doc = XML.parse(cfgFile);
69       Element root = doc.getDocumentElement();
70       
71       NodeList nl = root.getChildNodes();
72       for (int i = 0; i < nl.getLength(); i++)
73       {
74         Node node = nl.item(i);
75         if (node.getNodeType() == Node.ELEMENT_NODE)
76         {
77           Element el = (Element)node;
78           String JavaDoc nodeName = node.getNodeName();
79
80           if (nodeName.compareTo("node_filter") == 0)
81           {
82             NodeNameFilter nodeFilter = new NodeNameFilter();
83             nodeFilter.loadFromXML(el);
84             configuration.setNodeFilter(nodeFilter);
85           }
86           else
87           if (nodeName.compareTo("connections") == 0)
88           {
89             NodeList nl2 = el.getChildNodes();
90             for (int i2 = 0; i2 < nl2.getLength(); i2++)
91             {
92               Node node2 = nl2.item(i2);
93               if (node2.getNodeType() == Node.ELEMENT_NODE)
94               {
95                 Element el2 = (Element)node2;
96                 String JavaDoc nodeName2 = node2.getNodeName();
97
98                 if (nodeName2.compareTo("connection") == 0)
99                 {
100                   ShareGroups shareGroups = null;
101                   String JavaDoc searchNodeName = el2.getAttribute("node");
102
103                   if (searchNodeName.length() == 0)
104                   {
105                     throw new SAXException JavaDoc("Missing node name (attribute 'node' of tag 'connection')");
106                   }
107                   
108                   NodeInfo nodeInfo = new NodeInfo();
109                   try
110                   {
111                     nodeInfo.setNodeName(searchNodeName);
112                   }
113                   catch (NodeInfo.InvalidNodeNameException e)
114                   {
115                     Reporter.error("Invalid name of RMI search node: " + nodeName, e);
116                     nodeInfo = null;
117                   }
118                   
119                   NodeList nl3 = el2.getChildNodes();
120                   for (int i3 = 0; i3 < nl3.getLength(); i3++)
121                   {
122                     Node node3 = nl3.item(i3);
123                     if (node3.getNodeType() == Node.ELEMENT_NODE)
124                     {
125                       Element el3 = (Element)node3;
126                       String JavaDoc nodeName3 = node3.getNodeName();
127
128                       if (nodeName3.compareTo("share_groups") == 0)
129                       {
130                         shareGroups = new ShareGroups();
131                         shareGroups.loadFromXML(el3);
132                       }
133                     }
134                   }
135                   
136                   configuration.getConnections().add(new RMISearchConfiguration.Connection(searchNodeName, shareGroups != null ? (ShareGroups)shareGroups.clone() : null));
137                   if (nodeInfo != null)
138                   {
139                     boolean connectionFound = false;
140                     if (!start)
141                     {
142                       //do we have this connection?
143
synchronized (rmiSearchConnections)
144                       {
145                         Iterator it = rmiSearchConnections.iterator();
146                         while (it.hasNext())
147                         {
148                           RMISearchConnection connection = (RMISearchConnection)it.next();
149                           if (connection.isNode(nodeName))
150                           {
151                             connectionFound = true;
152                             break;
153                           }
154                         }
155                       }
156                     }
157                     
158                     if (!connectionFound) rmiSearchConnections.add(new RMISearchConnection(nodeInfo, shareGroups, false));
159                   }
160                 }
161               }
162             }
163           }
164         }
165       }
166     }
167     catch (IOException e)
168     {
169       Reporter.error("Error while loading (RMI) search_configuration XML format from file " + cfgFile, e);
170     }
171     catch (SAXException JavaDoc e)
172     {
173       Reporter.error("Error while parsing (RMI) search_configuration XML format from file " + cfgFile, e);
174     }
175   }
176
177   public void saveToStorage()
178   {
179     try
180     {
181       Document doc = XML.newDocument();
182       Element root = doc.createElement("search_configuration");
183       doc.appendChild(root);
184       
185       Element el;
186       NodeNameFilter nodeFilter = configuration.getNodeFilter();
187       if (nodeFilter != null)
188       {
189         el = doc.createElement("node_filter");
190         nodeFilter.saveToXML(el);
191         root.appendChild(el);
192       }
193       
194       el = doc.createElement("connections");
195       
196       List connections = configuration.getConnections();
197       synchronized (connections)
198       {
199         Iterator it = connections.iterator();
200         while (it.hasNext())
201         {
202           RMISearchConfiguration.Connection connection = (RMISearchConfiguration.Connection)it.next();
203           Element el2 = doc.createElement("connection");
204           el2.setAttribute("node", connection.getNodeName());
205           ShareGroups shareGroups = connection.getShareGroups();
206           if (shareGroups != null)
207           {
208             Element el3 = doc.createElement("share_groups");
209             shareGroups.saveToXML(el3);
210             el2.appendChild(el3);
211           }
212           el.appendChild(el2);
213         }
214       }
215       
216       root.appendChild(el);
217       
218       XML.dump(cfgFile, root);
219     }
220     catch (IOException e)
221     {
222       Reporter.error("Saving of (RMI) search_configuration XML format to file '" + cfgFile + "' failed", e);
223     }
224     
225   }
226   
227   public NodeNameFilter getNodeFilter()
228   {
229     return configuration.getNodeFilter();
230   }
231   
232   public RMISearchConfiguration getConfiguration()
233   {
234     return configuration;
235   }
236   
237   public void setConfiguration(RMISearchConfiguration configuration)
238   {
239     this.configuration = (RMISearchConfiguration)configuration.clone();
240     saveToStorage();
241   }
242   
243 }
244
Popular Tags