Search in sources :

Example 21 with WrappedException

use of com.revolsys.util.WrappedException in project com.revolsys.open by revolsys.

the class EsriCoordinateSystems method getProjectedCoordinateSystem.

public static ProjectedCoordinateSystem getProjectedCoordinateSystem(final int id) {
    ProjectedCoordinateSystem coordinateSystem = (ProjectedCoordinateSystem) COORDINATE_SYSTEM_BY_ID.get(id);
    if (coordinateSystem == null) {
        try (final ChannelReader reader = ChannelReader.newChannelReader("classpath:CoordinateSystems/esri/Projected.cs")) {
            while (true) {
                final int coordinateSystemId = reader.getInt();
                final String csName = reader.getStringUtf8ByteCount();
                final int geographicCoordinateSystemId = reader.getInt();
                final String projectionName = reader.getStringUtf8ByteCount();
                final Map<ParameterName, ParameterValue> parameters = readParameters(reader);
                final String unitName = reader.getStringUtf8ByteCount();
                final double conversionFactor = reader.getDouble();
                if (id == coordinateSystemId) {
                    LinearUnit linearUnit = LINEAR_UNITS_BY_NAME.get(unitName);
                    if (linearUnit == null) {
                        linearUnit = new LinearUnit(unitName, conversionFactor);
                        LINEAR_UNITS_BY_NAME.put(unitName, linearUnit);
                    }
                    final Authority authority = new BaseAuthority("ESRI", coordinateSystemId);
                    final GeographicCoordinateSystem geographicCoordinateSystem = getGeographicCoordinateSystem(geographicCoordinateSystemId);
                    coordinateSystem = new ProjectedCoordinateSystem(coordinateSystemId, csName, geographicCoordinateSystem, projectionName, parameters, linearUnit, authority);
                    COORDINATE_SYSTEM_BY_ID.put(id, coordinateSystem);
                    return coordinateSystem;
                }
            }
        } catch (final WrappedException e) {
            if (Exceptions.isException(e, EOFException.class)) {
                return null;
            } else {
                Logs.error("Cannot load coordinate system=" + id, e);
                throw e;
            }
        }
    }
    return coordinateSystem;
}
Also used : BaseAuthority(com.revolsys.geometry.cs.BaseAuthority) WrappedException(com.revolsys.util.WrappedException) LinearUnit(com.revolsys.geometry.cs.unit.LinearUnit) ParameterValue(com.revolsys.geometry.cs.ParameterValue) Authority(com.revolsys.geometry.cs.Authority) BaseAuthority(com.revolsys.geometry.cs.BaseAuthority) ProjectedCoordinateSystem(com.revolsys.geometry.cs.ProjectedCoordinateSystem) ParameterName(com.revolsys.geometry.cs.ParameterName) SingleParameterName(com.revolsys.geometry.cs.SingleParameterName) ChannelReader(com.revolsys.io.channels.ChannelReader) EOFException(java.io.EOFException) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem)

Example 22 with WrappedException

use of com.revolsys.util.WrappedException in project com.revolsys.open by revolsys.

the class EsriCoordinateSystems method getGeographicCoordinateSystem.

public static GeographicCoordinateSystem getGeographicCoordinateSystem(final int id) {
    GeographicCoordinateSystem coordinateSystem = (GeographicCoordinateSystem) COORDINATE_SYSTEM_BY_ID.get(id);
    if (coordinateSystem == null) {
        try (final ChannelReader reader = ChannelReader.newChannelReader("classpath:CoordinateSystems/esri/Geographic.cs")) {
            while (true) {
                final int coordinateSystemId = reader.getInt();
                final String csName = reader.getStringUtf8ByteCount();
                final String datumName = reader.getStringUtf8ByteCount();
                final String spheroidName = reader.getStringUtf8ByteCount();
                final double semiMajorAxis = reader.getDouble();
                final double inverseFlattening = reader.getDouble();
                final String primeMeridianName = reader.getStringUtf8ByteCount();
                final double longitude = reader.getDouble();
                final String angularUnitName = reader.getStringUtf8ByteCount();
                final double conversionFactor = reader.getDouble();
                if (id == coordinateSystemId) {
                    final Ellipsoid ellipsoid = new Ellipsoid(spheroidName, semiMajorAxis, inverseFlattening, null);
                    final PrimeMeridian primeMeridian = new PrimeMeridian(primeMeridianName, longitude, null);
                    final GeodeticDatum geodeticDatum = new GeodeticDatum(null, datumName, null, false, ellipsoid, primeMeridian);
                    AngularUnit angularUnit = ANGULAR_UNITS_BY_NAME.get(angularUnitName);
                    if (angularUnit == null) {
                        angularUnit = new AngularUnit(angularUnitName, conversionFactor, null);
                        ANGULAR_UNITS_BY_NAME.put(angularUnitName, angularUnit);
                    }
                    final Authority authority = new BaseAuthority("ESRI", coordinateSystemId);
                    coordinateSystem = new GeographicCoordinateSystem(coordinateSystemId, csName, geodeticDatum, primeMeridian, angularUnit, null, authority);
                    COORDINATE_SYSTEM_BY_ID.put(id, coordinateSystem);
                    return coordinateSystem;
                }
            }
        } catch (final WrappedException e) {
            if (Exceptions.isException(e, EOFException.class)) {
                return null;
            } else {
                Logs.error("Cannot load coordinate system=" + id, e);
                throw e;
            }
        }
    }
    return coordinateSystem;
}
Also used : BaseAuthority(com.revolsys.geometry.cs.BaseAuthority) WrappedException(com.revolsys.util.WrappedException) Authority(com.revolsys.geometry.cs.Authority) BaseAuthority(com.revolsys.geometry.cs.BaseAuthority) GeodeticDatum(com.revolsys.geometry.cs.datum.GeodeticDatum) PrimeMeridian(com.revolsys.geometry.cs.PrimeMeridian) AngularUnit(com.revolsys.geometry.cs.unit.AngularUnit) ChannelReader(com.revolsys.io.channels.ChannelReader) EOFException(java.io.EOFException) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem) Ellipsoid(com.revolsys.geometry.cs.Ellipsoid)

