예제 #1
0
  /**
   * Creates an instance of the given bean. The bean is first search as a class. If not found as
   * class, it is searched as a resource. There isn't a lazy initialization.
   *
   * @param beanName name of the bean (for now is also the file name, without .ser, into which the
   *     bean has been serialized) NOT NULL
   * @return a new instance of the serialized bean
   * @throws BeanInstantiationException if the bean cannot be istantiated
   * @throws BeanInitializationException if an initialization error occurred
   * @throws BeanNotFoundException if the bean does not exist
   */
  public static Object getNoInitBeanInstance(File beanName)
      throws BeanInstantiationException, BeanInitializationException, BeanNotFoundException {

    Object bean = null;

    InputStream is = null;
    try {
      is = new FileInputStream(beanName);
    } catch (FileNotFoundException ex) {
      throw new BeanNotFoundException(beanName.getAbsolutePath());
    }

    BeanExceptionListener exceptionListener = new BeanExceptionListener();

    XMLDecoder d = new XMLDecoder(is, null, exceptionListener);
    bean = d.readObject();
    d.close();

    if (exceptionListener.exceptionThrown()) {
      Throwable t = exceptionListener.getException();

      if (t.getCause() != null) {
        t = t.getCause();
      }
      throw new BeanInstantiationException("Error instantiating " + beanName, t);
    }

    return bean;
  }
예제 #2
0
  /**
   * Unmarshals the given serialized object into the corresponding instance using
   * <i>java.beans.XMLDecoder</i>.
   *
   * @param classLoader the class loader to use (null for the standard one)
   * @param xml XML serialized form
   * @return the unmarshalled instance
   * @throws BeanException in case of unmarshalling errors
   */
  public static Object unmarshal(ClassLoader classLoader, String xml) throws BeanException {
    XMLDecoder dec = null;
    ClassLoader oldClassLoader = null;

    if (classLoader != null) {
      oldClassLoader = Thread.currentThread().getContextClassLoader();
      Thread.currentThread().setContextClassLoader(classLoader);
    }

    BeanExceptionListener exceptionListener = new BeanExceptionListener();

    try {
      ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
      Object ret = null;

      dec = new XMLDecoder(is);
      dec.setExceptionListener(exceptionListener);
      ret = dec.readObject();
      dec.close();
      dec = null;

      if (exceptionListener.exceptionThrown()) {
        Throwable t = exceptionListener.getException();

        if (t.getCause() != null) {
          t = t.getCause();
        }
        throw new BeanInstantiationException("Error creating bean", t);
      }

      return ret;
    } finally {
      if (dec != null) {
        dec.close();
      }

      if (classLoader != null) {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
      }
    }
  }
예제 #3
0
  /**
   * Marshals the given object into XML using <i>java.beans.XMLEncoder</i>.
   *
   * @param loader the classloader to use
   * @param o the object to marshal
   * @return the XML form of the object
   * @throws BeanException in case of marshalling errors
   */
  public static String marshal(ClassLoader classLoader, Object o) throws BeanException {
    XMLEncoder enc = null;
    ClassLoader oldClassLoader = null;

    if (classLoader != null) {
      oldClassLoader = Thread.currentThread().getContextClassLoader();
      Thread.currentThread().setContextClassLoader(classLoader);
    }

    BeanExceptionListener exceptionListener = new BeanExceptionListener();

    try {
      ByteArrayOutputStream os = new ByteArrayOutputStream();

      enc = new XMLEncoder(os);

      enc.writeObject(o);

      enc.close();
      enc = null; // we need to do this here to get the string

      if (exceptionListener.exceptionThrown()) {
        Throwable t = exceptionListener.getException();
        throw new BeanException(t.toString(), t);
      }

      return os.toString();
    } finally {
      if (enc != null) {
        enc.close();
      }

      if (classLoader != null) {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
      }
    }
  }