KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > InvokeServer


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: InvokeServer.java,v 1.7 2002/12/29 11:15:56 per_nyfelt Exp $
8

9 package org.ozoneDB.core;
10
11 import java.io.IOException JavaDoc;
12 import java.net.Socket JavaDoc;
13 import org.ozoneDB.DxLib.*;
14 import org.ozoneDB.DxLib.net.*;
15 import org.ozoneDB.*;
16 import org.ozoneDB.core.DbRemote.*;
17 import org.ozoneDB.util.*;
18
19
20 /**
21  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
22  * @version $Revision: 1.7 $Date: 2002/12/29 11:15:56 $
23  */

24 public class InvokeServer extends DxMultiServer {
25
26     protected transient Env env;
27
28
29     public InvokeServer( Env _env, int port ) throws IOException JavaDoc{
30         super( port );
31         env = _env;
32     }
33
34
35     public void startup() throws Exception JavaDoc {
36         env.logWriter.newEntry( this, "startup...", LogWriter.INFO );
37     }
38
39
40     public void shutdown() throws Exception JavaDoc {
41         env.logWriter.newEntry( this, "shutdown...", LogWriter.INFO );
42         close();
43     }
44
45
46     public void handleClientEvent(DxMultiServerClient client,Object JavaDoc event) {
47 // env.logWriter.newEntry( this, "handleClientEvent()...", LogWriter.DEBUG3 );
48
try {
49             DbCommand cmd = (DbCommand)event;
50             cmd.env = env;
51
52             if (cmd instanceof DbOpen) {
53                 DbOpen command = (DbOpen)cmd;
54                 User user = env.userManager.userForName( command.userName() );
55
56                 if (user == null) {
57                     client.send( new PermissionDeniedException( "No such user: " + command.userName() + "." ) );
58                 } else {
59                     ((DbInvokeClient)client).user = user;
60                     ((CommandThread)Thread.currentThread()).setOwner( user );
61                     env.logWriter.newEntry( this, "user logged in: " + command.userName(), LogWriter.DEBUG );
62                     client.send( null );
63                 }
64             } else if (cmd instanceof DbCloseConn) {
65                 removeClient( client );
66             } else if (cmd instanceof DbReloadClasses) {
67                 env.classManager.dropClasses();
68                 // AbstractObjectContainer.flushMethodCache();
69
client.send( new Integer JavaDoc( 0 ) );
70             } else {
71                 /*
72                 User user = ((DbInvokeClient)client).user;
73                 env.transactionManager.handleCommand( cmd, user );
74                 */

75                 DbInvokeClient dbInvokeClient = (DbInvokeClient) client;
76
77                 cmd.setProxyObjectGate(dbInvokeClient.getProxyObjectGate());
78                 env.transactionManager.handleCommand(cmd,dbInvokeClient);
79
80                 if (cmd.shouldResultBeSentToClient()) {
81                     client.send(cmd.result);
82                 }
83             }
84         } catch (Error JavaDoc e) {
85             env.logWriter.newEntry( this, "handleClientEvent(): " + e, e, LogWriter.WARN );
86             throw e;
87         } catch (Exception JavaDoc e) {
88             // exceptions that are catched here are related to any network problem
89
// since user and internal exceptions are catched in Transactions or
90
// the TransactionManager
91

92             // TODO: should this shutdown the connection?
93
env.logWriter.newEntry( this, "handleClientEvent(): " + e, e, LogWriter.WARN );
94         }
95     }
96
97
98     public void handleClientException( DxMultiServerClient client, Exception JavaDoc e ) {
99         env.logWriter.newEntry( this, "handleClientException(): " + e, e, LogWriter.WARN );
100         removeClient( client );
101     }
102
103
104     public DxMultiServerClient newClient( Socket JavaDoc sock ) {
105         try {
106             DbInvokeClient dc = new DbInvokeClient( sock, this );
107             env.logWriter.newEntry( this, "connection established...", LogWriter.DEBUG );
108             return dc;
109         }
110         catch (Exception JavaDoc e) {
111             env.logWriter.newEntry( this, "newClient(): ", e, LogWriter.WARN );
112             return null;
113         }
114     }
115
116
117     public void removeClient( DxMultiServerClient client ) {
118         env.logWriter.newEntry( this, "close connection...", LogWriter.DEBUG3 );
119         env.logWriter.newEntry( this, "close pending transaction...", LogWriter.DEBUG3 );
120         env.transactionManager.handleCommand(new DbCloseConn(),(User) null);
121
122         super.removeClient( client );
123         String JavaDoc userName = ((DbInvokeClient)client).user != null ? ((DbInvokeClient)client).user.name() : "none";
124         env.logWriter.newEntry( this, "connection closed (user: " + userName + ")", LogWriter.DEBUG );
125     }
126
127
128     public Thread JavaDoc newThread( Runnable JavaDoc run ) {
129         Thread JavaDoc thread;
130         if (run == acceptor) {
131             thread = new Thread JavaDoc( threadGroup(), run );
132             thread.setPriority( Env.ACCEPT_THREAD_PRIORITY );
133         } else {
134             thread = new CommandThread( threadGroup(), run );
135             thread.setPriority( Env.TRANSACTION_THREAD_PRIORITY );
136         }
137         thread.setDaemon( true );
138         return thread;
139     }
140
141     /**
142         Starts filtering references to database objects ({@link OzoneProxy}s) which
143         are exported to clients at all client connections.
144         Every reference which is exported will be notified to the given GarbageCollector.
145         Additionally, references which are known to be used by clients are notified to the
146         given GarbageCollector within this call.
147     */

148     public void startFilterDatabaseObjectReferencesExports(GarbageCollector garbageCollector) {
149         synchronized (this) {
150             DxIterator i = iterator();
151
152             while (i.next()!=null) {
153                 ((DbInvokeClient) i.object()).getProxyObjectGate().startFilterDatabaseObjectReferencesExports(garbageCollector);
154             }
155         }
156     }
157 }
158
Popular Tags