KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > install > RepositoryServerInit


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.install;
17
18 import java.io.File JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import org.apache.commons.cli.CommandLine;
28 import org.apache.commons.cli.CommandLineParser;
29 import org.apache.commons.cli.Option;
30 import org.apache.commons.cli.Options;
31 import org.apache.commons.cli.PosixParser;
32 import org.jaxen.dom.DOMXPath;
33 import org.jaxen.SimpleVariableContext;
34 import org.outerj.daisy.install.InstallHelper.DatabaseInfo;
35 import org.outerj.daisy.install.InstallHelper.DatabaseParams;
36 import org.outerj.daisy.util.Gpw;
37 import org.outerj.daisy.util.VersionHelper;
38 import org.w3c.dom.*;
39
40 public class RepositoryServerInit {
41     private String JavaDoc bootstrapUser = "testuser";
42     private String JavaDoc bootstrapUserPassword = "testuser";
43     private String JavaDoc internalUserPassword = "defaultpwd";
44     private String JavaDoc smtpServer = "undefined";
45     private String JavaDoc mailFrom = "undefined@undefined._com";
46     private File JavaDoc daisyHome;
47     private File JavaDoc daisyDataDir;
48     private InstallHelper.DatabaseParams dbParams;
49     private boolean advancedMode;
50
51     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
52         new RepositoryServerInit().install(args);
53     }
54
55     private void install(String JavaDoc[] args) throws Exception JavaDoc {
56         Options options = new Options();
57         options.addOption(new Option("a", "advanced", false, "Advanced mode (asks more questions)"));
58         options.addOption(new Option("c", "conf", true, "Configuration file for automated install"));
59         options.addOption(new Option("v", "version", false, "Print version info"));
60
61         CommandLineParser parser = new PosixParser();
62         CommandLine cmd = parser.parse(options, args);
63
64         if (cmd.hasOption('v')) {
65             System.out.println(VersionHelper.getVersionString(getClass().getClassLoader(), "org/outerj/daisy/install/versioninfo.properties"));
66             System.exit(0);
67         }
68
69         advancedMode = cmd.hasOption("a") || InstallHelper.isDevelopmentSetup();
70
71         if (cmd.hasOption("c")) {
72             FileInputStream JavaDoc is = null;
73             try {
74                 is = new FileInputStream JavaDoc(cmd.getOptionValue("c"));
75                 Properties JavaDoc props = new Properties JavaDoc();
76                 props.load(is);
77                 runAutomatedInstall(props);
78             } finally {
79                 if (is != null)
80                     is.close();
81                 else
82                     System.out.println("Could not open " + cmd.getOptionValue("c") + " exiting ...");
83             }
84         } else {
85             initDatabase();
86
87             // In development setup, the data directory does not need to be created,
88
// nor does ActiveMQ need to be configured
89
if (!InstallHelper.isDevelopmentSetup()) {
90                 InstallHelper.printTitle("Daisy data directory creation.");
91                 System.out.println();
92                 System.out.println("The Daisy data directory is a directory in which the repository server");
93                 System.out.println("stores blob data, full text indexes, log files and configuration files.");
94                 System.out.println();
95                 System.out.println("Note: through custom configuration of the repository server, each of");
96                 System.out.println(" these items can be put in any location you like, it is only for");
97                 System.out.println(" your convenience that they are by default grouped into one directory.");
98                 System.out.println();
99                 System.out.println("If you let this program now create such a data directory, it will also");
100                 System.out.println("write the correct configuration based on the database parameters entered");
101                 System.out.println("earlier and other parameters entered below.");
102                 System.out.println();
103
104                 daisyHome = InstallHelper.getDaisyHome();
105
106                 boolean createDataDir = true;
107                 if (advancedMode) {
108                     System.out.println();
109                     createDataDir = InstallHelper.promptYesNo("Do you want to create a Daisy data directory now? [ yes/no, default: yes]", true);
110                 }
111
112                 if (createDataDir) {
113                     promptMailParams();
114                     userCreateDaisyDataDirectory();
115                 }
116
117                 jmsConfiguration(null, null);
118             }
119
120
121             System.out.println();
122             System.out.println("Finished.");
123         }
124     }
125
126     private void initDatabase() throws Exception JavaDoc {
127         InstallHelper.printTitle("Database initialisation");
128         System.out.println();
129         System.out.println("This part of the installation will create the database tables for the repository server.");
130         System.out.println();
131         System.out.println("First we need some connection parameters for the database. If the presented default values");
132         System.out.println("are fine, you can just press enter each time.");
133         System.out.println();
134         DatabaseInfo dbInfo = InstallHelper.chooseDatabase();
135         dbParams = InstallHelper.collectDatabaseParams(dbInfo, "daisy", "daisy", "daisyrepository");
136
137         System.out.println();
138         System.out.println("Now enter a name and password for an initial Daisy user.");
139         System.out.println("You'll need this user name and password to log in to Daisy,");
140         System.out.println("so be sure to remember these values.");
141         System.out.println();
142         bootstrapUser = InstallHelper.prompt("Enter username [default = " + bootstrapUser + "] : ", bootstrapUser);
143         bootstrapUserPassword = InstallHelper.prompt("Enter password [default = " + bootstrapUserPassword + "] : ", bootstrapUserPassword);
144         internalUserPassword = InstallHelper.isDevelopmentSetup() ? "defaultpwd" : InstallHelper.generatePassword();
145
146         if (advancedMode) {
147             System.out.println();
148             System.out.println("A user called 'internal' will be created which is used by various");
149             System.out.println("components inside the repository server to connect to the repository,");
150             System.out.println("as well as by the repository client in Cocoon to fill its caches.");
151             System.out.println("The default password presented below is secure-random generated.");
152             System.out.println("This password will be written in a configuration file, you do not");
153             System.out.println("need to remember it, so it does not matter it is complex.");
154             System.out.println();
155             internalUserPassword = InstallHelper.prompt("Enter password for user 'internal' [ default = " + internalUserPassword + " ] : ", internalUserPassword);
156         }
157
158         System.out.println();
159         dbParams.checkDatabase();
160         System.out.println();
161
162         boolean goOn = InstallHelper.promptYesNo("The database will now be created, continue? [default = yes] : ", true);
163         if (!goOn) {
164             System.exit(1);
165         }
166
167         System.out.println();
168         System.out.println("Please wait while creating the database...");
169         DatabaseCreator dbCreator = new DatabaseCreator();
170         dbCreator.run(dbParams.getUrl(), dbParams.getUser(), dbParams.getPassword(),
171                 bootstrapUser, bootstrapUserPassword, "internal", internalUserPassword);
172         System.out.println();
173         System.out.println("Finished creating database.");
174         System.out.println();
175     }
176
177     private void promptMailParams() throws Exception JavaDoc {
178         System.out.println();
179         System.out.println("If you want Daisy to be able to send out notification or registration mails,");
180         System.out.println("you need to provide an SMTP server.");
181         System.out.println();
182         smtpServer = InstallHelper.prompt("Enter SMTP server [ default = " + smtpServer + " ] : ", smtpServer);
183         mailFrom = InstallHelper.prompt("Enter \"from address\" for e-mails [ default = " + mailFrom + " ] : ", mailFrom);
184         System.out.println();
185     }
186
187     private void userCreateDaisyDataDirectory() throws Exception JavaDoc {
188         System.out.println();
189         System.out.println("Daisy data directory location:");
190         System.out.println("The Daisy data directory path you enter should be a non-existing or empty");
191         System.out.println("directory. For example, enter c:\\daisydata or /home/<someuser>/daisydata");
192         System.out.println();
193         daisyDataDir = InstallHelper.promptForEmptyDir("Enter the path of the data directory to create:", null);
194
195         createDaisyDataDirectory();
196     }
197
198     private void createDaisyDataDirectory() throws Exception JavaDoc {
199         daisyDataDir.mkdirs();
200
201         System.out.println("Will create Daisy data directory at:");
202         System.out.println(daisyDataDir.getAbsolutePath());
203
204         File JavaDoc blobStoreDir = new File JavaDoc(daisyDataDir, "blobstore");
205         blobStoreDir.mkdir();
206         File JavaDoc indexStoreDir = new File JavaDoc(daisyDataDir, "indexstore");
207         indexStoreDir.mkdir();
208         File JavaDoc pubReqsDir = new File JavaDoc(daisyDataDir, "pubreqs");
209         pubReqsDir.mkdir();
210         File JavaDoc logDir = new File JavaDoc(daisyDataDir, "logs");
211         logDir.mkdir();
212         File JavaDoc confDir = new File JavaDoc(daisyDataDir, "conf");
213         confDir.mkdir();
214
215         String JavaDoc sep = File.separator;
216
217         // create merlin config override file
218
File JavaDoc repoServerDir = new File JavaDoc(daisyHome, "repository-server");
219         File JavaDoc repoServerConfDir = new File JavaDoc(repoServerDir, "conf");
220         File JavaDoc repoServerConfigTemplate = new File JavaDoc(repoServerConfDir, "myconfig.xml.template");
221         File JavaDoc repoServerConfig = new File JavaDoc(confDir, "myconfig.xml");
222
223         InstallHelper.copyFile(repoServerConfigTemplate, repoServerConfig);
224         Document repoServerConfigDoc = InstallHelper.parseFile(repoServerConfig);
225
226         // configure blobstore, indexstore and pubreqs directories
227
DOMXPath blobstoreXPath = new DOMXPath("/targets/target[@path='/daisy/repository/blobstore']/configuration/directory");
228         Element blobStoreEl = (Element)blobstoreXPath.selectSingleNode(repoServerConfigDoc);
229         InstallHelper.setElementValue(blobStoreEl, "${daisy.datadir}" + sep + "blobstore");
230
231         DOMXPath indexstoreXPath = new DOMXPath("/targets/target[@path='/daisy/repository/fullTextIndex']/configuration/indexDirectory");
232         Element indexStoreEl = (Element)indexstoreXPath.selectSingleNode(repoServerConfigDoc);
233         InstallHelper.setElementValue(indexStoreEl, "${daisy.datadir}" + sep + "indexstore");
234
235         DOMXPath pubreqsXPath = new DOMXPath("/targets/target[@path='/daisy/extensions/publisher/publisher']/configuration/publisherRequestDirectory");
236         Element pubreqsEl = (Element)pubreqsXPath.selectSingleNode(repoServerConfigDoc);
237         InstallHelper.setElementValue(pubreqsEl, "${daisy.datadir}" + sep + "pubreqs");
238
239         // update passwords for internal user
240
DOMXPath repositoryUserXPath = new DOMXPath("//repositoryUser");
241         List JavaDoc repositoryUserEls = repositoryUserXPath.selectNodes(repoServerConfigDoc);
242         Iterator JavaDoc repositoryUserElsIt = repositoryUserEls.iterator();
243         while (repositoryUserElsIt.hasNext()) {
244             Element el = (Element)repositoryUserElsIt.next();
245             el.setAttribute("password", internalUserPassword);
246         }
247
248         // update driver registrar params
249
addJdbcDriverToConf(repoServerConfigDoc, dbParams);
250
251         // update db params
252
DOMXPath dataSourceConfXPath = new DOMXPath("/targets/target[@path='/daisy/datasource/datasource']/configuration");
253         DOMXPath dbUserNameXPath = new DOMXPath("username");
254         DOMXPath dbPasswordXPath = new DOMXPath("password");
255         DOMXPath dbUrlXPath = new DOMXPath("url");
256         Element dataSourceConfEl = (Element)dataSourceConfXPath.selectSingleNode(repoServerConfigDoc);
257         Element dbUserNameEl = (Element)dbUserNameXPath.selectSingleNode(dataSourceConfEl);
258         InstallHelper.setElementValue(dbUserNameEl, dbParams.getUser());
259         Element dbPasswordEl = (Element)dbPasswordXPath.selectSingleNode(dataSourceConfEl);
260         InstallHelper.setElementValue(dbPasswordEl, dbParams.getPassword());
261         Element dbUrlEl = (Element)dbUrlXPath.selectSingleNode(dataSourceConfEl);
262         InstallHelper.setElementValue(dbUrlEl, dbParams.getUrl());
263
264
265         // update mail params
266
DOMXPath smtpXPath = new DOMXPath("/targets/target[@path='/daisy/emailer/emailer']/configuration/smtpHost");
267         Element smtpEl = (Element)smtpXPath.selectSingleNode(repoServerConfigDoc);
268         InstallHelper.setElementValue(smtpEl, smtpServer);
269
270         DOMXPath mailFromXPath = new DOMXPath("/targets/target[@path='/daisy/emailer/emailer']/configuration/fromAddress");
271         Element mailFromEl = (Element)mailFromXPath.selectSingleNode(repoServerConfigDoc);
272         InstallHelper.setElementValue(mailFromEl, mailFrom);
273
274         // set mbean server password to something random
275
String JavaDoc jmxPwd = Gpw.generate(8);
276         DOMXPath mbeanHttpXPath = new DOMXPath("/targets/target[@path='/daisy/jmx/mbeanserver']/configuration/httpAdaptor");
277         Element mbeanHttpEl = (Element)mbeanHttpXPath.selectSingleNode(repoServerConfigDoc);
278         mbeanHttpEl.setAttribute("password", jmxPwd);
279         DOMXPath mbeanXmlHttpXPath = new DOMXPath("/targets/target[@path='/daisy/jmx/mbeanserver']/configuration/xmlHttpAdaptor");
280         Element mbeanXmlHttpEl = (Element)mbeanXmlHttpXPath.selectSingleNode(repoServerConfigDoc);
281         mbeanXmlHttpEl.setAttribute("password", jmxPwd);
282
283
284
285         InstallHelper.saveDocument(repoServerConfig, repoServerConfigDoc);
286
287         // copy log configuration
288
File JavaDoc logConfigTemplate = new File JavaDoc(repoServerConfDir, "logkit.xml");
289         File JavaDoc logConfig = new File JavaDoc(confDir, "logkit.xml");
290         InstallHelper.copyFile(logConfigTemplate, logConfig);
291
292         // create sample in pubreqs directory
293
File JavaDoc samplePubReqs = new File JavaDoc(pubReqsDir, "sample");
294         samplePubReqs.mkdirs();
295         InstallHelper.copyStream(getClass().getClassLoader().getResourceAsStream("org/outerj/daisy/install/pubreqs-sample/README.txt"), new File JavaDoc(pubReqsDir, "README.txt"));
296         InstallHelper.copyStream(getClass().getClassLoader().getResourceAsStream("org/outerj/daisy/install/pubreqs-sample/default.xml"), new File JavaDoc(samplePubReqs, "default.xml"));
297         InstallHelper.copyStream(getClass().getClassLoader().getResourceAsStream("org/outerj/daisy/install/pubreqs-sample/mapping.xml"), new File JavaDoc(samplePubReqs, "mapping.xml"));
298     }
299
300     /**
301      * Add the JDBC driver in the config to the list of drivers to be loaded.
302      *
303      * @return true if config changed, false if not (= when driver was already present)
304      */

