1 22 package org.jboss.test.jmx.interceptors; 23 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.ObjectOutputStream ; 28 import java.io.FileInputStream ; 29 import java.io.ObjectInputStream ; 30 import java.lang.reflect.Method ; 31 32 import javax.naming.Name ; 33 import javax.naming.NameNotFoundException ; 34 import javax.management.MBeanException ; 35 import javax.management.ReflectionException ; 36 37 import org.jboss.mx.interceptor.AbstractInterceptor; 38 import org.jboss.mx.interceptor.Interceptor; 39 import org.jboss.mx.server.Invocation; 40 import org.jboss.logging.Logger; 41 42 48 public final class JNDIPersistence 49 extends AbstractInterceptor 50 { 51 private static Logger log = Logger.getLogger(JNDIPersistence.class); 52 53 private File storeDirectory; 54 55 public File getStoreDirectory() 56 { 57 return storeDirectory; 58 } 59 public void setStoreDirectory(File storeDirectory) 60 { 61 log.info("setStoreDirectory: "+storeDirectory); 62 if( storeDirectory.exists() == false ) 63 storeDirectory.mkdir(); 64 this.storeDirectory = storeDirectory; 65 } 66 67 public Object invoke(Invocation invocation) throws Throwable 69 { 70 String opName = invocation.getName(); 71 log.info("invoke, opName="+opName); 72 73 if( opName == null || opName.equals("invoke") == false ) 75 { 76 Interceptor i = invocation.nextInterceptor(); 77 return i.invoke(invocation); 78 } 79 80 Object [] args = invocation.getArgs(); 81 org.jboss.invocation.Invocation invokeInfo = 82 (org.jboss.invocation.Invocation) args[0]; 83 84 Object [] iargs = invokeInfo.getArguments(); 85 for(int a = 0; a < args.length; a ++) 86 log.info(" args["+a+"]="+iargs[a]); 87 Method method = invokeInfo.getMethod(); 88 String methodName = method.getName(); 89 log.info("methodName: "+methodName); 90 Object value = null; 91 if( methodName.equals("bind") ) 92 { 93 log.info("Dispatching bind"); 94 invocation.nextInterceptor().invoke(invocation); 95 log.info("Saving bind data"); 97 Name name = (Name ) iargs[0]; 98 Object data = iargs[1]; 99 try 100 { 101 writeBinding(name, data); 102 } 103 catch(Throwable e) 104 { 105 log.error("Failed to write binding", e); 106 throw e; 107 } 108 } 109 else if( methodName.equals("lookup") ) 110 { 111 log.info("Dispatching lookup"); 112 try 113 { 114 value = invocation.nextInterceptor().invoke(invocation); 115 log.info("lookup returned: "+value); 116 } 117 catch(Throwable ex) 118 { 119 ex = getException(ex); 120 log.info("InvocationException: ", ex); 121 if( ex instanceof NameNotFoundException ) 122 { 123 log.info("NameNotFoundException in lookup, finding data"); 124 Name name = (Name ) iargs[0]; 125 try 126 { 127 value = readBinding(name); 128 if( value == null ) 129 throw ex; 130 } 131 catch(Throwable e2) 132 { 133 log.error("Failed to read binding", e2); 134 throw e2; 135 } 136 } 137 } 138 } 139 else 140 { 141 value = invocation.nextInterceptor().invoke(invocation); 142 } 143 144 return value; 145 } 146 147 private void writeBinding(Name name, Object data) 148 throws IOException 149 { 150 File dataFile = new File (storeDirectory, name.toString()); 151 FileOutputStream fos = new FileOutputStream (dataFile); 152 ObjectOutputStream oos = new ObjectOutputStream (fos); 153 oos.writeObject(data); 154 oos.close(); 155 fos.close(); 156 log.info("Wrote data binding to: "+dataFile); 157 } 158 159 private Object readBinding(Name name) 160 throws IOException , ClassNotFoundException 161 { 162 File dataFile = new File (storeDirectory, name.toString()); 163 if( dataFile.exists() == false ) 164 return null; 165 166 FileInputStream fis = new FileInputStream (dataFile); 167 ObjectInputStream ois = new ObjectInputStream (fis); 168 Object data = ois.readObject(); 169 ois.close(); 170 fis.close(); 171 log.info("Read data binding from: "+dataFile); 172 return data; 173 } 174 175 181 Throwable getException(Throwable ex) 182 { 183 if( ex instanceof MBeanException ) 184 { 185 MBeanException mbe = (MBeanException ) ex; 186 ex = mbe.getTargetException(); 187 } 188 else if( ex instanceof ReflectionException ) 189 { 190 ReflectionException re = (ReflectionException ) ex; 191 ex = re.getTargetException(); 192 } 193 return ex; 194 } 195 } 196 | Popular Tags |