Changeset 30731 in osm for applications/editors/josm/plugins/opendata/src
- Timestamp:
- 2014-10-18T15:31:53+02:00 (10 years ago)
- Location:
- applications/editors/josm/plugins/opendata/src/org
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/opendata/src/org/geotools/data/shapefile/files/TabFiles.java
r30566 r30731 47 47 48 48 import org.geotools.data.DataUtilities; 49 import org.openstreetmap.josm.Main; 49 50 50 51 /** … … 52 53 */ 53 54 public class TabFiles extends ShpFiles { 54 55 55 56 /** 56 57 * The urls for each type of file that is associated with the shapefile. The … … 60 61 61 62 /** 62 * A read/write lock, so that we can have concurrent readers 63 * A read/write lock, so that we can have concurrent readers 63 64 */ 64 65 private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); … … 74 75 */ 75 76 private final MemoryMapCache mapCache = new MemoryMapCache(); 76 77 77 78 private boolean memoryMapCacheEnabled; 78 79 79 80 80 81 //////////////////////////////////////////////////// 81 82 82 83 public TabFiles(File headerFile, File dataFile) throws IllegalArgumentException { 83 84 super(fakeShpFile(headerFile)); // Useless but necessary … … 85 86 urls.put(ShpFileType.DBF, DataUtilities.fileToURL(dataFile)); 86 87 } 87 88 88 89 /** 89 90 * This is really ugly. Used only to give a fake shp file to ShpFiles constructor to avoid IllegalArgument at initialization. … … 92 93 return DataUtilities.fileToURL(new File(headerFile.getAbsolutePath()+".shp")); 93 94 } 94 95 95 96 private String baseName(Object obj) { 96 97 if (obj instanceof URL) { … … 99 100 return null; 100 101 } 101 102 102 103 private String toBase(String path) { 103 104 return path.substring(0, path.toLowerCase().lastIndexOf(".tab")); … … 105 106 106 107 //////////////////////////////////////////////////// 107 108 108 109 private void init(URL url) { 109 110 String base = baseName(url); … … 119 120 120 121 for (ShpFileType type : ShpFileType.values()) { 121 122 122 123 String extensionWithPeriod = type.extensionWithPeriod; 123 124 if (upperCase) { … … 126 127 extensionWithPeriod = extensionWithPeriod.toLowerCase(); 127 128 } 128 129 129 130 URL newURL; 130 131 String string = base + extensionWithPeriod; … … 139 140 140 141 // if the files are local check each file to see if it exists 141 // if not then search for a file of the same name but try all combinations of the 142 // if not then search for a file of the same name but try all combinations of the 142 143 // different cases that the extension can be made up of. 143 144 // IE Shp, SHP, Shp, ShP etc... … … 166 167 File[] files = directory.listFiles(new FilenameFilter(){ 167 168 169 @Override 168 170 public boolean accept(File dir, String name) { 169 171 return file.getName().equalsIgnoreCase(name); 170 172 } 171 173 172 174 }); 173 175 if( files.length>0 ){ … … 175 177 return files[0].toURI().toURL(); 176 178 } catch (MalformedURLException e) { 177 //ShapefileDataStoreFactory.LOGGER().log(Level.SEVERE, "",e);179 Main.error(e); 178 180 } 179 181 } … … 190 192 } 191 193 194 @Override 192 195 public void dispose() { 193 196 if (numberOfLocks() != 0) { … … 200 203 /** 201 204 * Writes to the log all the lockers and when they were constructed. 202 * 205 * 203 206 * @param logLevel 204 207 * the level at which to log. 205 208 */ 209 @Override 206 210 public void logCurrentLockers(Level logLevel) { 207 211 for (Collection<ShpFilesLocker> lockerList : lockers.values()) { … … 209 213 StringBuilder sb = new StringBuilder("The following locker still has a lock: "); 210 214 sb.append(locker); 211 //ShapefileDataStoreFactory.LOGGER().log(logLevel,sb.toString(), locker.getTrace());215 Main.error(sb.toString()); 212 216 } 213 217 } … … 217 221 * Returns the URLs (in string form) of all the files for the shapefile 218 222 * datastore. 219 * 223 * 220 224 * @return the URLs (in string form) of all the files for the shapefile 221 225 * datastore. 222 226 */ 227 @Override 223 228 public Map<ShpFileType, String> getFileNames() { 224 229 Map<ShpFileType, String> result = new HashMap<>(); … … 235 240 * Returns the string form of the url that identifies the file indicated by 236 241 * the type parameter or null if it is known that the file does not exist. 237 * 242 * 238 243 * <p> 239 244 * Note: a URL should NOT be constructed from the string instead the URL 240 245 * should be obtained through calling one of the aquireLock methods. 241 * 246 * 242 247 * @param type 243 248 * indicates the type of file the caller is interested in. 244 * 249 * 245 250 * @return the string form of the url that identifies the file indicated by 246 251 * the type parameter or null if it is known that the file does not 247 252 * exist. 248 253 */ 254 @Override 249 255 public String get(ShpFileType type) { 250 256 return urls.get(type).toExternalForm(); … … 255 261 * is not thread safe so do not count on it to have a completely accurate 256 262 * picture but it can be useful debugging 257 * 263 * 258 264 * @return the number of locks on the current set of shapefile files. 259 265 */ 266 @Override 260 267 public int numberOfLocks() { 261 268 int count = 0; … … 269 276 * get*Channel methods are used when reading or writing to the file is 270 277 * desired. 271 * 272 * 278 * 279 * 273 280 * @see #getInputStream(ShpFileType, FileReader) 274 281 * @see #getReadChannel(ShpFileType, FileReader) 275 282 * @see #getWriteChannel(ShpFileType, FileReader) 276 * 283 * 277 284 * @param type 278 285 * the type of the file desired. … … 280 287 * the object that is requesting the File. The same object 281 288 * must release the lock and is also used for debugging. 282 * @return the File type requested 283 */ 289 * @return the File type requested 290 */ 291 @Override 284 292 public File acquireReadFile(ShpFileType type, 285 293 FileReader requestor) { … … 290 298 return DataUtilities.urlToFile(url); 291 299 } 292 300 293 301 /** 294 302 * Acquire a URL for read only purposes. It is recommended that get*Stream or 295 303 * get*Channel methods are used when reading or writing to the file is 296 304 * desired. 297 * 298 * 305 * 306 * 299 307 * @see #getInputStream(ShpFileType, FileReader) 300 308 * @see #getReadChannel(ShpFileType, FileReader) 301 309 * @see #getWriteChannel(ShpFileType, FileReader) 302 * 310 * 303 311 * @param type 304 312 * the type of the file desired. … … 308 316 * @return the URL to the file of the type requested 309 317 */ 318 @Override 310 319 public URL acquireRead(ShpFileType type, FileReader requestor) { 311 320 URL url = urls.get(type); 312 321 if (url == null) 313 322 return null; 314 323 315 324 readWriteLock.readLock().lock(); 316 325 Collection<ShpFilesLocker> threadLockers = getCurrentThreadLockers(); … … 326 335 * desired. 327 336 * </p> 328 * 337 * 329 338 * @see #getInputStream(ShpFileType, FileReader) 330 339 * @see #getReadChannel(ShpFileType, FileReader) 331 340 * @see #getWriteChannel(ShpFileType, FileReader) 332 * 341 * 333 342 * @param type 334 343 * the type of the file desired. … … 338 347 * @return A result object containing the URL or the reason for the failure. 339 348 */ 349 @Override 340 350 public Result<URL, State> tryAcquireRead(ShpFileType type, 341 351 FileReader requestor) { … … 344 354 return new Result<>(null, State.NOT_EXIST); 345 355 } 346 356 347 357 boolean locked = readWriteLock.readLock().tryLock(); 348 358 if (!locked) { 349 359 return new Result<>(null, State.LOCKED); 350 360 } 351 361 352 362 getCurrentThreadLockers().add(new ShpFilesLocker(url, requestor)); 353 363 … … 358 368 * Unlocks a read lock. The file and requestor must be the the same as the 359 369 * one of the lockers. 360 * 370 * 361 371 * @param file 362 372 * file that was locked … … 364 374 * the class that requested the file 365 375 */ 376 @Override 366 377 public void unlockRead(File file, FileReader requestor) { 367 378 Collection<URL> allURLS = urls.values(); … … 376 387 * Unlocks a read lock. The url and requestor must be the the same as the 377 388 * one of the lockers. 378 * 389 * 379 390 * @param url 380 391 * url that was locked … … 382 393 * the class that requested the url 383 394 */ 395 @Override 384 396 public void unlockRead(URL url, FileReader requestor) { 385 397 if (url == null) { … … 404 416 405 417 /** 406 * Acquire a File for read and write purposes. 418 * Acquire a File for read and write purposes. 407 419 * <p> It is recommended that get*Stream or 408 420 * get*Channel methods are used when reading or writing to the file is 409 421 * desired. 410 422 * </p> 411 * 423 * 412 424 * @see #getInputStream(ShpFileType, FileReader) 413 425 * @see #getReadChannel(ShpFileType, FileReader) 414 426 * @see #getWriteChannel(ShpFileType, FileReader) 415 427 416 * 428 * 417 429 * @param type 418 430 * the type of the file desired. … … 422 434 * @return the File to the file of the type requested 423 435 */ 436 @Override 424 437 public File acquireWriteFile(ShpFileType type, 425 438 FileWriter requestor) { … … 431 444 } 432 445 /** 433 * Acquire a URL for read and write purposes. 446 * Acquire a URL for read and write purposes. 434 447 * <p> It is recommended that get*Stream or 435 448 * get*Channel methods are used when reading or writing to the file is 436 449 * desired. 437 450 * </p> 438 * 451 * 439 452 * @see #getInputStream(ShpFileType, FileReader) 440 453 * @see #getReadChannel(ShpFileType, FileReader) 441 454 * @see #getWriteChannel(ShpFileType, FileReader) 442 455 443 * 456 * 444 457 * @param type 445 458 * the type of the file desired. … … 449 462 * @return the URL to the file of the type requested 450 463 */ 464 @Override 451 465 public URL acquireWrite(ShpFileType type, FileWriter requestor) { 452 466 URL url = urls.get(type); … … 460 474 readWriteLock.writeLock().lock(); 461 475 threadLockers.add(new ShpFilesLocker(url, requestor)); 462 mapCache.cleanFileCache(url); 476 mapCache.cleanFileCache(url); 463 477 return url; 464 478 } 465 479 466 480 /** 467 481 * Tries to acquire a URL for read/write purposes. Returns null if the … … 471 485 * desired. 472 486 * </p> 473 * 487 * 474 488 * @see #getInputStream(ShpFileType, FileReader) 475 489 * @see #getReadChannel(ShpFileType, FileReader) 476 490 * @see #getWriteChannel(ShpFileType, FileReader) 477 491 478 * 492 * 479 493 * @param type 480 494 * the type of the file desired. … … 484 498 * @return A result object containing the URL or the reason for the failure. 485 499 */ 500 @Override 486 501 public Result<URL, State> tryAcquireWrite(ShpFileType type, 487 502 FileWriter requestor) { 488 503 489 504 URL url = urls.get(type); 490 505 if (url == null) { … … 511 526 * Unlocks a read lock. The file and requestor must be the the same as the 512 527 * one of the lockers. 513 * 528 * 514 529 * @param file 515 530 * file that was locked … … 517 532 * the class that requested the file 518 533 */ 534 @Override 519 535 public void unlockWrite(File file, FileWriter requestor) { 520 536 Collection<URL> allURLS = urls.values(); … … 529 545 * Unlocks a read lock. The requestor must be have previously obtained a 530 546 * lock for the url. 531 * 532 * 547 * 548 * 533 549 * @param url 534 550 * url that was locked … … 536 552 * the class that requested the url 537 553 */ 554 @Override 538 555 public void unlockWrite(URL url, FileWriter requestor) { 539 556 if (url == null) { … … 551 568 + " to have locked the url but it does not hold the lock for the URL"); 552 569 } 553 570 554 571 if(threadLockers.size() == 0) { 555 572 lockers.remove(Thread.currentThread()); … … 560 577 readWriteLock.writeLock().unlock(); 561 578 } 562 579 563 580 /** 564 581 * Returns the list of lockers attached to a given thread, or creates it if missing … … 586 603 } 587 604 } 588 605 589 606 /** 590 607 * Re-takes the read locks in preparation for lock downgrade … … 602 619 /** 603 620 * Determine if the location of this shapefile is local or remote. 604 * 621 * 605 622 * @return true if local, false if remote 606 623 */ … … 613 630 * files cannot be deleted return false. 614 631 */ 632 @Override 615 633 public boolean delete() { 616 634 BasicShpFileWriter requestor = new BasicShpFileWriter("ShpFiles for deleting all files"); … … 638 656 * Opens a input stream for the indicated file. A read lock is requested at 639 657 * the method call and released on close. 640 * 658 * 641 659 * @param type 642 660 * the type of file to open the stream to. … … 644 662 * the object requesting the stream 645 663 * @return an input stream 646 * 664 * 647 665 * @throws IOException 648 666 * if a problem occurred opening the stream. 649 667 */ 668 @Override 650 669 public InputStream getInputStream(ShpFileType type, 651 670 final FileReader requestor) throws IOException { … … 684 703 } 685 704 } 686 705 687 706 /** 688 707 * Opens a output stream for the indicated file. A write lock is requested at 689 708 * the method call and released on close. 690 * 709 * 691 710 * @param type 692 711 * the type of file to open the stream to. … … 694 713 * the object requesting the stream 695 714 * @return an output stream 696 * 715 * 697 716 * @throws IOException 698 717 * if a problem occurred opening the stream. 699 718 */ 719 @Override 700 720 public OutputStream getOutputStream(ShpFileType type, 701 721 final FileWriter requestor) throws IOException { … … 703 723 704 724 try { 705 725 706 726 OutputStream out; 707 727 if (isLocalTab()) { … … 713 733 out = connection.getOutputStream(); 714 734 } 715 735 716 736 FilterOutputStream output = new FilterOutputStream(out) { 717 737 … … 755 775 * channel is closed. 756 776 * </p> 757 * 777 * 758 778 * @param type 759 779 * the type of file to open the channel to. 760 780 * @param requestor 761 781 * the object requesting the channel 762 * 763 */ 782 * 783 */ 784 @Override 764 785 public ReadableByteChannel getReadChannel(ShpFileType type, 765 786 FileReader requestor) throws IOException { … … 770 791 771 792 File file = DataUtilities.urlToFile(url); 772 793 773 794 @SuppressWarnings("resource") 774 795 RandomAccessFile raf = new RandomAccessFile(file, "r"); … … 801 822 * a generic channel for remote urls, however both shape and dbf writing can 802 823 * only occur with a local FileChannel channel. 803 * 824 * 804 825 * <p> 805 826 * A write lock is obtained when this method is called and released when the 806 827 * channel is closed. 807 828 * </p> 808 * 809 * 829 * 830 * 810 831 * @param type 811 832 * the type of file to open the stream to. 812 833 * @param requestor 813 834 * the object requesting the stream 814 * 835 * 815 836 * @return a WritableByteChannel for the provided file type 816 * 837 * 817 838 * @throws IOException 818 839 * if there is an error opening the stream 819 840 */ 841 @Override 820 842 public WritableByteChannel getWriteChannel(ShpFileType type, 821 843 FileWriter requestor) throws IOException { … … 860 882 * Obtains a Storage file for the type indicated. An id is provided so that 861 883 * the same file can be obtained at a later time with just the id 862 * 884 * 863 885 * @param type 864 886 * the type of file to create and return 865 * 887 * 866 888 * @return StorageFile 867 889 * @throws IOException 868 890 * if temporary files cannot be created 869 891 */ 892 @Override 870 893 public StorageFile getStorageFile(ShpFileType type) throws IOException { 871 894 String baseName = getTypeName(); … … 877 900 } 878 901 902 @Override 879 903 public String getTypeName() { 880 904 String path = SHP.toBase(urls.get(SHP)); … … 888 912 return path.substring(slash, dot); 889 913 } 890 914 891 915 /** 892 916 * Internal method that the file channel decorators will call to allow reuse of the memory mapped buffers … … 899 923 * @throws IOException 900 924 */ 925 @Override 901 926 MappedByteBuffer map(FileChannel wrapped, URL url, MapMode mode, long position, long size) throws IOException { 902 927 if(memoryMapCacheEnabled) { … … 906 931 } 907 932 } 908 933 909 934 /** 910 935 * Returns the status of the memory map cache. When enabled the memory mapped portions of the files are cached and shared … … 912 937 * @param memoryMapCacheEnabled 913 938 */ 939 @Override 914 940 public boolean isMemoryMapCacheEnabled() { 915 941 return memoryMapCacheEnabled; … … 921 947 * @param memoryMapCacheEnabled 922 948 */ 949 @Override 923 950 public void setMemoryMapCacheEnabled(boolean memoryMapCacheEnabled) { 924 951 this.memoryMapCacheEnabled = memoryMapCacheEnabled; … … 931 958 * Returns true if the file exists. Throws an exception if the file is not 932 959 * local. 933 * 960 * 934 961 * @param fileType 935 962 * the type of file to check existance for. 936 * 963 * 937 964 * @return true if the file exists. 938 * 965 * 939 966 * @throws IllegalArgumentException 940 967 * if the files are not local. -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/AbstractModule.java
r30723 r30731 10 10 import java.util.List; 11 11 12 import org.openstreetmap.josm.Main; 12 13 import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry; 13 14 import org.openstreetmap.josm.gui.preferences.SourceEntry; … … 24 25 25 26 protected final ModuleInformation info; 26 27 27 28 public AbstractModule(ModuleInformation info) { 28 29 this.info = info; … … 50 51 ExtendedSourceEntry src; 51 52 if (handler != null && (src = handler.getMapPaintStyle()) != null) { 52 try { 53 // Copy style sheet to disk to allow JOSM to load it at startup (even making the plugin "early" does not allow it) 54 String path = OdPlugin.getInstance().getResourcesDirectory()+File.separator+src.url.replace(OdConstants.PROTO_RSRC, "").replace('/', File.separatorChar); 55 56 int n = 0; 57 byte[] buffer = new byte[4096]; 58 InputStream in = getClass().getResourceAsStream(src.url.substring(OdConstants.PROTO_RSRC.length()-1)); 59 new File(path.substring(0, path.lastIndexOf(File.separatorChar))).mkdirs(); 60 FileOutputStream out = new FileOutputStream(path); 53 // Copy style sheet to disk to allow JOSM to load it at startup 54 // (even making the plugin "early" does not allow it) 55 String path = OdPlugin.getInstance().getResourcesDirectory() + File.separator 56 + src.url.replace(OdConstants.PROTO_RSRC, "").replace('/', File.separatorChar); 57 58 int n = 0; 59 byte[] buffer = new byte[4096]; 60 try (InputStream in = getClass().getResourceAsStream( 61 src.url.substring(OdConstants.PROTO_RSRC.length()-1)); 62 FileOutputStream out = new FileOutputStream(path)) { 63 String dir = path.substring(0, path.lastIndexOf(File.separatorChar)); 64 if (new File(dir).mkdirs() && Main.isDebugEnabled()) { 65 Main.debug("Created directory: "+dir); 66 } 61 67 while ((n = in.read(buffer)) > 0) { 62 68 out.write(buffer, 0, n); 63 69 } 64 out.close();65 in.close();66 67 70 // Add source pointing to the local file 68 71 src.url = OdConstants.PROTO_FILE+path; 69 72 sources.add(src); 70 73 } catch (IOException e) { 71 System.err.println(e.getMessage());74 Main.error(e.getMessage()); 72 75 } 73 76 } … … 96 99 }; 97 100 } 98 101 99 102 @Override 100 103 public final List<AbstractDataSetHandler> getNewlyInstanciatedHandlers() { … … 104 107 try { 105 108 result.add(handlerClass.newInstance()); 106 } catch ( Throwablet) {107 System.err.println("Cannot instantiate "+handlerClass+" because of "+t.getClass().getName()+": "+t.getMessage());109 } catch (InstantiationException | IllegalAccessException t) { 110 Main.error("Cannot instantiate "+handlerClass+" because of "+t.getClass().getName()+": "+t.getMessage()); 108 111 } 109 112 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ModuleHandler.java
r30723 r30731 247 247 + "Delete from preferences?</html>", module.name, module.className); 248 248 } 249 } catch ( Throwablee) {250 e.printStackTrace();249 } catch (Exception e) { 250 Main.error(e); 251 251 } 252 252 if (msg != null && confirmDisableModule(parent, msg, module.name)) { … … 550 550 551 551 final File[] files = moduleDir.listFiles(new FilenameFilter() { 552 @Override 552 553 public boolean accept(File dir, String name) { 553 554 return name.endsWith(".jar.new"); -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ModuleInformation.java
r30723 r30731 143 143 this.attr.putAll(other.attr); 144 144 } 145 145 146 146 private static final ImageIcon extractIcon(String iconPath, File jarFile, boolean suppressWarnings) { 147 147 return new ImageProvider(iconPath).setArchive(jarFile).setMaxWidth(24).setMaxHeight(24).setOptional(true).setSuppressWarnings(suppressWarnings).get(); … … 234 234 try { 235 235 return klass.getConstructor(ModuleInformation.class).newInstance(this); 236 } catch ( Throwablet) {236 } catch (Exception t) { 237 237 throw new ModuleException(name, t); 238 238 } … … 251 251 try{ 252 252 return (Class<? extends Module>) Class.forName(className, true, classLoader); 253 } catch ( Throwablet) {253 } catch (Exception t) { 254 254 throw new ModuleException(name, t); 255 255 } … … 260 260 return f.toURI().toURL(); 261 261 } catch (MalformedURLException ex) { 262 Main.warn(ex.getMessage()); 262 263 return null; 263 264 } … … 337 338 } 338 339 339 /**340 * Sets the name341 * @param name342 */343 /*public void setName(String name) {344 this.name = name;345 }*/346 347 340 public ImageIcon getScaledIcon() { 348 341 if (icon == null)
Note:
See TracChangeset
for help on using the changeset viewer.