Search in sources :

Example 1 with ConfigurationElement

use of org.datanucleus.plugin.ConfigurationElement in project datanucleus-rdbms by datanucleus.

the class BaseDatastoreAdapter method loadDatastoreMappings.

/**
 * Load all datastore mappings defined in the associated plugins via the plugin mechanism.
 * All individual DatastoreAdapters should load up their own built-in support.
 * @param mgr the PluginManager
 * @param clr the ClassLoaderResolver
 */
protected void loadDatastoreMappings(PluginManager mgr, ClassLoaderResolver clr) {
    // Load from plugin mechanism
    ConfigurationElement[] elems = mgr.getConfigurationElementsForExtension("org.datanucleus.store.rdbms.datastore_mapping", null, null);
    if (elems != null) {
        for (ConfigurationElement elem : elems) {
            String javaName = elem.getAttribute("java-type").trim();
            String rdbmsMappingClassName = elem.getAttribute("rdbms-mapping-class");
            String jdbcType = elem.getAttribute("jdbc-type");
            String sqlType = elem.getAttribute("sql-type");
            String defaultJava = elem.getAttribute("default");
            boolean defaultForJavaType = false;
            if (defaultJava != null) {
                if (defaultJava.equalsIgnoreCase("true")) {
                    defaultForJavaType = Boolean.TRUE.booleanValue();
                }
            }
            Class mappingType = null;
            if (!StringUtils.isWhitespace(rdbmsMappingClassName)) {
                try {
                    mappingType = mgr.loadClass(elem.getExtension().getPlugin().getSymbolicName(), rdbmsMappingClassName);
                } catch (NucleusException ne) {
                    NucleusLogger.DATASTORE.error(Localiser.msg("041013", rdbmsMappingClassName));
                }
                Set includes = new HashSet();
                Set excludes = new HashSet();
                for (ConfigurationElement childElem : elem.getChildren()) {
                    if (childElem.getName().equals("includes")) {
                        includes.add(childElem.getAttribute("vendor-id"));
                    } else if (childElem.getName().equals("excludes")) {
                        excludes.add(childElem.getAttribute("vendor-id"));
                    }
                }
                if (!excludes.contains(getVendorID())) {
                    if (includes.isEmpty() || includes.contains(getVendorID())) {
                        registerDatastoreMapping(javaName, mappingType, jdbcType, sqlType, defaultForJavaType);
                    }
                }
            }
        }
    }
}
Also used : ConfigurationElement(org.datanucleus.plugin.ConfigurationElement) ResultSet(java.sql.ResultSet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) NucleusException(org.datanucleus.exceptions.NucleusException) HashSet(java.util.HashSet)

Example 2 with ConfigurationElement

use of org.datanucleus.plugin.ConfigurationElement in project datanucleus-core by datanucleus.

the class NucleusContextHelper method createStoreManagerForProperties.

/**
 * Method to create a StoreManager based on the specified properties passed in.
 * @param props The overall persistence properties
 * @param datastoreProps Persistence properties to apply to the datastore
 * @param clr ClassLoader resolver
 * @param nucCtx NucleusContext
 * @return The StoreManager
 * @throws NucleusUserException if impossible to create the StoreManager (not in CLASSPATH?, invalid definition?)
 */
public static StoreManager createStoreManagerForProperties(Map<String, Object> props, Map<String, Object> datastoreProps, ClassLoaderResolver clr, NucleusContext nucCtx) {
    Extension[] exts = nucCtx.getPluginManager().getExtensionPoint("org.datanucleus.store_manager").getExtensions();
    Class[] ctrArgTypes = new Class[] { ClassConstants.CLASS_LOADER_RESOLVER, ClassConstants.PERSISTENCE_NUCLEUS_CONTEXT, Map.class };
    Object[] ctrArgs = new Object[] { clr, nucCtx, datastoreProps };
    StoreManager storeMgr = null;
    // Try using the URL of the data source
    String url = (String) props.get(PropertyNames.PROPERTY_CONNECTION_URL.toLowerCase());
    if (url != null) {
        int idx = url.indexOf(':');
        if (idx > -1) {
            url = url.substring(0, idx);
        }
        for (int e = 0; storeMgr == null && e < exts.length; e++) {
            ConfigurationElement[] confElm = exts[e].getConfigurationElements();
            for (int c = 0; storeMgr == null && c < confElm.length; c++) {
                String urlKey = confElm[c].getAttribute("url-key");
                if (url == null || urlKey.equalsIgnoreCase(url)) {
                    // Either no URL, or url defined so take this StoreManager
                    try {
                        storeMgr = (StoreManager) nucCtx.getPluginManager().createExecutableExtension("org.datanucleus.store_manager", "url-key", url == null ? urlKey : url, "class-name", ctrArgTypes, ctrArgs);
                    } catch (InvocationTargetException ex) {
                        Throwable t = ex.getTargetException();
                        if (t instanceof RuntimeException) {
                            throw (RuntimeException) t;
                        } else if (t instanceof Error) {
                            throw (Error) t;
                        } else {
                            throw new NucleusException(t.getMessage(), t).setFatal();
                        }
                    } catch (Exception ex) {
                        throw new NucleusException(ex.getMessage(), ex).setFatal();
                    }
                }
            }
        }
    } else {
        // Assumed to be using RDBMS since only that allows ConnectionFactory/ConnectionFactoryName TODO If any other stores start supporting ConnectionFactory then update this
        try {
            storeMgr = (StoreManager) nucCtx.getPluginManager().createExecutableExtension("org.datanucleus.store_manager", "key", "rdbms", "class-name", ctrArgTypes, ctrArgs);
        } catch (InvocationTargetException ex) {
            Throwable t = ex.getTargetException();
            if (t instanceof RuntimeException) {
                throw (RuntimeException) t;
            } else if (t instanceof Error) {
                throw (Error) t;
            } else {
                throw new NucleusException(t.getMessage(), t).setFatal();
            }
        } catch (Exception ex) {
            throw new NucleusException(ex.getMessage(), ex).setFatal();
        }
    }
    if (storeMgr == null) {
        throw new NucleusUserException(Localiser.msg("008004", url)).setFatal();
    }
    return storeMgr;
}
Also used : ConfigurationElement(org.datanucleus.plugin.ConfigurationElement) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TransactionIsolationNotSupportedException(org.datanucleus.exceptions.TransactionIsolationNotSupportedException) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) InvocationTargetException(java.lang.reflect.InvocationTargetException) StoreManager(org.datanucleus.store.StoreManager) Extension(org.datanucleus.plugin.Extension) NucleusException(org.datanucleus.exceptions.NucleusException) Map(java.util.Map)

