Пример #1
0
  public void serialize(Object sourceObj, OutputStream os, Hashtable extendedContext)
      throws Exception {

    try {
      final ClassLoader urlClassLoader = GlobalContext.getComponentClassLoader();
      /* this will replace the default system class loader with the new custom classloader, so that XMLEncoder will use the new custom classloader to lookup a class */
      Thread.currentThread().setContextClassLoader(urlClassLoader);
    } catch (Exception e) {
      System.out.println("can't replace classloader");
    }

    try {
      TypeDesc desc =
          (TypeDesc)
              sourceObj
                  .getClass()
                  .getMethod("getTypeDesc", new Class[] {})
                  .invoke(sourceObj, new Object[] {});
      QName xmlType = desc.getXmlType();
      //
      // xmlType = (QName)extendedContext.get("qName");
      //
      xmlType =
          new QName(
              "http://" + sourceObj.getClass().getName(),
              org.uengine.util.UEngineUtil.getClassNameOnly(sourceObj.getClass()));
      //			xmlType = new QName(Constants.URI_SOAP11_ENV, Constants.ELEM_ENVELOPE );

      org.apache.axis.encoding.Serializer ser =
          (org.apache.axis.encoding.Serializer)
              sourceObj
                  .getClass()
                  .getMethod("getSerializer", new Class[] {String.class, Class.class, QName.class})
                  .invoke(sourceObj, new Object[] {"", sourceObj.getClass(), xmlType});

      // Writer w = new OutputStreamWriter(os);
      StringWriter w = new StringWriter();
      SerializationContext context =
          new SerializationContextImpl(w) {
            public MessageContext getMessageContext() {
              return new MessageContext(null) {
                public String getEncodingStyle() {
                  return "";
                }
              };
            }
          };
      ser.serialize(xmlType, (org.xml.sax.Attributes) null, sourceObj, context);

      // System.out.println(w);
      // w.write("SdfasdfsadfA");

      BufferedOutputStream bao = new BufferedOutputStream(os);

      bao.write(w.toString().getBytes());

      bao.flush();
      bao.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }