KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > admin > database > DatabaseScripts


1 // $Id: DatabaseScripts.java 10570 2005-09-13 09:35:40Z pvollenweider $
2
//
3
// ____.
4
// __/\ ______| |__/\. _______
5
// __ .____| | \ | +----+ \
6
// _______| /--| | | - \ _ | : - \_________
7
// \\______: :---| : : | : | \________>
8
// |__\---\_____________:______: :____|____:_____\
9
// /_____|
10
//
11
// . . . i n j a h i a w e t r u s t . . .
12
//
13
//
14
// DatabaseScripts
15
//
16
// 30.03.2001 AK added in jahia.
17
// 01.04.2001 AK change the package.
18
//
19

20 package org.jahia.admin.database;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import org.jahia.bin.Jahia;
33 import org.jahia.utils.PathResolver;
34
35
36
37 /**
38  * desc: This class is used by the installation and the administration
39  * to get all informations required from database scripts, like msaccess.script
40  * or hypersonic.script, from the jahia database script path (a jahiafiles
41  * subfolder).
42  *
43  * Copyright: Copyright (c) 2002
44  * Company: Jahia Ltd
45  *
46  * @author Alexandre Kraft
47  * @version 1.0
48  */

49 public class DatabaseScripts {
50
51     private static org.apache.log4j.Logger logger =
52             org.apache.log4j.Logger.getLogger(DatabaseScripts.class);
53
54     /**
55      * Default constructor.
56      * @author Alexandre Kraft
57      */

58     public DatabaseScripts()
59     {
60         // do nothing :o)
61
} // end constructor
62

63
64
65     /**
66      * Get an enumeration containing all the database scripts in File objects
67      * from the jahia database scripts path.
68      * @author Alexandre Kraft
69      *
70      * @return Enumeration containing all scripts in File objects.
71      */

72     public Enumeration JavaDoc getDatabaseScriptsFileObjects()
73     throws IOException JavaDoc
74     {
75         File JavaDoc scriptsFolderFileObject = new File JavaDoc( Jahia.jahiaDatabaseScriptsPath );
76         File JavaDoc[] scriptsListFileArray = scriptsFolderFileObject.listFiles();
77         Vector JavaDoc scriptsListVector = new Vector JavaDoc();
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     } // getDatabaseScriptsFileObjects
89

90
91
92     /**
93      * Get a vector containing all database scripts informations in four-entry
94      * HashMap's. These HashMap contains the script name, script username,
95      * script password and script driver for each database.
96      * @author Alexandre Kraft
97      *
98      * @param fileObjects Enumeration containing all scripts File objects.
99      * @return Vector containing all scripts infos in HashMap.
100      */

101     public Vector JavaDoc getDatabaseScriptsInfos( Enumeration JavaDoc fileObjects, PathResolver pathResolver )
102     throws IOException JavaDoc
103     {
104         Vector JavaDoc scriptsInfosVector = new Vector JavaDoc();
105
106         while(fileObjects.hasMoreElements())
107         {
108             File JavaDoc scriptFileObject = (File JavaDoc) fileObjects.nextElement();
109             FileInputStream JavaDoc scriptInputStream = new FileInputStream JavaDoc(scriptFileObject.getPath());
110             Properties JavaDoc scriptProperties = new Properties JavaDoc();
111
112             scriptProperties.load( scriptInputStream );
113
114             Enumeration JavaDoc scriptPropertiesEnum = scriptProperties.propertyNames();
115             HashMap JavaDoc scriptPropertiesHash = new HashMap JavaDoc();
116
117             while(scriptPropertiesEnum.hasMoreElements())
118             {
119                 String JavaDoc scriptPropertyName = (String JavaDoc) scriptPropertiesEnum.nextElement();
120                 try {
121                     if(scriptPropertyName.startsWith("jahia.")) {
122                         String JavaDoc scriptPropertyValue = scriptProperties.getProperty(scriptPropertyName);
123                         if ("jahia.database.url".equals(scriptPropertyName)) {
124                             // let's test if the URL contains a context clause
125
int contextMarker = scriptPropertyValue.indexOf("$context/");
126                             if (contextMarker != -1) {
127                                 // note : here below we DO leave the first slash in, this is not a bug.
128
String JavaDoc rightOfContext = scriptPropertyValue.substring(contextMarker + "$context".length());
129                                 String JavaDoc leftOfContext = scriptPropertyValue.substring(0, contextMarker);
130                                 scriptPropertyValue = leftOfContext + pathResolver.resolvePath(rightOfContext);
131                             }
132                         }
133                         scriptPropertiesHash.put(scriptPropertyName, scriptPropertyValue);
134                     }
135                 } catch (IndexOutOfBoundsException JavaDoc ioobe) {
136                     // this property-name length is smaller than 6 characters. do nothing.
137
}
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             // now let's see if we can load the database driver class. If
146
// we can we add the database settings to the list, otherwise we
147
// don't.
148
String JavaDoc driverName = (String JavaDoc) 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 JavaDoc 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     } // getDatabaseScriptsInfos
169

170
171
172     /**
173      * Get a enumeration containing all lines of the sql runtime from a
174      * database script. This database script is getted in parameter like
175      * a File object. The method use the BufferedReader object on a
176      * FileReader object instanciate on the script file name.
177      * @author Alexandre Kraft
178      *
179      * @param fileObject File object of the database script file.
180      * @return Enumeration containing all lines of the database script.
181      */

182     public Enumeration JavaDoc getDatabaseScriptsRuntime( File JavaDoc fileObject )
183     throws IOException JavaDoc
184     {
185         Vector JavaDoc scriptsRuntimeVector = new Vector JavaDoc();
186
187         BufferedReader JavaDoc buffered = new BufferedReader JavaDoc( new FileReader JavaDoc(fileObject.getPath()) );
188         String JavaDoc 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 JavaDoc ioobe) {
199                         // this property-name length is smaller than 6 characters. do nothing.
200
}
201                 }
202             }
203         }
204         buffered.close();
205
206         return scriptsRuntimeVector.elements();
207     } // getDatabaseScriptsRuntime
208

209
210 } // end DatabaseScripts
211
Popular Tags