Example 3 with ConfigurationElement

use of org.datanucleus.plugin.ConfigurationElement in project datanucleus-core by datanucleus.

the class TypeManagerImpl method loadTypeConverters.

/**
 * Method to load the java type that are currently registered in the PluginManager.
 * @param mgr the PluginManager
 * @param clr the ClassLoaderResolver
 */
private void loadTypeConverters(PluginManager mgr) {
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("016007"));
    }
    // Load up built-in converters
    registerConverter("dn.boolean-yn", new org.datanucleus.store.types.converters.BooleanYNConverter(), Boolean.class, Character.class, false, null);
    registerConverter("dn.boolean-integer", new org.datanucleus.store.types.converters.BooleanIntegerConverter(), Boolean.class, Integer.class, false, null);
    registerConverter("dn.character-string", new org.datanucleus.store.types.converters.CharacterStringConverter(), Character.class, String.class, false, null);
    registerConverter("dn.bigdecimal-string", new org.datanucleus.store.types.converters.BigDecimalStringConverter(), BigDecimal.class, String.class, false, null);
    registerConverter("dn.bigdecimal-double", new org.datanucleus.store.types.converters.BigDecimalDoubleConverter(), BigDecimal.class, Double.class, false, null);
    registerConverter("dn.biginteger-string", new org.datanucleus.store.types.converters.BigIntegerStringConverter(), BigInteger.class, String.class, false, null);
    registerConverter("dn.biginteger-long", new org.datanucleus.store.types.converters.BigIntegerLongConverter(), BigInteger.class, Long.class, false, null);
    registerConverter("dn.bitset-string", new org.datanucleus.store.types.converters.BitSetStringConverter(), BitSet.class, String.class, false, null);
    if (// Not present in some JDKs
    ClassUtils.isClassPresent("java.awt.Color", clr)) {
        // java.awt
        registerConverter("dn.color-string", new org.datanucleus.store.types.converters.ColorStringConverter(), java.awt.Color.class, String.class, false, null);
        registerConverter("dn.color-components", new org.datanucleus.store.types.converters.ColorComponentsConverter(), java.awt.Color.class, int[].class, false, null);
        registerConverter("dn.bufferedimage-bytearray", new org.datanucleus.store.types.converters.BufferedImageByteArrayConverter(), java.awt.image.BufferedImage.class, byte[].class, false, null);
        registerConverter("dn.bufferedimage-bytebuffer", new org.datanucleus.store.types.converters.BufferedImageByteBufferConverter(), java.awt.image.BufferedImage.class, ByteBuffer.class, false, null);
    }
    registerConverter("dn.class-string", new org.datanucleus.store.types.converters.ClassStringConverter(), Class.class, String.class, false, null);
    registerConverter("dn.integer-string", new org.datanucleus.store.types.converters.IntegerStringConverter(), Integer.class, String.class, false, null);
    registerConverter("dn.long-string", new org.datanucleus.store.types.converters.LongStringConverter(), Long.class, String.class, false, null);
    registerConverter("dn.currency-string", new org.datanucleus.store.types.converters.CurrencyStringConverter(), Currency.class, String.class, false, null);
    registerConverter("dn.locale-string", new org.datanucleus.store.types.converters.LocaleStringConverter(), Locale.class, String.class, false, null);
    registerConverter("dn.stringbuffer-string", new org.datanucleus.store.types.converters.StringBufferStringConverter(), StringBuffer.class, String.class, false, null);
    registerConverter("dn.stringbuilder-string", new org.datanucleus.store.types.converters.StringBuilderStringConverter(), StringBuilder.class, String.class, false, null);
    registerConverter("dn.timezone-string", new org.datanucleus.store.types.converters.TimeZoneStringConverter(), TimeZone.class, String.class, false, null);
    registerConverter("dn.uri-string", new org.datanucleus.store.types.converters.URIStringConverter(), java.net.URI.class, String.class, false, null);
    registerConverter("dn.url-string", new org.datanucleus.store.types.converters.URLStringConverter(), java.net.URL.class, String.class, false, null);
    registerConverter("dn.uuid-string", new org.datanucleus.store.types.converters.UUIDStringConverter(), UUID.class, String.class, false, null);
    // Date/Time
    registerConverter("dn.date-long", new org.datanucleus.store.types.converters.DateLongConverter(), java.util.Date.class, Long.class, false, null);
    registerConverter("dn.date-string", new org.datanucleus.store.types.converters.DateStringConverter(), java.util.Date.class, String.class, false, null);
    registerConverter("dn.sqldate-long", new org.datanucleus.store.types.converters.SqlDateLongConverter(), java.sql.Date.class, Long.class, false, null);
    registerConverter("dn.sqldate-string", new org.datanucleus.store.types.converters.SqlDateStringConverter(), java.sql.Date.class, String.class, false, null);
    registerConverter("dn.sqldate-date", new org.datanucleus.store.types.converters.SqlDateStringConverter(), java.sql.Date.class, java.util.Date.class, false, null);
    registerConverter("dn.sqltime-long", new org.datanucleus.store.types.converters.SqlTimeStringConverter(), java.sql.Time.class, Long.class, false, null);
    registerConverter("dn.sqltime-string", new org.datanucleus.store.types.converters.SqlTimeStringConverter(), java.sql.Time.class, String.class, false, null);
    registerConverter("dn.sqltime-date", new org.datanucleus.store.types.converters.SqlTimeStringConverter(), java.sql.Time.class, java.util.Date.class, false, null);
    registerConverter("dn.sqltimestamp-long", new org.datanucleus.store.types.converters.SqlTimestampStringConverter(), java.sql.Timestamp.class, Long.class, false, null);
    registerConverter("dn.sqltimestamp-date", new org.datanucleus.store.types.converters.SqlTimestampStringConverter(), java.sql.Timestamp.class, java.util.Date.class, false, null);
    registerConverter("dn.sqltimestamp-string", new org.datanucleus.store.types.converters.SqlTimestampStringConverter(), java.sql.Timestamp.class, String.class, false, null);
    registerConverter("dn.calendar-string", new org.datanucleus.store.types.converters.CalendarStringConverter(), Calendar.class, String.class, false, null);
    registerConverter("dn.calendar-date", new org.datanucleus.store.types.converters.CalendarDateConverter(), Calendar.class, java.util.Date.class, false, null);
    registerConverter("dn.calendar-timestamp", new org.datanucleus.store.types.converters.CalendarTimestampConverter(), Calendar.class, java.sql.Timestamp.class, false, null);
    registerConverter("dn.calendar-components", new org.datanucleus.store.types.converters.CalendarComponentsConverter(), Calendar.class, Object[].class, false, null);
    // Serializable
    registerConverter("dn.serializable-string", new org.datanucleus.store.types.converters.SerializableStringConverter(), java.io.Serializable.class, String.class, false, null);
    registerConverter("dn.serializable-bytearray", new org.datanucleus.store.types.converters.SerializableByteArrayConverter(), java.io.Serializable.class, byte[].class, false, null);
    registerConverter("dn.serializable-bytebuffer", new org.datanucleus.store.types.converters.SerializableByteBufferConverter(), java.io.Serializable.class, ByteBuffer.class, false, null);
    // Arrays
    registerConverter("dn.bytearray-bytebuffer", new org.datanucleus.store.types.converters.ByteArrayByteBufferConverter(), byte[].class, ByteBuffer.class, false, null);
    registerConverter("dn.booleanarray-bytebuffer", new org.datanucleus.store.types.converters.BooleanArrayByteBufferConverter(), boolean[].class, ByteBuffer.class, false, null);
    registerConverter("dn.chararray-bytebuffer", new org.datanucleus.store.types.converters.CharArrayByteBufferConverter(), char[].class, ByteBuffer.class, false, null);
    registerConverter("dn.doublearray-bytebuffer", new org.datanucleus.store.types.converters.DoubleArrayByteBufferConverter(), double[].class, ByteBuffer.class, false, null);
    registerConverter("dn.floatarray-bytebuffer", new org.datanucleus.store.types.converters.FloatArrayByteBufferConverter(), float[].class, ByteBuffer.class, false, null);
    registerConverter("dn.intarray-bytebuffer", new org.datanucleus.store.types.converters.IntArrayByteBufferConverter(), int[].class, ByteBuffer.class, false, null);
    registerConverter("dn.longarray-bytebuffer", new org.datanucleus.store.types.converters.LongArrayByteBufferConverter(), long[].class, ByteBuffer.class, false, null);
    registerConverter("dn.shortarray-bytebuffer", new org.datanucleus.store.types.converters.ShortArrayByteBufferConverter(), short[].class, ByteBuffer.class, false, null);
    registerConverter("dn.bigintegerarray-bytebuffer", new org.datanucleus.store.types.converters.BigIntegerArrayByteBufferConverter(), BigInteger[].class, ByteBuffer.class, false, null);
    registerConverter("dn.bigdecimalarray-bytebuffer", new org.datanucleus.store.types.converters.BigDecimalArrayByteBufferConverter(), BigDecimal[].class, ByteBuffer.class, false, null);
    // java.time
    registerConverter("dn.localdate-string", new org.datanucleus.store.types.converters.LocalDateStringConverter(), LocalDate.class, String.class, false, null);
    registerConverter("dn.localdate-sqldate", new org.datanucleus.store.types.converters.LocalDateSqlDateConverter(), LocalDate.class, java.sql.Date.class, false, null);
    registerConverter("dn.localdate-date", new org.datanucleus.store.types.converters.LocalDateDateConverter(), LocalDate.class, java.util.Date.class, false, null);
    registerConverter("dn.localtime-string", new org.datanucleus.store.types.converters.LocalTimeStringConverter(), LocalTime.class, String.class, false, null);
    registerConverter("dn.localtime-sqltime", new org.datanucleus.store.types.converters.LocalTimeSqlTimeConverter(), LocalTime.class, java.sql.Time.class, false, null);
    registerConverter("dn.localtime-date", new org.datanucleus.store.types.converters.LocalTimeDateConverter(), LocalTime.class, java.util.Date.class, false, null);
    registerConverter("dn.localtime-long", new org.datanucleus.store.types.converters.LocalTimeLongConverter(), LocalTime.class, Long.class, false, null);
    registerConverter("dn.localdatetime-string", new org.datanucleus.store.types.converters.LocalDateTimeStringConverter(), LocalDateTime.class, String.class, false, null);
    registerConverter("dn.localdatetime-timestamp", new org.datanucleus.store.types.converters.LocalDateTimeTimestampConverter(), LocalDateTime.class, java.sql.Timestamp.class, false, null);
    registerConverter("dn.localdatetime-date", new org.datanucleus.store.types.converters.LocalDateTimeDateConverter(), LocalDateTime.class, java.util.Date.class, false, null);
    registerConverter("dn.offsettime-string", new org.datanucleus.store.types.converters.OffsetTimeStringConverter(), OffsetTime.class, String.class, false, null);
    registerConverter("dn.offsettime-long", new org.datanucleus.store.types.converters.OffsetTimeLongConverter(), OffsetTime.class, Long.class, false, null);
    registerConverter("dn.offsettime-sqltime", new org.datanucleus.store.types.converters.OffsetTimeSqlTimeConverter(), OffsetTime.class, java.sql.Time.class, false, null);
    registerConverter("dn.offsetdatetime-string", new org.datanucleus.store.types.converters.OffsetDateTimeStringConverter(), OffsetDateTime.class, String.class, false, null);
    registerConverter("dn.offsetdatetime-timestamp", new org.datanucleus.store.types.converters.OffsetDateTimeTimestampConverter(), OffsetDateTime.class, java.sql.Timestamp.class, false, null);
    registerConverter("dn.offsetdatetime-date", new org.datanucleus.store.types.converters.OffsetDateTimeDateConverter(), OffsetDateTime.class, java.util.Date.class, false, null);
    registerConverter("dn.duration-string", new org.datanucleus.store.types.converters.DurationStringConverter(), Duration.class, String.class, false, null);
    registerConverter("dn.duration-long", new org.datanucleus.store.types.converters.DurationLongConverter(), Duration.class, Long.class, false, null);
    registerConverter("dn.duration-double", new org.datanucleus.store.types.converters.DurationDoubleConverter(), Duration.class, Double.class, false, null);
    registerConverter("dn.period-string", new org.datanucleus.store.types.converters.PeriodStringConverter(), Period.class, String.class, false, null);
    registerConverter("dn.period-components", new org.datanucleus.store.types.converters.PeriodComponentsConverter(), Period.class, int[].class, false, null);
    registerConverter("dn.instant-timestamp", new org.datanucleus.store.types.converters.InstantTimestampConverter(), Instant.class, java.sql.Timestamp.class, false, null);
    registerConverter("dn.instant-date", new org.datanucleus.store.types.converters.InstantDateConverter(), Instant.class, java.util.Date.class, false, null);
    registerConverter("dn.instant-string", new org.datanucleus.store.types.converters.InstantStringConverter(), Instant.class, String.class, false, null);
    registerConverter("dn.instant-long", new org.datanucleus.store.types.converters.InstantLongConverter(), Instant.class, Long.class, false, null);
    registerConverter("dn.year-string", new org.datanucleus.store.types.converters.YearStringConverter(), Year.class, String.class, false, null);
    registerConverter("dn.year-integer", new org.datanucleus.store.types.converters.YearIntegerConverter(), Year.class, Integer.class, false, null);
    registerConverter("dn.yearmonth-string", new org.datanucleus.store.types.converters.YearMonthStringConverter(), YearMonth.class, String.class, false, null);
    registerConverter("dn.yearmonth-components", new org.datanucleus.store.types.converters.YearMonthComponentsConverter(), YearMonth.class, int[].class, false, null);
    registerConverter("dn.yearmonth-sqldate", new org.datanucleus.store.types.converters.YearMonthSqlDateConverter(), YearMonth.class, java.sql.Date.class, false, null);
    registerConverter("dn.yearmonth-date", new org.datanucleus.store.types.converters.YearMonthDateConverter(), YearMonth.class, java.util.Date.class, false, null);
    registerConverter("dn.monthday-string", new org.datanucleus.store.types.converters.MonthDayStringConverter(), MonthDay.class, String.class, false, null);
    registerConverter("dn.monthday-components", new org.datanucleus.store.types.converters.MonthDayComponentsConverter(), MonthDay.class, int[].class, false, null);
    registerConverter("dn.monthday-sqldate", new org.datanucleus.store.types.converters.MonthDaySqlDateConverter(), MonthDay.class, java.sql.Date.class, false, null);
    registerConverter("dn.monthday-date", new org.datanucleus.store.types.converters.MonthDayDateConverter(), MonthDay.class, java.util.Date.class, false, null);
    registerConverter("dn.zoneid-string", new org.datanucleus.store.types.converters.ZoneIdStringConverter(), ZoneId.class, String.class, false, null);
    registerConverter("dn.zoneoffset-string", new org.datanucleus.store.types.converters.ZoneOffsetStringConverter(), ZoneOffset.class, String.class, false, null);
    registerConverter("dn.zoneddatetime-string", new org.datanucleus.store.types.converters.ZonedDateTimeStringConverter(), ZonedDateTime.class, String.class, false, null);
    registerConverter("dn.zoneddatetime-timestamp", new org.datanucleus.store.types.converters.ZonedDateTimeTimestampConverter(), ZonedDateTime.class, java.sql.Timestamp.class, false, null);
    // Add on any plugin mechanism types
    ConfigurationElement[] elems = mgr.getConfigurationElementsForExtension("org.datanucleus.type_converter", null, null);
    if (elems != null) {
        for (int i = 0; i < elems.length; i++) {
            String name = elems[i].getAttribute("name").trim();
            String memberTypeName = elems[i].getAttribute("member-type").trim();
            String datastoreTypeName = elems[i].getAttribute("datastore-type").trim();
            String converterClsName = elems[i].getAttribute("converter-class").trim();
            Class memberType = null;
            try {
                // Use plugin manager to instantiate the converter in case its in separate plugin
                TypeConverter conv = (TypeConverter) mgr.createExecutableExtension("org.datanucleus.type_converter", "name", name, "converter-class", null, null);
                memberType = clr.classForName(memberTypeName);
                Class datastoreType = clr.classForName(datastoreTypeName);
                registerConverter(name, conv, memberType, datastoreType, false, null);
            } catch (Exception e) {
                if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
                    if (memberType != null) {
                        NucleusLogger.PERSISTENCE.debug("TypeConverter for " + memberTypeName + "<->" + datastoreTypeName + " using " + converterClsName + " not instantiable (missing dependencies?) so ignoring");
                    } else {
                        NucleusLogger.PERSISTENCE.debug("TypeConverter for " + memberTypeName + "<->" + datastoreTypeName + " ignored since java type not present in CLASSPATH");
                    }
                }
            }
        }
    }
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("016008"));
        if (typeConverterMap != null) {
            List<Class> typesList = new ArrayList<Class>(typeConverterMap.keySet());
            Collections.sort(typesList, ALPHABETICAL_ORDER);
            for (Class javaType : typesList) {
                Set<Class> datastoreTypes = typeConverterMap.get(javaType).keySet();
                StringBuilder str = new StringBuilder();
                for (Class datastoreCls : datastoreTypes) {
                    if (str.length() > 0) {
                        str.append(',');
                    }
                    str.append(StringUtils.getNameOfClass(datastoreCls));
                }
                NucleusLogger.PERSISTENCE.debug("TypeConverter(s) available for " + StringUtils.getNameOfClass(javaType) + " to : " + str.toString());
            }
        }
    }
}
Also used : ClassStringConverter(org.datanucleus.store.types.converters.ClassStringConverter) ArrayList(java.util.ArrayList) ConfigurationElement(org.datanucleus.plugin.ConfigurationElement) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) TypeConverter(org.datanucleus.store.types.converters.TypeConverter)

