KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > roblisa > classfinder > ClassFinder


1 /**
2  * ClassFinder - a javadoc webserver.
3  * Copyright (C) 2003 Rob Nielsen
4  * rob@roblisa.com
5  * http://www.roblisa.com/java/classfinder/
6  *
7  * Released under the GNU GPL - http://www.gnu.org/copyleft/gpl.html
8  */

9 package com.roblisa.classfinder;
10
11 import java.io.*;
12 import java.net.*;
13 import java.util.*;
14 import java.text.*;
15
16 public class ClassFinder extends WebHandler
17 {
18     // define the mount points for the various structures
19
static final String DOC_ROOT="/doc/";
20     static final String SRC_ROOT="/src/";
21     static final String DOC_SUMMARY_ROOT="/doc-summary/";
22     static final String SEARCH="/search";
23     static final String SELECT="/select";
24
25     static final String DOC_SEARCH="/doc/search";
26
27     private static JavadocSummaryGenerator jsg=new JavadocSummaryGenerator();
28
29     private static Library lib;
30
31     public static void main(String[] args)
32     {
33         Properties props=new Properties();
34         try
35         {
36             props.load(new FileInputStream("classfinder.properties"));
37         }
38         catch(IOException e)
39         {
40              System.out.println("Can't load properties file!");
41         }
42         int port=8008;
43         try
44         {
45             port=Integer.parseInt(props.getProperty("serverPort","8008"));
46         }
47         catch(NumberFormatException e)
48         {
49             // go with the default
50
}
51         int interval=-1;
52         try
53         {
54             interval=Integer.parseInt(props.getProperty("interval","-1"));
55         }
56         catch(NumberFormatException e)
57         {
58             // go with the default
59
}
60
61
62         // fix for JDK 1.4.2 to allow shortcuts to work.
63
System.getProperties().put("swing.disableFileChooserSpeedFix","true");
64
65         String log=props.getProperty("logFile",null);
66         if (log!=null&&log.length()>0)
67         {
68             try
69             {
70                 System.out.println("Logging all output to "+log);
71                 PrintStream ps=new PrintStream(new FileOutputStream(log,true));
72                 System.setOut(ps);
73                 System.setErr(ps);
74                 WebHandler.setLog(ps);
75             }
76             catch(IOException e)
77             {
78                  System.out.println("Can't open log file: "+log);
79             }
80         }
81         System.out.println("______________________________________");
82         System.out.println("ClassFinder 1.11 starting on port "+port);
83         System.out.println(new Date());
84
85         // set proxies
86
String proxyHost=props.getProperty("proxyHost");
87         String proxyPort=props.getProperty("proxyPort");
88         if (proxyHost!=null&&proxyPort!=null)
89         {
90             System.getProperties().put("http.proxyHost",proxyHost);
91             System.getProperties().put("http.proxyPort",proxyPort);
92
93             System.out.print("Using proxy "+proxyHost+":"+proxyPort+" for URLs");
94
95             String proxyUser=props.getProperty("proxyUser");
96             String proxyPassword=props.getProperty("proxyPassword");
97             if (proxyUser!=null&&proxyPassword!=null)
98             {
99                 System.getProperties().put("http.proxyUser",proxyUser);
100                 System.getProperties().put("http.proxyPassword",proxyPassword);
101                 System.out.print(" (with authentication)");
102             }
103             System.out.println();
104         }
105
106         try
107         {
108             ServerSocket ss = new ServerSocket(port);
109
110             lib=new Library("deploy","static");
111             if (interval>0)
112             {
113                 lib.setInterval(interval);
114                 lib.watchDirectory(true);
115             }
116
117             while (true)
118             {
119                 Socket s = ss.accept();
120                 synchronized (handlers)
121                 {
122                     if (handlers.isEmpty())
123                     {
124                         WebHandler wh = new ClassFinder();
125                         wh.setSocket(s);
126                         (new Thread(wh)).start();
127                     }
128                     else
129                     {
130                         WebHandler wh = (WebHandler) handlers.remove(0);
131                         wh.setSocket(s);
132                     }
133                 }
134             }
135         }
136         catch(BindException be)
137         {
138             System.out.println("The server cannot start on port "+port+" as it is in use by another process.");
139         }
140         catch(IOException e)
141         {
142             e.printStackTrace();
143         }
144     }
145
146     public String getServerName()
147     {
148         return "ClassFinder";
149     }
150
151     public void handleGet(String path,PrintStream ps) throws IOException
152     {
153         // check for shutdown
154
if (path.startsWith("/shutdown"))
155         {
156             ps.println("<html><head><title>Server Shutdown</title></head>");
157             ps.println("<body><h1>Server Shutdown</h1>");
158             ps.print("The server is now shutdown. ");
159             ps.println("</body></html>");
160             sendResponse(HTTP_OK,"OK");
161             System.out.println("Server shutdown by user.");
162             System.exit(0);
163         }
164
165         // see if the required file is a static file
166
File file=new File("static"+path);
167         if (file.exists())
168         {
169             if (file.isDirectory())
170             {
171                 if (path.charAt(path.length()-1)!='/')
172                 {
173                     sendResponse(HTTP_MOVED_PERM,path+"/");
174                     return;
175                 }
176                 file=new File(file,"index.html");
177             }
178             if (file.exists())
179             {
180                 if (setLastModified(file.lastModified()))
181                 {
182                     getStream(new FileInputStream(file),ps);
183                     sendResponse(HTTP_OK,"OK");
184                     return;
185                 }
186                 else
187                 {
188                     sendResponse(HTTP_NOT_MODIFIED,"File Not Modified");
189                     return;
190                 }
191             }
192         }
193
194         // see if the required file is mapped
195
{
196             String urlStr=lib.getMappings().mapLocalToFile(path);
197             if (urlStr!=null)
198             {
199                 if (path.charAt(path.length()-1)=='/')
200                     urlStr+="index.html";
201                 URL url=new URL(urlStr);
202                 URLConnection con=url.openConnection();
203                 con.connect();
204                 long lastModified=con.getLastModified();
205                 if (setLastModified(lastModified))
206                 {
207                     getStream(url.openStream(),ps);
208                     sendResponse(HTTP_OK,"OK");
209                     return;
210                 }
211                 else
212                 {
213                     sendResponse(HTTP_NOT_MODIFIED,"File Not Modified");
214                     return;
215                 }
216             }
217         }
218
219         // process select packages
220
if (path.startsWith(SELECT))
221         {
222             selectResponse(ps,path);
223             sendResponse(HTTP_OK,"OK");
224             return;
225         }
226
227         // process general search
228

229         if (path.startsWith(SEARCH))
230         {
231             searchResponse(ps,path);
232             sendResponse(HTTP_OK,"OK");
233             return;
234         }
235
236         // process javadoc search
237

238             if (path.startsWith(DOC_SEARCH))
239         {
240             Map params=getParams(path);
241             String match=(String)params.get("q");
242             LibClass[] list=lib.getClasses(match,LibClass.DOC);
243             jsg.printClassList(ps,true,"Search Results",list);
244             sendResponse(HTTP_OK,"OK");
245             return;
246         }
247
248         // all other files could be modified by changes to the library,
249
// so set the last modified date here
250
if (!setLastModified(lib.getLastModified()))
251         {
252             sendResponse(HTTP_NOT_MODIFIED,"File not modified");
253             return;
254         }
255
256         // generate the file extension
257
String ext="";
258         int hash=path.lastIndexOf('#');
259         if (hash==-1)
260             hash=path.length();
261         int dot=path.lastIndexOf('.',hash);
262         if (dot!=-1)
263             ext=path.substring(dot,hash);
264
265         if (path.startsWith(DOC_SUMMARY_ROOT))
266         {
267             int slash=path.indexOf('/',13);
268             if (slash!=-1)
269             {
270                 try
271                 {
272                     int i=Integer.parseInt(path.substring(13,slash));
273                     int s2=path.indexOf('/',slash+1);
274                     if (s2!=-1&&!path.substring(slash+1,s2).equals("index-files"))
275                     {
276                         sendResponse(HTTP_MOVED_TEMP,DOC_ROOT+path.substring(slash+1));
277                         return;
278                     }
279                     else
280                     {
281                         List ja=lib.getJavadocRoots();
282                         if (i>=0&&i<ja.size())
283                         {
284                             String root=(String)ja.get(i);
285                             try
286                             {
287                                 URL url=new URL(root+path.substring(slash+1));
288                                 InputStream is=url.openStream();
289                                 if (is!=null)
290                                 {
291                                     if (ext.equals(".html"))
292                                         is=new URLFilterStream(is,path,null,lib,i,lib.getMappings());
293                                     getStream(is,ps);
294                                     sendResponse(HTTP_OK,"OK");
295                                     return;
296                                 }
297                             }
298                             catch(IOException e)
299                             {
300                                 System.out.println(e);
301                             }
302                             String fname=path.substring(slash+1);
303                             if (fname.equals("index-all.html"))
304                             {
305                                 sendResponse(HTTP_MOVED_TEMP,DOC_SUMMARY_ROOT+i+"/index-files/index-1.html");
306                                 return;
307                             }
308                             else
309                             if (fname.equals("overview-summary.html"))
310                             {
311                                 String pack=lib.getPackageForRoot(root);
312                                 if (pack!=null)
313                                 {
314                                     sendResponse(HTTP_MOVED_TEMP,DOC_ROOT+pack.replace('.','/')+"/package-summary.html");
315                                     return;
316                                 }
317                                 else
318                                 {
319                                     System.out.println("Collection has no overview-summary.html or package associated.");
320                                     System.out.println(root);
321                                 }
322                             }
323                         }
324                     }
325                 }
326                 catch(NumberFormatException e)
327                 {
328                     System.out.println("Not a number: "+path.substring(0,slash));
329                 }
330             }
331         }
332
333         boolean doc=path.startsWith(DOC_ROOT);
334
335         if (doc)
336         {
337             String fname=path.substring(5);
338
339             if (fname.equals("allclasses-frame.html")||fname.equals("allclasses-noframe.html"))
340             {
341                 boolean frame=path.length()==26;
342
343                 LibClass[] list=lib.getClasses(null,LibClass.DOC);
344                 jsg.printClassList(ps,frame,"All Classes",list);
345                 sendResponse(HTTP_OK,"OK");
346                 return;
347             }
348             else
349             if (fname.equals("overview-frame.html"))
350             {
351                 jsg.printOverviewFrame(ps,lib.getDocPackages());
352                 sendResponse(HTTP_OK,"OK");
353                 return;
354             }
355             else
356             if (fname.startsWith("index-files/index-"))
357             {
358                 sendResponse(HTTP_MOVED_TEMP,ClassFinder.DOC_ROOT+"index-all.html");
359                 return;
360             }
361             else
362             if (jsg.isSummaryFile(fname))
363             {
364                 jsg.printSplitupDocFile(ps,fname,lib.getJavadocTitles());
365                 sendResponse(HTTP_OK,"OK");
366                 return;
367             }
368         }
369
370         boolean SRC=path.startsWith(SRC_ROOT);
371
372         if (doc||src)
373         {
374
375             String getext=ext;
376             String dir=path.substring(5,dot);
377             LibPackage pck=lib.getPackage(dir);
378             LibClass cla=(LibClass)lib.lookupClass(dir.replace('/','.'));
379             String urlStr=doc?pck.getDocRoot():pck.getJavaRoot();
380             if (src&&urlStr==null)
381             {
382                 urlStr=pck.getClassRoot();
383                 getext=".class";
384             }
385             else
386             if (src&&ext.equals(".html"))
387             {
388                 getext=".java";
389             }
390             if (urlStr!=null)
391             {
392                 URL url=new URL(urlStr+dir+getext);
393                 if (ext.equals(".java")&&url.getProtocol().equals("file")&&isLocalHost())
394                 {
395                     sendResponse(HTTP_MOVED_TEMP,url.toString());
396                     return;
397                 }
398
399                 File tempFile=null;
400
401                 try
402                 {
403                     InputStream is=url.openStream();
404                     if (getext.equals(".class"))
405                     {
406                         tempFile=File.createTempFile("ClassFinder",".class");
407                         is=lib.decompile(is,tempFile);
408                     }
409                     if (is!=null)
410                     {
411                         if (doc&&getext.equals(".html"))
412                             is=new URLFilterStream(is,path,cla,lib,lib.getID(urlStr),lib.getMappings());
413                         if (cla!=null)
414                         {
415                             if (src&&ext.equals(".html"))
416                                 is=new JavaToHTMLFilterStream(is,cla,lib,true);
417                             lib.loaded(cla);
418                         }
419                         getStream(is,ps);
420                         sendResponse(HTTP_OK,"OK");
421                         if (tempFile!=null)
422                             tempFile.delete();
423                         return;
424                     }
425                 }
426                 catch(IOException e)
427                 {
428                     System.out.println(e);
429                     if (tempFile!=null)
430                         tempFile.delete();
431                 }
432             }
433             if (cla!=null)
434             {
435                 send404(ps,cla,path);
436                 sendResponse(HTTP_NOT_FOUND,"File Not Found");
437             }
438         }
439     }
440
441     public Map getParams(String path)
442     {
443         Map map=new HashMap();
444         int query=path.indexOf('?');
445         while(query!=-1)
446         {
447             int eq=path.indexOf('=',query+1);
448             String p=path.substring(query+1,eq);
449             query=path.indexOf('&',eq+1);
450             String v=path.substring(eq+1,query==-1?path.length():query);
451             try
452             {
453                 v=URLDecoder.decode(v,"UTF-8");
454             }
455             catch(UnsupportedEncodingException e)
456             {
457                 System.out.println("Can't decode UTF-8! Go figure.");
458             }
459             map.put(p,v);
460         }
461         return map;
462     }
463
464     public void selectResponse(PrintStream ps,String path) throws IOException
465     {
466         Map params=getParams(path);
467         File[] files=lib.getDeployFiles();
468
469         if (params.containsKey("submit"))
470         {
471             for(int i=0;i<files.length;i++)
472                 lib.block(files[i],params.containsKey("p"+i));
473             lib.reload();
474         }
475
476         ps.println("<html><head><title>Select</title></head><body>");
477         ps.println("Choose the packages to use.<br>");
478         ps.println("<form method=GET action=\"select\"><ul>");
479         for(int i=0;i<files.length;i++)
480         {
481             ps.print("<li><input type=\"checkbox\" name=\"p");
482             ps.print(i);
483             ps.print("\"");
484             if (!lib.isBlocked(files[i]))
485                 ps.print(" checked");
486             ps.print(">");
487             String name=files[i].getName();
488             if (files[i].isFile())
489             {
490                 int dot=name.lastIndexOf('.');
491                 if (dot!=-1)
492                     name=name.substring(0,dot);
493             }
494             ps.print(name);
495             ps.println("</input></li>");
496         }
497         ps.println("</ul>");
498         ps.println("<input type=\"submit\" name=\"submit\" value=\"Update\">");
499         ps.println("</form>");
500         ps.println("<a HREF=\"/\">Return</a> to the main page.</body></html>");
501     }
502
503     public void searchResponse(PrintStream ps,String path) throws IOException
504     {
505         LibClass[] classes=null;
506         Map params=getParams(path);
507         boolean frames=params.get("f")!=null;
508
509         String match=(String)params.get("q");
510         if (match!=null)
511             classes=lib.getClasses(match,0);
512
513         if (frames&&!path.startsWith("/searchf"))
514         {
515             ps.println("<html><head><title>Search</title></head>");
516             ps.println("<frameset rows=\"80%, 20%\">");
517             if (classes!=null&&classes.length>0)
518             {
519                 String dir=classes[0].hasResource(LibClass.DOC)?"doc/":"src/";
520                 ps.println("<frame name=\"view\" SRC=\""+dir+classes[0].getClassPath()+".html\">");
521             }
522             else
523                 ps.println("<frame name=\"view\" SRC=\"search-tips.html\">");
524             ps.print("<frame SRC=\"searchf?");
525             if (match!=null)
526                 ps.print("q="+URLEncoder.encode(match,"UTF-8")+"&");
527             ps.print("f=t\">");
528             ps.println("</frameset></html>");
529             return;
530         }
531         ps.println("<html><head><title>Search</title></head>");
532         ps.println("<body onLoad=\"document.f.q.focus()\">");
533         ps.print("<form name=\"f\" action=\"search\" target=\"_parent\" method=\"GET\">");
534         ps.print("<input type=\"text\" name=\"q\" size=\"30\" value=\"");
535         if (match!=null)
536             ps.print(match);
537         ps.println("\">");
538         ps.println("<input type=\"submit\" value=\"Search\">");
539         ps.print("<input type=\"checkbox\" name=\"f\" value=\"t\"");
540         if (frames)
541             ps.print(" checked");
542         ps.println(">Use Frames</input>");
543         ps.print(" <a HREF=\"search-tips.html\"");
544         if (frames)
545             ps.print(" target=\"view\"");
546         ps.println(">Search Tips</a>");
547         ps.print("- <a HREF=\"select\"");
548         if (frames)
549             ps.print(" target=\"view\"");
550         ps.println(">Select packages</a>");
551
552         ps.println("</form>");
553         if (classes!=null)
554         {
555             ps.println("<ul>");
556             for(int i=0;i<classes.length;i++)
557             {
558                 LibClass c=classes[i];
559                 ps.print("<li>");
560                 boolean doc=c.hasResource(LibClass.DOC);
561                 String dir=c.getClassPath();
562
563                 if (doc)
564                 {
565                     ps.print("<a HREF=\"doc/");
566                     ps.print(dir);
567                     ps.print(".html\"");
568                     if (frames)
569                         ps.print("target=\"view\"");
570                     ps.print(">");
571                 }
572                 String cla=c.getClassName();
573                 int dot=cla.lastIndexOf('.');
574                 ps.print(cla.substring(0,dot+1));
575                 ps.print("<b>");
576                 ps.print(cla.substring(dot+1));//,dot+1+match.length()));
577
ps.print("</b>");
578                 if (doc)
579                     ps.print("</a>");
580                 if (classes[i].hasResource(LibClass.SRC)||classes[i].hasResource(LibClass.CLA))
581                 {
582                     ps.print(" <a HREF=\"src/");
583                     ps.print(dir);
584                     ps.print(".html\"");
585                     if (frames)
586                         ps.print("target=\"view\"");
587                     ps.print(">(Source)</a>");
588                 }
589                 ps.println("</li>");
590             }
591             ps.println("</ul>");
592         }
593         ps.println("</body></html>");
594     }
595
596     void send404(PrintStream ps,LibClass cla,String path) throws IOException
597     {
598         if (cla==null)
599         {
600             ps.println("<html><head><title>File Not Found</title></head>");
601             ps.println("<body><h1>File Not Found</h1>");
602             ps.print("I have no idea what you are looking for. ");
603             ps.println("</body></html>");
604         }
605         else
606         {
607             boolean SRC=path.startsWith(SRC_ROOT);
608             ps.print("<html><head><title>No "+(src?"Source":"Javadoc")+" Found</title></head>");
609             ps.println("<body><h1>No "+(src?"Source":"Javadoc")+" Found</h1>");
610             ps.print("The "+(src?"source code":"javadoc page")+" for the class <b>");
611             ps.print(cla.getClassName());
612             ps.println("</b> is not available to ClassFinder.<p>");
613             if (src)
614             {
615                 if (cla.hasResource(LibClass.CLA))
616                 {
617                     ps.println("However, the class file is available. If you download ");
618                     ps.println("JAD from <a HREF=\"http://kpdus.tripod.com/jad.html\">");
619                     ps.println("http://kpdus.tripod.com/jad.html\"</a> and place the executable ");
620                     ps.println("(or a shortcut) in the deploy directory, you can view the ");
621                     ps.println("decompiled source code.");
622                 }
623                 if (cla.hasResource(LibClass.DOC))
624                 {
625                     ps.print("You can view the <a HREF=\""+DOC_ROOT);
626                     ps.print(cla.getClassPath());
627                     ps.print(".html\">javadoc</a>, though.");
628                 }
629             }
630             else
631             {
632                 String google="http://www.google.com/search?q="+URLEncoder.encode("allinurl:"+cla.getClassPath()+".html -\"class-use\"","UTF-8")+"&meta=lr%3Dlang_en&btnI=";
633
634                 ps.print("You could try a <a HREF=\"");
635                 ps.print(google);
636                 ps.print("\">searching the web</a> for it");
637                 if (cla.hasResource(LibClass.SRC)||cla.hasResource(LibClass.SRC))
638                 {
639                     ps.print(" or view the <a HREF=\""+SRC_ROOT);
640                     ps.print(cla.getClassPath());
641                     ps.print(".html\">source code</a>");
642                 }
643                 ps.println(".");
644             }
645             ps.println("</body></html>");
646         }
647     }
648 }
649
Popular Tags