KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > rmi > server > RmiServerServerSide


1 package com.daffodilwoods.rmi.server;
2
3 import java.rmi.*;
4 import java.rmi.server.*;
5 import java.util.*;
6
7 import com.daffodilwoods.daffodildb.server.serversystem.*;
8 import com.daffodilwoods.database.resource.*;
9 import com.daffodilwoods.rmi.interfaces.*;
10 import java.lang.reflect.*;
11 import com.daffodilwoods.daffodildb.utils.ThreadSafeCounter;
12
13 public class RmiServerServerSide extends UnicastRemoteObject implements
14         _RmiServer {
15
16     _Server serverSystemInterface;
17     private String JavaDoc hostName;
18     private int portNumber;
19     private Thread JavaDoc startServerTimeThread;
20     Map map = new TreeMap(String.CASE_INSENSITIVE_ORDER);
21
22
23     public RmiServerServerSide(_Server serverSystemInterface) throws
24             RemoteException, java.net.UnknownHostException JavaDoc {
25         super();
26         this.serverSystemInterface = serverSystemInterface;
27         hostName = java.net.InetAddress.getLocalHost().getHostName();
28     }
29
30     public _RmiConnection getConnection(String JavaDoc databaseName, Properties prop) throws
31             DException, RemoteException {
32         RmiPreparedStatementFactory factory = (RmiPreparedStatementFactory) map.
33                                               get(databaseName);
34         if (factory == null) {
35             factory = new RmiPreparedStatementFactory();
36             map.put(databaseName, factory);
37         }
38         RmiConnectionServerSide conObj = new RmiConnectionServerSide(serverSystemInterface.
39                                              getConnection(
40                 databaseName, prop), factory);
41         /*Do Client limit handling here*/
42         try {
43             conObj.setClient(getClientHost());
44             RmiClientHelper.getInstance().performAction(getClientHost(), true,conObj);
45         }catch(ServerNotActiveException sna){
46             throw new DException("DSE0",new String JavaDoc[]{"Server is not active "});
47         } catch (DException ex) {
48             throw ex;
49         }
50
51         /*End of Client Limit handling*/
52         return conObj;
53     }
54
55
56     public _RmiUser getUser(String JavaDoc userName, String JavaDoc password) throws DException,
57             RemoteException {
58         return new RmiUserServerSide(serverSystemInterface.getUser(userName,
59                 password));
60     }
61
62     public String JavaDoc getHostName() throws RemoteException {
63         return hostName;
64     }
65
66     public int getPortNumber() throws RemoteException {
67         return portNumber;
68     }
69
70     public void setPortNumber(int portNumber) {
71         this.portNumber = portNumber;
72     }
73
74     public boolean isValidUser(String JavaDoc userName, String JavaDoc password) throws
75             DException, RemoteException {
76         return serverSystemInterface.isValidUser(userName, password);
77     }
78
79     public _RmiDXAResource getDxaResource() throws DException, RemoteException {
80         return new RmiDXAResourceServerSide(serverSystemInterface.
81                                             getDxaResource());
82     }
83
84     public void shutDown(boolean exit) throws RemoteException {
85         if (startServerTimeThread != null) {
86             /** This block is executed when server shutdown normally.
87              * During starting server we set thread object that is
88              * used during calling removeShutdownHook .
89              */

90             Runtime JavaDoc rt = Runtime.getRuntime();
91             Class JavaDoc cc = rt.getClass();
92             Method mt = null;
93             try {
94                 mt = cc.getDeclaredMethod("removeShutdownHook",
95                                           new Class JavaDoc[] {Thread JavaDoc.class});
96             } catch (SecurityException JavaDoc ex1) {
97                 System.err.println(ex1);
98             } catch (NoSuchMethodException JavaDoc ex1) {
99                 System.err.println(ex1);
100             }
101             try {
102                 if (mt != null) {
103                     mt.invoke(rt, new Object JavaDoc[] {startServerTimeThread});
104                 }
105             } catch (InvocationTargetException ex) {
106             } catch (IllegalAccessException JavaDoc ex) {
107             }
108         }
109         if (exit)
110             System.exit(0);
111     }
112
113     public void changeHome(String JavaDoc home) throws DException, RemoteException {
114         serverSystemInterface.changeHome(home);
115     }
116
117     public void addSchedule(String JavaDoc databaseName, String JavaDoc scheduleName,
118                             String JavaDoc timeIntervalForBackup, String JavaDoc backupType,
119                             String JavaDoc backupPath, String JavaDoc databaseNameForBackup,
120                             long lastBackupTime) throws DException,
121             RemoteException {
122         serverSystemInterface.addSchedule(databaseName, scheduleName,
123                                           timeIntervalForBackup, backupType,
124                                           backupPath, databaseNameForBackup,
125                                           lastBackupTime);
126     }
127
128     public ArrayList getAllDatabases(String JavaDoc userName) throws DException,
129             RemoteException {
130         return serverSystemInterface.getAllDatabases(userName);
131     }
132
133     public void getInconsistentOnlineBackup(String JavaDoc destination,
134                                             String JavaDoc databaseNameSource,
135                                             String JavaDoc databaseNameDestination,
136                                             boolean overwrite) throws
137             DException, RemoteException {
138         serverSystemInterface.getInconsistentOnlineBackup(destination,
139                 databaseNameSource, databaseNameDestination, overwrite);
140     }
141
142     public void offlineBackup(String JavaDoc userName, String JavaDoc password,
143                               String JavaDoc destination, String JavaDoc databaseNameSource,
144                               String JavaDoc databaseNameDestination, boolean overwrite) throws
145             DException, RemoteException {
146         serverSystemInterface.offlineBackup(userName, password, destination,
147                                             databaseNameSource,
148                                             databaseNameDestination, overwrite);
149     }
150
151     public ArrayList getScheduleForDatabase(String JavaDoc databaseName) throws
152             DException, RemoteException {
153
154         return serverSystemInterface.getScheduleForDatabase(databaseName);
155     }
156
157     public void dropSchedule(String JavaDoc scheduleName) throws DException,
158             RemoteException {
159
160         serverSystemInterface.dropSchedule(scheduleName);
161     }
162
163     public void restore(String JavaDoc userName, String JavaDoc password, String JavaDoc sourcePath,
164                         String JavaDoc databaseNameSource,
165                         String JavaDoc databaseNameDestination, boolean overwrite) throws
166             DException, RemoteException {
167         serverSystemInterface.restore(userName, password, sourcePath,
168                                       databaseNameSource,
169                                       databaseNameDestination, overwrite);
170     }
171
172     public void restoreSaveMode(String JavaDoc oldDBName, String JavaDoc newdbName) throws
173             DException, RemoteException {
174         serverSystemInterface.restoreSaveMode(oldDBName, newdbName);
175     }
176
177     public void setThreadObject(Thread JavaDoc th) {
178         startServerTimeThread = th;
179     }
180
181     /*Class that helps client to work with DaffodilDB server on a well defined terms of limitations*/
182     public static final class RmiClientHelper {
183         private final static RmiClientHelper THIS = new RmiClientHelper();
184         private final HashMap clients = new HashMap(); // Key as client and value as number of connections that is hold by this client
185
private final int LIMIT = 10;
186         private final DException limitEx = new DException("DSE8218",new String JavaDoc[]{" "+LIMIT+" "});
187         private RmiClientHelper() {}
188
189         public static RmiClientHelper getInstance() {
190             return THIS;
191         }
192
193         public synchronized void performAction(String JavaDoc client, boolean isConnected , _RmiConnection conObj) throws
194              DException, RemoteException {
195             if( LIMIT == -1) // Unlimited time
196
return;
197             if (isConnected) { /*Connection time*/
198
199                 if (clients.containsKey(client)) { // Client already exists
200
ThreadSafeCounter tsc = (ThreadSafeCounter)clients.get(client);
201                     tsc.inreaseCount();
202                     clients.put(client,tsc);
203                 } else if (clients.size() < LIMIT) { // Means new client and client size is alredy < 10
204
clients.put(client, new ThreadSafeCounter(1));
205                 } else { // New Client and clients are already uptolimit
206
conObj.close();
207                     throw limitEx;
208                 }
209             } else { /*Connection close time*/
210
211                 if (!clients.containsKey(client)) // Client not exists but it seems impossible
212
return; // Slightly ignore it .
213

214                 else {
215                     /*Algo for client exists . :
216                      IF connection hold by client is == 1 THEN remove client from clients list ;
217                      ELSE no of connections hold by this client will be decremented by this 1 ;
218                      */

219                     ThreadSafeCounter tsc = (ThreadSafeCounter)clients.get(client);
220                     if (tsc.getCount() == 1)
221                         clients.remove(client);
222                     else{
223                         tsc.decreaseCount();
224                         clients.put(client,tsc);
225                     }
226                 }
227
228             }
229         }
230
231         public boolean isClientExists(String JavaDoc client) {
232             return clients.containsKey(client);
233         }
234
235         public void handleIfClientNotExists(String JavaDoc client) throws
236                 DException {
237             if (!clients.containsKey(client))
238                 throw limitEx;
239         }
240
241
242         public String JavaDoc toString() {
243             return "RmiClientHelper [" + clients.toString() + "] hashCode " +
244                     hashCode();
245         }
246
247
248     }
249     /*End of class RmiClientHelper*/
250
251
252 }
253
Popular Tags