Search in sources :

Example 26 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project beam by apache.

the class ProtoCoder method getExtensionRegistry.

/**
   * Returns the {@link ExtensionRegistry} listing all known Protocol Buffers extension messages
   * to {@code T} registered with this {@link ProtoCoder}.
   */
public ExtensionRegistry getExtensionRegistry() {
    if (memoizedExtensionRegistry == null) {
        ExtensionRegistry registry = ExtensionRegistry.newInstance();
        for (Class<?> extensionHost : extensionHostClasses) {
            try {
                extensionHost.getDeclaredMethod("registerAllExtensions", ExtensionRegistry.class).invoke(null, registry);
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                throw new IllegalStateException(e);
            }
        }
        memoizedExtensionRegistry = registry.getUnmodifiable();
    }
    return memoizedExtensionRegistry;
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 27 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project beam by apache.

the class ProtobufUtil method verifyDeterministic.

/**
   * Recursively checks whether the specified class uses any Protocol Buffers fields that cannot
   * be deterministically encoded.
   *
   * @throws NonDeterministicException if the object cannot be encoded deterministically.
   */
static void verifyDeterministic(ProtoCoder<?> coder) throws NonDeterministicException {
    Class<? extends Message> message = coder.getMessageType();
    ExtensionRegistry registry = coder.getExtensionRegistry();
    Set<Descriptor> descriptors = getRecursiveDescriptorsForClass(message, registry);
    for (Descriptor d : descriptors) {
        for (FieldDescriptor fd : d.getFields()) {
            // be encoded deterministically.
            if (fd.isMapField()) {
                String reason = String.format("Protocol Buffers message %s transitively includes Map field %s (from file %s)." + " Maps cannot be deterministically encoded.", message.getName(), fd.getFullName(), fd.getFile().getFullName());
                throw new NonDeterministicException(coder, reason);
            }
        }
    }
}
Also used : NonDeterministicException(org.apache.beam.sdk.coders.Coder.NonDeterministicException) GenericDescriptor(com.google.protobuf.Descriptors.GenericDescriptor) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 28 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project drools by kiegroup.

the class AbstractKieModule method getCompilationCache.

@Override
public CompilationCache getCompilationCache(String kbaseName) {
    // Map< DIALECT, Map< RESOURCE, List<BYTECODE> > >
    CompilationCache cache = compilationCache.get(kbaseName);
    if (cache == null) {
        byte[] fileContents = getBytes(KieBuilderImpl.getCompilationCachePath(releaseId, kbaseName));
        if (fileContents != null) {
            ExtensionRegistry registry = KieModuleCacheHelper.buildRegistry();
            try {
                Header _header = KieModuleCacheHelper.readFromStreamWithHeaderPreloaded(new ByteArrayInputStream(fileContents), registry);
                if (!Drools.isCompatible(_header.getVersion().getVersionMajor(), _header.getVersion().getVersionMinor(), _header.getVersion().getVersionRevision())) {
                    // if cache has been built with an incompatible version avoid to use it
                    log.warn("The compilation cache has been built with an incompatible version. " + "You should recompile your project in order to use it with current release.");
                    return null;
                }
                KModuleCache _cache = KModuleCache.parseFrom(_header.getPayload());
                cache = new CompilationCache();
                for (CompilationData _data : _cache.getCompilationDataList()) {
                    for (CompDataEntry _entry : _data.getEntryList()) {
                        cache.addEntry(_data.getDialect(), _entry.getId(), _entry.getData().toByteArray());
                    }
                }
                compilationCache.put(kbaseName, cache);
            } catch (Exception e) {
                log.error("Unable to load compilation cache... ", e);
            }
        }
    }
    return cache;
}
Also used : KModuleCache(org.drools.compiler.kie.builder.impl.KieModuleCache.KModuleCache) CompilationData(org.drools.compiler.kie.builder.impl.KieModuleCache.CompilationData) CompDataEntry(org.drools.compiler.kie.builder.impl.KieModuleCache.CompDataEntry) Header(org.drools.compiler.kie.builder.impl.KieModuleCache.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 29 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project drools by kiegroup.

the class ProtobufInputMarshaller method loadAndParseSession.

private static ProtobufMessages.KnowledgeSession loadAndParseSession(MarshallerReaderContext context) throws IOException, ClassNotFoundException {
    ExtensionRegistry registry = PersisterHelper.buildRegistry(context, processMarshaller);
    ProtobufMessages.Header _header = PersisterHelper.readFromStreamWithHeaderPreloaded(context, registry);
    return ProtobufMessages.KnowledgeSession.parseFrom(_header.getPayload(), registry);
}
Also used : ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Aggregations

ExtensionRegistry (com.google.protobuf.ExtensionRegistry)29 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Descriptor (com.google.protobuf.Descriptors.Descriptor)4 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)4 MessageData (protos.MessageData)4 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)3 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)3 EnumMsg (protos.EnumMsg)3 IOException (java.io.IOException)2 Test (org.junit.Test)2 PrimitiveFields (protos.PrimitiveFields)2 StringMsg (protos.StringMsg)2 TypicalData (protos.TypicalData)2 TypicalDataMessage (protos.TypicalDataMessage)2 ByteString (com.google.protobuf.ByteString)1 CodedInputStream (com.google.protobuf.CodedInputStream)1 GenericDescriptor (com.google.protobuf.Descriptors.GenericDescriptor)1 ExtensionRegistryLite (com.google.protobuf.ExtensionRegistryLite)1 GeneratedMessage (com.google.protobuf.GeneratedMessage)1 Message (com.google.protobuf.Message)1