Example 23 with WrappedException

use of com.revolsys.util.WrappedException in project com.revolsys.open by revolsys.

the class JsonParser method read.

@SuppressWarnings("unchecked")
public static <V> V read(final Object source) {
    Reader reader;
    if (source instanceof Clob) {
        try {
            reader = ((Clob) source).getCharacterStream();
        } catch (final SQLException e) {
            throw new RuntimeException("Unable to read clob", e);
        }
    } else if (source instanceof Reader) {
        reader = (Reader) source;
    } else if (source instanceof CharSequence) {
        reader = new StringReader(source.toString());
    } else {
        try {
            final Resource resource = Resource.getResource(source);
            reader = resource.newBufferedReader();
        } catch (final WrappedException e) {
            reader = new StringReader(source.toString());
        }
    }
    try {
        final V value = (V) read(reader);
        return value;
    } finally {
        if (source instanceof Clob) {
            try {
                final Clob clob = (Clob) source;
                clob.free();
            } catch (final SQLException e) {
                throw new RuntimeException("Unable to free clob resources", e);
            }
        }
    }
}
Also used : WrappedException(com.revolsys.util.WrappedException) SQLException(java.sql.SQLException) StringReader(java.io.StringReader) Resource(com.revolsys.spring.resource.Resource) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) Clob(java.sql.Clob)

Example 24 with WrappedException

use of com.revolsys.util.WrappedException in project com.revolsys.open by revolsys.

the class MavenRepositoryCache method copyRepositoryResource.

private boolean copyRepositoryResource(final MavenRepository repository, final Resource resource, final String groupId, final String artifactId, final String version, final String type, final String classifier, final String specificVersion, final String algorithm, final String path) {
    final String sha1Digest = repository.getSha1(groupId, artifactId, version, type, classifier, specificVersion, algorithm);
    final Resource repositoryResource = repository.getRoot().newChildResource(path);
    try {
        if (Property.hasValue(sha1Digest)) {
            final InputStream in = repositoryResource.getInputStream();
            final DigestInputStream digestIn = new DigestInputStream(in, MessageDigest.getInstance("SHA-1"));
            resource.copyFrom(digestIn);
            final MessageDigest messageDigest = digestIn.getMessageDigest();
            final byte[] digest = messageDigest.digest();
            final String fileDigest = Hex.toHex(digest);
            if (!sha1Digest.equals(fileDigest)) {
                Logs.error(this, ".sha1 digest is different for: " + repositoryResource);
                resource.delete();
                return false;
            }
        } else {
            resource.copyFrom(repositoryResource);
        }
        Logs.info(this, "Download maven resource: " + repositoryResource);
        return true;
    } catch (Throwable e) {
        resource.delete();
        while (e instanceof WrappedException) {
            e = e.getCause();
        }
        if (e instanceof FileNotFoundException) {
            return false;
        } else if (e instanceof IOException) {
            final IOException ioException = (IOException) e;
            if (ioException.getMessage().contains(" 404 ")) {
                return false;
            }
        }
        Logs.error(this, "Unable to download MVN resource " + repositoryResource + "\n  " + e.getMessage(), e);
        return false;
    }
}
Also used : WrappedException(com.revolsys.util.WrappedException) DigestInputStream(java.security.DigestInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) PathResource(com.revolsys.spring.resource.PathResource) Resource(com.revolsys.spring.resource.Resource) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Example 25 with WrappedException

