Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.commons.marshall.jboss;
  
 import  org.jboss.marshalling.ExceptionListener;
 import  org.jboss.marshalling.Marshalling;
 import  org.jboss.marshalling.MarshallingConfiguration;
 import  org.jboss.marshalling.TraceInformation;
 import  org.jboss.marshalling.Unmarshaller;
 import  org.jboss.marshalling.reflect.SunReflectiveCreator;
 
 import java.net.URL;
 
 import static org.infinispan.commons.util.ReflectionUtil.EMPTY_CLASS_ARRAY;
 import static org.infinispan.commons.util.Util.EMPTY_OBJECT_ARRAY;

Common parent for both embedded and standalone JBoss Marshalling-based marshallers.

Author(s):
Galder ZamarreƱo
Sanne Grinovero
Dan Berindei
Since:
5.0
 
 public abstract class AbstractJBossMarshaller extends AbstractMarshaller implements StreamingMarshaller {
 
    protected static final BasicLogger log = BasicLogFactory.getLog(AbstractJBossMarshaller.class);
    protected static final boolean trace = .isTraceEnabled();
    protected static final JBossMarshallerFactory factory = new JBossMarshallerFactory();
    protected static final int DEF_INSTANCE_COUNT = 16;
    protected static final int DEF_CLASS_COUNT = 8;
    private static final int PER_THREAD_REUSABLE_INSTANCES = 6;
    private static final int RIVER_INTERNAL_BUFFER = 512;
 
    protected final MarshallingConfiguration baseCfg;

   
Marshaller thread local. In non-internal marshaller usages, such as Java Hot Rod client, this is a singleton shared by all so no urgent need for static here. JBMAR clears pretty much any state during finish(), so no urgent need to clear the thread local since it shouldn't be leaking. It might take a long time to warmup and pre-initialize all needed instances!
 
       @Override
       protected PerThreadInstanceHolder initialValue() {
          MarshallingConfiguration cfg = .clone();
          return new PerThreadInstanceHolder(cfg);
       }
    };
 
    public AbstractJBossMarshaller() {
       // Class resolver now set when marshaller/unmarshaller will be created
        = new MarshallingConfiguration();
       .setExternalizerCreator(new SunReflectiveCreator());
       .setExceptionListener(new DebuggingExceptionListener());
       .setClassExternalizerFactory(new SerializeWithExtFactory());
       .setInstanceCount();
       .setClassCount();
       .setVersion(3);
    }
 
    @Override
    final public void objectToObjectStream(final Object objfinal ObjectOutput outthrows IOException {
       out.writeObject(obj);
    }
 
    @Override
    final protected ByteBuffer objectToBuffer(final Object ofinal int estimatedSizethrows IOException {
       ExposedByteArrayOutputStream baos = new ExposedByteArrayOutputStream(estimatedSize);
       ObjectOutput marshaller = startObjectOutput(baosfalseestimatedSize);
       try {
          objectToObjectStream(omarshaller);
       } finally {
          finishObjectOutput(marshaller);
       }
       return new ByteBufferImpl(baos.getRawBuffer(), 0, baos.size());
    }
 
    @Override
    final public ObjectOutput startObjectOutput(final OutputStream osfinal boolean isReentrantfinal int estimatedSizethrows IOException {
       PerThreadInstanceHolder instanceHolder = .get();
       org.jboss.marshalling.Marshaller marshaller = instanceHolder.getMarshaller(estimatedSize);
       marshaller.start(Marshalling.createByteOutput(os));
       return marshaller;
    }
   final public void finishObjectOutput(final ObjectOutput oo) {
      try {
         if (.trace("Stop marshaller");
         ((org.jboss.marshalling.Marshaller) oo).finish();
      } catch (IOException ignored) {
      }
   }
   final public Object objectFromByteBuffer(final byte[] buffinal int offsetfinal int lengththrows IOException,
           ClassNotFoundException {
      ByteArrayInputStream is = new ByteArrayInputStream(bufoffsetlength);
      ObjectInput unmarshaller = startObjectInput(isfalse);
      Object o = null;
      try {
         o = objectFromObjectStream(unmarshaller);
      } finally {
         finishObjectInput(unmarshaller);
      }
      return o;
   }
   final public ObjectInput startObjectInput(final InputStream isfinal boolean isReentrantthrows IOException {
      PerThreadInstanceHolder instanceHolder = .get();
      Unmarshaller unmarshaller = instanceHolder.getUnmarshaller();
      if ()
         .tracef("Start unmarshaller after retrieving marshaller from %s",
                   isReentrant ? "factory" : "thread local");
      unmarshaller.start(Marshalling.createByteInput(is));
      return unmarshaller;
   }
   final public Object objectFromObjectStream(final ObjectInput inthrows IOExceptionClassNotFoundException {
      return in.readObject();
   }
   final public void finishObjectInput(final ObjectInput oi) {
      try {
         if ()
            .trace("Stop unmarshaller");
         if (oi != null) ((Unmarshaller) oi).finish();
      } catch (IOException ignored) {
      }
   }
   public boolean isMarshallable(Object othrows Exception {
      Class<?> clazz = o.getClass();
      boolean containsMarshallable = .isKnownMarshallable(clazz);
      if (containsMarshallable) {
         boolean marshallable = .isMarshallable(clazz);
         if ()
            .tracef("Marshallable type '%s' known and is marshallable=%b",
               clazz.getName(), marshallable);
         return marshallable;
      } else {
         if (isMarshallableCandidate(o)) {
            boolean isMarshallable = true;
            try {
               objectToBuffer(o);
            } catch (Exception e) {
               isMarshallable = false;
               throw e;
            } finally {
               .markMarshallable(clazzisMarshallable);
            }
            return isMarshallable;
         }
         return false;
      }
   }
   public void start() {
      // No-op
   }
   public void stop() {
       // Clear class cache
   }
   protected boolean isMarshallableCandidate(Object o) {
      return o instanceof Serializable;
   }
   protected static final class DebuggingExceptionListener implements ExceptionListener {
      private static final URL[] EMPTY_URLS = {};
      @Override
      public void handleMarshallingException(final Throwable problemfinal Object subject) {
         if (.isDebugEnabled()) {
            TraceInformation.addUserInformation(problem"toString = " + subject.toString());
         }
      }
      @Override
      public void handleUnmarshallingException(final Throwable problemfinal Class<?> subjectClass) {
         if (.isDebugEnabled()) {
            StringBuilder builder = new StringBuilder();
            ClassLoader cl = subjectClass.getClassLoader();
            builder.append("classloader hierarchy:");
            ClassLoader parent = cl;
            while (parent != null) {
               if (parent.equals(cl)) {
                  builder.append("\n\t\t-> type classloader = ").append(parent);
               } else {
                  builder.append("\n\t\t-> parent classloader = ").append(parent);
               }
               URL[] urls = getClassLoaderURLs(parent);
               if (urls != null) {
                  for (URL u : urlsbuilder.append("\n\t\t->...").append(u);
               }
               parent = parent.getParent();
            }
            TraceInformation.addUserInformation(problembuilder.toString());
         }
      }
      @Override
      public void handleUnmarshallingException(Throwable problem) {
         // no-op
      }
      private static URL[] getClassLoaderURLs(final ClassLoader cl) {
         URL[] urls = ;
         try {
            Class<?> returnType = urls.getClass();
            Class<?>[] parameterTypes = ;
            Method getURLs = cl.getClass().getMethod("getURLs"parameterTypes);
            if (returnType.isAssignableFrom(getURLs.getReturnType())) {
               Object[] args = ;
               urls = (URL[]) getURLs.invoke(clargs);
            }
         } catch (Exception ignore) {
         }
         return urls;
      }
   }
   private static final class PerThreadInstanceHolder implements RiverCloseListener {
      final MarshallingConfiguration configuration;
      int availableMarshallerIndex = 0;
      int availableUnMarshallerIndex = 0;
      PerThreadInstanceHolder(final MarshallingConfiguration threadDedicatedConfiguration) {
         this. = threadDedicatedConfiguration;
      }
      Unmarshaller getUnmarshaller() throws IOException {
         //as opposing to getMarshaller(int), in this case we don't have a good hint about initial buffer sizing
            //we're above the pool threshold: make a throw-away-after usage Marshaller
            .setBufferSize(512);//reset to default as it might be changed by getMarshaller
            return .createUnmarshaller();
         }
         else {
            if (unMarshaller != null) {
               ++;
               return unMarshaller;
            }
            else {
               .setBufferSize();//reset to default as it might be changed by getMarshaller
               unMarshaller = .createUnmarshaller();
               unMarshaller.setCloseListener(this);
               [] = unMarshaller;
               ++;
               return unMarshaller;
            }
         }
      }
      ExtendedRiverMarshaller getMarshaller(int estimatedSizethrows IOException {
            //we're above the pool threshold: make a throw-away-after usage Marshaller
            //setting the buffer as cheap as possible:
            .setBufferSize(estimatedSize);
            return .createMarshaller();
         }
         else {
            if (marshaller != null) {
               ++;
               return marshaller;
            }
            else {
               //we're going to pool this one, make sure the buffer size is set to a reasonable value
               //as we might have changed it previously:
               .setBufferSize();
               marshaller = .createMarshaller();
               marshaller.setCloseListener(this);
               [] = marshaller;
               ++;
               return marshaller;
            }
         }
      }
      @Override
      public void closeMarshaller() {
         --;
      }
      @Override
      public void closeUnmarshaller() {
         --;
      }
   }
New to GrepCode? Check out our FAQ X