001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.gast.lib;
025:
026: import java.io.File;
027: import java.io.FileNotFoundException;
028: import java.io.IOException;
029: import java.util.ArrayList;
030: import java.util.List;
031: import org.fao.geonet.util.McKoiDB;
032:
033: //=============================================================================
034:
035: public class EmbeddedDBLib {
036: //---------------------------------------------------------------------------
037: //---
038: //--- Constructor
039: //---
040: //---------------------------------------------------------------------------
041:
042: public EmbeddedDBLib(String appPath) throws IOException {
043: this .appPath = appPath;
044:
045: lines = Lib.text.load(appPath + MCKOI_CONFIG);
046: }
047:
048: //---------------------------------------------------------------------------
049: //---
050: //--- API methods
051: //---
052: //---------------------------------------------------------------------------
053:
054: public String getPort() {
055: return Lib.text.getProperty(lines, "jdbc_server_port");
056: }
057:
058: //---------------------------------------------------------------------------
059:
060: public String getUser() {
061: try {
062: List<String> lines = Lib.text.load(appPath + MCKOI_ACCOUNT);
063:
064: return Lib.text.getProperty(lines, "username");
065: } catch (IOException e) {
066: return null;
067: }
068: }
069:
070: //---------------------------------------------------------------------------
071:
072: public String getPassword() {
073: try {
074: List<String> lines = Lib.text.load(appPath + MCKOI_ACCOUNT);
075:
076: return Lib.text.getProperty(lines, "password");
077: } catch (IOException e) {
078: return null;
079: }
080: }
081:
082: //---------------------------------------------------------------------------
083:
084: public void setPort(String port) {
085: Lib.text.setProperty(lines, "jdbc_server_port", port);
086: }
087:
088: //---------------------------------------------------------------------------
089:
090: public void save() throws FileNotFoundException, IOException {
091: Lib.text.save(appPath + MCKOI_CONFIG, lines);
092: }
093:
094: //---------------------------------------------------------------------------
095:
096: public void createDB() throws Exception {
097: //--- first : remove old files
098:
099: Lib.io.cleanDir(new File(appPath + MCKOI_DATA));
100:
101: //--- second : generate a new random account
102:
103: String user = Lib.text.getRandomString(8);
104: String pass = Lib.text.getRandomString(8);
105:
106: //--- third : create database files
107:
108: String dbPath = appPath + MCKOI_CONFIG;
109:
110: McKoiDB mcKoi = new McKoiDB();
111: mcKoi.setConfigFile(dbPath);
112:
113: mcKoi.create(user, pass);
114:
115: //--- fourth : save it to a file
116:
117: ArrayList<String> al = new ArrayList<String>();
118: al.add("#--- DO NOT EDIT : file automatically generated");
119: al.add("username=" + user);
120: al.add("password=" + pass);
121:
122: Lib.text.save(appPath + MCKOI_ACCOUNT, al);
123: }
124:
125: //---------------------------------------------------------------------------
126: //---
127: //--- Variables
128: //---
129: //---------------------------------------------------------------------------
130:
131: private String appPath;
132: private List<String> lines;
133:
134: private static final String MCKOI_CONFIG = "/web/geonetwork/WEB-INF/db/db.conf";
135: private static final String MCKOI_ACCOUNT = "/web/geonetwork/WEB-INF/db/account.prop";
136: private static final String MCKOI_DATA = "/web/geonetwork/WEB-INF/db/data";
137: }
138:
139: //=============================================================================
|