1 3 package org.jgroups.conf; 4 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.jgroups.util.Util; 9 import org.jgroups.ChannelException; 10 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 import java.util.TreeMap ; 15 import java.io.ObjectStreamClass ; 16 17 31 public class ClassConfigurator { 32 static ClassConfigurator instance=null; 33 34 private final Map classMap=new HashMap (); private final Map magicMap=new TreeMap (); 38 39 private final Map streamMapId=new TreeMap (); 40 41 42 private final Map streamMapClass=new HashMap (); 43 44 protected final Log log=LogFactory.getLog(getClass()); 45 46 47 private ClassConfigurator() { 48 } 49 50 public void init() throws ChannelException { 51 try { 53 Thread.currentThread().getContextClassLoader().loadClass("javax.xml.parsers.DocumentBuilderFactory"); 56 57 MagicNumberReader reader=new MagicNumberReader(); 58 59 try { 61 String mnfile = System.getProperty("org.jgroups.conf.magicNumberFile"); 62 if(mnfile != null) { 63 if(log.isDebugEnabled()) log.debug("Using " + mnfile + " as magic number file"); 64 reader.setFilename(mnfile); 65 } 66 } 67 catch (SecurityException ex){ 68 } 69 70 ObjectStreamClass objStreamClass; 71 ClassMap[] mapping=reader.readMagicNumberMapping(); 72 if(mapping != null) { 73 for(int i=0; i < mapping.length; i++) { 74 Integer m=new Integer (mapping[i].getMagicNumber()); 75 try { 76 Class clazz=mapping[i].getClassForMap(); 77 objStreamClass=ObjectStreamClass.lookup(clazz); 78 if(objStreamClass == null) 79 throw new ChannelException("ObjectStreamClass for " + clazz + " not found"); 80 if(magicMap.containsKey(m)) { 81 throw new ChannelException("magic key " + m + " (" + clazz.getName() + ')' + 82 " is already in map; please make sure that " + 83 "all magic keys are unique"); 84 } 85 else { 86 magicMap.put(m, clazz); 87 classMap.put(clazz, m); 88 89 streamMapId.put(m, objStreamClass); 90 streamMapClass.put(objStreamClass, m); 91 } 92 } 93 catch(ClassNotFoundException cnf) { 94 throw new ChannelException("failed loading class: " + cnf); 95 } 96 } 97 if(log.isDebugEnabled()) log.debug("mapping is:\n" + printMagicMap()); 98 } 99 } 100 catch(ChannelException ex) { 101 throw ex; 102 } 103 catch(Throwable x) { 104 throw new ChannelException("failed reading the magic number mapping file", x); 106 } 107 } 108 109 110 public static ClassConfigurator getInstance(boolean init) throws ChannelException { 111 if(instance == null) { 112 instance=new ClassConfigurator(); 113 if(init) 114 instance.init(); 115 } 116 return instance; 117 } 118 119 120 127 public Class get(int magic) { 128 return (Class )magicMap.get(new Integer (magic)); 129 } 130 131 137 public Class get(String clazzname) { 138 try { 139 return Thread.currentThread().getContextClassLoader().loadClass(clazzname); 141 } 142 catch(Exception x) { 143 if(log.isErrorEnabled()) log.error(Util.getStackTrace(x)); 144 } 145 return null; 146 } 147 148 154 public int getMagicNumber(Class clazz) { 155 Integer i=(Integer )classMap.get(clazz); 156 if(i == null) 157 return -1; 158 else 159 return i.intValue(); 160 } 161 162 public int getMagicNumberFromObjectStreamClass(ObjectStreamClass objStream) { 163 Integer i=(Integer )streamMapClass.get(objStream); 164 if(i == null) 165 return -1; 166 else 167 return i.intValue(); 168 } 169 170 public ObjectStreamClass getObjectStreamClassFromMagicNumber(int magic_number) { 171 ObjectStreamClass retval=null; 172 retval=(ObjectStreamClass )streamMapId.get(new Integer (magic_number)); 173 return retval; 174 } 175 176 177 public String toString() { 178 return printMagicMap(); 179 } 180 181 public String printMagicMap() { 182 StringBuffer sb=new StringBuffer (); 183 Map.Entry entry; 184 185 for(Iterator it=magicMap.entrySet().iterator(); it.hasNext();) { 186 entry=(Map.Entry )it.next(); 187 sb.append(entry.getKey()).append(":\t").append(entry.getValue()).append('\n'); 188 } 189 return sb.toString(); 190 } 191 192 public String printClassMap() { 193 StringBuffer sb=new StringBuffer (); 194 Map.Entry entry; 195 196 for(Iterator it=classMap.entrySet().iterator(); it.hasNext();) { 197 entry=(Map.Entry )it.next(); 198 sb.append(entry.getKey()).append(": ").append(entry.getValue()).append('\n'); 199 } 200 return sb.toString(); 201 } 202 203 204 205 206 207 208 public static void main(String [] args) 209 throws Exception { 210 211 ClassConfigurator test=getInstance(true); 212 System.out.println('\n' + test.printMagicMap()); 213 } 214 } 215 | Popular Tags |