1 22 23 package org.gjt.sp.jedit; 24 25 import bsh.NameSpace; 27 import javax.swing.SwingUtilities ; 28 import java.io.*; 29 import java.net.*; 30 import java.util.Random ; 31 import org.gjt.sp.jedit.io.FileVFS; 32 import org.gjt.sp.util.Log; 33 35 59 public class EditServer extends Thread 60 { 61 EditServer(String portFile) 63 { 64 super("jEdit server daemon [" + portFile + "]"); 65 setDaemon(true); 66 this.portFile = portFile; 67 68 try 69 { 70 if(OperatingSystem.isUnix()) 76 { 77 new File(portFile).createNewFile(); 78 FileVFS.setPermissions(portFile,0600); 79 } 80 81 socket = new ServerSocket(0, 2, 84 InetAddress.getByName("127.0.0.1")); 85 authKey = Math.abs(new Random ().nextInt()); 86 int port = socket.getLocalPort(); 87 88 FileWriter out = new FileWriter(portFile); 89 90 try 91 { 92 out.write("b\n"); 93 out.write(String.valueOf(port)); 94 out.write("\n"); 95 out.write(String.valueOf(authKey)); 96 out.write("\n"); 97 } 98 finally 99 { 100 out.close(); 101 } 102 103 ok = true; 104 105 Log.log(Log.DEBUG,this,"jEdit server started on port " 106 + socket.getLocalPort()); 107 Log.log(Log.DEBUG,this,"Authorization key is " 108 + authKey); 109 } 110 catch(IOException io) 111 { 112 117 Log.log(Log.NOTICE,this,io); 118 } 119 } 121 public void run() 123 { 124 for(;;) 125 { 126 if(abort) 127 return; 128 129 Socket client = null; 130 try 131 { 132 client = socket.accept(); 133 134 client.setSoTimeout(1000); 138 139 Log.log(Log.MESSAGE,this,client + ": connected"); 140 141 DataInputStream in = new DataInputStream( 142 client.getInputStream()); 143 144 if(!handleClient(client,in)) 145 abort = true; 146 } 147 catch(Exception e) 148 { 149 if(!abort) 150 Log.log(Log.ERROR,this,e); 151 abort = true; 152 } 153 finally 154 { 155 168 } 169 } 170 } 172 179 public static void handleClient(boolean restore, String parent, 180 String [] args) 181 { 182 handleClient(restore,false,false,parent,args); 183 } 185 194 public static Buffer handleClient(boolean restore, 195 boolean newView, boolean newPlainView, String parent, 196 String [] args) 197 { 198 if(jEdit.getFirstView() == null) 200 { 201 205 Buffer buffer = jEdit.openFiles(null,parent,args); 206 207 if(jEdit.getBufferCount() == 0) 208 jEdit.newFile(null); 209 210 boolean restoreFiles = restore 211 && jEdit.getBooleanProperty("restore") 212 && (buffer == null 213 || jEdit.getBooleanProperty("restore.cli")); 214 215 View view = PerspectiveManager.loadPerspective( 216 restoreFiles); 217 218 if(view == null) 219 { 220 if(buffer == null) 221 buffer = jEdit.getFirstBuffer(); 222 view = jEdit.newView(null,buffer); 223 } 224 else if(buffer != null) 225 view.setBuffer(buffer); 226 227 return buffer; 228 } 229 else if(newPlainView) 230 { 231 Buffer buffer = jEdit.openFiles(null,parent,args); 233 if(buffer == null) 234 buffer = jEdit.getFirstBuffer(); 235 jEdit.newView(null,buffer,true); 236 return buffer; 237 } 238 else if(newView) 239 { 240 Buffer buffer = jEdit.openFiles(null,parent,args); 242 if(buffer == null) 243 buffer = jEdit.getFirstBuffer(); 244 jEdit.newView(jEdit.getActiveView(),buffer,false); 245 return buffer; 246 } 247 else 248 { 249 View view = jEdit.getActiveView(); 251 252 Buffer buffer = jEdit.openFiles(view,parent,args); 253 254 if (jEdit.getBooleanProperty("server.brokenToFront")) 259 view.setState(java.awt.Frame.ICONIFIED); 260 261 view.setState(java.awt.Frame.NORMAL); 263 view.requestFocus(); 264 view.toFront(); 265 266 return buffer; 267 } 268 } 270 boolean isOK() 272 { 273 return ok; 274 } 276 public int getPort() 278 { 279 return socket.getLocalPort(); 280 } 282 void stopServer() 284 { 285 abort = true; 286 try 287 { 288 socket.close(); 289 } 290 catch(IOException io) 291 { 292 } 293 294 new File(portFile).delete(); 295 } 297 299 private String portFile; 301 private ServerSocket socket; 302 private int authKey; 303 private boolean ok; 304 private boolean abort; 305 307 private boolean handleClient(final Socket client, DataInputStream in) 309 throws Exception 310 { 311 int key = in.readInt(); 312 if(key != authKey) 313 { 314 Log.log(Log.ERROR,this,client + ": wrong" 315 + " authorization key (got " + key 316 + ", expected " + authKey + ")"); 317 in.close(); 318 client.close(); 319 320 return false; 321 } 322 else 323 { 324 client.setSoTimeout(0); 326 327 Log.log(Log.DEBUG,this,client + ": authenticated" 328 + " successfully"); 329 330 final String script = in.readUTF(); 331 Log.log(Log.DEBUG,this,script); 332 333 SwingUtilities.invokeLater(new Runnable () 334 { 335 public void run() 336 { 337 try 338 { 339 NameSpace ns = new NameSpace( 340 BeanShell.getNameSpace(), 341 "EditServer namespace"); 342 ns.setVariable("socket",client); 343 BeanShell.eval(null,ns,script); 344 } 345 catch(bsh.UtilEvalError e) 346 { 347 Log.log(Log.ERROR,this,e); 348 } 349 finally 350 { 351 try 352 { 353 BeanShell.getNameSpace().setVariable("socket",null); 354 } 355 catch(bsh.UtilEvalError e) 356 { 357 Log.log(Log.ERROR,this,e); 358 } 359 } 360 } 361 }); 362 363 return true; 364 } 365 } 367 } 369 | Popular Tags |