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.callbacks;
025:
026: public class Server {
027: public static int PORT = 54321;
028: public static String DIRECTORY = "db";
029: public static String USERNAME = "admin";
030: public static String PASSWORD = "admin";
031:
032: // XXX: show how to cap logins
033: public static int LOGIN_COUNT = 0;
034:
035: public static void main(String args[]) throws Exception {
036: // call us on each user login/logout
037: org.myoodb.core.MyOodbManager.setLoginCallback(
038: org.myoodb.callbacks.Server.class, "userLogin");
039: org.myoodb.core.MyOodbManager.setLogoutCallback(
040: org.myoodb.callbacks.Server.class, "userLogout");
041:
042: // call us on each api method invocation
043: org.myoodb.core.MyOodbManager.setPreObjectMethodCallback(
044: org.myoodb.callbacks.Server.class, "preValidate");
045: org.myoodb.core.MyOodbManager.setPostObjectMethodCallback(
046: org.myoodb.callbacks.Server.class, "postValidate");
047:
048: // XXX: adding the "-xdb" option makes the object backing store 100% XML.
049:
050: org.myoodb.core.MyOodbMain.main(new String[] { "-create",
051: "-dbdir=" + DIRECTORY, "-tcpPort=" + PORT,
052: "-admin=" + USERNAME + ":" + PASSWORD });
053: }
054:
055: public static void userLogin(org.myoodb.core.DatabaseClient client)
056: throws Exception {
057: java.net.InetSocketAddress inetsock = (java.net.InetSocketAddress) client
058: .getSocket().getRemoteSocketAddress();
059:
060: int port = inetsock.getPort();
061: String user = client.getUser().getName();
062: String hostaddress = inetsock.getAddress().getHostAddress();
063:
064: // XXX: show how to cap logins
065: if (++LOGIN_COUNT < 3) {
066: System.out.println(" - user Login: " + user + " on "
067: + hostaddress + "/" + port);
068: } else {
069: System.out.println(" - to many logins - user Login: "
070: + user + " on " + hostaddress + "/" + port);
071:
072: throw new org.myoodb.exception.PermissionException(
073: "To many logins");
074: }
075: }
076:
077: public static void userLogout(org.myoodb.core.DatabaseClient client)
078: throws Exception {
079: java.net.InetSocketAddress inetsock = (java.net.InetSocketAddress) client
080: .getSocket().getRemoteSocketAddress();
081:
082: int port = inetsock.getPort();
083: String user = client.getUser().getName();
084: String hostaddress = inetsock.getAddress().getHostAddress();
085:
086: System.out.println(" - user Logout: " + user + " on "
087: + hostaddress + "/" + port);
088: }
089:
090: public static void preValidate(org.myoodb.MyOodbLocal self,
091: String methodName, Object[] args) throws Exception {
092: System.out.println(" - preValidate method invocation: " + self
093: + "." + methodName);
094: }
095:
096: public static void postValidate(org.myoodb.MyOodbLocal self,
097: String methodName, Object[] args) throws Exception {
098: System.out.println(" - postValidate method invocation: " + self
099: + "." + methodName);
100: }
101: }
|