PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ejb3/src/main/java/org/jboss/as/ejb3/iiop/handle/HandleDelegateImpl.java

https://bitbucket.org/cprenzberg/wildfly
Java | 119 lines | 68 code | 13 blank | 38 comment | 2 complexity | a344f131b82d4896055028cbe676486e MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.ejb3.iiop.handle;
  23. import java.io.IOException;
  24. import java.io.ObjectInputStream;
  25. import java.io.ObjectOutputStream;
  26. import javax.ejb.EJBHome;
  27. import javax.ejb.EJBObject;
  28. import javax.ejb.spi.HandleDelegate;
  29. import javax.naming.InitialContext;
  30. import javax.naming.NamingException;
  31. import javax.rmi.CORBA.Stub;
  32. import javax.rmi.PortableRemoteObject;
  33. import org.jboss.util.NestedRuntimeException;
  34. import org.omg.CORBA.BAD_OPERATION;
  35. import org.omg.CORBA.ORB;
  36. import org.omg.CORBA.portable.ObjectImpl;
  37. import static org.jboss.as.ejb3.EjbMessages.MESSAGES;
  38. /**
  39. * <P>Implementation of the javax.ejb.spi.HandleDelegate interface</P>
  40. * <p/>
  41. * <P>The HandleDelegate interface is implemented by the EJB container.
  42. * It is used by portable implementations of javax.ejb.Handle and
  43. * javax.ejb.HomeHandle. It is not used by EJB components or by client components.
  44. * It provides methods to serialize and deserialize EJBObject and EJBHome
  45. * references to streams.</P>
  46. * <p/>
  47. * <P>The HandleDelegate object is obtained by JNDI lookup at the reserved name
  48. * "java:comp/HandleDelegate".</P>
  49. *
  50. * @author Dimitris.Andreadis@jboss.org
  51. * @author adrian@jboss.com
  52. */
  53. public class HandleDelegateImpl implements HandleDelegate {
  54. public HandleDelegateImpl(final ClassLoader classLoader) {
  55. proxy = SerializationHackProxy.proxy(classLoader);
  56. }
  57. private final SerializationHackProxy proxy;
  58. public void writeEJBObject(final EJBObject ejbObject, final ObjectOutputStream oostream)
  59. throws IOException {
  60. oostream.writeObject(ejbObject);
  61. }
  62. public EJBObject readEJBObject(final ObjectInputStream oistream)
  63. throws IOException, ClassNotFoundException {
  64. final Object ejbObject = proxy.read(oistream);
  65. reconnect(ejbObject);
  66. return (EJBObject) PortableRemoteObject.narrow(ejbObject, EJBObject.class);
  67. }
  68. public void writeEJBHome(final EJBHome ejbHome, final ObjectOutputStream oostream)
  69. throws IOException {
  70. oostream.writeObject(ejbHome);
  71. }
  72. public EJBHome readEJBHome(final ObjectInputStream oistream)
  73. throws IOException, ClassNotFoundException {
  74. final Object ejbHome = proxy.read(oistream);
  75. reconnect(ejbHome);
  76. return (EJBHome) PortableRemoteObject.narrow(ejbHome, EJBHome.class);
  77. }
  78. protected void reconnect(Object object) throws IOException {
  79. if (object instanceof ObjectImpl) {
  80. try {
  81. // Check we are still connected
  82. ObjectImpl objectImpl = (ObjectImpl) object;
  83. objectImpl._get_delegate();
  84. } catch (BAD_OPERATION e) {
  85. try {
  86. // Reconnect
  87. final Stub stub = (Stub) object;
  88. final ORB orb = (ORB) new InitialContext().lookup("java:comp/ORB");
  89. stub.connect(orb);
  90. } catch (NamingException ne) {
  91. throw MESSAGES.failedToLookupORB();
  92. }
  93. }
  94. } else {
  95. throw MESSAGES.notAnObjectImpl(object.getClass());
  96. }
  97. }
  98. public static HandleDelegate getDelegate() {
  99. try {
  100. final InitialContext ctx = new InitialContext();
  101. return (HandleDelegate) ctx.lookup("java:comp/HandleDelegate");
  102. } catch (NamingException e) {
  103. throw new NestedRuntimeException(e);
  104. }
  105. }
  106. }