Example usage for java.io ObjectStreamClass getName

List of usage examples for java.io ObjectStreamClass getName

Introduction

In this page you can find the example usage for java.io ObjectStreamClass getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the class described by this descriptor.

Usage

From source file:com.jhash.oimadmin.Utils.java

public static ObjectInputStream getObjectInputStream(InputStream inputStream, URL... jars) throws IOException {
    logger.debug("Creating new Input Stream with input stream {}, URLS {}", inputStream, jars);
    if (jars != null) {
        try {/*from   ww w  . j a v  a 2s. c o m*/
            ClassLoader urlClassLoader = new URLClassLoader(jars, null);
            return new ObjectInputStream(inputStream) {

                protected Class<?> resolveClass(ObjectStreamClass desc)
                        throws IOException, ClassNotFoundException {
                    logger.debug("Trying to resolve class using description {}", desc);
                    String name = null;
                    try {
                        name = desc.getName();
                        logger.debug("Trying to load class {} using URL Class Loader {}", name, urlClassLoader);
                        Class<?> classValue = Class.forName(name, false, urlClassLoader);
                        logger.debug("Located class as {}", classValue);
                        return classValue;
                    } catch (ClassNotFoundException ex) {
                        logger.debug("Failed to locate Class " + name + ". Invoking parent method.", ex);
                        return super.resolveClass(desc);
                    }
                }
            };
        } catch (Exception exception) {
            logger.warn(
                    "Failed to create Class Loader with URL " + jars
                            + ". Returning standard object input stream with input stream " + inputStream,
                    exception);
            return new ObjectInputStream(inputStream);
        }
    } else {
        logger.debug("Returning standard object input stream since no additional urls were provided.");
        return new ObjectInputStream(inputStream);
    }
}

From source file:cascading.util.Util.java

public static Object deserializeBase64(String string, boolean decompress) throws IOException {
    if (string == null || string.length() == 0)
        return null;

    ObjectInputStream in = null;/*  ww  w .  j a  va 2  s.c o m*/

    try {
        ByteArrayInputStream bytes = new ByteArrayInputStream(Base64.decodeBase64(string.getBytes()));

        in = new ObjectInputStream(decompress ? new GZIPInputStream(bytes) : bytes) {
            @Override
            protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
                try {
                    return Class.forName(desc.getName(), false, Thread.currentThread().getContextClassLoader());
                } catch (ClassNotFoundException exception) {
                    return super.resolveClass(desc);
                }
            }
        };

        return in.readObject();
    } catch (ClassNotFoundException exception) {
        throw new FlowException("unable to deserialize data", exception);
    } finally {
        if (in != null)
            in.close();
    }
}

From source file:io.github.tavernaextras.biocatalogue.model.Util.java

/**
 * This method is "clones" an object supplied as an argument. It uses
 * serialisation to achieve this (as opposed to manually implementing deep
 * copying of all referenced objects in the graph of the provided object).
 * This technique is used to make sure that the new object will be exact
 * replica, but totally independent of the original one.
 * /*from w  w w  .  jav  a2s  .  c o m*/
 * Note that this code works ~100 times slower than it would do if deep copying
 * was implemented. However, this will not be used in tight loops (and in loops
 * at all), so for one-off tasks it is fine.
 * 
 * @author Dave Miller<br/>
 * Original version of the code in this method is taken from
 * <a href="http://www.javaworld.com/javaworld/javatips/jw-javatip76.html?page=2">
 *    http://www.javaworld.com/javaworld/javatips/jw-javatip76.html?page=2
 * </a> [accessed on 25/Feb/2010].
 * <br/><br/>
 * 
 * @author Subhajit Dasgupta<br/>
 * Example of using an alternative class loader during object de-serialisation
 * was taken from
 * <a href="http://blogs.sun.com/adventures/entry/desrializing_objects_custom_class_loaders">
 *    http://blogs.sun.com/adventures/entry/desrializing_objects_custom_class_loaders
 * </a> [accessed on 29/Mar/2010].
 * 
 * @return Deep copy of the provided object. If deep copying doesn't succeed,
 *         <code>null</code> is returned.
 */