Example 4 with ConfigurationElement

use of org.datanucleus.plugin.ConfigurationElement in project datanucleus-core by datanucleus.

the class QueryManagerImpl method getInMemoryEvaluatorForMethod.

/* (non-Javadoc)
     * @see org.datanucleus.store.query.QueryManager#getInMemoryEvaluatorForMethod(java.lang.Class, java.lang.String)
     */
@Override
public InvocationEvaluator getInMemoryEvaluatorForMethod(Class type, String methodName) {
    String lookupName = type != null ? (type.getName() + ":" + methodName) : methodName;
    // Hardcode support for Array.size()/Array.length()/Array.contains() since not currently pluggable
    if (type != null && type.isArray()) {
        lookupName = "ARRAY:" + methodName;
    }
    InvocationEvaluator eval = inmemoryQueryMethodEvaluatorByName.get(lookupName);
    if (eval != null) {
        return eval;
    }
    // Load built-in handler for this class+method
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(type != null ? type.getClassLoader() : null);
    if (type == null) {
        if ("Math.abs".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.AbsFunction();
        if ("Math.sqrt".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.SqrtFunction();
        if ("Math.acos".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.ArcCosineFunction();
        if ("Math.asin".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.ArcSineFunction();
        if ("Math.atan".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.ArcTangentFunction();
        if ("Math.cos".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.CosineFunction();
        if ("Math.sin".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.SineFunction();
        if ("Math.tan".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.TangentFunction();
        if ("Math.log".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.LogFunction();
        if ("Math.exp".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.ExpFunction();
        if ("Math.floor".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.FloorFunction();
        if ("Math.ceil".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.CeilFunction();
        if ("Math.toDegrees".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.DegreesFunction();
        if ("Math.toRadians".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.RadiansFunction();
        if ("CURRENT_DATE".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.CurrentDateFunction();
        if ("CURRENT_TIME".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.CurrentTimeFunction();
        if ("CURRENT_TIMESTAMP".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.CurrentTimestampFunction();
        if ("ABS".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.AbsFunction();
        if ("SQRT".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.SqrtFunction();
        if ("MOD".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.ModFunction();
        if ("COALESCE".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.CoalesceFunction();
        if ("COS".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.CosineFunction();
        if ("SIN".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.SineFunction();
        if ("TAN".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.TangentFunction();
        if ("ACOS".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.ArcCosineFunction();
        if ("ASIN".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.ArcSineFunction();
        if ("ATAN".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.ArcTangentFunction();
        if ("CEIL".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.CeilFunction();
        if ("FLOOR".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.FloorFunction();
        if ("LOG".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.LogFunction();
        if ("EXP".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.ExpFunction();
        if ("NULLIF".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.NullIfFunction();
        if ("SIZE".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.SizeFunction();
        if ("UPPER".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.UpperFunction();
        if ("LOWER".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.LowerFunction();
        if ("LENGTH".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.LengthFunction();
        if ("CONCAT".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.ConcatFunction();
        if ("SUBSTRING".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.SubstringFunction();
        if ("LOCATE".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.LocateFunction();
        if ("TRIM".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.TrimFunction();
        if ("TRIM_LEADING".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.TrimFunction();
        if ("TRIM_TRAILING".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.TrimFunction();
        if ("DEGREES".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.DegreesFunction();
        if ("RADIANS".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.RadiansFunction();
        if ("YEAR".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.TemporalYearMethod();
        if ("MONTH".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.TemporalMonthMethod();
        if ("MONTH_JAVA".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.TemporalMonthJavaMethod();
        if ("DAY".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.TemporalDayMethod();
        if ("HOUR".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.TemporalHourMethod();
        if ("MINUTE".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.TemporalMinuteMethod();
        if ("SECOND".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.TemporalSecondMethod();
        if (eval != null) {
            inmemoryQueryMethodEvaluatorByName.put(lookupName, eval);
            return eval;
        }
    } else {
        if (type != null && type.isArray()) {
            if ("size".equals(methodName))
                eval = new ArraySizeMethod();
            if ("length".equals(methodName))
                eval = new ArraySizeMethod();
            if ("contains".equals(methodName))
                eval = new ArrayContainsMethod();
        } else if (type.isEnum()) {
            if ("matches".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.EnumMatchesMethod();
            if ("toString".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.EnumToStringMethod();
            if ("ordinal".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.EnumOrdinalMethod();
        } else if ("java.lang.String".equals(type.getName())) {
            if ("charAt".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringCharAtMethod();
            if ("concat".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringConcatMethod();
            if ("endsWith".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringEndsWithMethod();
            if ("equals".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringEqualsMethod();
            if ("equalsIgnoreCase".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringEqualsIgnoreCaseMethod();
            if ("indexOf".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringIndexOfMethod();
            if ("length".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringLengthMethod();
            if ("matches".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringMatchesMethod();
            if ("startsWith".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringStartsWithMethod();
            if ("substring".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringSubstringMethod();
            if ("toUpperCase".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringToUpperCaseMethod();
            if ("toLowerCase".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringToLowerCaseMethod();
            if ("trim".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringTrimMethod();
            if ("trimLeft".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringTrimLeftMethod();
            if ("trimRight".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.StringTrimRightMethod();
        } else if (java.util.Collection.class.isAssignableFrom(type)) {
            if ("size".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.ContainerSizeMethod();
            if ("isEmpty".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.ContainerIsEmptyMethod();
            if ("contains".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.CollectionContainsMethod();
        } else if (java.util.Map.class.isAssignableFrom(type)) {
            if ("size".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.ContainerSizeMethod();
            if ("isEmpty".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.ContainerIsEmptyMethod();
            if ("containsKey".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.MapContainsKeyMethod();
            if ("containsValue".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.MapContainsValueMethod();
            if ("containsEntry".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.MapContainsEntryMethod();
            if ("get".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.MapGetMethod();
        } else if (java.util.Optional.class.isAssignableFrom(type)) {
            if ("isPresent".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.OptionalIsPresentMethod();
            if ("get".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.OptionalGetMethod();
            if ("orElse".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.OptionalOrElseMethod();
        } else if (java.util.Date.class.isAssignableFrom(type)) {
            if ("getTime".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.DateGetTimeMethod();
            if ("getDay".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.DateGetDayMethod();
            if ("getDate".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.DateGetDayMethod();
            if ("getDayOfWeek".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.DateGetDayOfWeekMethod();
            if ("getMonth".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.DateGetMonthMethod();
            if ("getYear".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.DateGetYearMethod();
            if ("getHour".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.DateGetHoursMethod();
            if ("getMinute".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.DateGetMinutesMethod();
            if ("getSecond".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.DateGetSecondsMethod();
        } else if (java.time.LocalDate.class.isAssignableFrom(type)) {
            if ("getDayOfMonth".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalDateGetDayOfMonth();
            if ("getDayOfWeek".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalDateGetDayOfWeek();
            if ("getMonthValue".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalDateGetMonthValue();
            if ("getYear".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalDateGetYear();
        } else if (java.time.LocalTime.class.isAssignableFrom(type)) {
            if ("getHour".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalTimeGetHour();
            if ("getMinute".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalTimeGetMinute();
            if ("getSecond".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalTimeGetSecond();
        } else if (java.time.LocalDateTime.class.isAssignableFrom(type)) {
            if ("getDayOfMonth".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalDateTimeGetDayOfMonth();
            if ("getDayOfWeek".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalDateTimeGetDayOfWeek();
            if ("getMonthValue".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalDateTimeGetMonthValue();
            if ("getYear".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalDateTimeGetYear();
            if ("getHour".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalDateTimeGetHour();
            if ("getMinute".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalDateTimeGetMinute();
            if ("getSecond".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.LocalDateTimeGetSecond();
        } else if (java.time.MonthDay.class.isAssignableFrom(type)) {
            if ("getDayOfMonth".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.MonthDayGetDayOfMonth();
            if ("getMonthValue".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.MonthDayGetMonthValue();
        } else if (java.time.Period.class.isAssignableFrom(type)) {
            if ("getDays".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.PeriodGetDays();
            if ("getMonths".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.PeriodGetMonths();
            if ("getYears".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.PeriodGetYears();
        } else if (java.time.YearMonth.class.isAssignableFrom(type)) {
            if ("getYear".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.YearMonthGetYear();
            if ("getMonthValue".equals(methodName))
                eval = new org.datanucleus.query.inmemory.method.YearMonthGetMonthValue();
        }
        if (eval == null && java.lang.Object.class.isAssignableFrom(type) && "getClass".equals(methodName))
            eval = new org.datanucleus.query.inmemory.method.ObjectGetClassMethod();
        if (eval != null) {
            inmemoryQueryMethodEvaluatorByName.put(lookupName, eval);
            return eval;
        }
    }
    // Fallback to the plugin mechanism
    PluginManager pluginMgr = nucleusCtx.getPluginManager();
    ConfigurationElement[] elems = pluginMgr.getConfigurationElementsForExtension("org.datanucleus.query_method_evaluators", "method", methodName);
    if (elems == null) {
        return null;
    }
    // TODO Lookup with class specified when type != null
    InvocationEvaluator requiredEvaluator = null;
    for (int i = 0; i < elems.length; i++) {
        try {
            String evalName = elems[i].getAttribute("evaluator");
            eval = (InvocationEvaluator) pluginMgr.createExecutableExtension("org.datanucleus.query_method_evaluators", new String[] { "method", "evaluator" }, new String[] { methodName, evalName }, "evaluator", null, null);
            String elemClsName = elems[i].getAttribute("class");
            if (elemClsName != null && StringUtils.isWhitespace(elemClsName)) {
                elemClsName = null;
            }
            if (elemClsName == null) {
                // Static method call
                if (type == null) {
                    // Evaluator is applicable to the required type
                    requiredEvaluator = eval;
                }
                inmemoryQueryMethodEvaluatorByName.put(lookupName, eval);
            } else {
                Class elemCls = clr.classForName(elemClsName);
                if (elemCls.isAssignableFrom(type)) {
                    // Evaluator is applicable to the required type
                    requiredEvaluator = eval;
                }
                inmemoryQueryMethodEvaluatorByName.put(lookupName, eval);
            }
        } catch (Exception e) {
        // Impossible to create the evaluator (class doesn't exist?) TODO Log this?
        }
    }
    return requiredEvaluator;
}
Also used : PluginManager(org.datanucleus.plugin.PluginManager) ArraySizeMethod(org.datanucleus.query.inmemory.method.ArraySizeMethod) ConfigurationElement(org.datanucleus.plugin.ConfigurationElement) InvocationEvaluator(org.datanucleus.query.inmemory.InvocationEvaluator) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) ArrayContainsMethod(org.datanucleus.query.inmemory.method.ArrayContainsMethod) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 5 with ConfigurationElement

use of org.datanucleus.plugin.ConfigurationElement in project datanucleus-core by datanucleus.

the class PluginParserTest method testParseExtensionPoint.

public void testParseExtensionPoint() {
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    NonManagedPluginRegistry mgr = new NonManagedPluginRegistry(clr, "EXCEPTION", true);
    assertEquals(0, mgr.getExtensionPoints().length);
    Bundle bundle0 = mgr.registerBundle(clr.getResource("/org/datanucleus/samples/plugin/MANIFEST0.MF", null));
    mgr.registerExtensionsForPlugin(clr.getResource("/org/datanucleus/samples/plugin/plugin1expoint.xml", null), bundle0);
    assertEquals(2, mgr.getExtensionPoints().length);
    assertNull(mgr.getExtensionPoint("testID"));
    assertNull(mgr.getExtensionPoint("testID2"));
    assertNotNull(mgr.getExtensionPoint("org.datanucleus.testID"));
    assertNotNull(mgr.getExtensionPoint("org.datanucleus.testID2"));
    ExtensionPoint point = mgr.getExtensionPoint("org.datanucleus.testID");
    assertEquals("testID", point.getId());
    assertEquals("org.datanucleus.testID", point.getUniqueId());
    assertEquals("testName", point.getName());
    assertNotNull(clr.getResource("/org/datanucleus/samples/plugin/plugin1.xsd", null));
    assertEquals(clr.getResource("/org/datanucleus/samples/plugin/plugin1.xsd", null), point.getSchema());
    assertEquals(0, point.getExtensions().length);
    mgr.registerExtensionsForPlugin(clr.getResource("/org/datanucleus/samples/plugin/plugin1.xml", null), bundle0);
    assertEquals(2, point.getExtensions().length);
    Extension[] exts = point.getExtensions();
    assertEquals(exts[0].getPlugin(), exts[1].getPlugin());
    assertEquals(2, exts[0].getConfigurationElements().length);
    ConfigurationElement[] level1 = exts[0].getConfigurationElements();
    assertEquals(2, level1[0].getChildren().length);
    assertEquals("level1", level1[0].getName());
    assertEquals(1, level1[0].getAttributeNames().length);
    assertEquals("1", level1[0].getAttribute("attr11"));
    assertNull(level1[0].getAttribute("attr11XXX"));
    ConfigurationElement[] level2 = level1[0].getChildren();
    assertEquals(1, level2[0].getChildren().length);
    assertEquals("level2", level2[0].getName());
    assertEquals(2, level2[0].getAttributeNames().length);
    assertEquals("attr21", level2[0].getAttributeNames()[0]);
    assertEquals("attr22", level2[0].getAttributeNames()[1]);
    assertEquals("2211", level2[0].getAttribute("attr21"));
    assertEquals("2221", level2[0].getAttribute("attr22"));
    assertNull(level2[0].getAttribute("attr11XXX"));
    assertEquals(0, level1[1].getChildren().length);
    assertEquals("2", level1[1].getAttribute("attr11"));
    assertEquals(1, exts[1].getConfigurationElements().length);
    level1 = exts[1].getConfigurationElements();
    assertEquals("A", level1[0].getAttribute("attr11"));
    assertEquals(0, level1[0].getChildren().length);
}
Also used : Extension(org.datanucleus.plugin.Extension) ConfigurationElement(org.datanucleus.plugin.ConfigurationElement) NonManagedPluginRegistry(org.datanucleus.plugin.NonManagedPluginRegistry) Bundle(org.datanucleus.plugin.Bundle) ExtensionPoint(org.datanucleus.plugin.ExtensionPoint) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Aggregations

ConfigurationElement (org.datanucleus.plugin.ConfigurationElement)9 NucleusException (org.datanucleus.exceptions.NucleusException)5 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)3 PluginManager (org.datanucleus.plugin.PluginManager)3 HashSet (java.util.HashSet)2 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 Extension (org.datanucleus.plugin.Extension)2 StoreManager (org.datanucleus.store.StoreManager)2 InputStream (java.io.InputStream)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1