1 /* |
|
2 * Copyright (c) 2009,2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.jmx.remote.protocol.iiop; |
|
27 |
|
28 import org.omg.CORBA.ORB; |
|
29 import org.omg.CORBA.portable.Delegate; |
|
30 import javax.rmi.PortableRemoteObject; |
|
31 import javax.rmi.CORBA.Stub; |
|
32 |
|
33 import java.util.Properties; |
|
34 import java.rmi.Remote; |
|
35 import java.rmi.RemoteException; |
|
36 import java.rmi.NoSuchObjectException; |
|
37 |
|
38 import com.sun.jmx.remote.internal.IIOPProxy; |
|
39 import java.io.SerializablePermission; |
|
40 import java.security.AccessControlContext; |
|
41 import java.security.AccessController; |
|
42 import java.security.Permissions; |
|
43 import java.security.PrivilegedActionException; |
|
44 import java.security.PrivilegedExceptionAction; |
|
45 import java.security.ProtectionDomain; |
|
46 |
|
47 /** |
|
48 * An implementation of IIOPProxy that simply delegates to the appropriate |
|
49 * RMI-IIOP and CORBA APIs. |
|
50 */ |
|
51 |
|
52 public class IIOPProxyImpl implements IIOPProxy { |
|
53 // special ACC used to initialize the IIOP stub |
|
54 // the only allowed privilege is SerializablePermission("enableSubclassImplementation") |
|
55 private static final AccessControlContext STUB_ACC; |
|
56 |
|
57 static { |
|
58 Permissions p = new Permissions(); |
|
59 p.add(new SerializablePermission("enableSubclassImplementation")); |
|
60 STUB_ACC = new AccessControlContext( |
|
61 new ProtectionDomain[]{ |
|
62 new ProtectionDomain(null, p) |
|
63 } |
|
64 ); |
|
65 } |
|
66 |
|
67 public IIOPProxyImpl() { } |
|
68 |
|
69 @Override |
|
70 public boolean isStub(Object obj) { |
|
71 return (obj instanceof Stub); |
|
72 } |
|
73 |
|
74 @Override |
|
75 public Object getDelegate(Object stub) { |
|
76 return ((Stub)stub)._get_delegate(); |
|
77 } |
|
78 |
|
79 @Override |
|
80 public void setDelegate(Object stub, Object delegate) { |
|
81 ((Stub)stub)._set_delegate((Delegate)delegate); |
|
82 } |
|
83 |
|
84 @Override |
|
85 public Object getOrb(Object stub) { |
|
86 try { |
|
87 return ((Stub)stub)._orb(); |
|
88 } catch (org.omg.CORBA.BAD_OPERATION x) { |
|
89 throw new UnsupportedOperationException(x); |
|
90 } |
|
91 } |
|
92 |
|
93 @Override |
|
94 public void connect(Object stub, Object orb) |
|
95 throws RemoteException |
|
96 { |
|
97 ((Stub)stub).connect((ORB)orb); |
|
98 } |
|
99 |
|
100 @Override |
|
101 public boolean isOrb(Object obj) { |
|
102 return (obj instanceof ORB); |
|
103 } |
|
104 |
|
105 @Override |
|
106 public Object createOrb(String[] args, Properties props) { |
|
107 return ORB.init(args, props); |
|
108 } |
|
109 |
|
110 @Override |
|
111 public Object stringToObject(Object orb, String str) { |
|
112 return ((ORB)orb).string_to_object(str); |
|
113 } |
|
114 |
|
115 @Override |
|
116 public String objectToString(Object orb, Object obj) { |
|
117 return ((ORB)orb).object_to_string((org.omg.CORBA.Object)obj); |
|
118 } |
|
119 |
|
120 @Override |
|
121 @SuppressWarnings("unchecked") |
|
122 public <T> T narrow(Object narrowFrom, Class<T> narrowTo) { |
|
123 return (T)PortableRemoteObject.narrow(narrowFrom, narrowTo); |
|
124 } |
|
125 |
|
126 @Override |
|
127 public void exportObject(Remote obj) throws RemoteException { |
|
128 PortableRemoteObject.exportObject(obj); |
|
129 } |
|
130 |
|
131 @Override |
|
132 public void unexportObject(Remote obj) throws NoSuchObjectException { |
|
133 PortableRemoteObject.unexportObject(obj); |
|
134 } |
|
135 |
|
136 @Override |
|
137 public Remote toStub(final Remote obj) throws NoSuchObjectException { |
|
138 if (System.getSecurityManager() == null) { |
|
139 return PortableRemoteObject.toStub(obj); |
|
140 } else { |
|
141 try { |
|
142 return AccessController.doPrivileged(new PrivilegedExceptionAction<Remote>() { |
|
143 |
|
144 @Override |
|
145 public Remote run() throws Exception { |
|
146 return PortableRemoteObject.toStub(obj); |
|
147 } |
|
148 }, STUB_ACC); |
|
149 } catch (PrivilegedActionException e) { |
|
150 if (e.getException() instanceof NoSuchObjectException) { |
|
151 throw (NoSuchObjectException)e.getException(); |
|
152 } |
|
153 throw new RuntimeException("Unexpected exception type", e.getException()); |
|
154 } |
|
155 } |
|
156 } |
|
157 } |
|