Search in sources :

Example 1 with Messages

use of org.apache.sis.util.resources.Messages in project sis by apache.

the class ConcurrentAuthorityFactory method release.

/**
 * Releases the Data Access Object previously obtained with {@link #getDataAccess()}.
 * This method marks the factory as available for reuse by other threads.
 *
 * <p>All arguments given to this method are for logging purpose only.</p>
 *
 * @param  caller  the caller method, or {@code null} for {@code "create" + type.getSimpleName()}.
 * @param  type    the type of the created object, or {@code null} for performing no logging.
 * @param  code    the code of the created object, or {@code null} if none.
 */
private void release(String caller, final Class<?> type, final String code) {
    // A null value here would be an error in our algorithm.
    final DataAccessRef<DAO> usage = currentDAO.get();
    if (--usage.depth == 0) {
        currentDAO.remove();
        long time = usage.timestamp;
        synchronized (availableDAOs) {
            // Must be done first in case an exception happen after this point.
            remainingDAOs++;
            recycle(usage);
            // We released only one data access, so awake only one thread - not all of them.
            availableDAOs.notify();
            time = usage.timestamp - time;
        }
        /*
             * Log only events that take longer than the threshold (e.g. 10 milliseconds).
             */
        if (time >= DURATION_FOR_LOGGING && type != null) {
            if (caller == null) {
                caller = "create".concat(type.getSimpleName());
            }
            final PerformanceLevel level = PerformanceLevel.forDuration(time, TimeUnit.NANOSECONDS);
            final Double duration = time / (double) StandardDateFormat.NANOS_PER_SECOND;
            final Messages resources = Messages.getResources(null);
            final LogRecord record;
            if (code != null) {
                record = resources.getLogRecord(level, Messages.Keys.CreateDurationFromIdentifier_3, type, code, duration);
            } else {
                record = resources.getLogRecord(level, Messages.Keys.CreateDuration_2, type, duration);
            }
            record.setLoggerName(Loggers.CRS_FACTORY);
            Logging.log(getClass(), caller, record);
        }
    }
    assert usage.depth >= 0 : usage;
}
Also used : Messages(org.apache.sis.util.resources.Messages) LogRecord(java.util.logging.LogRecord) PerformanceLevel(org.apache.sis.util.logging.PerformanceLevel)

Example 2 with Messages

use of org.apache.sis.util.resources.Messages in project sis by apache.

the class Warnings method toString.

/**
 * Returns a string representation of the warning messages in the given locale.
 * This method formats the warnings in a bullet list.
 *
 * @param  locale  the locale to use for formatting warning messages.
 * @return a string representation of the warning messages.
 */
public String toString(final Locale locale) {
    final StringBuilder buffer = new StringBuilder(250);
    final String lineSeparator = System.lineSeparator();
    final Messages resources = Messages.getResources(locale);
    buffer.append(resources.getString(isParsing ? Messages.Keys.IncompleteParsing_1 : Messages.Keys.NonConformFormatting_1, root));
    if (messages != null) {
        for (final Iterator<?> it = messages.iterator(); it.hasNext(); ) {
            final InternationalString i18n = (InternationalString) it.next();
            Exception cause = (Exception) it.next();
            final String message;
            if (i18n != null) {
                message = i18n.toString(locale);
            } else {
                /*
                     * If there is no message, then we must have at least an exception.
                     * Consequently a NullPointerException in following line would be a bug.
                     */
                final String[] sources = exceptionSources.get(cause);
                if (sources != null) {
                    message = Errors.getResources(locale).getString(Errors.Keys.UnparsableStringInElement_2, sources);
                } else {
                    message = cause.toString();
                    cause = null;
                }
            }
            buffer.append(lineSeparator).append(" • ").append(message);
            if (cause != null) {
                String details = Exceptions.getLocalizedMessage(cause, locale);
                if (details == null) {
                    details = cause.toString();
                }
                buffer.append(lineSeparator).append("   ").append(details);
            }
        }
    }
    /*
         * If the parser found some unknown elements, formats an enclosed bullet list for them.
         */
    if (!ignoredElements.isEmpty()) {
        final Vocabulary vocabulary = Vocabulary.getResources(locale);
        buffer.append(lineSeparator).append(" • ").append(resources.getString(Messages.Keys.UnknownElementsInText));
        for (final Map.Entry<String, List<String>> entry : ignoredElements.entrySet()) {
            buffer.append(lineSeparator).append("    ‣ ").append(vocabulary.getString(Vocabulary.Keys.Quoted_1, entry.getKey()));
            String separator = vocabulary.getString(Vocabulary.Keys.InBetweenWords);
            for (final String p : entry.getValue()) {
                buffer.append(separator).append(p);
                separator = ", ";
            }
            buffer.append('.');
        }
    }
    /*
         * There is intentionally line separator at the end of the last line, because the string returned by
         * this method is typically written or logged by a call to System.out.println(…) or something equivalent.
         * A trailing line separator cause a visual disruption in log records for instance.
         */
    return buffer.toString();
}
Also used : Vocabulary(org.apache.sis.util.resources.Vocabulary) Messages(org.apache.sis.util.resources.Messages) InternationalString(org.opengis.util.InternationalString) ArrayList(java.util.ArrayList) List(java.util.List) InternationalString(org.opengis.util.InternationalString) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Messages (org.apache.sis.util.resources.Messages)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 LogRecord (java.util.logging.LogRecord)1 PerformanceLevel (org.apache.sis.util.logging.PerformanceLevel)1 Vocabulary (org.apache.sis.util.resources.Vocabulary)1 InternationalString (org.opengis.util.InternationalString)1