305     private boolean addJdbcDriverToConf(Document repoServerConfigDoc, DatabaseParams dbParams) throws Exception JavaDoc {
306         DOMXPath driversXPath = new DOMXPath("/targets/target[@path='/daisy/driverregistrar/driverregistrar']/configuration/drivers");
307         Element driversEl = (Element)driversXPath.selectSingleNode(repoServerConfigDoc);
308
309         // Check if driver is already present
310
// Note: classname and classpath are inserted in the xpath expression via variables, since
311
// XPath does not provide a way to escape single or double quotes in strings
312
SimpleVariableContext vars = new SimpleVariableContext();
313         vars.setVariableValue("classname", dbParams.getDriverClassName());
314         vars.setVariableValue("classpath", dbParams.getDriverClassPath());
315         DOMXPath driverPresentXPath = new DOMXPath("count(driver[classname = $classname and classpath = $classpath]) > 0");
316         driverPresentXPath.setVariableContext(vars);
317         boolean driverPresent = driverPresentXPath.booleanValueOf(driversEl);
318         if (driverPresent)
319             return false;
320
321         Element driverEl = repoServerConfigDoc.createElement("driver");
322         Element driverClassNameEl = repoServerConfigDoc.createElement("classname");
323         InstallHelper.setElementValue(driverClassNameEl, dbParams.getDriverClassName());
324         Element driverClasspathEl = repoServerConfigDoc.createElement("classpath");
325         InstallHelper.setElementValue(driverClasspathEl, dbParams.getDriverClassPath());
326         driverEl.appendChild(driverClassNameEl);
327         driverEl.appendChild(driverClasspathEl);
328         driversEl.appendChild(driverEl);
329
330         addLineBreaks(driversEl);
331
332         return true;
333     }
334
335     private void updateOthers() throws Exception JavaDoc {
336         //
337
// Update cocoon.xconf
338
//
339
boolean updateCocoonXconf = true;
340         if (advancedMode) {
341             updateCocoonXconf = InstallHelper.promptYesNo("Shall I update the cocoon.xconf of the Daisy Wiki too?", true);
342         }
343         if (updateCocoonXconf) {
344             System.out.println();
345             System.out.println("Will now update the Daisy Wiki's cocoon.xconf file with the internal user password.");
346             System.out.println();
347
348             File JavaDoc xconfFile = new File JavaDoc(new File JavaDoc(new File JavaDoc(new File JavaDoc(daisyHome, "daisywiki"), "webapp"), "WEB-INF"), "cocoon.xconf");
349             if (!xconfFile.exists()) {
350                 System.out.println("Did not find cocoon.xconf at " + xconfFile.getAbsolutePath() + ", will skip updating it.");
351             } else {
352                 Document xconfDoc = InstallHelper.parseFile(xconfFile);
353                 DOMXPath cacheUserXPath = new DOMXPath("/cocoon/component[@class='org.outerj.daisy.repository.clientimpl.RemoteRepositoryManager']/cacheUser");
354                 Element credentialsEl = (Element)cacheUserXPath.selectSingleNode(xconfDoc);
355                 credentialsEl.setAttribute("password", internalUserPassword);
356                 System.out.println("Will now save the updated cocoon.xconf");
357                 InstallHelper.saveDocument(xconfFile, xconfDoc);
358             }
359         }
360     }
361
362     private void runAutomatedInstall(Properties JavaDoc props) throws Exception JavaDoc {
363         this.dbParams = new InstallHelper.DatabaseParams(
364                 InstallHelper.getPropertyValue(props,InitialisationProperties.DB_URL),
365                 InstallHelper.getPropertyValue(props,InitialisationProperties.DB_USER),
366                 InstallHelper.getPropertyValue(props,InitialisationProperties.DB_PASSWORD),
367                 InstallHelper.getPropertyValue(props,InitialisationProperties.DRIVER_CLASS),
368                 InstallHelper.getPropertyValue(props,InitialisationProperties.DRIVER_CLASSPATH));
369         dbParams.loadDriver();
370
371         DatabaseParams jmsDbParams = new InstallHelper.DatabaseParams(
372                 InstallHelper.getPropertyValue(props,InitialisationProperties.JMS_DB_URL),
373                 InstallHelper.getPropertyValue(props,InitialisationProperties.JMS_DB_USER),
374                 InstallHelper.getPropertyValue(props,InitialisationProperties.JMS_DB_PASSWORD),
375                 InstallHelper.getPropertyValue(props,InitialisationProperties.DRIVER_CLASS),
376                 InstallHelper.getPropertyValue(props,InitialisationProperties.DRIVER_CLASSPATH));
377
378         this.bootstrapUser = InstallHelper.getPropertyValue(props,InitialisationProperties.INITIAL_USER_LOGIN);
379         this.bootstrapUserPassword = InstallHelper.getPropertyValue(props,InitialisationProperties.INITIAL_USER_PASSWORD);
380         this.internalUserPassword = InstallHelper.getPropertyValue(props, InitialisationProperties.INTERNAL_USER_PASSWORD, InstallHelper.generatePassword());
381         this.smtpServer = InstallHelper.getPropertyValue(props,InitialisationProperties.SMTP_SERVER);
382         this.mailFrom = InstallHelper.getPropertyValue(props,InitialisationProperties.MAIL_FROM_ADDRESS);
383         this.daisyDataDir = new File JavaDoc (InstallHelper.getPropertyValue(props,InitialisationProperties.DATA_DIR_LOCATION));
384
385
386         daisyHome = InstallHelper.getDaisyHome();
387         DatabaseCreator dbCreator = new DatabaseCreator();
388         dbCreator.run(dbParams.getUrl(), dbParams.getUser(), dbParams.getPassword(),
389                 bootstrapUser, bootstrapUserPassword, "internal", internalUserPassword);
390         createDaisyDataDirectory();
391
392         jmsConfiguration(jmsDbParams, InstallHelper.getPropertyValue(props,InitialisationProperties.JMS_ADMIN_PASSWORD, Gpw.generate(8)) );
393     }
394
395     private void jmsConfiguration(DatabaseParams jmsDbParams, String JavaDoc jmsAdminPassword) throws Exception JavaDoc {
396         System.out.println();
397         System.out.println("===========================================================================");
398         System.out.println();
399         System.out.println("Configuration of embedded JMS service (ActiveMQ)");
400         System.out.println();
401         File JavaDoc jmsConfig = new File JavaDoc(daisyHome, "repository-server" + File.separator + "conf" + File.separator + "activemq-conf.xml.template");
402         File JavaDoc jmsConfigDest = new File JavaDoc(daisyDataDir, "conf" + File.separator + "activemq-conf.xml");
403
404         if (jmsDbParams == null) {
405             InstallHelper.DatabaseInfo dbInfo = InstallHelper.chooseDatabase();
406             jmsDbParams = InstallHelper.collectDatabaseParams(dbInfo, "activemq", "activemq", "activemq");
407         }
408
409         if (jmsAdminPassword == null)
410             jmsAdminPassword = Gpw.generate(8);
411
412         if (jmsAdminPassword == null && advancedMode) {
413             System.out.println();
414             System.out.println("I need a password for the JMS user. The default password suggested below is");
415             System.out.println("random generated.");
416             System.out.println();
417             jmsAdminPassword = InstallHelper.prompt("Enter a password for the ActiveMQ user [default = " + jmsAdminPassword + "]: ", jmsAdminPassword);
418         }
419
420         // Copy configuration files
421
InstallHelper.copyFile(new File JavaDoc(jmsConfig.getParentFile(), "login.config"), new File JavaDoc(jmsConfigDest.getParentFile(), "/login.config"));
422         InstallHelper.copyFile(new File JavaDoc(jmsConfig.getParentFile(), "groups.properties"), new File JavaDoc(jmsConfigDest.getParentFile(), "/groups.properties"));
423         InstallHelper.copyFile(new File JavaDoc(jmsConfig.getParentFile(), "users.properties"), new File JavaDoc(jmsConfigDest.getParentFile(), "/users.properties"));
424
425         // Update activemq configuration
426
Document configDoc = InstallHelper.parseFile(jmsConfig);
427
428         Element dbElement = configDoc.createElementNS("http://activemq.org/config/1.0", "bean");
429         dbElement.setAttribute("id", "dataSource");
430         dbElement.setAttribute("class", "org.apache.commons.dbcp.BasicDataSource");
431         dbElement.setAttribute("destroy-method", "close");
432
433         String JavaDoc activeMQJdbcURL = jmsDbParams.getUrl();
434         if (activeMQJdbcURL.startsWith("jdbc:mysql:")) {
435             String JavaDoc extraParams = "relaxAutoCommit=true&sessionVariables=storage_engine=InnoDB";
436             if (activeMQJdbcURL.indexOf('?') != -1)
437                 activeMQJdbcURL += "&" + extraParams;
438             else
439                 activeMQJdbcURL += "?" + extraParams;
440         }
441         dbElement.appendChild(createPropertyElement("url", activeMQJdbcURL, configDoc));
442         dbElement.appendChild(createPropertyElement("username", jmsDbParams.getUser(), configDoc));
443         dbElement.appendChild(createPropertyElement("password", jmsDbParams.getPassword(), configDoc));
444         dbElement.appendChild(createPropertyElement("defaultTransactionIsolation", "2", configDoc));
445         dbElement.appendChild(createPropertyElement("poolPreparedStatements", "true", configDoc));
446         addLineBreaks(dbElement);
447
448         DOMXPath dbConfigXPath = new DOMXPath("/amq:beans/amq:bean[@id='dataSource']");
449         dbConfigXPath.addNamespace("amq", "http://activemq.org/config/1.0");
450         Element oldDbConfig = (Element)dbConfigXPath.selectSingleNode(configDoc);
451         oldDbConfig.getParentNode().replaceChild(dbElement, oldDbConfig);
452
453         /* Configures the storage path for the ActiveMQ journal, currently not used.
454         DOMXPath jdbcAdapterPath = new DOMXPath("/amq:beans/amq:broker/amq:persistenceAdapter/amq:journaledJDBC");
455         jdbcAdapterPath.addNamespace("amq", "http://activemq.org/config/1.0");
456         Element jdbcAdapterEl = (Element)jdbcAdapterPath.selectSingleNode(configDoc);
457         jdbcAdapterEl.getAttributeNode("dataDirectory").setValue(daisyDataDir.getAbsolutePath() + "/activemq");
458         */

459
460         InstallHelper.saveDocument(jmsConfigDest, configDoc);
461
462         // Update users.properties
463
File JavaDoc usersFile = new File JavaDoc(jmsConfigDest.getParentFile(), "users.properties");
464         Properties JavaDoc activeMqUsers = new Properties JavaDoc();
465         activeMqUsers.put("admin", jmsAdminPassword);
466         storeProperties(activeMqUsers, "Users for ActiveMQ", usersFile);
467
468         // Update myconfig.xml
469
System.out.println("Updating the myconfig.xml with the JMS settings.");
470         File JavaDoc repoServerConfig = new File JavaDoc(daisyDataDir, "conf" + File.separator + "myconfig.xml");
471         Document repoServerConfigDoc = InstallHelper.parseFile(repoServerConfig);
472
473         DOMXPath credentialsXPath = new DOMXPath("/targets/target[@path='/daisy/jmsclient/jmsclient']/configuration/jmsConnection/credentials");
474         Element credentialsEl = (Element)credentialsXPath.selectSingleNode(repoServerConfigDoc);
475         credentialsEl.setAttribute("password", jmsAdminPassword);
476
477         DOMXPath amqConfXPath = new DOMXPath("/targets/target[@path='/daisy/jmsclient/jmsclient']/configuration/jmsConnection/initialContext/property[@name='java.naming.provider.url']/@value");
478         Node amqConfEl = (Node)amqConfXPath.selectSingleNode(repoServerConfigDoc);
479         amqConfEl.setNodeValue("vm://DaisyJMS?brokerConfig=${double-url-encode:xbean:${file-to-uri:${daisy.datadir}/conf/activemq-conf.xml}}");
480
481         addJdbcDriverToConf(repoServerConfigDoc, jmsDbParams);
482
483         System.out.println("Will now save the updated myconfig.xml");
484         InstallHelper.saveDocument(repoServerConfig, repoServerConfigDoc);
485     }
486
487     private Element createPropertyElement (String JavaDoc name, String JavaDoc value, Document doc) {
488         Element el = doc.createElementNS("http://activemq.org/config/1.0", "property");
489         el.setAttribute("name", name);
490         el.setAttribute("value", value);
491         return el;
492     }
493
494     private void storeProperties(Properties JavaDoc properties, String JavaDoc comment, File JavaDoc file) throws Exception JavaDoc {
495         try {
496             OutputStream JavaDoc os = null;
497             try {
498                 os = new FileOutputStream JavaDoc(file);
499                 properties.store(os, comment);
500             } finally {
501                 if (os != null)
502                     os.close();
503             }
504         } catch (IOException JavaDoc e) {
505             throw new Exception JavaDoc("Error storing properties to " + file.getAbsolutePath(), e);
506         }
507     }
508
509     private void addLineBreaks(Element element) {
510         boolean lastWasTextNode = false;
511         Node node = element.getFirstChild();
512         while (node != null) {
513             if (node instanceof Element) {
514                 if (!lastWasTextNode) {
515                     Node text = element.getOwnerDocument().createTextNode("\n");
516                     element.insertBefore(text, node);
517                 }
518                 addLineBreaks((Element)node);
519             }
520             lastWasTextNode = node instanceof Text;
521             node = node.getNextSibling();
522         }
523
524         if (!lastWasTextNode && element.getFirstChild() != null) {
525             Node text = element.getOwnerDocument().createTextNode("\n");
526             element.appendChild(text);
527         }
528     }
529
530     private class InitialisationProperties {
531
532         public static final String JavaDoc DRIVER_CLASS = "driverClass";
533         public static final String JavaDoc DRIVER_CLASSPATH = "driverClassPath";
534         public static final String JavaDoc DB_URL = "dbUrl";
535         public static final String JavaDoc DB_USER = "dbUser";
536         public static final String JavaDoc DB_PASSWORD = "dbUserPassword";
537         public static final String JavaDoc INITIAL_USER_LOGIN = "initialUserLogin";
538         public static final String JavaDoc INITIAL_USER_PASSWORD = "initialUserPassword";
539         public static final String JavaDoc INTERNAL_USER_PASSWORD = "internalUserPassword";
540         public static final String JavaDoc SMTP_SERVER = "smtpServer";
541         public static final String JavaDoc MAIL_FROM_ADDRESS = "mailFromAddress";
542         public static final String JavaDoc DATA_DIR_LOCATION = "dataDirLocation";
543
544         public static final String JavaDoc JMS_DB_URL = "jmsDbUrl";
545         public static final String JavaDoc JMS_DB_USER = "jmsDbUser";
546         public static final String JavaDoc JMS_DB_PASSWORD = "jmsDbUserPassword";
547         public static final String JavaDoc JMS_ADMIN_PASSWORD = "jmsAdminPassword";
548     }
549 }
550
Popular Tags