1 package org.apache.fulcrum.intake; 2 3 56 import java.beans.PropertyDescriptor ; 57 import java.io.File ; 58 import java.io.FileInputStream ; 59 import java.io.FileOutputStream ; 60 import java.io.InputStream ; 61 import java.io.ObjectInputStream ; 62 import java.io.ObjectOutputStream ; 63 import java.io.OutputStream ; 64 import java.lang.reflect.Method ; 65 import java.util.HashMap ; 66 import java.util.Iterator ; 67 import java.util.List ; 68 import java.util.Map ; 69 70 import org.apache.commons.pool.KeyedObjectPool; 71 import org.apache.commons.pool.KeyedPoolableObjectFactory; 72 import org.apache.commons.pool.impl.StackKeyedObjectPool; 73 74 import org.apache.fulcrum.BaseService; 75 import org.apache.fulcrum.InitializationException; 76 import org.apache.fulcrum.ServiceException; 77 import org.apache.fulcrum.intake.model.Group; 78 import org.apache.fulcrum.intake.transform.XmlToAppData; 79 import org.apache.fulcrum.intake.xmlmodel.AppData; 80 import org.apache.fulcrum.intake.xmlmodel.XmlGroup; 81 82 89 public class TurbineIntakeService 90 extends BaseService 91 implements IntakeService 92 { 93 94 private String [] groupNames; 95 96 97 private Map groupNameMap; 98 99 100 private Map groupKeyMap; 101 102 103 private Map getterMap; 104 105 106 private Map setterMap; 107 108 109 111 112 private AppData appData; 113 114 115 KeyedObjectPool keyedPool; 116 117 private static final int GETTER = 0; 119 private static final int SETTER = 1; 120 121 124 public TurbineIntakeService() 125 { 126 } 127 128 133 public void init() 134 throws InitializationException 135 { 136 String xmlPath = getConfiguration() 137 .getString(XML_PATH, XML_PATH_DEFAULT); 138 String appDataPath = getConfiguration() 139 .getString(SERIAL_XML, SERIAL_XML_DEFAULT); 140 141 String SERIALIZED_ERROR_MSG = 142 "Intake initialization could not be serialized " + 143 "because writing to " + appDataPath + " was not " + 144 "allowed. This will require that the xml file be " + 145 "parsed when restarting the application."; 146 147 if ( xmlPath == null ) 148 { 149 String pathError = 150 "Path to intake.xml was not specified. Check that the" + 151 " property exists in TR.props and was loaded."; 152 getCategory().error(pathError); 153 throw new InitializationException(pathError); 154 } 155 156 File serialAppData = null; 157 File xmlFile = null; 158 xmlFile = new File (xmlPath); 159 if ( !xmlFile.canRead() ) 160 { 161 xmlPath = getRealPath(xmlPath); 163 xmlFile = new File (xmlPath); 164 if ( !xmlFile.canRead() ) 165 { 166 String pathError = 167 "Could not read input file. Even tried relative to" 168 + " webapp root."; 169 getCategory().error(pathError); 170 throw new InitializationException(pathError); 171 } 172 } 173 174 serialAppData = new File (appDataPath); 175 try 176 { 177 serialAppData.createNewFile(); 178 serialAppData.delete(); 179 } 180 catch (Exception e) 181 { 182 appDataPath = getRealPath(appDataPath); 184 serialAppData = new File (appDataPath); 185 try 186 { 187 serialAppData.createNewFile(); 188 serialAppData.delete(); 189 } 190 catch (Exception ee) 191 { 192 getCategory().info(SERIALIZED_ERROR_MSG); 193 } 194 } 195 196 try 197 { 198 if ( serialAppData.exists() 199 && serialAppData.lastModified() > xmlFile.lastModified() ) 200 { 201 InputStream in = null; 202 try 203 { 204 in = new FileInputStream (serialAppData); 205 ObjectInputStream p = new ObjectInputStream (in); 206 appData = (AppData)p.readObject(); 207 } 208 catch (Exception e) 209 { 210 writeAppData(xmlPath, appDataPath, serialAppData); 212 } 213 finally 214 { 215 if (in != null) 216 { 217 in.close(); 218 } 219 } 220 } 221 else 222 { 223 writeAppData(xmlPath, appDataPath, serialAppData); 224 } 225 226 groupNames = new String [appData.getGroups().size()]; 227 groupKeyMap = new HashMap (); 228 groupNameMap = new HashMap (); 229 getterMap = new HashMap (); 230 setterMap = new HashMap (); 231 String pkg = appData.getBasePackage(); 233 234 int maxPooledGroups = 0; 235 List glist = appData.getGroups(); 236 for ( int i=glist.size()-1; i>=0; i-- ) 237 { 238 XmlGroup g = (XmlGroup)glist.get(i); 239 String groupName = g.getName(); 240 groupNames[i] = groupName; 241 groupKeyMap.put(groupName, g.getKey()); 242 groupNameMap.put(g.getKey(), groupName); 243 maxPooledGroups = 244 Math.max(maxPooledGroups, 245 Integer.parseInt(g.getPoolCapacity())); 246 List classNames = g.getMapToObjects(); 247 Iterator iter2 = classNames.iterator(); 248 while (iter2.hasNext()) 249 { 250 String className = (String )iter2.next(); 251 if ( !getterMap.containsKey(className) ) 252 { 253 getterMap.put(className, new HashMap ()); 254 setterMap.put(className, new HashMap ()); 255 } 256 } 257 } 258 259 KeyedPoolableObjectFactory factory = 260 new Group.GroupFactory(appData); 261 keyedPool = new StackKeyedObjectPool(factory, maxPooledGroups); 262 263 setInit(true); 264 } 265 catch (Exception e) 266 { 267 throw new InitializationException( 268 "TurbineIntakeService failed to initialize", e); 269 } 270 } 271 272 276 private void writeAppData(String xmlPath, String appDataPath, File serialAppData) 277 throws Exception 278 { 279 XmlToAppData xmlApp = new XmlToAppData(); 280 appData = xmlApp.parseFile(xmlPath); 281 OutputStream out = null; 282 InputStream in = null; 283 try 284 { 285 out = new FileOutputStream (serialAppData); 287 ObjectOutputStream p = new ObjectOutputStream (out); 288 p.writeObject(appData); 289 p.flush(); 290 291 in = new FileInputStream (serialAppData); 294 ObjectInputStream pin = new ObjectInputStream (in); 295 appData = (AppData)pin.readObject(); 296 } 297 catch (Exception e) 298 { 299 getCategory().info( 300 "Intake initialization could not be serialized " + 301 "because writing to " + appDataPath + " was not " + 302 "allowed. This will require that the xml file be " + 303 "parsed when restarting the application."); 304 } 305 finally 306 { 307 if (out != null) 308 { 309 out.close(); 310 } 311 if (in != null) 312 { 313 in.close(); 314 } 315 } 316 } 317 318 326 public Group getGroup(String groupName) 327 throws ServiceException 328 { 329 Group group = null; 330 if (groupName == null) 331 { 332 throw new ServiceException ( 333 "Intake TurbineIntakeService.getGroup(groupName) is null"); 334 } 335 try 336 { 337 group = (Group)keyedPool.borrowObject(groupName); 338 } 339 catch (Exception e) 340 { 341 new ServiceException(e); 342 } 343 return group; 344 } 345 346 347 353 public void releaseGroup(Group instance) 354 { 355 if (instance != null) 356 { 357 String name = instance.getIntakeGroupName(); 358 try 359 { 360 keyedPool.returnObject(name, instance); 361 } 362 catch (Exception e) 363 { 364 new ServiceException(e); 365 } 366 } 368 else 369 { 370 } 372 } 373 374 379 public int getSize(String name) 380 { 381 return keyedPool.getNumActive(name) + keyedPool.getNumIdle(name); 382 } 383 384 389 public String [] getGroupNames() 390 { 391 return groupNames; 392 } 393 394 400 public String getGroupKey(String groupName) 401 { 402 return (String )groupKeyMap.get(groupName); 403 } 404 405 411 public String getGroupName(String groupKey) 412 { 413 return (String )groupNameMap.get(groupKey); 414 } 415 416 423 public Method getFieldSetter(String className, String propName) 424 { 425 Map settersForClassName = (Map )setterMap.get(className); 426 Method setter = (Method )settersForClassName.get(propName); 427 428 if ( setter == null ) 429 { 430 PropertyDescriptor pd = null; 431 synchronized(setterMap) 432 { 433 try 434 { 435 pd = new PropertyDescriptor (propName, 438 Class.forName(className)); 439 setter = pd.getWriteMethod(); 440 ((Map )setterMap.get(className)).put(propName, setter); 441 if ( setter == null ) 442 { 443 getCategory().error("Intake: setter for '" + propName 444 + "' in class '" + className 445 + "' could not be found."); 446 } 447 } 448 catch (Exception e) 449 { 450 getCategory().error(e); 451 } 452 } 453 synchronized(getterMap) 456 { 457 try 458 { 459 Method getter = pd.getReadMethod(); 460 ((Map )getterMap.get(className)).put(propName, getter); 461 } 462 catch (Exception e) 463 { 464 } 466 } 467 } 468 return setter; 469 } 470 471 478 public Method getFieldGetter(String className, String propName) 479 { 480 Map gettersForClassName = (Map )getterMap.get(className); 481 Method getter = (Method )gettersForClassName.get(propName); 482 483 if ( getter == null ) 484 { 485 PropertyDescriptor pd = null; 486 synchronized(getterMap) 487 { 488 try 489 { 490 pd = new PropertyDescriptor (propName, 493 Class.forName(className)); 494 getter = pd.getReadMethod(); 495 ((Map )getterMap.get(className)).put(propName, getter); 496 if ( getter == null ) 497 { 498 getCategory().error("Intake: getter for '" + propName 499 + "' in class '" + className 500 + "' could not be found."); 501 } 502 } 503 catch (Exception e) 504 { 505 getCategory().error(e); 506 } 507 } 508 synchronized(setterMap) 511 { 512 try 513 { 514 Method setter = pd.getWriteMethod(); 515 ((Map )setterMap.get(className)).put(propName, setter); 516 } 517 catch (Exception e) 518 { 519 } 521 } 522 } 523 return getter; 524 } 525 526 } 527 | Popular Tags |