1 package org.astrogrid.registry.server;
2
3 import org.w3c.dom.Document;
4 import org.w3c.dom.NodeList;
5 import org.w3c.dom.Node;
6 import org.w3c.dom.Element;
7 import java.util.HashMap;
8 import java.util.LinkedList;
9 import java.util.LinkedHashMap;
10 import org.astrogrid.registry.server.query.RegistryQueryService;
11 import java.text.DateFormat;
12 import java.util.Calendar;
13
14 import org.astrogrid.config.Config;
15 import org.astrogrid.util.DomHelper;
16 import java.io.IOException;
17 import java.net.MalformedURLException;
18 import javax.xml.parsers.ParserConfigurationException;
19 import org.xml.sax.SAXException;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.astrogrid.xmldb.eXist.server.QueryDBService;
24
25 /***
26 * RegistryServerHelper Helper class is one to have it's name changed later.
27 * Currently it helps or does some utility work like keep hold of status
28 * messages, and the authority id's managed by this registry and other
29 * registries.
30 *
31 * @author Kevin Benson
32 */
33 public class RegistryServerHelper {
34
35
36 /***
37 * contains status information about the registry.
38 */
39 private static String statusMessage = "";
40
41 private static LinkedList ll = null;
42
43 private static HashMap manageAuthorities = null;
44
45 private static HashMap otherManagedAuths = null;
46
47 /***
48 * conf - Config variable to access the configuration for the server normally
49 * jndi to a config file.
50 * @see org.astrogrid.config.Config
51 */
52 public static Config conf = null;
53
54 /***
55 * Logging variable for writing information to the logs
56 */
57 private static final Log log = LogFactory.getLog(RegistryServerHelper.class);
58
59 private static String versionNumber = null;
60
61 private static String defaultRoot = null;
62
63 /***
64 * Static to be used on the initiatian of this class for the config
65 */
66 static {
67 if(conf == null) {
68 conf = org.astrogrid.config.SimpleConfig.getSingleton();
69 versionNumber = conf.getString("org.astrogrid.registry.version");
70 defaultRoot = conf.getString("registry.rootNode.default",null);
71 }
72 }
73
74 public static String getDefaultVersionNumber() {
75 return versionNumber;
76 }
77
78 public static String getRootNodeName(String versionNumber) {
79 return conf.getString("registry.rootNode." + versionNumber,defaultRoot);
80 }
81
82 public static String getRootNodeLocalName(String versionNumber) {
83 String val = getRootNodeName(versionNumber);
84 return val.substring((val.indexOf(":")+1));
85 }
86
87 /***
88 * Gets the text out of the First authority id element.
89 * Need to use RegistryServerHelper class to get the NodeList. It has
90 * already these common methods in it. Once it gets the NodeList
91 * then return the text in the first child.
92 * @param doc xml element normally the full DOM root element.
93 * @return AuthorityID text
94 */
95 public static String getAuthorityID(Element doc) {
96 NodeList nl = doc.getElementsByTagNameNS("*","Identifier" );
97 String val = null;
98 if(nl.getLength() == 0) {
99 nl = doc.getElementsByTagNameNS("*","identifier" );
100 if(nl.getLength() == 0)
101 return null;
102 }
103
104 NodeList authNodeList = ((Element)nl.item(0)).getElementsByTagNameNS("*","AuthorityID");
105
106 if(authNodeList.getLength() == 0) {
107 val = nl.item(0).getFirstChild().getNodeValue();
108 int index = val.indexOf("/",7);
109 if( index != -1 && index > 6)
110 return val.substring(6,index);
111 else
112 return val.substring(6);
113 }
114 return authNodeList.item(0).getFirstChild().getNodeValue();
115 }
116
117 /***
118 * Gets the text out of the First ResourceKey element.
119 * Need to use RegistryServerHelper class to get the NodeList. It has
120 * already these common methods in it. Once it gets the NodeList
121 * then return the text in the first child.
122 * @param doc xml element normally the full DOM root element.
123 * @return ResourceKey text
124 */
125 public static String getResourceKey(Element doc) {
126 NodeList nl = doc.getElementsByTagNameNS("*","Identifier" );
127 if(nl.getLength() == 0) {
128 nl = doc.getElementsByTagNameNS("*","identifier" );
129 if(nl.getLength() == 0)
130 return null;
131 }
132 NodeList resNodeList = ((Element)nl.item(0)).getElementsByTagNameNS("*","ResourceKey");
133 String val = null;
134 if(resNodeList.getLength() == 0) {
135 val = nl.item(0).getFirstChild().getNodeValue();
136 int index = val.indexOf("/",7);
137 if(index != -1 && index > 6 && val.length() > (index+1))
138 return val.substring(index+1);
139 }else {
140 if(resNodeList.item(0).hasChildNodes())
141 return resNodeList.item(0).getFirstChild().getNodeValue();
142 }
143
144 return "";
145 }
146
147
148 public static String getIdentifier(Node nd) throws IOException {
149 String ident = getAuthorityID((Element)nd);
150 String resKey = getResourceKey((Element)nd);
151 if(resKey != null && resKey.trim().length() > 0) ident += "/" + resKey;
152 return ident;
153 }
154
155 /***
156 * @param versionNumber
157 * @return
158 */
159 public static String getXQLDeclarations(String versionNumber) {
160 versionNumber = versionNumber.replace('.','_');
161 String declarations = conf.getString("declare.namespace." + versionNumber,"");
162 return declarations;
163 }
164
165 public static String getRegistryVersionFromNode(Node nd) {
166 if(nd == null || Node.ELEMENT_NODE != nd.getNodeType()) {
167 log.info("not a ELEMENT NODE TIME TO JUST DEFAULT IT");
168 return conf.getString("org.astrogrid.registry.version",null);
169 }
170
171 String version = nd.getNamespaceURI();
172 if(version != null && version.trim().length() > 0 &&
173 version.startsWith("http://www.ivoa.net/xml/VOResource")) {
174 return version.substring(version.lastIndexOf("v")+1);
175 }
176
177 version = DomHelper.getNodeAttrValue((Element)nd,"vr","xmlns");
178 if(version != null && version.trim().length() > 0) {
179 return version.substring(version.lastIndexOf("v")+1);
180 }
181
182 version = DomHelper.getNodeAttrValue((Element)nd,"xmlns");
183 if(version != null && version.trim().length() > 0 &&
184 version.startsWith("http://www.ivoa.net/xml/VOResource")) {
185 return version.substring(version.lastIndexOf("v")+1);
186 }
187
188 Node parentNode = nd.getParentNode();
189 if(parentNode != null) {
190 return getRegistryVersionFromNode(parentNode);
191 }
192
193
194 return conf.getString("org.astrogrid.registry.version",null);
195 }
196
197 /***
198 *
199 * @throws IOException
200 */
201 public static void initStatusMessage() throws IOException {
202 log.debug("start initStatusMessage");
203 statusMessage = " ";
204
205 RegistryQueryService rs = new RegistryQueryService();
206 Document regEntry = null;
207 try {
208 regEntry = rs.loadRegistry(null);
209 }catch(Exception e) {
210 e.printStackTrace();
211 log.error(e);
212 regEntry = null;
213 }
214 if(regEntry != null) {
215
216 NodeList nl = regEntry.getElementsByTagNameNS("*","ManagedAuthority");
217
218 if(nl.getLength() > 0) {
219 statusMessage += "Authorities owned by this Registry: |";
220 for(int i=0;i < nl.getLength();i++) {
221 statusMessage += nl.item(i).getFirstChild().getNodeValue() +
222 "|";
223 }
224 }
225 if(nl.getLength() > 0) {
226 statusMessage += "This Registries access url is " +
227 DomHelper.getNodeTextValue(regEntry,"AccessURL","vr") + "|";
228 }
229 }
230 log.debug("end initStatusMessage");
231 }
232
233 public static void addStatusMessage(String message) {
234 log.debug("start addStatusMessage");
235 if(ll == null || ll.size() <= 0) {
236 ll = new LinkedList();
237 }
238 Calendar rightNow = Calendar.getInstance();
239 ll.add(0,"Time Entry at: " + DateFormat.getDateInstance().format(
240 rightNow.getTime()) + " " + message + "|");
241 while(ll.size() >= 51) {
242 ll.removeLast();
243 }
244 log.debug("end addStatusMessage");
245 }
246
247 public static String getStatusMessage() throws IOException {
248 log.debug("start getStatusMessage");
249 if(ll == null || ll.size() <= 0) {
250 ll = new LinkedList();
251 }
252 initStatusMessage();
253 for(int i = 0;i < ll.size();i++) {
254 statusMessage += ll.get(i);
255 }
256 log.debug("end getStatusMessage");
257 return statusMessage;
258 }
259
260 public static HashMap getOtherManagedAuthorities(String collectionName,String regVersion) throws SAXException, MalformedURLException, ParserConfigurationException, IOException {
261 log.debug("start getOtherManagedAuthorities");
262
263 processManagedAuthorities(collectionName, regVersion);
264
265 log.debug("end getOtherManagedAuthorities");
266 return otherManagedAuths;
267 }
268
269 public static HashMap getManagedAuthorities(String collectionName,String regVersion) throws SAXException, MalformedURLException, ParserConfigurationException, IOException {
270 log.debug("start getManagedAuthorities");
271
272 processManagedAuthorities(collectionName, regVersion);
273
274 log.debug("end getManagedAuthorities");
275 return manageAuthorities;
276 }
277
278 public static void processManagedAuthorities(String collectionName,String regVersion) throws SAXException, MalformedURLException, ParserConfigurationException, IOException {
279
280 if(otherManagedAuths == null)
281 otherManagedAuths = new HashMap();
282 if(manageAuthorities == null)
283 manageAuthorities = new HashMap();
284
285 otherManagedAuths.clear();
286 manageAuthorities.clear();
287 QueryDBService qdb = new QueryDBService();
288 String xqlQuery = QueryHelper.queryForRegistries(regVersion);
289 Document registries = qdb.runQuery(collectionName,xqlQuery);
290
291 NodeList resources = registries.getElementsByTagNameNS("*","Resource");
292 log.info("Number of Resources found loading up registries = " + resources.getLength());
293 HashMap tempHash = new HashMap();
294 boolean sameRegistry = false;
295 String regAuthID = conf.getString("org.astrogrid.registry.authorityid");
296 String val = null;
297
298 for(int j = 0;j < resources.getLength();j++) {
299 NodeList mgList = ((Element)resources.item(j)).getElementsByTagNameNS("*","ManagedAuthority");
300 if(mgList.getLength() == 0) {
301 mgList = ((Element)resources.item(j)).getElementsByTagNameNS("*","managedAuthority");
302 }
303 log.info("mglist size = " + mgList.getLength());
304 for(int i = 0;i < mgList.getLength();i++) {
305 val = mgList.item(i).getFirstChild().getNodeValue();
306 tempHash.put(val,null);
307 if(val != null && regAuthID.equals(val.trim()))
308 sameRegistry = true;
309 }
310 if(sameRegistry) {
311 manageAuthorities.putAll(tempHash);
312 sameRegistry = false;
313 }else {
314 otherManagedAuths.putAll(tempHash);
315 }
316 tempHash.clear();
317 }
318 }
319
320 }