1 24 25 26 package org.netbeans.modules.javadoc.httpfs; 27 28 import java.io.*; 29 import java.net.HttpURLConnection ; 30 import java.util.*; 31 32 import org.openide.cookies.InstanceCookie; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.Repository; 35 import org.openide.loaders.DataObject; 36 import org.openide.loaders.DataObjectNotFoundException; 37 38 43 class HTTPRootFileObject 44 extends HTTPFileObject implements Runnable { 45 46 private static final String IDE_SETTINGS_NAME = "Services/org-netbeans-core-IDESettings.settings"; private Thread refreshThread; 48 private Date lastRefreshDate; 49 private boolean threadIsRunning; 50 private boolean refreshPending; 51 52 private static boolean proxyInit; 53 54 61 HTTPRootFileObject( 62 HTTPFileSystem parentFileSystem 63 ) { 64 65 super( "/", parentFileSystem ); 67 refreshThread = new Thread ( this ); 69 threadIsRunning = true; 70 refreshPending = false; 71 refreshThread.start( ); 72 73 } 74 75 76 82 protected HTTPRootFileObject( 83 ) { 84 85 super( ); 86 87 } 88 89 90 95 void triggerRefresh( ) { 96 97 refreshPending = true; 98 refreshThread.interrupt( ); 99 100 } 101 102 103 109 public void run( ) { 110 111 int refreshInterval; 113 Calendar nextRefreshTime; 115 boolean docsHaveChanged; 117 118 119 refreshRootContents( ); 121 refreshThread.setPriority( Thread.MIN_PRIORITY ); 122 123 while( threadIsRunning ) { 124 125 docsHaveChanged = false; 126 127 refreshInterval = parentFileSystem.getRefreshRate( ); 129 if( refreshInterval > 0 ) { 130 131 nextRefreshTime = new GregorianCalendar( ); 133 nextRefreshTime.setTime( lastRefreshDate ); 134 nextRefreshTime.add( Calendar.MINUTE, refreshInterval ); 135 136 if( nextRefreshTime.before( new GregorianCalendar( ) ) ) { 138 139 docsHaveChanged = hasDocumentationChanged( ); 141 142 } 143 144 } 145 146 if( docsHaveChanged || refreshPending ) { 148 149 refreshPending = false; 151 refreshRootContents( ); 152 153 } 154 155 try { 156 157 Thread.sleep( 60 * 1000 ); 159 160 } catch( InterruptedException e ) { 161 162 164 } 165 166 } 167 168 } 169 170 171 179 private boolean hasDocumentationChanged( 180 ) { 181 182 HTTPFileObject packageFile; 184 HttpURLConnection fileConnection; 186 Date currentPackageFileDate; 188 boolean hasChanged; 190 191 192 packageFile = child( "package-list", false ); 195 if( packageFile != null ) { 197 198 try { 199 200 currentPackageFileDate = packageFile.lastModified( ); 202 fileConnection = (HttpURLConnection )packageFile.fileURL.openConnection( ); 203 fileConnection.setRequestMethod( "HEAD" ); 205 hasChanged = currentPackageFileDate.before( new Date( fileConnection.getLastModified( ) ) ); 206 207 fileConnection.disconnect( ); 208 209 } catch( IOException e ) { 210 211 hasChanged = false; 213 214 } 215 216 } else { 218 219 hasChanged = true; 221 222 } 223 return hasChanged; 224 225 } 226 227 228 233 private void refreshRootContents( ) { 234 initHTTPProxyHack(); 235 HTTPFileObject packageFile; 237 HTTPFileObject indexDirectory; 239 BufferedReader packageReader; 241 String packageName; 243 int indexFileNumber; 245 246 247 parentFileSystem.setState( HTTPFileSystem.STATE_READING ); 249 250 removeAllChildren( ); 252 253 if( addOptionalChild( "/package-list" ) ) { 256 packageFile = child( "package-list", false ); addChild( "/allclasses-frame.html" ); addOptionalChild( "/deprecated-list.html" ); addOptionalChild( "/help-doc.html" ); addOptionalChild( "/index.html" ); addChild( "/overview-frame.html" ); addChild( "/overview-summary.html" ); addOptionalChild( "/overview-tree.html" ); addChild( "/packages.html" ); addChild( "/serialized-form.html" ); addChild( "/stylesheet.css" ); 268 if( !addOptionalChild( "/index-all.html" ) ) { 271 indexFileNumber = 1; 273 while( addOptionalChild( "/index-" + indexFileNumber + ".html" ) ) { 275 indexFileNumber++; 276 277 } 278 279 if( indexFileNumber == 1 ) { 281 282 indexDirectory = new HTTPFileObject( "/index-files/", parentFileSystem ); if( !indexDirectory.addOptionalChild( "/index-files/index-all.html" ) ) { 287 indexFileNumber = 1; 289 while( indexDirectory.addOptionalChild( "/index-files/index-" + indexFileNumber + ".html" ) ) { 291 indexFileNumber++; 292 293 } 294 if( indexFileNumber != 1 ) { 296 297 addChild( indexDirectory ); 298 299 } 300 301 303 } else { 304 305 addChild( indexDirectory ); 306 307 } 308 309 } 310 311 } 312 try { 313 314 packageReader = new BufferedReader( new InputStreamReader( packageFile.getInputStream( ) ) ); 316 packageName = packageReader.readLine( ); 317 while( packageName != null ) { 318 319 addPackage( packageName ); 321 packageName = packageReader.readLine( ); 322 323 } 324 packageReader.close( ); 325 326 } catch( IOException e ) { 327 328 330 } 331 332 } 333 lastRefreshDate = new Date( ); 334 335 parentFileSystem.setState( HTTPFileSystem.STATE_COMPLETE ); 337 338 } 339 340 341 348 private void addPackage( String packageName ) { 349 350 StringTokenizer packageParser; 352 String packagePart; 354 HTTPFileObject packageDirectory; 356 357 358 packageParser = new StringTokenizer( packageName, "." ); packageDirectory = this; 361 362 while( packageParser.hasMoreElements( ) ) { 364 365 packagePart = (String )packageParser.nextElement( ); 366 367 if( packageDirectory.child( packagePart, false ) == null ) { 369 370 packageDirectory.addChild( packageDirectory.uriStem + packagePart + "/" ); 372 } 373 packageDirectory = packageDirectory.child( packagePart, false ); 374 375 } 376 packageDirectory.makePackage( ); 378 379 } 380 381 382 389 protected void finalize( 390 ) throws Throwable { 391 392 super.finalize( ); 393 394 threadIsRunning = false; 396 refreshThread.interrupt( ); 397 398 } 399 400 406 static void initHTTPProxyHack() { 407 if (proxyInit) 408 return; 409 FileObject f = Repository.getDefault().getDefaultFileSystem().findResource(IDE_SETTINGS_NAME); 410 try { 411 DataObject d = DataObject.find(f); 412 InstanceCookie ic = (InstanceCookie)d.getCookie(InstanceCookie.class); 413 if (ic != null) { 414 Object o = ic.instanceCreate(); proxyInit = true; 417 } 418 } catch (DataObjectNotFoundException ex) { 419 } catch (IOException ex) { 420 proxyInit = true; 421 } catch (ClassNotFoundException ex) { 422 proxyInit = true; 423 } 424 } 425 } 426 | Popular Tags |