1 21 package com.db4o.cs; 22 23 import java.io.*; 24 25 import com.db4o.*; 26 import com.db4o.config.*; 27 import com.db4o.ext.*; 28 import com.db4o.foundation.*; 29 import com.db4o.foundation.network.*; 30 import com.db4o.inside.*; 31 32 public class YapServer implements ObjectServer, ExtObjectServer, Runnable , 33 YapSocketFakeServer { 34 private String i_name; 35 36 private YapServerSocket i_serverSocket; 37 38 private int i_threadIDGen = 1; 39 40 private Collection4 i_threads = new Collection4(); 41 42 private YapFile i_yapFile; 43 44 public YapServer(final YapFile a_yapFile, int a_port) { 45 a_yapFile.setServer(true); 46 i_name = "db4o ServerSocket FILE: " + a_yapFile.toString() + " PORT:" 47 + a_port; 48 i_yapFile = a_yapFile; 49 Config4Impl config = (Config4Impl) i_yapFile.configure(); 50 config.callbacks(false); 51 config.isServer(true); 52 53 a_yapFile.produceYapClass(a_yapFile.i_handlers.ICLASS_STATICCLASS); 54 55 config.exceptionalClasses().forEachValue(new Visitor4() { 57 public void visit(Object a_object) { 58 a_yapFile.produceYapClass(a_yapFile.reflector().forName( 59 ((Config4Class) a_object).getName())); 60 } 61 }); 62 63 if (a_port > 0) { 64 try { 65 i_serverSocket = new YapServerSocket(a_port); 66 i_serverSocket.setSoTimeout(config.timeoutServerSocket()); 67 } catch (IOException e) { 68 Exceptions4.throwRuntimeException(30, "" + a_port); 69 } 70 71 new Thread (this).start(); 72 75 synchronized (this) { 76 try { 77 wait(1000); 78 } catch (Exception e) { 81 } 82 } 83 } 84 } 85 86 public void backup(String path) throws IOException { 87 i_yapFile.backup(path); 88 } 89 90 final void checkClosed() { 91 if (i_yapFile == null) { 92 Exceptions4.throwRuntimeException(20, i_name); 93 } 94 i_yapFile.checkClosed(); 95 } 96 97 public boolean close() { 98 synchronized (Global4.lock) { 99 Cool.sleepIgnoringInterruption(100); 103 try { 104 if (i_serverSocket != null) { 105 i_serverSocket.close(); 106 } 107 } catch (Exception e) { 108 if (Deploy.debug) { 109 System.out 110 .println("YapServer.close() ServerSocket failed to close."); 111 } 112 } 113 i_serverSocket = null; 114 boolean isClosed = i_yapFile == null ? true : i_yapFile.close(); 115 synchronized (i_threads) { 116 Iterator4 i = new Collection4(i_threads).iterator(); 117 while (i.moveNext()) { 118 ((YapServerThread) i.current()).close(); 119 } 120 } 121 i_yapFile = null; 122 return isClosed; 123 } 124 } 125 126 public Configuration configure() { 127 return i_yapFile.configure(); 128 } 129 130 public ExtObjectServer ext() { 131 return this; 132 } 133 134 YapServerThread findThread(int a_threadID) { 135 synchronized (i_threads) { 136 Iterator4 i = i_threads.iterator(); 137 while (i.moveNext()) { 138 YapServerThread serverThread = (YapServerThread) i.current(); 139 if (serverThread.i_threadID == a_threadID) { 140 return serverThread; 141 } 142 } 143 } 144 return null; 145 } 146 147 public void grantAccess(String userName, String password) { 148 synchronized (i_yapFile.i_lock) { 149 checkClosed(); 150 i_yapFile.showInternalClasses(true); 151 try { 152 User existing = getUser(userName); 153 if (existing != null) { 154 setPassword(existing, password); 155 } else { 156 addUser(userName, password); 157 } 158 i_yapFile.commit(); 159 } finally { 160 i_yapFile.showInternalClasses(false); 161 } 162 } 163 } 164 165 private void addUser(String userName, String password) { 166 i_yapFile.set(new User(userName, password)); 167 } 168 169 private void setPassword(User existing, String password) { 170 existing.password = password; 171 i_yapFile.set(existing); 172 } 173 174 User getUser(String userName) { 175 final ObjectSet result = queryUsers(userName); 176 if (!result.hasNext()) { 177 return null; 178 } 179 return (User) result.next(); 180 } 181 182 private ObjectSet queryUsers(String userName) { 183 return i_yapFile.get(new User(userName, null)); 184 } 185 186 public ObjectContainer objectContainer() { 187 return i_yapFile; 188 } 189 190 public ObjectContainer openClient() { 191 return openClient(Db4o.cloneConfiguration()); 192 } 193 194 public ObjectContainer openClient(Configuration config) { 195 try { 196 YapClient client = new YapClient(config, openClientSocket(), 197 YapConst.EMBEDDED_CLIENT_USER + (i_threadIDGen - 1), "", 198 false); 199 client.blockSize(i_yapFile.blockSize()); 200 return client; 201 } catch (IOException e) { 202 e.printStackTrace(); 203 } 204 return null; 205 } 206 207 public YapSocketFake openClientSocket() { 208 int timeout = ((Config4Impl) configure()).timeoutClientSocket(); 209 YapSocketFake clientFake = new YapSocketFake(this, timeout); 210 YapSocketFake serverFake = new YapSocketFake(this, timeout, clientFake); 211 try { 212 YapServerThread thread = new YapServerThread(this, i_yapFile, 213 serverFake, i_threadIDGen++, true); 214 synchronized (i_threads) { 215 i_threads.add(thread); 216 } 217 thread.start(); 218 return clientFake; 219 } catch (Exception e) { 220 e.printStackTrace(); 221 } 222 return null; 223 224 } 225 226 void removeThread(YapServerThread aThread) { 227 synchronized (i_threads) { 228 i_threads.remove(aThread); 229 } 230 } 231 232 public void revokeAccess(String userName) { 233 synchronized (i_yapFile.i_lock) { 234 i_yapFile.showInternalClasses(true); 235 try { 236 checkClosed(); 237 deleteUsers(userName); 238 i_yapFile.commit(); 239 } finally { 240 i_yapFile.showInternalClasses(false); 241 } 242 } 243 } 244 245 private void deleteUsers(String userName) { 246 ObjectSet set = queryUsers(userName); 247 while (set.hasNext()) { 248 i_yapFile.delete(set.next()); 249 } 250 } 251 252 public void run() { 253 Thread.currentThread().setName(i_name); 254 i_yapFile.logMsg(31, "" + i_serverSocket.getLocalPort()); 255 synchronized (this) { 256 this.notify(); 257 } 258 while (i_serverSocket != null) { 259 try { 260 YapServerThread thread = new YapServerThread(this, i_yapFile, 261 i_serverSocket.accept(), i_threadIDGen++, false); 262 synchronized (i_threads) { 263 i_threads.add(thread); 264 } 265 thread.start(); 266 } catch (Exception e) { 267 } 268 } 269 } 270 } 271 | Popular Tags |