1 23 24 package org.objectweb.clif.supervisor.lib; 25 26 import java.util.Map ; 27 import java.util.HashMap ; 28 import java.util.Enumeration ; 29 import java.util.StringTokenizer ; 30 import java.util.zip.ZipEntry ; 31 import java.util.jar.JarFile ; 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.FileInputStream ; 36 import java.io.DataOutputStream ; 37 import java.io.DataInputStream ; 38 import java.net.ServerSocket ; 39 import java.net.Socket ; 40 import java.net.SocketException ; 41 42 43 62 public class CodeServer extends ServerSocket implements Runnable 63 { 64 static public final int NO_SUCH_RESOURCE = -1; 65 static public final int RESOURCE_TOO_BIG = -2; 66 static public final String PATH_PROPERTY = "clif.codeserver.path"; 67 static public final String DEFAULT_PATH = ""; 68 static public final String PATH_SEPARATOR = ";"; 69 static public final String EXTENSION_DIR = "./lib/ext/"; 70 static private Map libExtMap = new HashMap (); 71 static private File [] paths; 72 73 static 74 { 75 try 77 { 78 File dir = new File (EXTENSION_DIR); 79 if (dir.canRead()) 80 { 81 String [] list = dir.list(); 82 for (int i=0 ; i<list.length ; ++i) 83 { 84 if (list[i].endsWith(".jar")) 85 { 86 JarFile jar = new JarFile (EXTENSION_DIR + list[i]); 87 Enumeration entries = jar.entries(); 88 while (entries.hasMoreElements()) 89 { 90 libExtMap.put( 91 ((ZipEntry )entries.nextElement()).getName(), 92 jar); 93 } 94 } 95 } 96 } 97 } 98 catch (IOException ex) 99 { 100 System.err.println( 101 "Warning - problem while indexing " + EXTENSION_DIR + " Jar files.\n" + ex); 102 ex.printStackTrace(System.err); 103 } 104 StringTokenizer parser = new StringTokenizer ( 106 System.getProperty(PATH_PROPERTY, DEFAULT_PATH), 107 PATH_SEPARATOR); 108 paths = new File [parser.countTokens()]; 109 for (int i=0 ; i<paths.length ; ++i) 110 { 111 paths[i] = new File (parser.nextToken()); 112 } 113 } 114 115 116 120 public CodeServer(int port) 121 throws IOException 122 { 123 super(port); 124 setReuseAddress(true); 125 new Thread (this, "code server").start(); 126 } 127 128 129 public void run() 130 { 131 while (true) 132 { 133 try 134 { 135 new ClassServerRequest(accept()); 136 } 137 catch (IOException ex) 138 { 139 ex.printStackTrace(System.err); 140 } 141 } 142 } 143 144 145 class ClassServerRequest extends Thread 146 { 147 Socket sock; 148 149 150 public ClassServerRequest(Socket sock) 151 { 152 super("code server request handler " + sock); 153 this.sock = sock; 154 try 155 { 156 sock.setReuseAddress(true); 157 sock.setSoLinger(false, -1); 158 } 159 catch (SocketException ex) 160 { 161 ex.printStackTrace(System.err); 162 } 163 start(); 164 } 165 166 167 public void run() 168 { 169 DataOutputStream douts; 170 DataInputStream dins; 171 try 172 { 173 douts = new DataOutputStream (sock.getOutputStream()); 174 dins = new DataInputStream (sock.getInputStream()); 175 } 176 catch (IOException ex) 177 { 178 throw new Error ("can't properly handle incoming connection to codeserver", ex); 179 } 180 while (sock != null) 181 { 182 try 183 { 184 String filename = dins.readUTF(); 185 File inputFile = new File (filename); 187 JarFile jar = (JarFile )libExtMap.get(filename); 188 if (jar == null) 189 { 190 for (int i=0 ; i<paths.length && !inputFile.canRead() ; ++i) 191 { 192 inputFile = new File (paths[i], filename); 193 } 194 } 195 if (jar == null && ! inputFile.canRead()) 196 { 197 douts.writeInt(NO_SUCH_RESOURCE); 198 douts.flush(); 199 } 200 else 201 { 202 long file_length = 0; 203 ZipEntry entry = null; 204 if (jar == null) 205 { 206 file_length = inputFile.length(); 207 } 208 else 209 { 210 entry = jar.getEntry(filename); 211 file_length = entry.getSize(); 212 } 213 InputStream is; 214 if (jar != null) 215 { 216 is = jar.getInputStream(entry); 217 } 218 else 219 { 220 is = new FileInputStream (inputFile); 221 } 222 if (file_length > Integer.MAX_VALUE) 223 { 224 douts.writeInt(RESOURCE_TOO_BIG); 225 douts.flush(); 226 } 227 else 228 { 229 douts.writeInt((int)file_length); 230 byte[] buffer = new byte[(int)file_length]; 231 while (file_length > 0) 232 { 233 int n = is.read(buffer, 0, (int)file_length); 234 douts.write(buffer, 0, n); 235 file_length -= n; 236 } 237 douts.flush(); 238 } 239 } 240 } 241 catch (IOException ex) 242 { 243 try 244 { 245 sock.close(); 246 } 247 catch (IOException exc) 248 { 249 System.err.println("Exception in codeserver while closing connection"); 250 ex.printStackTrace(System.err); 251 } 252 System.err.println("Exception in code server while serving classes"); 253 ex.printStackTrace(System.err); 254 sock = null; 255 } 256 } 257 } 258 } 259 } 260 | Popular Tags |