1 20 package org.jahia.admin.database; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileReader ; 26 import java.io.IOException ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.Properties ; 30 import java.util.Vector ; 31 32 import org.jahia.bin.Jahia; 33 import org.jahia.utils.PathResolver; 34 35 36 37 49 public class DatabaseScripts { 50 51 private static org.apache.log4j.Logger logger = 52 org.apache.log4j.Logger.getLogger(DatabaseScripts.class); 53 54 58 public DatabaseScripts() 59 { 60 } 63 64 65 72 public Enumeration getDatabaseScriptsFileObjects() 73 throws IOException 74 { 75 File scriptsFolderFileObject = new File ( Jahia.jahiaDatabaseScriptsPath ); 76 File [] scriptsListFileArray = scriptsFolderFileObject.listFiles(); 77 Vector scriptsListVector = new Vector (); 78 79 for(int i=0; i<scriptsListFileArray.length; i++) { 80 if(!scriptsListFileArray[i].isDirectory()) { 81 if (scriptsListFileArray[i].getName().endsWith(".script")) { 82 scriptsListVector.add( scriptsListFileArray[i] ); 83 } 84 } 85 } 86 87 return scriptsListVector.elements(); 88 } 90 91 92 101 public Vector getDatabaseScriptsInfos( Enumeration fileObjects, PathResolver pathResolver ) 102 throws IOException 103 { 104 Vector scriptsInfosVector = new Vector (); 105 106 while(fileObjects.hasMoreElements()) 107 { 108 File scriptFileObject = (File ) fileObjects.nextElement(); 109 FileInputStream scriptInputStream = new FileInputStream (scriptFileObject.getPath()); 110 Properties scriptProperties = new Properties (); 111 112 scriptProperties.load( scriptInputStream ); 113 114 Enumeration scriptPropertiesEnum = scriptProperties.propertyNames(); 115 HashMap scriptPropertiesHash = new HashMap (); 116 117 while(scriptPropertiesEnum.hasMoreElements()) 118 { 119 String scriptPropertyName = (String ) scriptPropertiesEnum.nextElement(); 120 try { 121 if(scriptPropertyName.startsWith("jahia.")) { 122 String scriptPropertyValue = scriptProperties.getProperty(scriptPropertyName); 123 if ("jahia.database.url".equals(scriptPropertyName)) { 124 int contextMarker = scriptPropertyValue.indexOf("$context/"); 126 if (contextMarker != -1) { 127 String rightOfContext = scriptPropertyValue.substring(contextMarker + "$context".length()); 129 String leftOfContext = scriptPropertyValue.substring(0, contextMarker); 130 scriptPropertyValue = leftOfContext + pathResolver.resolvePath(rightOfContext); 131 } 132 } 133 scriptPropertiesHash.put(scriptPropertyName, scriptPropertyValue); 134 } 135 } catch (IndexOutOfBoundsException ioobe) { 136 } 138 } 139 if(scriptPropertiesHash.size() == 7) { 140 scriptPropertiesHash.put("jahia.database.script", scriptFileObject.getName()); 141 } else { 142 logger.warn("Script " + scriptFileObject + " has invalid number of settings, ignoring..."); 143 } 144 145 String driverName = (String ) scriptPropertiesHash.get("jahia.database.driver"); 149 if (driverName != null) { 150 boolean loadedSuccessfully = false; 151 try { 152 Class.forName(driverName); 153 loadedSuccessfully = true; 154 } catch (ClassNotFoundException cfne) { 155 logger.debug("Database driver class " + driverName + " not found. Ignoring database script entry."); 156 } 157 158 if (loadedSuccessfully) { 159 logger.debug("Successfully loaded database setting from " + scriptFileObject); 160 scriptsInfosVector.add(scriptPropertiesHash); 161 } 162 } else { 163 logger.debug("No driver found for database script " + scriptFileObject + ", ignoring database entry."); 164 } 165 } 166 167 return scriptsInfosVector; 168 } 170 171 172 182 public Enumeration getDatabaseScriptsRuntime( File fileObject ) 183 throws IOException 184 { 185 Vector scriptsRuntimeVector = new Vector (); 186 187 BufferedReader buffered = new BufferedReader ( new FileReader (fileObject.getPath()) ); 188 String buffer = ""; 189 190 while((buffer = buffered.readLine()) != null) 191 { 192 if(buffer.trim().length() > 0) { 193 if(!buffer.trim().substring(0,1).equals("#")) { 194 try { 195 if(!buffer.trim().substring(0,6).equals("jahia.")) { 196 scriptsRuntimeVector.add(buffer); 197 } 198 } catch (IndexOutOfBoundsException ioobe) { 199 } 201 } 202 } 203 } 204 buffered.close(); 205 206 return scriptsRuntimeVector.elements(); 207 } 209 210 } | Popular Tags |