001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.core;
025:
026: import java.io.*;
027: import java.util.*;
028: import java.lang.reflect.*;
029:
030: import org.myoodb.*;
031: import org.myoodb.util.*;
032: import org.myoodb.tools.*;
033: import org.myoodb.exception.*;
034:
035: import java.lang.reflect.Method;
036:
037: public final class MyOodbMain {
038: private static final org.myoodb.util.Logger LOGGER = org.myoodb.util.Logger
039: .getLogger(MyOodbMain.class);
040:
041: private static volatile boolean s_stopped = false;
042: private static volatile boolean m_checkForStaleMemoryFlag = true;
043:
044: private static void setupShutdownHook() throws Exception {
045: Method hookMethod = Runtime.class.getMethod("addShutdownHook",
046: new Class[] { Thread.class });
047: hookMethod.invoke(Runtime.getRuntime(),
048: new Object[] { createShutdownHook() });
049: }
050:
051: private static Runnable createShutdownHook() {
052: Thread shutdownHook = new Thread() {
053: public void run() {
054: if (MyOodbManager.getTheManager().getShuttingdownFlag() == false) {
055: try {
056: MyOodbManager.getTheManager().setExitingFlag(
057: true);
058:
059: while (s_stopped == false) {
060: sleep(1000);
061: }
062: } catch (InterruptedException e) {
063: LOGGER.error(null, e);
064: }
065: }
066: }
067: };
068:
069: return shutdownHook;
070: }
071:
072: public static void setCheckForStaleMemoryFlag(boolean flag) {
073: m_checkForStaleMemoryFlag = flag;
074: }
075:
076: public static boolean getCheckForStaleMemoryFlag() {
077: return m_checkForStaleMemoryFlag;
078: }
079:
080: public static void main(String[] args) throws Exception
081: {
082: int tcpPort = 54321;
083: int tcpsPort = -1;
084:
085: int udpPort = -1;
086: int udpsPort = -1;
087:
088: String webconfig = null;
089: String backupDir = null;
090: String restoreDir = null;
091: String username = "admin";
092: String password = "admin";
093:
094: boolean helpFlag = false;
095: boolean dbCreateFlag = false;
096: boolean dbVerifyFlag = false;
097: boolean dbDefragFlag = false;
098: boolean storeAsXmlFlag = false;
099:
100: String dbdir = File.separator + "tmp" + File.separator + "db";
101:
102: for (int i = 0; i < args.length; i++)
103: {
104: if (args[i].startsWith("-create"))
105: {
106: if (org.myoodb.core.FileHelper.getMemoryResidentFlag() == true)
107: {
108: throw new PermissionException("File System not supported for Memory Resident Database");
109: }
110:
111: dbCreateFlag = true;
112: }
113: else if (args[i].startsWith("-dbdir="))
114: {
115: dbdir = args[i].substring(7);
116: }
117: else if (args[i].startsWith("-admin="))
118: {
119: String usernamepassword = args[i].substring(7);
120: String[] tokens = usernamepassword.split(":");
121: username = tokens[0];
122: password = tokens[1];
123: }
124: else if (args[i].startsWith("-xdb"))
125: {
126: storeAsXmlFlag = true;
127: }
128: else if (args[i].startsWith("-sql="))
129: {
130: throw new PermissionException("Not Implemented");
131: }
132: else if (args[i].startsWith("-tcpPort="))
133: {
134: tcpPort = Integer.parseInt(args[i].substring(9));
135: }
136: else if (args[i].startsWith("-tcpsPort="))
137: {
138: tcpsPort = Integer.parseInt(args[i].substring(10));
139: }
140: else if (args[i].startsWith("-udpPort="))
141: {
142: udpPort = Integer.parseInt(args[i].substring(9));
143: }
144: else if (args[i].startsWith("-udpsPort="))
145: {
146: throw new PermissionException("Not Implemented");
147: }
148: else if (args[i].startsWith("-webconfig="))
149: {
150: webconfig = args[i].substring(11);
151: }
152: else if (args[i].startsWith("-backup="))
153: {
154: backupDir = args[i].substring(8);
155: }
156: else if (args[i].startsWith("-restore="))
157: {
158: restoreDir = args[i].substring(9);
159: }
160: else if (args[i].startsWith("-verify"))
161: {
162: dbVerifyFlag = true;
163: }
164: else if (args[i].startsWith("-defrag"))
165: {
166: dbDefragFlag = true;
167: }
168: else if (args[i].startsWith("-h"))
169: {
170: helpFlag = true;
171: }
172: }
173:
174: if ((args.length == 0) || (helpFlag == true))
175: {
176: if (LOGGER.isInfoEnabled() == true)
177: {
178: LOGGER.info("usage: [-create] [-dbdir=<dir>] [-admin=<username:password>] [-xdb] [-sql=<url>] [-tcpPort=<port>] [-tcpsPort=<port>] [-udpPort=<port>] [-webconfig=<file>] [-backup=<dir>] [-restore=<dir>] [-verify] [-defrag]");
179: LOGGER.info("");
180: LOGGER.info(" -create create database (if neccessary)");
181: LOGGER.info(" -dbdir=<dir> database directory (if neccessary)");
182: LOGGER.info(" -admin=<username:password> create admin user/password (if neccessary)");
183: LOGGER.info("");
184: LOGGER.info(" -xdb store data as pure xml");
185: LOGGER.info(" -sql=url store data as pure sql (jdbc:url)");
186: LOGGER.info("");
187: LOGGER.info(" -tcpPort=<port> database tcp port (-1 to turn off)");
188: LOGGER.info(" -tcpsPort=<port> database secure tcp port");
189: LOGGER.info(" -udpPort=<port> database udp port");
190: LOGGER.info(" -udpsPort=<port> database secure udp port");
191: LOGGER.info(" -webconfig=<file> web server configuration file");
192: LOGGER.info("");
193: LOGGER.info(" -backup=<dir> backup database ( exits after execution )");
194: LOGGER.info(" -restore=<dir> restore database ( exits after execution)");
195: LOGGER.info("");
196: LOGGER.info(" -verify verify database ( integrity check )");
197: LOGGER.info(" -defrag defragment database ( exits after execution)");
198: }
199:
200: System.exit(1);
201: }
202:
203: Thread.currentThread().setPriority(MyOodbManager.SERVER_THREAD_PRIORITY);
204:
205: try
206: {
207: // Check if a DB exists and make new database if not.
208: if ((dbCreateFlag == true) && (MyOodbInstall.dbExists(dbdir) == false))
209: {
210: MyOodbInstall.createDatabase(dbdir);
211: }
212:
213: MyOodbManager manager = new MyOodbManager(dbdir, storeAsXmlFlag);
214:
215: if (manager.getUserManager().getUser(username) == null)
216: {
217: manager.getUserManager().newUser(username, password);
218: }
219:
220: if (dbVerifyFlag == true)
221: {
222: manager.getStoreManager().verifyDatabase();
223: }
224:
225: if (backupDir != null)
226: {
227: manager.getStoreManager().backupDatabase(backupDir);
228: return;
229: }
230:
231: if (restoreDir != null)
232: {
233: manager.getStoreManager().restoreDatabase(restoreDir);
234: return;
235: }
236:
237: if (dbDefragFlag == true)
238: {
239: manager.getStoreManager().defragDatabase();
240: return;
241: }
242:
243: if (@waitForTransactions@ == true)
244: {
245: setupShutdownHook();
246: }
247:
248: if (tcpPort == -1)
249: {
250: if (udpPort != -1)
251: {
252: throw new PermissionException("UDP Services requires the configuration of the tcp port");
253: }
254:
255: if (webconfig == null)
256: {
257: throw new PermissionException("Web Services requires the configuration of the tcp port");
258: }
259: }
260:
261: manager.startup(username, password, tcpPort, tcpsPort);
262:
263: if (udpPort != -1)
264: {
265: java.net.DatagramSocket socket = new java.net.DatagramSocket(udpPort);
266: socket.setReuseAddress(true);
267:
268: org.myoodb.core.MyOodbTunnelUdp.start(socket);
269: }
270:
271: if (webconfig != null)
272: {
273: final String fwebconfig = webconfig;
274:
275: Runnable webService = new Runnable()
276: {
277: public void run()
278: {
279: org.mortbay.start.Main.main(new String[] {fwebconfig});
280: }
281: };
282:
283: Thread thread = new Thread(webService);
284: thread.setDaemon(true);
285: thread.setName("MyOodb WebService");
286: thread.start();
287: }
288:
289: // XXX: THE MAIN LOOP
290: int gcCount = 0;
291: while ((manager.getExitingFlag() == false) && (manager.getShuttingdownFlag() == false))
292: {
293: if ((getCheckForStaleMemoryFlag() == true) && (org.myoodb.core.FileHelper.getMemoryResidentFlag() == false))
294: {
295: manager.getStoreManager().staleMemoryCheck();
296: }
297:
298: Thread.sleep(5000);
299:
300: if (gcCount == 6)
301: {
302: System.runFinalization();
303: System.gc();
304: gcCount = 0;
305: }
306: else
307: {
308: gcCount++;
309: }
310: }
311:
312: manager.shutdown();
313: s_stopped = true;
314:
315: if (MyOodbManager.getSystemExitFlag() == true)
316: {
317: System.exit(0);
318: }
319: }
320: catch (Exception e)
321: {
322: MyOodbManager manager = MyOodbManager.getTheManager();
323:
324: if (manager != null)
325: {
326: manager.shutdown();
327: }
328:
329: LOGGER.error("Unable to initialize server:", e);
330:
331: if (MyOodbManager.getSystemExitFlag() == true)
332: {
333: System.exit(1);
334: }
335: else
336: {
337: throw e;
338: }
339: }
340: }
341: }
|