KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > interceptors > JNDIPersistence


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jmx.interceptors;
23
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31
32 import javax.naming.Name JavaDoc;
33 import javax.naming.NameNotFoundException JavaDoc;
34 import javax.management.MBeanException JavaDoc;
35 import javax.management.ReflectionException JavaDoc;
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 /** A simple file based persistence interceptor that saves the value of
43  * Naming.bind() calls as serialized objects.
44  *
45  * @author Scott.Stark@jboss.org
46  * @version $Revision: 37406 $
47  */

48 public final class JNDIPersistence
49    extends AbstractInterceptor
50 {
51    private static Logger log = Logger.getLogger(JNDIPersistence.class);
52
53    private File JavaDoc storeDirectory;
54
55    public File JavaDoc getStoreDirectory()
56    {
57       return storeDirectory;
58    }
59    public void setStoreDirectory(File JavaDoc storeDirectory)
60    {
61       log.info("setStoreDirectory: "+storeDirectory);
62       if( storeDirectory.exists() == false )
63          storeDirectory.mkdir();
64       this.storeDirectory = storeDirectory;
65    }
66
67    // Interceptor overrides -----------------------------------------
68
public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
69    {
70       String JavaDoc opName = invocation.getName();
71       log.info("invoke, opName="+opName);
72
73       // If this is not the invoke(Invocation) op just pass it along
74
if( opName == null || opName.equals("invoke") == false )
75       {
76          Interceptor i = invocation.nextInterceptor();
77          return i.invoke(invocation);
78       }
79       
80       Object JavaDoc[] args = invocation.getArgs();
81       org.jboss.invocation.Invocation invokeInfo =
82          (org.jboss.invocation.Invocation) args[0];
83
84       Object JavaDoc[] iargs = invokeInfo.getArguments();
85       for(int a = 0; a < args.length; a ++)
86          log.info(" args["+a+"]="+iargs[a]);
87       Method JavaDoc method = invokeInfo.getMethod();
88       String JavaDoc methodName = method.getName();
89       log.info("methodName: "+methodName);
90       Object JavaDoc value = null;
91       if( methodName.equals("bind") )
92       {
93          log.info("Dispatching bind");
94          invocation.nextInterceptor().invoke(invocation);
95          // Bind succeeded, save the value
96
log.info("Saving bind data");
97          Name JavaDoc name = (Name JavaDoc) iargs[0];
98          Object JavaDoc data = iargs[1];
99          try
100          {
101             writeBinding(name, data);
102          }
103          catch(Throwable JavaDoc 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 JavaDoc ex)
118          {
119             ex = getException(ex);
120             log.info("InvocationException: ", ex);
121             if( ex instanceof NameNotFoundException JavaDoc )
122             {
123                log.info("NameNotFoundException in lookup, finding data");
124                Name JavaDoc name = (Name JavaDoc) iargs[0];
125                try
126                {
127                   value = readBinding(name);
128                   if( value == null )
129                      throw ex;
130                }
131                catch(Throwable JavaDoc 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 JavaDoc name, Object JavaDoc data)
148       throws IOException JavaDoc
149    {
150       File JavaDoc dataFile = new File JavaDoc(storeDirectory, name.toString());
151       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(dataFile);
152       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(fos);
153       oos.writeObject(data);
154       oos.close();
155       fos.close();
156       log.info("Wrote data binding to: "+dataFile);
157    }
158
159    private Object JavaDoc readBinding(Name JavaDoc name)
160       throws IOException JavaDoc, ClassNotFoundException JavaDoc
161    {
162       File JavaDoc dataFile = new File JavaDoc(storeDirectory, name.toString());
163       if( dataFile.exists() == false )
164          return null;
165
166       FileInputStream JavaDoc fis = new FileInputStream JavaDoc(dataFile);
167       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(fis);
168       Object JavaDoc data = ois.readObject();
169       ois.close();
170       fis.close();
171       log.info("Read data binding from: "+dataFile);
172       return data;
173    }
174
175    /** Unwrap the InvocationException to see what the Naming service
176     * exception really was.
177     *
178     * @param ex the wrapped InvocationException
179     * @return the underlying initial exception
180     */

181    Throwable JavaDoc getException(Throwable JavaDoc ex)
182    {
183       if( ex instanceof MBeanException JavaDoc )
184       {
185          MBeanException JavaDoc mbe = (MBeanException JavaDoc) ex;
186          ex = mbe.getTargetException();
187       }
188       else if( ex instanceof ReflectionException JavaDoc )
189       {
190          ReflectionException JavaDoc re = (ReflectionException JavaDoc) ex;
191          ex = re.getTargetException();
192       }
193       return ex;
194    }
195 }
196
Popular Tags