KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > callbacks > Server


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.callbacks;
25
26 public class Server
27 {
28     public static int PORT = 54321;
29     public static String JavaDoc DIRECTORY = "db";
30     public static String JavaDoc USERNAME = "admin";
31     public static String JavaDoc PASSWORD = "admin";
32
33     // XXX: show how to cap logins
34
public static int LOGIN_COUNT = 0;
35
36     public static void main(String JavaDoc args[]) throws Exception JavaDoc
37     {
38         // call us on each user login/logout
39
org.myoodb.core.MyOodbManager.setLoginCallback(org.myoodb.callbacks.Server.class, "userLogin");
40         org.myoodb.core.MyOodbManager.setLogoutCallback(org.myoodb.callbacks.Server.class, "userLogout");
41
42         // call us on each api method invocation
43
org.myoodb.core.MyOodbManager.setPreObjectMethodCallback(org.myoodb.callbacks.Server.class, "preValidate");
44         org.myoodb.core.MyOodbManager.setPostObjectMethodCallback(org.myoodb.callbacks.Server.class, "postValidate");
45
46         org.myoodb.core.MyOodbMain.main(new String JavaDoc[] {"-c", "-d" + DIRECTORY, "-p" + PORT, "-u" + USERNAME + ":" + PASSWORD});
47     }
48
49     public static void userLogin(org.myoodb.core.DatabaseClient client) throws Exception JavaDoc
50     {
51         java.net.InetSocketAddress JavaDoc inetsock = (java.net.InetSocketAddress JavaDoc) client.getSocket().getRemoteSocketAddress();
52
53         int port = inetsock.getPort();
54         String JavaDoc user = client.getUser().getName();
55         String JavaDoc hostaddress = inetsock.getAddress().getHostAddress();
56
57         // XXX: show how to cap logins
58
if (++LOGIN_COUNT < 3)
59         {
60             System.out.println(" - user Login: " + user + " on " + hostaddress + "/" + port);
61         }
62         else
63         {
64             System.out.println(" - to many logins - user Login: " + user + " on " + hostaddress + "/" + port);
65
66             throw new org.myoodb.exception.PermissionException("To many logins");
67         }
68     }
69
70     public static void userLogout(org.myoodb.core.DatabaseClient client) throws Exception JavaDoc
71     {
72         java.net.InetSocketAddress JavaDoc inetsock = (java.net.InetSocketAddress JavaDoc) client.getSocket().getRemoteSocketAddress();
73
74         int port = inetsock.getPort();
75         String JavaDoc user = client.getUser().getName();
76         String JavaDoc hostaddress = inetsock.getAddress().getHostAddress();
77
78         System.out.println(" - user Logout: " + user + " on " + hostaddress + "/" + port);
79     }
80
81     public static void preValidate(org.myoodb.MyOodbLocal self, String JavaDoc methodName, Object JavaDoc[] args) throws Exception JavaDoc
82     {
83         System.out.println(" - preValidate method invocation: " + self + "." + methodName);
84     }
85
86     public static void postValidate(org.myoodb.MyOodbLocal self, String JavaDoc methodName, Object JavaDoc[] args) throws Exception JavaDoc
87     {
88         System.out.println(" - postValidate method invocation: " + self + "." + methodName);
89     }
90 }
91
Popular Tags