public static Object deepCopy(Object objectToCopy) {
    // a "safety net" - a class loader of BioCatalogue perspective may be used in
    // de-serialisation process to make sure that all classes are recognised
    // (system class loader may not be able to "see" all BioCatalogue plugin's files,
    //  but just those in Taverna's /lib folder)
    final ClassLoader[] customClassLoaders = new ClassLoader[] {
            BioCataloguePerspective.class.getClassLoader() };

    try {
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);

            // serialise and pass the object
            oos.writeObject(objectToCopy);
            oos.flush();

            // read and return the new object
            ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bin) {
                /**
                 * <code>resolveClass()</code> method is overridden to make use of
                 * custom ClassLoader in the de-serialisation process.
                 * <br/>
                 * This is needed to make sure that the ClassLoader of the BioCatalogue
                 * perspective is used as opposed to the system ClassLoader which will
                 * only be able to see classes from Taverna's /lib folder.
                 */
                protected Class<?> resolveClass(ObjectStreamClass desc)
                        throws IOException, ClassNotFoundException {
                    String className = desc.getName();
                    try {
                        // attempt to use default class loader
                        return Class.forName(className);
                    } catch (ClassNotFoundException exc) {
                        // default class loader was unable to locate a required class -
                        // attempt to use one of the provided class loaders
                        for (ClassLoader cl : customClassLoaders) {
                            try {
                                return cl.loadClass(className);
                            } catch (ClassNotFoundException e) {
                                /* do nothing here - there may be other class loaders to try */
                            }
                        }
                        // none of the class loaders was able to recognise the currently
                        // de-serialised class, so it's indeed an exception
                        throw new ClassNotFoundException(className
                                + " -- neither system, nor alternative class loaders were able to load this class");
                    }
                }
            };
            return ois.readObject();
        } catch (Exception e) {
            logger.error("Could not perform deep copy of " + objectToCopy.getClass() + " instance", e);
        } finally {
            oos.close();
            ois.close();
        }
    } catch (Exception e) {
        logger.error(
                "Could not close object streams during deep copy of " + objectToCopy.getClass() + " instance");
    }

    // Error occurred - couldn't produce the deep copy...
    return null;
}

From source file:com.espertech.esper.util.ObjectInputStreamWithTCCL.java

@Override
public Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {

    if (log.isDebugEnabled()) {
        log.debug("Resolving class " + desc.getName() + " id " + desc.getSerialVersionUID() + " classloader "
                + Thread.currentThread().getContextClassLoader().getClass());
    }//www .ja  va 2s. c  om

    ClassLoader currentTccl = null;
    try {
        currentTccl = Thread.currentThread().getContextClassLoader();
        if (currentTccl != null) {
            return currentTccl.loadClass(desc.getName());
        }
    } catch (Exception e) {
    }
    return super.resolveClass(desc);
}

From source file:grails.plugin.cache.web.filter.redis.GrailsDeserializer.java

public Object deserialize(InputStream inputStream) throws IOException {
    ObjectInputStream ois = new ObjectInputStream(inputStream) {
        @Override/*from w w  w . jav a 2  s  .com*/
        protected Class<?> resolveClass(ObjectStreamClass osc) throws IOException, ClassNotFoundException {
            try {
                return Thread.currentThread().getContextClassLoader().loadClass(osc.getName());
            } catch (Exception e) {
                return super.resolveClass(osc);
            }
        }
    };

    try {
        return ois.readObject();
    } catch (ClassNotFoundException e) {
        throw new NestedIOException("Failed to deserialize object type", e);
    }
}

From source file:com.bitranger.parknshop.common.recommend.util.io.CustomClassLoaderObjectInputStream.java

@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
    if (classLoader == null) {
        return super.resolveClass(desc);
    } else {/*from   w  w  w. j  a  va 2s  .co  m*/
        String name = desc.getName();
        logger.debug("resolving class {}", name);
        return ClassUtils.getClass(classLoader, name);
    }
}

