public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
   InvocationHandler invocationHandler = Proxy.getInvocationHandler(source);
   addInterfacesToXml(source, writer);
   writer.startNode("handler");
   writer.addAttribute("class", mapper.serializedClass(invocationHandler.getClass()));
   context.convertAnother(invocationHandler);
   writer.endNode();
 }
 public static Object getDepackageObject(Object object) {
   if (object == null) {
     return null;
   }
   if (!(object instanceof Proxy)) {
     return object;
   }
   InvocationHandler h = Proxy.getInvocationHandler((Proxy) object);
   ProxyDepackage depackage;
   synchronized (map) {
     depackage = map.get(h.getClass().getName());
   }
   if (depackage == null) {
     throw new RuntimeException("Cannot proxy depackager for `" + h.getClass().getName() + "`.");
   }
   return depackage.depackage((Proxy) object);
 }
 /**
  * Marks a borrowed connection as no longer usable.
  *
  * @param connection The connection (proxy) to be marked.
  */
 public static void renderUnuseable(Connection connection) {
   if (connection != null && Proxy.isProxyClass(connection.getClass())) {
     InvocationHandler handler = Proxy.getInvocationHandler(connection);
     if (BorrowedConnectionProxy.class.isAssignableFrom(handler.getClass())) {
       ((BorrowedConnectionProxy) handler).useable = false;
     }
   }
 }
 private static String getProxyUri(Object proxy)
     throws SecurityException, NoSuchFieldException, IllegalArgumentException,
         IllegalAccessException {
   InvocationHandler handler = Proxy.getInvocationHandler(proxy);
   Field f = handler.getClass().getDeclaredField("url");
   f.setAccessible(true);
   String url = (String) f.get(handler);
   return url;
 }
 /** Given a Hibernate Session, retrieve the "real" session if it's a proxy. */
 public static Session getRealSession(Session session) {
   Session realSession = session;
   if (Proxy.isProxyClass(session.getClass())) {
     InvocationHandler handler = Proxy.getInvocationHandler(session);
     try {
       Field fld = handler.getClass().getDeclaredField("realSession");
       fld.setAccessible(true);
       realSession = (Session) fld.get(handler);
     } catch (Exception ex) {
       throw new RuntimeException(
           "Error while getting the real Hibernate session out of proxy session", ex);
     }
   }
   return realSession;
 }
 @Override
 public Object read(final Kryo kryo, final Input input, final Class<Object> type) {
   final InvocationHandler invocationHandler = (InvocationHandler) kryo.readClassAndObject(input);
   final Class<?>[] interfaces = kryo.readObject(input, Class[].class);
   final ClassLoader classLoader = kryo.getClass().getClassLoader(); // TODO: can we do this?
   try {
     return Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
   } catch (final RuntimeException e) {
     System.err.println(
         getClass().getName()
             + ".read:\n"
             + "Could not create proxy using classLoader "
             + classLoader
             + ","
             + " have invocationhandler.classloader: "
             + invocationHandler.getClass().getClassLoader()
             + " have contextclassloader: "
             + Thread.currentThread().getContextClassLoader());
     throw e;
   }
 }
示例#7
0
 private static void checkStub(Remote stub, Class<? extends Remote> stubClass) {
   // Check remote stub is from the expected class.
   //
   if (stub.getClass() != stubClass) {
     if (!Proxy.isProxyClass(stub.getClass())) {
       throw new SecurityException("Expecting a " + stubClass.getName() + " stub!");
     } else {
       InvocationHandler handler = Proxy.getInvocationHandler(stub);
       if (handler.getClass() != RemoteObjectInvocationHandler.class) {
         throw new SecurityException(
             "Expecting a dynamic proxy instance with a "
                 + RemoteObjectInvocationHandler.class.getName()
                 + " invocation handler!");
       } else {
         stub = (Remote) handler;
       }
     }
   }
   // Check RemoteRef in stub is from the expected class
   // "sun.rmi.server.UnicastRef2".
   //
   RemoteRef ref = ((RemoteObject) stub).getRef();
   if (ref.getClass() != UnicastRef2.class) {
     throw new SecurityException(
         "Expecting a " + UnicastRef2.class.getName() + " remote reference in stub!");
   }
   // Check RMIClientSocketFactory in stub is from the expected class
   // "javax.rmi.ssl.SslRMIClientSocketFactory".
   //
   LiveRef liveRef = ((UnicastRef2) ref).getLiveRef();
   RMIClientSocketFactory csf = liveRef.getClientSocketFactory();
   if (csf == null || csf.getClass() != SslRMIClientSocketFactory.class) {
     throw new SecurityException(
         "Expecting a "
             + SslRMIClientSocketFactory.class.getName()
             + " RMI client socket factory in stub!");
   }
 }
 /**
  * Convenience method to generate a single-interface proxy using the handler's classloader
  *
  * @param <T> The type of object to proxy
  * @param type The type of object to proxy
  * @param handler The handler that intercepts/overrides method calls.
  * @return proxied object
  */
 public <T> T newProxyInstance(Class<T> type, InvocationHandler handler) {
   return type.cast(
       Proxy.newProxyInstance(
           handler.getClass().getClassLoader(), new Class<?>[] {type}, handler));
 }