/** * Logs a debug message for {@link #getServiceProvider} method. Note: we are not required to * insert the method name ({@code "GetServiceProvider"}) in the message because it is part of the * informations already stored by {@link LogRecord}, and formatted by the default {@link * java.util.logging.SimpleFormatter}. * * @param status {@code "ENTRY"}, {@code "RETURN"} or {@code "THROW"}, according {@link Logger} * conventions. * @param category The category given to the {@link #getServiceProvider} method. * @param key The key being examined, or {@code null}. * @param message Optional message, or {@code null} if none. * @param type Optional class to format after the message, or {@code null}. */ private static void debug( final String status, final Class<?> category, final Hints.Key key, final String message, final Class type) { final StringBuilder buffer = new StringBuilder(status); buffer .append(Utilities.spaces(Math.max(1, 7 - status.length()))) .append('(') .append(Classes.getShortName(category)); if (key != null) { buffer.append(", ").append(key); } buffer.append(')'); if (message != null) { buffer.append(": ").append(message); } if (type != null) { buffer.append(' ').append(Classes.getShortName(type)).append('.'); } final LogRecord record = new LogRecord(DEBUG_LEVEL, buffer.toString()); record.setSourceClassName(FactoryRegistry.class.getName()); record.setSourceMethodName("getServiceProvider"); record.setLoggerName(LOGGER.getName()); LOGGER.log(record); }
/** Invoked when a factory can't be loaded. Log a warning, but do not stop the process. */ private static void loadingFailure( final Class<?> category, final Throwable error, final boolean showStackTrace) { final String name = Classes.getShortName(category); final StringBuilder cause = new StringBuilder(Classes.getShortClassName(error)); final String message = error.getLocalizedMessage(); if (message != null) { cause.append(": "); cause.append(message); } final LogRecord record = Loggings.format(Level.WARNING, LoggingKeys.CANT_LOAD_SERVICE_$2, name, cause.toString()); if (showStackTrace) { record.setThrown(error); } record.setSourceClassName(FactoryRegistry.class.getName()); record.setSourceMethodName("scanForPlugins"); record.setLoggerName(LOGGER.getName()); LOGGER.log(record); }
/** Formats the specified position. */ static String toString(final DirectPosition position) { final StringBuilder buffer = new StringBuilder(Classes.getShortClassName(position)).append('['); final int dimension = position.getDimension(); for (int i = 0; i < dimension; i++) { if (i != 0) { buffer.append(", "); } buffer.append(position.getOrdinate(i)); } return buffer.append(']').toString(); }
/** * Scans {@linkplain System#getProperties system properties} for any property keys defined in this * class, and add their values to the specified map of hints. For example if the {@value * #FORCE_LONGITUDE_FIRST_AXIS_ORDER} system property is defined, then the {@link * Hints#FORCE_LONGITUDE_FIRST_AXIS_ORDER FORCE_LONGITUDE_FIRST_AXIS_ORDER} hint will be added to * the set of hints. * * @return {@code true} if at least one hint changed as a result of this scan, or {@code false} * otherwise. */ static boolean scanForSystemHints(final Hints hints) { assert Thread.holdsLock(hints); boolean changed = false; synchronized (BINDINGS) { for (final Map.Entry<String, RenderingHints.Key> entry : BINDINGS.entrySet()) { final String propertyKey = entry.getKey(); final String property; try { property = System.getProperty(propertyKey); } catch (SecurityException e) { unexpectedException(e); continue; } if (property != null) { /* * Converts the system property value from String to Object (java.lang.Boolean * or java.lang.Number). We perform this conversion only if the key is exactly * of kind Hints.Key, not a subclass like ClassKey, in order to avoid useless * class loading on 'getValueClass()' method invocation (ClassKey don't make * sense for Boolean and Number, which are the only types that we convert here). */ Object value = property; final RenderingHints.Key hintKey = entry.getValue(); if (hintKey.getClass().equals(Hints.Key.class)) { final Class<?> type = ((Hints.Key) hintKey).getValueClass(); if (type.equals(Boolean.class)) { value = Boolean.valueOf(property); } else if (Number.class.isAssignableFrom(type)) try { value = Classes.valueOf(type, property); } catch (NumberFormatException e) { unexpectedException(e); continue; } } final Object old; try { old = hints.put(hintKey, value); } catch (IllegalArgumentException e) { // The property value is illegal for this hint. unexpectedException(e); continue; } if (!changed && !Utilities.equals(old, value)) { changed = true; } } } } return changed; }
/** * Returns a string representation of this envelope. The default implementation is okay for * occasional formatting (for example for debugging purpose). */ @Override public String toString() { final StringBuilder buffer = new StringBuilder(Classes.getShortClassName(this)).append('['); final int dimension = getDimension(); for (int i = 0; i < dimension; i++) { if (i != 0) { buffer.append(", "); } buffer.append(getMinimum(i)).append(" : ").append(getMaximum(i)); } return buffer.append(']').toString(); }
/** * {@linkplain #registerServiceProvider Registers} all factories given by the supplied iterator. * * @param factories The factories (or "service providers") to register. * @param category the category under which to register the providers. * @param message A buffer where to write the logging message. * @return {@code true} if at least one factory has been registered. */ private <T> boolean register( final Iterator<T> factories, final Class<T> category, final StringBuilder message) { boolean newServices = false; final String lineSeparator = System.getProperty("line.separator", "\n"); while (factories.hasNext()) { T factory; try { factory = factories.next(); } catch (OutOfMemoryError error) { // Makes sure that we don't try to handle this error. throw error; } catch (NoClassDefFoundError error) { /* * A provider can't be registered because of some missing dependencies. * This occurs for example when trying to register the WarpTransform2D * math transform on a machine without JAI installation. Since the factory * may not be essential (this is the case of WarpTransform2D), just skip it. */ loadingFailure(category, error, false); continue; } catch (ExceptionInInitializerError error) { /* * If an exception occured during class initialization, log the cause. * The ExceptionInInitializerError alone doesn't help enough. */ final Throwable cause = error.getCause(); if (cause != null) { loadingFailure(category, cause, true); } throw error; } catch (Error error) { if (!Classes.getShortClassName(error).equals("ServiceConfigurationError")) { // We want to handle sun.misc.ServiceConfigurationError only. Unfortunatly, we // need to rely on reflection because this error class is not a commited API. // TODO: Check if the error is catchable with JSE 6. throw error; } /* * Failed to register a factory for a reason probably related to the plugin * initialisation. It may be some factory-dependent missing resources. */ loadingFailure(category, error, true); continue; } if (category.isAssignableFrom(factory.getClass())) { final Class<? extends T> factoryClass = factory.getClass().asSubclass(category); /* * If the factory implements more than one interface and an * instance were already registered, reuse the same instance * instead of duplicating it. */ final T replacement = getServiceProviderByClass(factoryClass); if (replacement != null) { factory = replacement; // Need to register anyway, because the category may not be // the same. } if (registerServiceProvider(factory, category)) { /* * The factory is now registered. Add it to the message to * be logged. We will log all factories together in a single * log event because some registration (e.g. * MathTransformProviders) would be otherwise quite verbose. */ message.append(lineSeparator); message.append(" "); message.append(factoryClass.getName()); newServices = true; } } } return newServices; }
/** * Write SQL string to create tables in the test database based on the property files. * * @param propertyFiles Property files from app-schema-test suite. * @param parser The parser (WKT or an SC4O one for 3D tests) * @throws IllegalAttributeException * @throws NoSuchElementException * @throws IOException */ private void createTables(Map<String, File> propertyFiles, String parser) throws IllegalAttributeException, NoSuchElementException, IOException { StringBuffer buf = new StringBuffer(); StringBuffer spatialIndex = new StringBuffer(); // drop table procedure I copied from Victor's Oracle_Data_ref_set.sql buf.append("CREATE OR REPLACE PROCEDURE DROP_TABLE_OR_VIEW(TabName in Varchar2) IS ") .append("temp number:=0;") .append(" tes VARCHAR2 (200) := TabName;") .append(" drp_stmt VARCHAR2 (200):=null;") .append("BEGIN select count(*) into temp from user_tables where TABLE_NAME = tes;") .append("if temp = 1 then drp_stmt := 'Drop Table '||tes;") .append("EXECUTE IMMEDIATE drp_stmt;") // drop views too .append("else select count(*) into temp from user_views where VIEW_NAME = tes;") .append("if temp = 1 then drp_stmt := 'Drop VIEW '||tes;") .append("EXECUTE IMMEDIATE drp_stmt;end if;end if;") .append("EXCEPTION WHEN OTHERS THEN ") .append( "raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);") .append("END DROP_TABLE_OR_VIEW;\n"); for (String fileName : propertyFiles.keySet()) { File file = new File(propertyFiles.get(fileName), fileName); try (PropertyFeatureReader reader = new PropertyFeatureReader("test", file)) { SimpleFeatureType schema = reader.getFeatureType(); String tableName = schema.getName().getLocalPart().toUpperCase(); // drop table if exists buf.append("CALL DROP_TABLE_OR_VIEW('").append(tableName).append("')\n"); // create the table buf.append("CREATE TABLE ").append(tableName).append("("); // + pkey int size = schema.getAttributeCount() + 1; String[] fieldNames = new String[size]; List<String> createParams = new ArrayList<String>(); int j = 0; String type; String field; int spatialIndexCounter = 0; for (PropertyDescriptor desc : schema.getDescriptors()) { field = desc.getName().toString().toUpperCase(); fieldNames[j] = field; if (desc instanceof GeometryDescriptor) { type = "SDO_GEOMETRY"; // Update spatial index int srid = getSrid(((GeometryType) desc.getType())); spatialIndex .append("DELETE FROM user_sdo_geom_metadata WHERE table_name = '") .append(tableName) .append("'\n"); spatialIndex .append("Insert into user_sdo_geom_metadata ") .append("(TABLE_NAME,COLUMN_NAME,DIMINFO,SRID)") .append("values ('") .append(tableName) .append("','") .append(field) .append("',MDSYS.SDO_DIM_ARRAY(MDSYS.SDO_DIM_ELEMENT('X',140.962,144.909,0.00001),") .append("MDSYS.SDO_DIM_ELEMENT('Y',-38.858,-33.98,0.00001)") .append( // support 3d index ((GeometryDescriptor) desc).getCoordinateReferenceSystem() != null && ((GeometryDescriptor) desc) .getCoordinateReferenceSystem() .getCoordinateSystem() .getDimension() == 3 ? ", MDSYS.SDO_DIM_ELEMENT('Z',-100000, 100000, 1) )," : "),") .append(srid) .append(")\n"); // ensure it's <= 30 characters to avoid Oracle exception String indexName = (tableName.length() <= 26 ? tableName : tableName.substring(0, 26)) + "_IDX"; if (spatialIndexCounter > 0) { // to avoid duplicate index name when there are > 1 geometry in the same table indexName += spatialIndexCounter; } spatialIndex .append("CREATE INDEX \"") .append(indexName) .append("\" ON \"") .append(tableName) .append("\"(\"") .append(field) .append("\") ") .append("INDEXTYPE IS \"MDSYS\".\"SPATIAL_INDEX\"\n"); spatialIndexCounter++; } else { type = Classes.getShortName(desc.getType().getBinding()); if (type.equalsIgnoreCase("String")) { type = "NVARCHAR2(250)"; } else if (type.equalsIgnoreCase("Double")) { type = "NUMBER"; } // etc. assign as required } createParams.add(field + " " + type); j++; } // Add numeric PK for sorting fieldNames[j] = "PKEY"; createParams.add("PKEY VARCHAR2(30)"); buf.append(StringUtils.join(createParams.iterator(), ", ")); buf.append(")\n"); buf.append( "ALTER TABLE " + tableName + " ADD CONSTRAINT " + tableName + " PRIMARY KEY (PKEY)\n"); // then insert rows SimpleFeature feature; FeatureId id; while (reader.hasNext()) { buf.append("INSERT INTO ").append(tableName).append("("); feature = reader.next(); buf.append(StringUtils.join(fieldNames, ", ")); buf.append(") "); buf.append("VALUES ("); Collection<Property> properties = feature.getProperties(); String[] values = new String[size]; int valueIndex = 0; for (Property prop : properties) { Object value = prop.getValue(); if (value instanceof Geometry) { // use wkt writer to convert geometry to string, so third dimension can be supported // if present. Geometry geom = (Geometry) value; value = new WKTWriter(geom.getCoordinate().z == Double.NaN ? 2 : 3).write(geom); } if (value == null || value.toString().equalsIgnoreCase("null")) { values[valueIndex] = "null"; } else if (prop.getType() instanceof GeometryType) { int srid = getSrid(((GeometryType) prop.getType())); StringBuffer geomValue = new StringBuffer(parser + "('"); geomValue.append(value).append("'"); if (srid > -1) { // attach srid geomValue.append(", ").append(srid); } geomValue.append(")"); values[valueIndex] = geomValue.toString(); } else if (prop.getType().getBinding().getSimpleName().equalsIgnoreCase("DATE")) { values[valueIndex] = "TO_DATE('" + value + "', 'yyyy-MM-dd')"; } else { values[valueIndex] = "'" + value + "'"; } valueIndex++; } id = feature.getIdentifier(); // insert primary key values[valueIndex] = "'" + id.toString() + "'"; buf.append(StringUtils.join(values, ",")); buf.append(")\n"); } } buf.append(spatialIndex.toString()); spatialIndex.delete(0, spatialIndex.length()); if (buf.length() > 0) { this.sql = buf.toString(); } } }
/** * Implementation of {@link #unexpectedException(Logger, Class, String, Throwable)}. * * @param logger Where to log the error, or {@code null}. * @param classe The fully qualified class name where the error occurred, or {@code null}. * @param method The method where the error occurred, or {@code null}. * @param error The error. * @param level The logging level. * @return {@code true} if the error has been logged, or {@code false} if the logger doesn't log * anything at the specified level. */ private static boolean unexpectedException( Logger logger, String classe, String method, final Throwable error, final Level level) { /* * Checks if loggable, inferring the logger from the classe name if needed. */ if (error == null) { return false; } if (logger == null && classe != null) { final int separator = classe.lastIndexOf('.'); final String paquet = (separator >= 1) ? classe.substring(0, separator - 1) : ""; logger = getLogger(paquet); } if (logger != null && !logger.isLoggable(level)) { return false; } /* * Loggeable, so complete the null argument from the stack trace if we can. */ if (logger == null || classe == null || method == null) { String paquet = (logger != null) ? logger.getName() : null; final StackTraceElement[] elements = error.getStackTrace(); for (int i = 0; i < elements.length; i++) { /* * Searchs for the first stack trace element with a classname matching the * expected one. We compare preferably against the name of the class given * in argument, or against the logger name (taken as the package name) otherwise. */ final StackTraceElement element = elements[i]; final String classname = element.getClassName(); if (classe != null) { if (!classname.equals(classe)) { continue; } } else if (paquet != null) { if (!classname.startsWith(paquet)) { continue; } final int length = paquet.length(); if (classname.length() > length) { // We expect '.' but we accept also '$' or end of string. final char separator = classname.charAt(length); if (Character.isJavaIdentifierPart(separator)) { continue; } } } /* * Now that we have a stack trace element from the expected class (or any * element if we don't know the class), make sure that we have the right method. */ final String methodName = element.getMethodName(); if (method != null && !methodName.equals(method)) { continue; } /* * Now computes every values that are null, and stop the loop. */ if (paquet == null) { final int separator = classname.lastIndexOf('.'); paquet = (separator >= 1) ? classname.substring(0, separator - 1) : ""; logger = getLogger(paquet); if (!logger.isLoggable(level)) { return false; } } if (classe == null) { classe = classname; } if (method == null) { method = methodName; } break; } /* * The logger may stay null if we have been unable to find a suitable * stack trace. Fallback on the global logger. * * TODO: Use GLOBAL_LOGGER_NAME constant when we will be allowed to target Java 6. */ if (logger == null) { logger = getLogger("global"); if (!logger.isLoggable(level)) { return false; } } } /* * Now prepare the log message. If we have been unable to figure out a source class and * method name, we will fallback on Java logging default mechanism, which may returns a * less relevant name than our attempt to use the logger name as the package name. */ final StringBuilder buffer = new StringBuilder(Classes.getShortClassName(error)); final String message = error.getLocalizedMessage(); if (message != null) { buffer.append(": ").append(message); } final LogRecord record = new LogRecord(level, buffer.toString()); if (classe != null) { record.setSourceClassName(classe); } if (method != null) { record.setSourceMethodName(method); } if (level.intValue() > 500) { record.setThrown(error); } record.setLoggerName(logger.getName()); logger.log(record); return true; }
/** Returns a string representation of this entry. */ @Override public String toString() { return Classes.getShortClassName(this) + "[key=" + key + ", value=" + value + ']'; }