From source file:com.web.server.node.NodeObjectInputStream.java

public Class resolveClass(ObjectStreamClass desc) throws ClassNotFoundException, IOException {
    try {/*from w  w w . ja  v  a2 s  .  c o m*/
        return super.resolveClass(desc);
    } catch (ClassNotFoundException e) {
        if (desc.getName().equals("int"))
            return int.class;
        else if (desc.getName().equals("float")) {
            return float.class;
        } else if (desc.getName().equals("double")) {
            return double.class;
        } else if (desc.getName().equals("long")) {
            return long.class;
        } else if (desc.getName().equals("boolean")) {
            return boolean.class;
        }
    }
    return null;
}

From source file:com.app.server.node.NodeObjectInputStream.java

public Class resolveClass(ObjectStreamClass desc) throws ClassNotFoundException, IOException {
    try {//from   ww  w. j a  va 2s .c  o  m
        return super.resolveClass(desc);
    } catch (Exception e) {
        log.error("Error in resolving class " + desc.getName(), e);
        if (desc.getName().equals("int"))
            return int.class;
        else if (desc.getName().equals("float")) {
            return float.class;
        } else if (desc.getName().equals("double")) {
            return double.class;
        } else if (desc.getName().equals("long")) {
            return long.class;
        } else if (desc.getName().equals("boolean")) {
            return boolean.class;
        }
    }
    return null;
}

From source file:majordodo.worker.TaskModeAwareExecutorFactory.java

@Override
public TaskExecutor createTaskExecutor(String taskType, Map<String, Object> parameters) {
    String mode = (String) parameters.getOrDefault("mode", Task.MODE_DEFAULT);
    if (mode.equals(Task.MODE_EXECUTE_FACTORY)) {
        return inner.createTaskExecutor(taskType, parameters);
    }/*  w  ww  . java2 s  . com*/
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try {
        String parameter = (String) parameters.getOrDefault("parameter", "");
        if (parameter.startsWith("base64:")) {
            parameter = parameter.substring("base64:".length());
            byte[] serializedObjectData = Base64.getDecoder().decode(parameter);
            ByteArrayInputStream ii = new ByteArrayInputStream(serializedObjectData);
            ObjectInputStream is = new ObjectInputStream(ii) {
                @Override
                public Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
                    try {
                        return tccl.loadClass(desc.getName());
                    } catch (Exception e) {
                    }
                    return super.resolveClass(desc);
                }

            };
            TaskExecutor res = (TaskExecutor) is.readUnshared();
            return res;
        } else if (parameter.startsWith("newinstance:")) {
            parameter = parameter.substring("newinstance:".length());
            URLClassLoader cc = (URLClassLoader) tccl;
            Class clazz = Class.forName(parameter, true, tccl);
            TaskExecutor res = (TaskExecutor) clazz.newInstance();
            return res;
        } else {
            throw new RuntimeException("bad parameter: " + parameter);
        }
    } catch (Exception err) {
        return new TaskExecutor() {
            @Override
            public String executeTask(Map<String, Object> parameters) throws Exception {
                throw err;
            }

        };
    }

}

From source file:com.haulmont.cuba.core.sys.serialization.StandardSerialization.java

@Override
public Object deserialize(InputStream is) {
    try {/*  www .  jav a  2  s. c o m*/
        ObjectInputStream ois;
        boolean isObjectStream = is instanceof ObjectInputStream;
        if (isObjectStream) {
            ois = (ObjectInputStream) is;
        } else {
            ois = new ObjectInputStream(is) {
                @Override
                protected Class<?> resolveClass(ObjectStreamClass desc)
                        throws IOException, ClassNotFoundException {
                    return ClassUtils.getClass(StandardSerialization.class.getClassLoader(), desc.getName());
                }
            };
        }
        return ois.readObject();
    } catch (IOException ex) {
        throw new IllegalArgumentException("Failed to deserialize object", ex);
    } catch (ClassNotFoundException ex) {
        throw new IllegalStateException("Failed to deserialize object type", ex);
    }
}