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 hostName; 18 private int portNumber; 19 private Thread startServerTimeThread; 20 Map map = new TreeMap(String.CASE_INSENSITIVE_ORDER); 21 22 23 public RmiServerServerSide(_Server serverSystemInterface) throws 24 RemoteException, java.net.UnknownHostException { 25 super(); 26 this.serverSystemInterface = serverSystemInterface; 27 hostName = java.net.InetAddress.getLocalHost().getHostName(); 28 } 29 30 public _RmiConnection getConnection(String 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 42 try { 43 conObj.setClient(getClientHost()); 44 RmiClientHelper.getInstance().performAction(getClientHost(), true,conObj); 45 }catch(ServerNotActiveException sna){ 46 throw new DException("DSE0",new String []{"Server is not active "}); 47 } catch (DException ex) { 48 throw ex; 49 } 50 51 52 return conObj; 53 } 54 55 56 public _RmiUser getUser(String userName, String password) throws DException, 57 RemoteException { 58 return new RmiUserServerSide(serverSystemInterface.getUser(userName, 59 password)); 60 } 61 62 public String 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 userName, String 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 90 Runtime rt = Runtime.getRuntime(); 91 Class cc = rt.getClass(); 92 Method mt = null; 93 try { 94 mt = cc.getDeclaredMethod("removeShutdownHook", 95 new Class [] {Thread .class}); 96 } catch (SecurityException ex1) { 97 System.err.println(ex1); 98 } catch (NoSuchMethodException ex1) { 99 System.err.println(ex1); 100 } 101 try { 102 if (mt != null) { 103 mt.invoke(rt, new Object [] {startServerTimeThread}); 104 } 105 } catch (InvocationTargetException ex) { 106 } catch (IllegalAccessException ex) { 107 } 108 } 109 if (exit) 110 System.exit(0); 111 } 112 113 public void changeHome(String home) throws DException, RemoteException { 114 serverSystemInterface.changeHome(home); 115 } 116 117 public void addSchedule(String databaseName, String scheduleName, 118 String timeIntervalForBackup, String backupType, 119 String backupPath, String 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 userName) throws DException, 129 RemoteException { 130 return serverSystemInterface.getAllDatabases(userName); 131 } 132 133 public void getInconsistentOnlineBackup(String destination, 134 String databaseNameSource, 135 String databaseNameDestination, 136 boolean overwrite) throws 137 DException, RemoteException { 138 serverSystemInterface.getInconsistentOnlineBackup(destination, 139 databaseNameSource, databaseNameDestination, overwrite); 140 } 141 142 public void offlineBackup(String userName, String password, 143 String destination, String databaseNameSource, 144 String 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 databaseName) throws 152 DException, RemoteException { 153 154 return serverSystemInterface.getScheduleForDatabase(databaseName); 155 } 156 157 public void dropSchedule(String scheduleName) throws DException, 158 RemoteException { 159 160 serverSystemInterface.dropSchedule(scheduleName); 161 } 162 163 public void restore(String userName, String password, String sourcePath, 164 String databaseNameSource, 165 String 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 oldDBName, String newdbName) throws 173 DException, RemoteException { 174 serverSystemInterface.restoreSaveMode(oldDBName, newdbName); 175 } 176 177 public void setThreadObject(Thread th) { 178 startServerTimeThread = th; 179 } 180 181 182 public static final class RmiClientHelper { 183 private final static RmiClientHelper THIS = new RmiClientHelper(); 184 private final HashMap clients = new HashMap(); private final int LIMIT = 10; 186 private final DException limitEx = new DException("DSE8218",new String []{" "+LIMIT+" "}); 187 private RmiClientHelper() {} 188 189 public static RmiClientHelper getInstance() { 190 return THIS; 191 } 192 193 public synchronized void performAction(String client, boolean isConnected , _RmiConnection conObj) throws 194 DException, RemoteException { 195 if( LIMIT == -1) return; 197 if (isConnected) { 198 199 if (clients.containsKey(client)) { ThreadSafeCounter tsc = (ThreadSafeCounter)clients.get(client); 201 tsc.inreaseCount(); 202 clients.put(client,tsc); 203 } else if (clients.size() < LIMIT) { clients.put(client, new ThreadSafeCounter(1)); 205 } else { conObj.close(); 207 throw limitEx; 208 } 209 } else { 210 211 if (!clients.containsKey(client)) return; 214 else { 215 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 client) { 232 return clients.containsKey(client); 233 } 234 235 public void handleIfClientNotExists(String client) throws 236 DException { 237 if (!clients.containsKey(client)) 238 throw limitEx; 239 } 240 241 242 public String toString() { 243 return "RmiClientHelper [" + clients.toString() + "] hashCode " + 244 hashCode(); 245 } 246 247 248 } 249 250 251 252 } 253 | Popular Tags |