1 9 10 package org.enhydra.util; 11 12 13 import java.io.File ; 14 import java.io.FileFilter ; 15 import java.io.InputStream ; 16 import java.lang.reflect.Method ; 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.Enumeration ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Set ; 24 import java.util.jar.JarFile ; 25 import java.util.zip.ZipEntry ; 26 27 import javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.NodeList ; 33 34 import com.lutris.appserver.server.Enhydra; 35 import com.lutris.logging.Logger; 36 import com.lutris.util.Config; 37 38 41 public class Utils { 42 43 44 48 public static List getCachedDOClassesForApplication(Config cfg, ClassLoader cl) { 49 List retVal = new ArrayList (); 50 boolean loaded = false; 51 52 String [] databaseNames; 53 try { 55 databaseNames = cfg.getStrings("DatabaseManager.Databases"); 56 for (int i = 0; i < databaseNames.length; i++) { 57 String databaseName = databaseNames[i]; 58 Object domlPath = 59 cfg.get("DatabaseManager.DB." + databaseName + ".domlFile"); 60 if (domlPath != null) 61 retVal.addAll( 62 getCachedDOClassesFromDoml( 63 domlPath.toString(), cl, databaseName)); 64 } 65 if (retVal.isEmpty()) 66 loaded = false; 67 else 68 loaded = true; 69 } catch (Throwable e) { 70 retVal.clear(); 71 loaded = false; 72 } 73 74 75 if(!loaded) { 77 try { 78 databaseNames = cfg.getStrings("DatabaseManager.Databases"); 79 80 for (int i = 0; i < databaseNames.length; i++) { 81 String databaseName = databaseNames[i]; 82 Object domlPath = cfg.get("DatabaseManager.DB." + databaseName + ".DomlClasspath"); 83 String domlClassPathName = domlPath.toString(); 84 InputStream domlStream =cl.getResourceAsStream(domlClassPathName); 85 if (domlStream != null) { 86 retVal.addAll( 87 getCachedDOClassesFromDoml( 88 domlStream, 89 cl,databaseName)); 90 } else { 91 Enhydra.getLogChannel().write( 92 Logger.DEBUG, 93 "Resource " +domlClassPathName+" don't exist on application classpath!"); 94 } 95 } 96 97 if (retVal.isEmpty()) 98 loaded = false; 99 else 100 loaded = true; 101 102 }catch(Throwable e) { 103 retVal.clear(); 104 loaded = false; 105 } 107 108 } 109 110 if (!loaded) { 112 try { 113 databaseNames = cfg.getStrings("DatabaseManager.Databases"); 114 for (int i = 0; i < databaseNames.length; i++) { 115 String databaseName = databaseNames[i]; 116 Object classList = 118 cfg.get( 119 "DatabaseManager.DB." + databaseName + ".ClassList"); 120 if (classList != null) { 121 retVal.addAll( 122 getCachedDOClassesFromClassList( 123 new File (classList.toString()),cl,databaseName)); 124 } 125 } 126 if (retVal.isEmpty()) 127 loaded = false; 128 else 129 loaded = true; 130 } catch (Throwable e) { 131 retVal.clear(); 132 loaded = false; 133 } 134 } 135 136 if (!loaded) { 138 try { 139 databaseNames = cfg.getStrings("DatabaseManager.Databases"); 140 InputStream classListStream = 141 cl.getResourceAsStream( 142 "org/enhydra/dods/DODSClassList.xml"); 143 if (classListStream != null) { 144 retVal.addAll(getCachedDOClassesFromClassListAsResource(classListStream,cl,databaseNames[0])); 145 } else { 146 Enhydra.getLogChannel().write( 147 Logger.DEBUG, 148 "Resource org/enhydra/dods/DODSClassList.xml don't exist !"); 149 } 150 if (retVal.isEmpty()) 151 loaded = false; 152 else 153 loaded = true; 154 } catch (Throwable e) { 155 retVal.clear(); 156 loaded = false; 157 } 158 } 159 160 if (!loaded) { 162 String [] classPath = new String [0]; 164 String cpKey = "Server.ClassPath"; 165 if (cfg.containsKey(cpKey)) { 166 try { 167 classPath = cfg.getStrings(cpKey); 168 return getCachedDOClassesForPath( 169 classPath[0], 170 cl,""); 171 } catch (Throwable ex) { 172 } 174 } 175 } 176 return retVal; 177 } 178 179 180 181 187 public static List getCachedDOClassesFromDoml(InputStream doml,ClassLoader cl,String databaseName) { 188 List retVal = new ArrayList (); 189 try { 190 191 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 193 DocumentBuilder builder = factory.newDocumentBuilder(); 194 Document doc = builder.parse(doml); 196 NodeList tableNodes = doc.getElementsByTagName("table"); 197 for(int i = 0;i < tableNodes.getLength();i++) { 198 String tableName = ((Element )tableNodes.item(i)).getAttribute("id"); 199 if( tableName.startsWith("root.") ) 200 tableName = tableName.substring(5, tableName.length()); 201 String tableClassName = tableName + "DO"; 202 Class cls=cl.loadClass(tableClassName); 204 Method mth; 210 mth=cls.getMethod("refreshCache",new Class [] {}); 211 mth=cls.getMethod("getConfigurationAdministration",new Class [] {}); 212 retVal.add(new DOTable(databaseName,cls)); 213 } 215 220 }catch(Throwable e) { 221 e.printStackTrace(); 222 } 223 return retVal; 224 } 225 226 227 233 public static List getCachedDOClassesFromDoml(String domlPath,ClassLoader cl, String databaseName) { 234 List retVal = new ArrayList (); 235 try { 236 237 File domlFile = new File (domlPath); 238 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 239 DocumentBuilder builder = factory.newDocumentBuilder(); 240 if( domlFile.exists() ) { 241 Document doc = builder.parse(domlFile); 242 NodeList tableNodes = doc.getElementsByTagName("table"); 243 for(int i = 0;i < tableNodes.getLength();i++) { 244 String tableName = ((Element )tableNodes.item(i)).getAttribute("id"); 245 if( tableName.startsWith("root.") ) 246 tableName = tableName.substring(5, tableName.length()); 247 String tableClassName = tableName + "DO"; 248 Class cls=cl.loadClass(tableClassName); 250 Method mth; 256 mth=cls.getMethod("refreshCache",new Class [] {}); 257 mth=cls.getMethod("getConfigurationAdministration",new Class [] {}); 258 retVal.add(new DOTable(databaseName, cls)); 259 } 260 } else { 261 Enhydra.getLogChannel().write(Logger.DEBUG, 262 "Doml file "+domlPath+" don't exist !"); 263 } 264 265 }catch(Throwable e) { 266 Enhydra.getLogChannel().write(Logger.DEBUG, 268 "Loading DO classes from doml file was unsuccessful!"); 269 } 270 return retVal; 271 } 272 273 279 public static List getCachedDOClassesFromClassList(File classListFile,ClassLoader cl, String databaseName) { 280 List retVal = new ArrayList (); 281 try { 282 283 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 284 DocumentBuilder builder = factory.newDocumentBuilder(); 285 if( classListFile.exists() ) { 286 Document doc = builder.parse(classListFile); 287 NodeList classesNodes = doc.getElementsByTagName("CLASSES"); 288 for(int j=0; j<classesNodes.getLength(); j++) { 289 Element docClasses = (Element )classesNodes.item(j); 290 String dbName = ((Element )classesNodes.item(j)).getAttribute("databaseName"); 291 if(dbName==null || dbName.equals("")) 292 dbName = databaseName; 293 294 NodeList tableNodes = doc.getElementsByTagName("CLASS"); 295 for(int i = 0;i < tableNodes.getLength();i++) { 296 String tableName = ((Element )tableNodes.item(i)).getAttribute("name"); 297 if( tableName.startsWith("root.") ) 298 tableName = tableName.substring(5, tableName.length()); 299 String tableClassName = tableName; 300 Class cls=cl.loadClass(tableClassName); 302 Method mth; 308 mth=cls.getMethod("refreshCache",new Class [] {}); 309 mth=cls.getMethod("getConfigurationAdministration",new Class [] {}); 310 retVal.add(new DOTable(databaseName,cls)); 311 } 312 } 313 } else { 314 Enhydra.getLogChannel().write(Logger.DEBUG, 315 "ClassList file "+classListFile.getAbsolutePath()+" don't exist !"); 316 } 317 318 }catch(Throwable e) { 319 Enhydra.getLogChannel().write(Logger.DEBUG, 321 "Loading DO classes from class list was unsuccessful!"); 322 } 323 return retVal; 324 } 325 326 332 public static List getCachedDOClassesFromClassListAsResource(InputStream classListInputStream,ClassLoader cl,String databaseName) { 333 List retVal = new ArrayList (); 334 try { 335 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 336 DocumentBuilder builder = factory.newDocumentBuilder(); 337 Document doc = builder.parse(classListInputStream); 338 NodeList classesNodes = doc.getElementsByTagName("CLASSES"); 339 for(int j=0; j<classesNodes.getLength(); j++) { 340 Element docClasses = (Element )classesNodes.item(j); 341 String dbName = ((Element )classesNodes.item(j)).getAttribute("databaseName"); 342 if(dbName==null || dbName.equals("")) 343 dbName = databaseName; 344 NodeList tableNodes = docClasses.getElementsByTagName("CLASS"); 345 for(int i = 0;i < tableNodes.getLength();i++) { 346 String tableName = ((Element )tableNodes.item(i)).getAttribute("name"); 347 if( tableName.startsWith("root.") ) 348 tableName = tableName.substring(5, tableName.length()); 349 String tableClassName = tableName; 350 Class cls=cl.loadClass(tableClassName); 351 Method mth; 357 mth=cls.getMethod("refreshCache",new Class [] {}); 358 mth=cls.getMethod("getConfigurationAdministration",new Class [] {}); 359 retVal.add(new DOTable(dbName,cls)); 360 } 361 } 362 }catch(Throwable e) { 363 Enhydra.getLogChannel().write(Logger.DEBUG, 365 "Loading DO classes from class list was unsuccessful!"); 366 } 367 return retVal; 368 } 369 370 373 private static List getCachedDOClassesForPath (String classPath,ClassLoader cl, String databaseName) { 374 List cachedDOClasses=new ArrayList (); 375 if (classPath.endsWith(".jar")) { 377 JarFile jfile=null; 378 try { 379 jfile=new JarFile (classPath,false); 380 } catch (Throwable ex) { 381 return cachedDOClasses; 382 } 383 Enumeration e = jfile.entries(); 385 while (e.hasMoreElements()) { 387 try { 388 ZipEntry entry = (ZipEntry )e.nextElement(); 389 String entryname = entry.getName(); 390 if (entryname.endsWith("DO.class")) { 392 String className = entryname.substring(0,entryname.length()-6); 394 if (className.startsWith("/")) className = className.substring(1); 396 className = className.replace('/','.'); 398 399 Class cls=cl.loadClass(className); 402 Method mth; 409 mth=cls.getMethod("refreshCache",new Class [] {}); 411 mth=cls.getMethod("getConfigurationAdministration",new Class [] {}); 413 cachedDOClasses.add(new DOTable(databaseName,cls)); 414 } 415 } catch (Throwable thr) { 416 } 417 } 418 } else { File startingFolder=new File (classPath); 421 if (startingFolder.exists() && startingFolder.isDirectory()) { 423 Set allDOClassFiles=new HashSet (); 425 traverseDOClasses(startingFolder,allDOClassFiles); 426 Iterator files=allDOClassFiles.iterator(); 427 while (files.hasNext()) { 428 try { 429 File f=(File )files.next(); 430 String fullFileName=f.getAbsolutePath(); 431 String className=fullFileName.substring(0,fullFileName.length()-6); 433 className=className.substring(classPath.length()+1,className.length()); 435 className = className.replace(File.separatorChar,'.'); 437 Class cls=cl.loadClass(className); 439 Method mth; 445 mth=cls.getMethod("refreshCache",new Class [] {}); 446 mth=cls.getMethod("getConfigurationAdministration",new Class [] {}); 447 cachedDOClasses.add(cls); 448 } catch (Throwable thr) {} 449 } 450 } 451 } 452 return cachedDOClasses; 453 } 454 455 460 private static void traverseDOClasses(File f,Collection c) { 461 if (!f.exists()) { 462 return; 464 } 465 if (f.isDirectory()) { 466 File [] children = f.listFiles(doClassFileFilter); 467 for (int i=0; i<children.length; i++) { 468 traverseDOClasses(children[i],c); 469 } 470 } else { 471 c.add(f); 472 } 473 } 474 475 479 private static DOClassFileFilter doClassFileFilter=new DOClassFileFilter(); 480 481 482 } 483 484 488 class DOClassFileFilter implements FileFilter { 489 public boolean accept (File pathname) { 490 return (pathname.isDirectory() || pathname.getName().endsWith("DO.class")); 491 } 492 } 493 | Popular Tags |