use of com.revolsys.util.WrappedException in project com.revolsys.open by revolsys.

the class AbstractUpdateField method finish.

private void finish() {
    if (this.recordCount > 100) {
        final int confirm = JOptionPane.showConfirmDialog(this, "<html>Update <b style='color:#32CD32'>" + this.recordCountString + "</b> records? This may take a long time or fail if there are many records.</html>", "Update Records?", JOptionPane.OK_CANCEL_OPTION);
        if (confirm != JOptionPane.OK_OPTION) {
            setVisible(false);
            return;
        }
    }
    final ProgressMonitor progressMonitor = new ProgressMonitor(this, getProgressMonitorTitle(), "Updated 0 of " + this.recordCountString, 0, 100);
    progressMonitor.setProgress(0);
    setVisible(false);
    final AtomicInteger progress = new AtomicInteger();
    final SwingWorker<?, ?> task = new SwingWorker<Void, Void>() {

        @Override
        protected Void doInBackground() throws Exception {
            final Set<String> fieldNames = new LinkedHashSet<>();
            fieldNames.add(AbstractUpdateField.this.fieldDefinition.getName());
            fieldNames.addAll(AbstractUpdateField.this.layer.getFieldNames());
            final RecordLayerErrors errors = new RecordLayerErrors("Setting Field Values", AbstractUpdateField.this.layer, fieldNames);
            AbstractUpdateField.this.tableModel.forEachRecord((record) -> {
                if (progressMonitor.isCanceled()) {
                    throw new CancellationException();
                } else {
                    try {
                        updateRecord(record);
                    } catch (final WrappedException e) {
                        errors.addRecord(record, e.getCause());
                    } catch (final Throwable e) {
                        errors.addRecord(record, e);
                    }
                    final int updateCount = progress.incrementAndGet();
                    if (updateCount < AbstractUpdateField.this.recordCount) {
                        final int updatePercent = (int) Math.floor(updateCount * 100 / (double) AbstractUpdateField.this.recordCount);
                        if (updatePercent < 100) {
                            setProgress(updatePercent);
                        }
                    }
                }
            });
            setProgress(100);
            if (!isCancelled()) {
                errors.showErrorDialog();
            }
            return null;
        }

        @Override
        protected void done() {
        }
    };
    task.addPropertyChangeListener((event) -> {
        if ("progress" == event.getPropertyName()) {
            final int percent = (Integer) event.getNewValue();
            progressMonitor.setProgress(percent);
            progressMonitor.setNote("Updated " + progress + " of " + this.recordCountString);
        }
    });
    Invoke.worker(task);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WrappedException(com.revolsys.util.WrappedException) RecordLayerErrors(com.revolsys.swing.map.layer.record.table.model.RecordLayerErrors) ProgressMonitor(javax.swing.ProgressMonitor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CancellationException(java.util.concurrent.CancellationException) SwingWorker(javax.swing.SwingWorker)

Aggregations

WrappedException (com.revolsys.util.WrappedException)28 ChannelReader (com.revolsys.io.channels.ChannelReader)20 EOFException (java.io.EOFException)19 NoSuchResourceException (com.revolsys.spring.resource.NoSuchResourceException)14 ParameterValueString (com.revolsys.geometry.cs.ParameterValueString)12 ParameterName (com.revolsys.geometry.cs.ParameterName)7 Area (com.revolsys.geometry.cs.Area)4 Authority (com.revolsys.geometry.cs.Authority)4 ParameterValue (com.revolsys.geometry.cs.ParameterValue)4 LinearUnit (com.revolsys.geometry.cs.unit.LinearUnit)4 IntHashMap (com.revolsys.collection.map.IntHashMap)3 BaseAuthority (com.revolsys.geometry.cs.BaseAuthority)3 CoordinateOperationMethod (com.revolsys.geometry.cs.CoordinateOperationMethod)3 Ellipsoid (com.revolsys.geometry.cs.Ellipsoid)3 GeographicCoordinateSystem (com.revolsys.geometry.cs.GeographicCoordinateSystem)3 PrimeMeridian (com.revolsys.geometry.cs.PrimeMeridian)3 GeodeticDatum (com.revolsys.geometry.cs.datum.GeodeticDatum)3 VerticalDatum (com.revolsys.geometry.cs.datum.VerticalDatum)3 AngularUnit (com.revolsys.geometry.cs.unit.AngularUnit)3 UnitOfMeasure (com.revolsys.geometry.cs.unit.UnitOfMeasure)3