public static Object readData(String filePath) {
   Object result = null;
   try {
     filePath = StringUtil.checkFullPathLength(filePath);
     FileLockManager.startFileRead(filePath);
     if (isValid(filePath)) {
       FileInputStream os = new FileInputStream(filePath);
       XMLDecoder decoder = new XMLDecoder(os);
       decoder.setExceptionListener(
           new ExceptionListener() {
             public void exceptionThrown(Exception exception) {
               log.error("readData(): error", exception);
             }
           });
       result = decoder.readObject();
       decoder.close();
     }
   } catch (java.io.FileNotFoundException fnfe) {
     log.trace("readData(): file not found exception, filePath=" + filePath);
   } catch (Error e) {
     log.error("readData(): error, filePath=" + filePath, e);
   } catch (Exception e) {
     log.error("readData(): exception, filePath=" + filePath, e);
   } finally {
     FileLockManager.endFileRead(filePath);
   }
   return result;
 }
Пример #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);
      }
    }
  }