1 3 package org.jgroups.conf; 4 5 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.jgroups.util.Util; 14 import org.w3c.dom.Document ; 15 import org.w3c.dom.Node ; 16 import org.w3c.dom.NodeList ; 17 18 import javax.xml.parsers.DocumentBuilder ; 19 import javax.xml.parsers.DocumentBuilderFactory ; 20 import java.io.FileInputStream ; 21 import java.io.FileNotFoundException ; 22 import java.io.InputStream ; 23 24 public class MagicNumberReader { 25 private static final boolean xml_debug=false; 26 public static final String MAGIC_NUMBER_FILE="jg-magic-map.xml"; 27 28 public String mMagicNumberFile=MAGIC_NUMBER_FILE; 29 30 protected static final Log log=LogFactory.getLog(MagicNumberReader.class); 31 32 public void setFilename(String file) { 33 mMagicNumberFile=file; 34 } 35 36 42 public ClassMap[] readMagicNumberMapping() { 43 try { 44 InputStream stream=Thread.currentThread().getContextClassLoader().getResourceAsStream(mMagicNumberFile); 46 if(stream == null) { 48 try { 49 if(log.isTraceEnabled()) 50 log.trace("Could not read " + mMagicNumberFile + " as Resource from the CLASSPATH, will try to read it from file."); 51 stream=new FileInputStream (mMagicNumberFile); 52 if(stream != null && log.isTraceEnabled()) 53 log.trace("Magic number File found at '" + mMagicNumberFile + '\''); 54 } 55 catch(FileNotFoundException fnfe) { 56 if(log.isWarnEnabled()) 57 log.warn("Failed reading - '" + mMagicNumberFile + "' is not found, got error '" + 58 fnfe.getLocalizedMessage() + "'. Please make sure it is in the CLASSPATH or in the " + 59 "specified location. Will continue, but marshalling will be slower"); 60 } 61 } 62 63 if(stream == null) { 64 return new ClassMap[0]; 65 } 66 return parse(stream); 67 } 68 catch(Exception x) { 69 if(xml_debug) x.printStackTrace(); 70 String error=Util.getStackTrace(x); 71 if(log.isErrorEnabled()) log.error(error); 72 } 73 return new ClassMap[0]; 74 } 75 76 protected static ClassMap[] parse(InputStream stream) throws Exception { 77 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 78 factory.setValidating(false); DocumentBuilder builder=factory.newDocumentBuilder(); 80 builder.setEntityResolver(new ClassPathEntityResolver()); 81 Document document=builder.parse(stream); 82 NodeList class_list=document.getElementsByTagName("class"); 83 java.util.Vector v=new java.util.Vector (); 84 for(int i=0; i < class_list.getLength(); i++) { 85 if(class_list.item(i).getNodeType() == Node.ELEMENT_NODE) { 86 v.addElement(parseClassData(class_list.item(i))); 87 } 88 } 89 ClassMap[] data=new ClassMap[v.size()]; 90 v.copyInto(data); 91 return data; 92 } 94 protected static ClassMap parseClassData(Node protocol) 95 throws java.io.IOException { 96 try { 97 protocol.normalize(); 98 int pos=0; 99 NodeList children=protocol.getChildNodes(); 100 107 108 110 111 String clazzname=null; 112 String desc=null; 113 String preload=null; 114 String magicnumber=null; 115 116 for(int i=0; i < children.getLength(); i++) { 117 if(children.item(i).getNodeType() == Node.ELEMENT_NODE) { 118 pos++; 119 switch(pos) { 120 case 1: 121 desc=children.item(i).getFirstChild().getNodeValue(); 122 break; 123 case 2: 124 clazzname=children.item(i).getFirstChild().getNodeValue(); 125 break; 126 case 3: 127 preload=children.item(i).getFirstChild().getNodeValue(); 128 break; 129 case 4: 130 magicnumber=children.item(i).getFirstChild().getNodeValue(); 131 break; 132 } } } 136 return new ClassMap(clazzname, desc, Boolean.valueOf(preload).booleanValue(), Integer.valueOf(magicnumber).intValue()); 137 } 138 catch(Exception x) { 139 if(x instanceof java.io.IOException ) 140 throw (java.io.IOException )x; 141 else { 142 143 if(xml_debug) x.printStackTrace(); 144 String error=Util.getStackTrace(x); 145 if(log.isErrorEnabled()) log.error(error); 146 throw new java.io.IOException (x.getMessage()); 147 } } } 150 151 152 } 153 | Popular Tags |