Ignore:
Timestamp:
2014-10-18T15:31:53+02:00 (10 years ago)
Author:
donvip
Message:

[josm_opendata] fix some sonar issues

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  
    4747
    4848import org.geotools.data.DataUtilities;
     49import org.openstreetmap.josm.Main;
    4950
    5051/**
     
    5253 */
    5354public class TabFiles extends ShpFiles {
    54    
     55
    5556    /**
    5657     * The urls for each type of file that is associated with the shapefile. The
     
    6061
    6162    /**
    62      * A read/write lock, so that we can have concurrent readers 
     63     * A read/write lock, so that we can have concurrent readers
    6364     */
    6465    private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
     
    7475     */
    7576    private final MemoryMapCache mapCache = new MemoryMapCache();
    76    
     77
    7778    private boolean memoryMapCacheEnabled;
    78    
    79    
     79
     80
    8081    ////////////////////////////////////////////////////
    81    
     82
    8283    public TabFiles(File headerFile, File dataFile) throws IllegalArgumentException {
    8384        super(fakeShpFile(headerFile)); // Useless but necessary
     
    8586        urls.put(ShpFileType.DBF, DataUtilities.fileToURL(dataFile));
    8687    }
    87    
     88
    8889    /**
    8990     * This is really ugly. Used only to give a fake shp file to ShpFiles constructor to avoid IllegalArgument at initialization.
     
    9293        return DataUtilities.fileToURL(new File(headerFile.getAbsolutePath()+".shp"));
    9394    }
    94    
     95
    9596    private String baseName(Object obj) {
    9697        if (obj instanceof URL) {
     
    99100        return null;
    100101    }
    101    
     102
    102103    private String toBase(String path) {
    103104        return path.substring(0, path.toLowerCase().lastIndexOf(".tab"));
     
    105106
    106107    ////////////////////////////////////////////////////
    107    
     108
    108109    private void init(URL url) {
    109110        String base = baseName(url);
     
    119120
    120121        for (ShpFileType type : ShpFileType.values()) {
    121            
     122
    122123            String extensionWithPeriod = type.extensionWithPeriod;
    123124            if (upperCase) {
     
    126127                extensionWithPeriod = extensionWithPeriod.toLowerCase();
    127128            }
    128            
     129
    129130            URL newURL;
    130131            String string = base + extensionWithPeriod;
     
    139140
    140141        // 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
    142143        // different cases that the extension can be made up of.
    143144        // IE Shp, SHP, Shp, ShP etc...
     
    166167        File[] files = directory.listFiles(new FilenameFilter(){
    167168
     169            @Override
    168170            public boolean accept(File dir, String name) {
    169171                return file.getName().equalsIgnoreCase(name);
    170172            }
    171            
     173
    172174        });
    173175        if( files.length>0 ){
     
    175177                return files[0].toURI().toURL();
    176178            } catch (MalformedURLException e) {
    177                 //ShapefileDataStoreFactory.LOGGER().log(Level.SEVERE, "", e);
     179                Main.error(e);
    178180            }
    179181        }
     
    190192    }
    191193
     194    @Override
    192195    public void dispose() {
    193196        if (numberOfLocks() != 0) {
     
    200203    /**
    201204     * Writes to the log all the lockers and when they were constructed.
    202      * 
     205     *
    203206     * @param logLevel
    204207     *                the level at which to log.
    205208     */
     209    @Override
    206210    public void logCurrentLockers(Level logLevel) {
    207211        for (Collection<ShpFilesLocker> lockerList : lockers.values()) {
     
    209213                StringBuilder sb = new StringBuilder("The following locker still has a lock: ");
    210214                sb.append(locker);
    211                 //ShapefileDataStoreFactory.LOGGER().log(logLevel, sb.toString(), locker.getTrace());
     215                Main.error(sb.toString());
    212216            }
    213217        }
     
    217221     * Returns the URLs (in string form) of all the files for the shapefile
    218222     * datastore.
    219      * 
     223     *
    220224     * @return the URLs (in string form) of all the files for the shapefile
    221225     *         datastore.
    222226     */
     227    @Override
    223228    public Map<ShpFileType, String> getFileNames() {
    224229        Map<ShpFileType, String> result = new HashMap<>();
     
    235240     * Returns the string form of the url that identifies the file indicated by
    236241     * the type parameter or null if it is known that the file does not exist.
    237      * 
     242     *
    238243     * <p>
    239244     * Note: a URL should NOT be constructed from the string instead the URL
    240245     * should be obtained through calling one of the aquireLock methods.
    241      * 
     246     *
    242247     * @param type
    243248     *                indicates the type of file the caller is interested in.
    244      * 
     249     *
    245250     * @return the string form of the url that identifies the file indicated by
    246251     *         the type parameter or null if it is known that the file does not
    247252     *         exist.
    248253     */
     254    @Override
    249255    public String get(ShpFileType type) {
    250256        return urls.get(type).toExternalForm();
     
    255261     * is not thread safe so do not count on it to have a completely accurate
    256262     * picture but it can be useful debugging
    257      * 
     263     *
    258264     * @return the number of locks on the current set of shapefile files.
    259265     */
     266    @Override
    260267    public int numberOfLocks() {
    261268        int count = 0;
     
    269276     * get*Channel methods are used when reading or writing to the file is
    270277     * desired.
    271      * 
    272      * 
     278     *
     279     *
    273280     * @see #getInputStream(ShpFileType, FileReader)
    274281     * @see #getReadChannel(ShpFileType, FileReader)
    275282     * @see #getWriteChannel(ShpFileType, FileReader)
    276      * 
     283     *
    277284     * @param type
    278285     *                the type of the file desired.
     
    280287     *                the object that is requesting the File. The same object
    281288     *                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
    284292    public File acquireReadFile(ShpFileType type,
    285293            FileReader requestor) {
     
    290298        return DataUtilities.urlToFile(url);
    291299    }
    292    
     300
    293301    /**
    294302     * Acquire a URL for read only purposes.  It is recommended that get*Stream or
    295303     * get*Channel methods are used when reading or writing to the file is
    296304     * desired.
    297      * 
    298      * 
     305     *
     306     *
    299307     * @see #getInputStream(ShpFileType, FileReader)
    300308     * @see #getReadChannel(ShpFileType, FileReader)
    301309     * @see #getWriteChannel(ShpFileType, FileReader)
    302      * 
     310     *
    303311     * @param type
    304312     *                the type of the file desired.
     
    308316     * @return the URL to the file of the type requested
    309317     */
     318    @Override
    310319    public URL acquireRead(ShpFileType type, FileReader requestor) {
    311320        URL url = urls.get(type);
    312321        if (url == null)
    313322            return null;
    314        
     323
    315324        readWriteLock.readLock().lock();
    316325        Collection<ShpFilesLocker> threadLockers = getCurrentThreadLockers();
     
    326335     * desired.
    327336     * </p>
    328      * 
     337     *
    329338     * @see #getInputStream(ShpFileType, FileReader)
    330339     * @see #getReadChannel(ShpFileType, FileReader)
    331340     * @see #getWriteChannel(ShpFileType, FileReader)
    332      * 
     341     *
    333342     * @param type
    334343     *                the type of the file desired.
     
    338347     * @return A result object containing the URL or the reason for the failure.
    339348     */
     349    @Override
    340350    public Result<URL, State> tryAcquireRead(ShpFileType type,
    341351            FileReader requestor) {
     
    344354            return new Result<>(null, State.NOT_EXIST);
    345355        }
    346        
     356
    347357        boolean locked = readWriteLock.readLock().tryLock();
    348358        if (!locked) {
    349359            return new Result<>(null, State.LOCKED);
    350360        }
    351        
     361
    352362        getCurrentThreadLockers().add(new ShpFilesLocker(url, requestor));
    353363
     
    358368     * Unlocks a read lock. The file and requestor must be the the same as the
    359369     * one of the lockers.
    360      * 
     370     *
    361371     * @param file
    362372     *                file that was locked
     
    364374     *                the class that requested the file
    365375     */
     376    @Override
    366377    public void unlockRead(File file, FileReader requestor) {
    367378        Collection<URL> allURLS = urls.values();
     
    376387     * Unlocks a read lock. The url and requestor must be the the same as the
    377388     * one of the lockers.
    378      * 
     389     *
    379390     * @param url
    380391     *                url that was locked
     
    382393     *                the class that requested the url
    383394     */
     395    @Override
    384396    public void unlockRead(URL url, FileReader requestor) {
    385397        if (url == null) {
     
    404416
    405417    /**
    406      * Acquire a File for read and write purposes. 
     418     * Acquire a File for read and write purposes.
    407419     * <p> It is recommended that get*Stream or
    408420     * get*Channel methods are used when reading or writing to the file is
    409421     * desired.
    410422     * </p>
    411      * 
     423     *
    412424     * @see #getInputStream(ShpFileType, FileReader)
    413425     * @see #getReadChannel(ShpFileType, FileReader)
    414426     * @see #getWriteChannel(ShpFileType, FileReader)
    415427
    416      * 
     428     *
    417429     * @param type
    418430     *                the type of the file desired.
     
    422434     * @return the File to the file of the type requested
    423435     */
     436    @Override
    424437    public File acquireWriteFile(ShpFileType type,
    425438            FileWriter requestor) {
     
    431444    }
    432445    /**
    433      * Acquire a URL for read and write purposes. 
     446     * Acquire a URL for read and write purposes.
    434447     * <p> It is recommended that get*Stream or
    435448     * get*Channel methods are used when reading or writing to the file is
    436449     * desired.
    437450     * </p>
    438      * 
     451     *
    439452     * @see #getInputStream(ShpFileType, FileReader)
    440453     * @see #getReadChannel(ShpFileType, FileReader)
    441454     * @see #getWriteChannel(ShpFileType, FileReader)
    442455
    443      * 
     456     *
    444457     * @param type
    445458     *                the type of the file desired.
     
    449462     * @return the URL to the file of the type requested
    450463     */
     464    @Override
    451465    public URL acquireWrite(ShpFileType type, FileWriter requestor) {
    452466        URL url = urls.get(type);
     
    460474        readWriteLock.writeLock().lock();
    461475        threadLockers.add(new ShpFilesLocker(url, requestor));
    462         mapCache.cleanFileCache(url);       
     476        mapCache.cleanFileCache(url);
    463477        return url;
    464478    }
    465    
     479
    466480    /**
    467481     * Tries to acquire a URL for read/write purposes. Returns null if the
     
    471485     * desired.
    472486     * </p>
    473      * 
     487     *
    474488     * @see #getInputStream(ShpFileType, FileReader)
    475489     * @see #getReadChannel(ShpFileType, FileReader)
    476490     * @see #getWriteChannel(ShpFileType, FileReader)
    477491
    478      * 
     492     *
    479493     * @param type
    480494     *                the type of the file desired.
     
    484498     * @return A result object containing the URL or the reason for the failure.
    485499     */
     500    @Override
    486501    public Result<URL, State> tryAcquireWrite(ShpFileType type,
    487502            FileWriter requestor) {
    488        
     503
    489504        URL url = urls.get(type);
    490505        if (url == null) {
     
    511526     * Unlocks a read lock. The file and requestor must be the the same as the
    512527     * one of the lockers.
    513      * 
     528     *
    514529     * @param file
    515530     *                file that was locked
     
    517532     *                the class that requested the file
    518533     */
     534    @Override
    519535    public void unlockWrite(File file, FileWriter requestor) {
    520536        Collection<URL> allURLS = urls.values();
     
    529545     * Unlocks a read lock. The requestor must be have previously obtained a
    530546     * lock for the url.
    531      * 
    532      * 
     547     *
     548     *
    533549     * @param url
    534550     *                url that was locked
     
    536552     *                the class that requested the url
    537553     */
     554    @Override
    538555    public void unlockWrite(URL url, FileWriter requestor) {
    539556        if (url == null) {
     
    551568                            + " to have locked the url but it does not hold the lock for the URL");
    552569        }
    553        
     570
    554571        if(threadLockers.size() == 0) {
    555572            lockers.remove(Thread.currentThread());
     
    560577        readWriteLock.writeLock().unlock();
    561578    }
    562    
     579
    563580    /**
    564581     * Returns the list of lockers attached to a given thread, or creates it if missing
     
    586603        }
    587604    }
    588    
     605
    589606    /**
    590607     * Re-takes the read locks in preparation for lock downgrade
     
    602619    /**
    603620     * Determine if the location of this shapefile is local or remote.
    604      * 
     621     *
    605622     * @return true if local, false if remote
    606623     */
     
    613630     * files cannot be deleted return false.
    614631     */
     632    @Override
    615633    public boolean delete() {
    616634        BasicShpFileWriter requestor = new BasicShpFileWriter("ShpFiles for deleting all files");
     
    638656     * Opens a input stream for the indicated file. A read lock is requested at
    639657     * the method call and released on close.
    640      * 
     658     *
    641659     * @param type
    642660     *                the type of file to open the stream to.
     
    644662     *                the object requesting the stream
    645663     * @return an input stream
    646      * 
     664     *
    647665     * @throws IOException
    648666     *                 if a problem occurred opening the stream.
    649667     */
     668    @Override
    650669    public InputStream getInputStream(ShpFileType type,
    651670            final FileReader requestor) throws IOException {
     
    684703        }
    685704    }
    686    
     705
    687706    /**
    688707     * Opens a output stream for the indicated file. A write lock is requested at
    689708     * the method call and released on close.
    690      * 
     709     *
    691710     * @param type
    692711     *                the type of file to open the stream to.
     
    694713     *                the object requesting the stream
    695714     * @return an output stream
    696      * 
     715     *
    697716     * @throws IOException
    698717     *                 if a problem occurred opening the stream.
    699718     */
     719    @Override
    700720    public OutputStream getOutputStream(ShpFileType type,
    701721            final FileWriter requestor) throws IOException {
     
    703723
    704724        try {
    705            
     725
    706726            OutputStream out;
    707727            if (isLocalTab()) {
     
    713733                out = connection.getOutputStream();
    714734            }
    715            
     735
    716736            FilterOutputStream output = new FilterOutputStream(out) {
    717737
     
    755775     * channel is closed.
    756776     * </p>
    757      * 
     777     *
    758778     * @param type
    759779     *                the type of file to open the channel to.
    760780     * @param requestor
    761781     *                the object requesting the channel
    762      *
    763      */
     782     *
     783     */
     784    @Override
    764785    public ReadableByteChannel getReadChannel(ShpFileType type,
    765786            FileReader requestor) throws IOException {
     
    770791
    771792                File file = DataUtilities.urlToFile(url);
    772                
     793
    773794                @SuppressWarnings("resource")
    774795                RandomAccessFile raf = new RandomAccessFile(file, "r");
     
    801822     * a generic channel for remote urls, however both shape and dbf writing can
    802823     * only occur with a local FileChannel channel.
    803      * 
     824     *
    804825     * <p>
    805826     * A write lock is obtained when this method is called and released when the
    806827     * channel is closed.
    807828     * </p>
    808      * 
    809      * 
     829     *
     830     *
    810831     * @param type
    811832     *                the type of file to open the stream to.
    812833     * @param requestor
    813834     *                the object requesting the stream
    814      * 
     835     *
    815836     * @return a WritableByteChannel for the provided file type
    816      * 
     837     *
    817838     * @throws IOException
    818839     *                 if there is an error opening the stream
    819840     */
     841    @Override
    820842    public WritableByteChannel getWriteChannel(ShpFileType type,
    821843            FileWriter requestor) throws IOException {
     
    860882     * Obtains a Storage file for the type indicated. An id is provided so that
    861883     * the same file can be obtained at a later time with just the id
    862      * 
     884     *
    863885     * @param type
    864886     *                the type of file to create and return
    865      * 
     887     *
    866888     * @return StorageFile
    867889     * @throws IOException
    868890     *                 if temporary files cannot be created
    869891     */
     892    @Override
    870893    public StorageFile getStorageFile(ShpFileType type) throws IOException {
    871894        String baseName = getTypeName();
     
    877900    }
    878901
     902    @Override
    879903    public String getTypeName() {
    880904        String path = SHP.toBase(urls.get(SHP));
     
    888912        return path.substring(slash, dot);
    889913    }
    890    
     914
    891915    /**
    892916     * Internal method that the file channel decorators will call to allow reuse of the memory mapped buffers
     
    899923     * @throws IOException
    900924     */
     925    @Override
    901926    MappedByteBuffer map(FileChannel wrapped, URL url, MapMode mode, long position, long size) throws IOException {
    902927        if(memoryMapCacheEnabled) {
     
    906931        }
    907932    }
    908    
     933
    909934    /**
    910935     * Returns the status of the memory map cache. When enabled the memory mapped portions of the files are cached and shared
     
    912937     * @param memoryMapCacheEnabled
    913938     */
     939    @Override
    914940    public boolean isMemoryMapCacheEnabled() {
    915941        return memoryMapCacheEnabled;
     
    921947     * @param memoryMapCacheEnabled
    922948     */
     949    @Override
    923950    public void setMemoryMapCacheEnabled(boolean memoryMapCacheEnabled) {
    924951        this.memoryMapCacheEnabled = memoryMapCacheEnabled;
     
    931958     * Returns true if the file exists. Throws an exception if the file is not
    932959     * local.
    933      * 
     960     *
    934961     * @param fileType
    935962     *                the type of file to check existance for.
    936      * 
     963     *
    937964     * @return true if the file exists.
    938      * 
     965     *
    939966     * @throws IllegalArgumentException
    940967     *                 if the files are not local.
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/AbstractModule.java

    r30723 r30731  
    1010import java.util.List;
    1111
     12import org.openstreetmap.josm.Main;
    1213import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
    1314import org.openstreetmap.josm.gui.preferences.SourceEntry;
     
    2425
    2526    protected final ModuleInformation info;
    26    
     27
    2728    public AbstractModule(ModuleInformation info) {
    2829        this.info = info;
     
    5051            ExtendedSourceEntry src;
    5152            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                    }
    6167                    while ((n = in.read(buffer)) > 0) {
    6268                        out.write(buffer, 0, n);
    6369                    }
    64                     out.close();
    65                     in.close();
    66 
    6770                    // Add source pointing to the local file
    6871                    src.url = OdConstants.PROTO_FILE+path;
    6972                    sources.add(src);
    7073                } catch (IOException e) {
    71                     System.err.println(e.getMessage());
     74                    Main.error(e.getMessage());
    7275                }
    7376            }
     
    9699        };
    97100    }
    98    
     101
    99102    @Override
    100103    public final List<AbstractDataSetHandler> getNewlyInstanciatedHandlers() {
     
    104107                try {
    105108                    result.add(handlerClass.newInstance());
    106                 } catch (Throwable t) {
    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());
    108111                }
    109112            }
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ModuleHandler.java

    r30723 r30731  
    247247                        + "Delete from preferences?</html>", module.name, module.className);
    248248            }
    249         }  catch (Throwable e) {
    250             e.printStackTrace();
     249        }  catch (Exception e) {
     250            Main.error(e);
    251251        }
    252252        if (msg != null && confirmDisableModule(parent, msg, module.name)) {
     
    550550
    551551        final File[] files = moduleDir.listFiles(new FilenameFilter() {
     552            @Override
    552553            public boolean accept(File dir, String name) {
    553554                return name.endsWith(".jar.new");
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ModuleInformation.java

    r30723 r30731  
    143143        this.attr.putAll(other.attr);
    144144    }
    145    
     145
    146146    private static final ImageIcon extractIcon(String iconPath, File jarFile, boolean suppressWarnings) {
    147147        return new ImageProvider(iconPath).setArchive(jarFile).setMaxWidth(24).setMaxHeight(24).setOptional(true).setSuppressWarnings(suppressWarnings).get();
     
    234234        try {
    235235            return klass.getConstructor(ModuleInformation.class).newInstance(this);
    236         } catch (Throwable t) {
     236        } catch (Exception t) {
    237237            throw new ModuleException(name, t);
    238238        }
     
    251251        try{
    252252            return (Class<? extends Module>) Class.forName(className, true, classLoader);
    253         } catch (Throwable t) {
     253        } catch (Exception t) {
    254254            throw new ModuleException(name, t);
    255255        }
     
    260260            return f.toURI().toURL();
    261261        } catch (MalformedURLException ex) {
     262            Main.warn(ex.getMessage());
    262263            return null;
    263264        }
     
    337338    }
    338339
    339     /**
    340      * Sets the name
    341      * @param name
    342      */
    343     /*public void setName(String name) {
    344         this.name = name;
    345     }*/
    346 
    347340    public ImageIcon getScaledIcon() {
    348341        if (icon == null)
Note: See TracChangeset for help on using the changeset viewer.