KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > reflect > Reflector


1 /*
2  * Created on Nov 29, 2003
3  *
4 /*
5 Copyright (c) 2003 eInnovation Inc. All rights reserved
6
7 This library is free software; you can redistribute it and/or modify it under the terms
8 of the GNU Lesser General Public License as published by the Free Software Foundation;
9 either version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU Lesser General Public License for more details.
14 */

15 package com.openedit.modules.reflect;
16
17 import java.beans.IntrospectionException JavaDoc;
18 import java.beans.Introspector JavaDoc;
19 import java.beans.MethodDescriptor JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import com.openedit.OpenEditException;
28 import com.openedit.WebPageRequest;
29 import com.openedit.modules.BaseModule;
30
31 /**
32  * @author Matt Avery, mavery@einnovation.com
33  *
34  * Could make this an immutable object and eliminate the WebPageContext argument
35  * to the public methods.
36  */

37 public class Reflector extends BaseModule
38 {
39     public void reflectOnSessionObjects( WebPageRequest inContext ) throws OpenEditException
40     {
41         
42         for (Iterator JavaDoc iter = inContext.getParameterMap().keySet().iterator(); iter.hasNext();)
43         {
44             String JavaDoc key = (String JavaDoc) iter.next();
45             int dotIndex = key.indexOf('.');
46             if (dotIndex > 0)
47             {
48                 String JavaDoc sessionObjectKey = key.substring(0, dotIndex);
49                 Object JavaDoc sessionObject = inContext.getSessionValue(sessionObjectKey);
50                 if (sessionObject == null)
51                 {
52                     return;
53                 }
54                 String JavaDoc methodName = key.substring(dotIndex + 1, key.length());
55                 List JavaDoc methods = findMethods(sessionObject, methodName);
56                 for ( Iterator JavaDoc iterator = methods.iterator(); iterator.hasNext(); )
57                 {
58                     Method JavaDoc method = (Method JavaDoc) iterator.next();
59                     try
60                     {
61                         invokeMethod(inContext, method, sessionObject);
62                     }
63                     catch (Exception JavaDoc ex)
64                     {
65                         //log.error( ex );
66
if ( ex instanceof OpenEditException)
67                         {
68                             throw (OpenEditException)ex;
69                         }
70                         throw new OpenEditException(ex);
71                     }
72                 }
73             }
74         }
75
76     }
77
78     /** Two methods could have the same name but different signatures
79      *
80      * @param inObject
81      * @param inMethodName
82      * @return
83      */

84     public List JavaDoc findMethods(Object JavaDoc inObject, String JavaDoc inMethodName)
85     {
86         List JavaDoc methods = new ArrayList JavaDoc();
87         MethodDescriptor JavaDoc[] methodDescriptors;
88         try
89         {
90             methodDescriptors = Introspector.getBeanInfo(inObject.getClass()).getMethodDescriptors();
91         }
92         catch (IntrospectionException JavaDoc e)
93         {
94             return methods;
95         }
96         for (int i = 0; i < methodDescriptors.length; i++)
97         {
98             if ( methodDescriptors[i].getName().equals( inMethodName ) )
99             {
100                 methods.add( methodDescriptors[i].getMethod() );
101             }
102         }
103         return methods;
104     }
105
106     protected void invokeMethod(WebPageRequest inContext, Method JavaDoc method, Object JavaDoc sessionObject) throws Exception JavaDoc
107     {
108         try
109         {
110             method.invoke(sessionObject, new Object JavaDoc[] { inContext });
111         }
112         catch (InvocationTargetException JavaDoc ite)
113         {
114             Throwable JavaDoc throwable = ite.getTargetException();
115             if (throwable instanceof Exception JavaDoc)
116             {
117                 throw (Exception JavaDoc) throwable;
118             }
119             /*
120             else if (throwable instanceof Message)
121             {
122                 throw (Message) throwable;
123             }
124                 */

125             else if (throwable instanceof Error JavaDoc)
126             {
127                 throw (Error JavaDoc) throwable;
128             }
129             else
130             {
131                 throw new OpenEditException(throwable.getMessage());
132             }
133         }
134
135     }
136     public void reflectOnRequestParameters(WebPageRequest inContext, Object JavaDoc inObject) throws Exception JavaDoc
137     {
138         Set JavaDoc keys = inContext.getParameterMap().keySet();
139         for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();)
140         {
141             String JavaDoc key = (String JavaDoc) iter.next();
142             Object JavaDoc value;
143             List JavaDoc methods = findMethods( inObject, key );
144
145             for ( Iterator JavaDoc iterator = methods.iterator(); iterator.hasNext(); )
146             {
147                 Method JavaDoc method = (Method JavaDoc) iterator.next();
148                 
149                 Class JavaDoc[] parameterTypes = method.getParameterTypes();
150                 if ( parameterTypes.length == 1 )
151                 {
152                     Class JavaDoc parameterType = parameterTypes[0];
153                     if ( parameterType.equals( String JavaDoc.class ) )
154                     {
155                         value = inContext.getRequestParameter( key );
156                     }
157                     else if ( parameterType.equals( String JavaDoc[].class ) )
158                     {
159                         value = inContext.getRequestParameters( key );
160                     }
161                     else if ( parameterType.equals( int.class ) )
162                     {
163                         value = new Integer JavaDoc( inContext.getRequestParameter( key ) );
164                     }
165                     else if ( parameterType.equals( long.class ) )
166                     {
167                         value = new Long JavaDoc( inContext.getRequestParameter( key ) );
168                     }
169                     else if ( parameterType.equals( double.class ) )
170                     {
171                         value = new Double JavaDoc( inContext.getRequestParameter( key ) );
172                     }
173                     else if ( parameterType.equals( byte.class ) )
174                     {
175                         value = new Byte JavaDoc( inContext.getRequestParameter( key ) );
176                     }
177                     else if ( parameterTypes.equals( boolean.class ) )
178                     {
179                         value = new Boolean JavaDoc( inContext.getRequestParameter( key ) );
180                     }
181                     else
182                     {
183                         continue;
184                     }
185     
186                     method.invoke( inObject, new Object JavaDoc[] { value } );
187                 }
188             }
189         }
190     }
191
192 }
193
Popular Tags