1 55 63 package com.openedit.util; 64 65 import java.lang.reflect.Constructor ; 66 import java.lang.reflect.Modifier ; 67 68 import com.openedit.ModuleManager; 69 import com.openedit.WebPageRequest; 70 71 81 public class SessionTool 82 { 83 protected WebPageRequest fieldContext; 84 protected ModuleManager fieldModuleManager; 85 86 public SessionTool( WebPageRequest inContext, ModuleManager inManager) 87 { 88 fieldContext = inContext; 89 fieldModuleManager = inManager; 90 } 91 92 public Object sessionInstance( String inSessionKey, String inClassName ) throws Exception 93 { 94 Object instance = getContext().getSessionValue( inSessionKey ); 95 if ( instance == null ) 96 { 97 instance = construct( inSessionKey, inClassName ); 98 getContext().putSessionValue( inSessionKey, instance ); 99 } 100 return instance; 101 } 102 103 protected WebPageRequest getContext() 104 { 105 return fieldContext; 106 } 107 108 public Object construct( String inKey, String inClassName ) throws Exception 109 { 110 if ( getModuleManager().containsModule( inKey ) ) 111 { 112 return getModuleManager().getBean( inKey ); 113 } 114 Class newClass = Class.forName( inClassName ); 115 Constructor [] constructors = newClass.getConstructors(); 116 for ( int i = 0; i < constructors.length; i++ ) 117 { 118 Class [] argumentClasses = constructors[i].getParameterTypes(); 119 if ( argumentClasses.length == 0 ) 120 { 121 return constructors[i].newInstance( null ); 122 } 123 if ( argumentClasses.length == 1 124 && argumentClasses[0].equals( WebPageRequest.class ) ) 125 { 126 return constructors[i].newInstance( new Object [] { getContext() } ); 127 } 128 } 129 return newClass.newInstance(); 130 } 131 132 138 public static Object newInstance(String className) throws Exception 139 { 140 Class cls = Class.forName(className); 141 Class [] params = new Class [0]; 142 143 try 144 { 145 Constructor constructor = cls.getConstructor(params); 146 return constructor.newInstance( new Object [0] ); 147 } 148 catch (Exception ex) 149 { 150 Constructor constructor = cls.getDeclaredConstructor(params); 151 if ( Modifier.isPrivate( constructor.getModifiers() ) ) 152 return cls; } 154 return null; 155 } 156 157 165 public static Object newInstance(String className, Object param) 166 throws Exception 167 { 168 return newInstance( className, new Object [] {param} ); 169 } 170 171 178 public static Object newInstance(String className, Object [] params) 179 throws Exception 180 { 181 Class cls = Class.forName(className); 182 Constructor constructor = getConstructor(cls, params); 183 return constructor.newInstance(params); 184 } 185 186 201 public static Constructor getConstructor(Class cls, Object [] params) 202 { 203 Constructor [] constructors = cls.getConstructors(); 204 205 for (int i = 0; i < constructors.length; ++i ) 206 { 207 Class [] parameterTypes = constructors[i].getParameterTypes(); 208 209 if (parameterTypes.length == params.length) 212 { 213 for (int j = 0; ; j++) 216 { 217 if (j >= parameterTypes.length) 218 return constructors[i]; 220 Class c = parameterTypes[j]; 221 Object p = params[j]; 222 if ( c.isPrimitive() ) 223 { 224 try 225 { 226 if ( c != p.getClass().getField("TYPE").get(p) ) 227 break; 228 } catch (Exception ex) { 229 break; } 231 } 232 else if ( (p != null) && 233 !c.isAssignableFrom( p.getClass() ) ) 234 break; 235 } } } 239 return null; 240 } 241 242 public ModuleManager getModuleManager() 243 { 244 return fieldModuleManager; 245 } 246 } 247 | Popular Tags |