mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-22 12:04:39 +02:00
8187443: Forest Consolidation: Move files to unified layout
Reviewed-by: darcy, ihse
This commit is contained in:
parent
270fe13182
commit
3789983e89
56923 changed files with 3 additions and 15727 deletions
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2002, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.jndi.rmi.registry;
|
||||
|
||||
|
||||
import java.rmi.*;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
import javax.naming.*;
|
||||
|
||||
|
||||
/**
|
||||
* The ReferenceWrapper class is a Remote wrapper for Reference
|
||||
* objects. It wraps around a Reference on the server, and makes the
|
||||
* Reference accessible to clients.
|
||||
*
|
||||
* @author Scott Seligman
|
||||
*/
|
||||
|
||||
|
||||
public class ReferenceWrapper
|
||||
extends UnicastRemoteObject
|
||||
implements RemoteReference
|
||||
{
|
||||
protected Reference wrappee; // reference being wrapped
|
||||
|
||||
public ReferenceWrapper(Reference wrappee)
|
||||
throws NamingException, RemoteException
|
||||
{
|
||||
this.wrappee = wrappee;
|
||||
}
|
||||
|
||||
public Reference getReference() throws RemoteException {
|
||||
return wrappee;
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 6078186197417641456L;
|
||||
}
|
|
@ -0,0 +1,642 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.jndi.rmi.registry;
|
||||
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Properties;
|
||||
import java.rmi.*;
|
||||
import java.rmi.server.*;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
|
||||
import javax.naming.*;
|
||||
import javax.naming.spi.NamingManager;
|
||||
|
||||
|
||||
/**
|
||||
* A RegistryContext is a context representing a remote RMI registry.
|
||||
*
|
||||
* @author Scott Seligman
|
||||
*/
|
||||
|
||||
|
||||
public class RegistryContext implements Context, Referenceable {
|
||||
|
||||
private Hashtable<String, Object> environment;
|
||||
private Registry registry;
|
||||
private String host;
|
||||
private int port;
|
||||
private static final NameParser nameParser = new AtomicNameParser();
|
||||
private static final String SOCKET_FACTORY = "com.sun.jndi.rmi.factory.socket";
|
||||
/**
|
||||
* Determines whether classes may be loaded from an arbitrary URL code base.
|
||||
*/
|
||||
static final boolean trustURLCodebase;
|
||||
static {
|
||||
// System property to control whether classes may be loaded from an
|
||||
// arbitrary URL codebase
|
||||
PrivilegedAction<String> act = () -> System.getProperty(
|
||||
"com.sun.jndi.rmi.object.trustURLCodebase", "false");
|
||||
String trust = AccessController.doPrivileged(act);
|
||||
trustURLCodebase = "true".equalsIgnoreCase(trust);
|
||||
}
|
||||
|
||||
Reference reference = null; // ref used to create this context, if any
|
||||
|
||||
// Environment property that, if set, indicates that a security
|
||||
// manager should be installed (if none is already in place).
|
||||
public static final String SECURITY_MGR =
|
||||
"java.naming.rmi.security.manager";
|
||||
|
||||
/**
|
||||
* Returns a context for the registry at a given host and port.
|
||||
* If "host" is null, uses default host.
|
||||
* If "port" is non-positive, uses default port.
|
||||
* Cloning of "env" is handled by caller; see comments within
|
||||
* RegistryContextFactory.getObjectInstance(), for example.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public RegistryContext(String host, int port, Hashtable<?, ?> env)
|
||||
throws NamingException
|
||||
{
|
||||
environment = (env == null)
|
||||
? new Hashtable<String, Object>(5)
|
||||
: (Hashtable<String, Object>) env;
|
||||
if (environment.get(SECURITY_MGR) != null) {
|
||||
installSecurityMgr();
|
||||
}
|
||||
|
||||
// chop off '[' and ']' in an IPv6 literal address
|
||||
if ((host != null) && (host.charAt(0) == '[')) {
|
||||
host = host.substring(1, host.length() - 1);
|
||||
}
|
||||
|
||||
RMIClientSocketFactory socketFactory =
|
||||
(RMIClientSocketFactory) environment.get(SOCKET_FACTORY);
|
||||
registry = getRegistry(host, port, socketFactory);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a clone of a registry context. The context's private state
|
||||
* is independent of the original's (so closing one context, for example,
|
||||
* won't close the other).
|
||||
*/
|
||||
// %%% Alternatively, this could be done with a clone() method.
|
||||
@SuppressWarnings("unchecked") // clone()
|
||||
RegistryContext(RegistryContext ctx) {
|
||||
environment = (Hashtable<String, Object>)ctx.environment.clone();
|
||||
registry = ctx.registry;
|
||||
host = ctx.host;
|
||||
port = ctx.port;
|
||||
reference = ctx.reference;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
protected void finalize() {
|
||||
close();
|
||||
}
|
||||
|
||||
public Object lookup(Name name) throws NamingException {
|
||||
if (name.isEmpty()) {
|
||||
return (new RegistryContext(this));
|
||||
}
|
||||
Remote obj;
|
||||
try {
|
||||
obj = registry.lookup(name.get(0));
|
||||
} catch (NotBoundException e) {
|
||||
throw (new NameNotFoundException(name.get(0)));
|
||||
} catch (RemoteException e) {
|
||||
throw (NamingException)wrapRemoteException(e).fillInStackTrace();
|
||||
}
|
||||
return (decodeObject(obj, name.getPrefix(1)));
|
||||
}
|
||||
|
||||
public Object lookup(String name) throws NamingException {
|
||||
return lookup(new CompositeName(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* If the object to be bound is both Remote and Referenceable, binds the
|
||||
* object itself, not its Reference.
|
||||
*/
|
||||
public void bind(Name name, Object obj) throws NamingException {
|
||||
if (name.isEmpty()) {
|
||||
throw (new InvalidNameException(
|
||||
"RegistryContext: Cannot bind empty name"));
|
||||
}
|
||||
try {
|
||||
registry.bind(name.get(0), encodeObject(obj, name.getPrefix(1)));
|
||||
} catch (AlreadyBoundException e) {
|
||||
NamingException ne = new NameAlreadyBoundException(name.get(0));
|
||||
ne.setRootCause(e);
|
||||
throw ne;
|
||||
} catch (RemoteException e) {
|
||||
throw (NamingException)wrapRemoteException(e).fillInStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void bind(String name, Object obj) throws NamingException {
|
||||
bind(new CompositeName(name), obj);
|
||||
}
|
||||
|
||||
public void rebind(Name name, Object obj) throws NamingException {
|
||||
if (name.isEmpty()) {
|
||||
throw (new InvalidNameException(
|
||||
"RegistryContext: Cannot rebind empty name"));
|
||||
}
|
||||
try {
|
||||
registry.rebind(name.get(0), encodeObject(obj, name.getPrefix(1)));
|
||||
} catch (RemoteException e) {
|
||||
throw (NamingException)wrapRemoteException(e).fillInStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void rebind(String name, Object obj) throws NamingException {
|
||||
rebind(new CompositeName(name), obj);
|
||||
}
|
||||
|
||||
public void unbind(Name name) throws NamingException {
|
||||
if (name.isEmpty()) {
|
||||
throw (new InvalidNameException(
|
||||
"RegistryContext: Cannot unbind empty name"));
|
||||
}
|
||||
try {
|
||||
registry.unbind(name.get(0));
|
||||
} catch (NotBoundException e) {
|
||||
// method is idempotent
|
||||
} catch (RemoteException e) {
|
||||
throw (NamingException)wrapRemoteException(e).fillInStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void unbind(String name) throws NamingException {
|
||||
unbind(new CompositeName(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename is implemented by this sequence of operations:
|
||||
* lookup, bind, unbind. The sequence is not performed atomically.
|
||||
*/
|
||||
public void rename(Name oldName, Name newName) throws NamingException {
|
||||
bind(newName, lookup(oldName));
|
||||
unbind(oldName);
|
||||
}
|
||||
|
||||
public void rename(String name, String newName) throws NamingException {
|
||||
rename(new CompositeName(name), new CompositeName(newName));
|
||||
}
|
||||
|
||||
public NamingEnumeration<NameClassPair> list(Name name) throws
|
||||
NamingException {
|
||||
if (!name.isEmpty()) {
|
||||
throw (new InvalidNameException(
|
||||
"RegistryContext: can only list \"\""));
|
||||
}
|
||||
try {
|
||||
String[] names = registry.list();
|
||||
return (new NameClassPairEnumeration(names));
|
||||
} catch (RemoteException e) {
|
||||
throw (NamingException)wrapRemoteException(e).fillInStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<NameClassPair> list(String name) throws
|
||||
NamingException {
|
||||
return list(new CompositeName(name));
|
||||
}
|
||||
|
||||
public NamingEnumeration<Binding> listBindings(Name name)
|
||||
throws NamingException
|
||||
{
|
||||
if (!name.isEmpty()) {
|
||||
throw (new InvalidNameException(
|
||||
"RegistryContext: can only list \"\""));
|
||||
}
|
||||
try {
|
||||
String[] names = registry.list();
|
||||
return (new BindingEnumeration(this, names));
|
||||
} catch (RemoteException e) {
|
||||
throw (NamingException)wrapRemoteException(e).fillInStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public NamingEnumeration<Binding> listBindings(String name) throws
|
||||
NamingException {
|
||||
return listBindings(new CompositeName(name));
|
||||
}
|
||||
|
||||
public void destroySubcontext(Name name) throws NamingException {
|
||||
throw (new OperationNotSupportedException());
|
||||
}
|
||||
|
||||
public void destroySubcontext(String name) throws NamingException {
|
||||
throw (new OperationNotSupportedException());
|
||||
}
|
||||
|
||||
public Context createSubcontext(Name name) throws NamingException {
|
||||
throw (new OperationNotSupportedException());
|
||||
}
|
||||
|
||||
public Context createSubcontext(String name) throws NamingException {
|
||||
throw (new OperationNotSupportedException());
|
||||
}
|
||||
|
||||
public Object lookupLink(Name name) throws NamingException {
|
||||
return lookup(name);
|
||||
}
|
||||
|
||||
public Object lookupLink(String name) throws NamingException {
|
||||
return lookup(name);
|
||||
}
|
||||
|
||||
public NameParser getNameParser(Name name) throws NamingException {
|
||||
return nameParser;
|
||||
}
|
||||
|
||||
public NameParser getNameParser(String name) throws NamingException {
|
||||
return nameParser;
|
||||
}
|
||||
|
||||
public Name composeName(Name name, Name prefix) throws NamingException {
|
||||
Name result = (Name)prefix.clone();
|
||||
return result.addAll(name);
|
||||
}
|
||||
|
||||
public String composeName(String name, String prefix)
|
||||
throws NamingException
|
||||
{
|
||||
return composeName(new CompositeName(name),
|
||||
new CompositeName(prefix)).toString();
|
||||
}
|
||||
|
||||
public Object removeFromEnvironment(String propName)
|
||||
throws NamingException
|
||||
{
|
||||
return environment.remove(propName);
|
||||
}
|
||||
|
||||
public Object addToEnvironment(String propName, Object propVal)
|
||||
throws NamingException
|
||||
{
|
||||
if (propName.equals(SECURITY_MGR)) {
|
||||
installSecurityMgr();
|
||||
}
|
||||
return environment.put(propName, propVal);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") // clone()
|
||||
public Hashtable<String, Object> getEnvironment() throws NamingException {
|
||||
return (Hashtable<String, Object>)environment.clone();
|
||||
}
|
||||
|
||||
public void close() {
|
||||
environment = null;
|
||||
registry = null;
|
||||
// &&& If we were caching registry connections, we would probably
|
||||
// uncache this one now.
|
||||
}
|
||||
|
||||
public String getNameInNamespace() {
|
||||
return ""; // Registry has an empty name
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an RMI registry reference for this context.
|
||||
*<p>
|
||||
* If this context was created from a reference, that reference is
|
||||
* returned. Otherwise, an exception is thrown if the registry's
|
||||
* host is "localhost" or the default (null). Although this could
|
||||
* possibly make for a valid reference, it's far more likely to be
|
||||
* an easily made error.
|
||||
*
|
||||
* @see RegistryContextFactory
|
||||
*/
|
||||
public Reference getReference() throws NamingException {
|
||||
if (reference != null) {
|
||||
return (Reference)reference.clone(); // %%% clone the addrs too?
|
||||
}
|
||||
if (host == null || host.equals("localhost")) {
|
||||
throw (new ConfigurationException(
|
||||
"Cannot create a reference for an RMI registry whose " +
|
||||
"host was unspecified or specified as \"localhost\""));
|
||||
}
|
||||
String url = "rmi://";
|
||||
|
||||
// Enclose IPv6 literal address in '[' and ']'
|
||||
url = (host.indexOf(':') > -1) ? url + "[" + host + "]" :
|
||||
url + host;
|
||||
if (port > 0) {
|
||||
url += ":" + Integer.toString(port);
|
||||
}
|
||||
RefAddr addr = new StringRefAddr(RegistryContextFactory.ADDRESS_TYPE,
|
||||
url);
|
||||
return (new Reference(RegistryContext.class.getName(),
|
||||
addr,
|
||||
RegistryContextFactory.class.getName(),
|
||||
null));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Wrap a RemoteException inside a NamingException.
|
||||
*/
|
||||
public static NamingException wrapRemoteException(RemoteException re) {
|
||||
|
||||
NamingException ne;
|
||||
|
||||
if (re instanceof ConnectException) {
|
||||
ne = new ServiceUnavailableException();
|
||||
|
||||
} else if (re instanceof AccessException) {
|
||||
ne = new NoPermissionException();
|
||||
|
||||
} else if (re instanceof StubNotFoundException ||
|
||||
re instanceof UnknownHostException) {
|
||||
ne = new ConfigurationException();
|
||||
|
||||
} else if (re instanceof ExportException ||
|
||||
re instanceof ConnectIOException ||
|
||||
re instanceof MarshalException ||
|
||||
re instanceof UnmarshalException ||
|
||||
re instanceof NoSuchObjectException) {
|
||||
ne = new CommunicationException();
|
||||
|
||||
} else if (re instanceof ServerException &&
|
||||
re.detail instanceof RemoteException) {
|
||||
ne = wrapRemoteException((RemoteException)re.detail);
|
||||
|
||||
} else {
|
||||
ne = new NamingException();
|
||||
}
|
||||
ne.setRootCause(re);
|
||||
return ne;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the registry at a given host, port and socket factory.
|
||||
* If "host" is null, uses default host.
|
||||
* If "port" is non-positive, uses default port.
|
||||
* If "socketFactory" is null, uses the default socket.
|
||||
*/
|
||||
private static Registry getRegistry(String host, int port,
|
||||
RMIClientSocketFactory socketFactory)
|
||||
throws NamingException
|
||||
{
|
||||
// %%% We could cache registry connections here. The transport layer
|
||||
// may already reuse connections.
|
||||
try {
|
||||
if (socketFactory == null) {
|
||||
return LocateRegistry.getRegistry(host, port);
|
||||
} else {
|
||||
return LocateRegistry.getRegistry(host, port, socketFactory);
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
throw (NamingException)wrapRemoteException(e).fillInStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to install a security manager if none is currently in
|
||||
* place.
|
||||
*/
|
||||
private static void installSecurityMgr() {
|
||||
|
||||
try {
|
||||
System.setSecurityManager(new SecurityManager());
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes an object prior to binding it in the registry. First,
|
||||
* NamingManager.getStateToBind() is invoked. If the resulting
|
||||
* object is Remote, it is returned. If it is a Reference or
|
||||
* Referenceable, the reference is wrapped in a Remote object.
|
||||
* Otherwise, an exception is thrown.
|
||||
*
|
||||
* @param name The object's name relative to this context.
|
||||
*/
|
||||
private Remote encodeObject(Object obj, Name name)
|
||||
throws NamingException, RemoteException
|
||||
{
|
||||
obj = NamingManager.getStateToBind(obj, name, this, environment);
|
||||
|
||||
if (obj instanceof Remote) {
|
||||
return (Remote)obj;
|
||||
}
|
||||
if (obj instanceof Reference) {
|
||||
return (new ReferenceWrapper((Reference)obj));
|
||||
}
|
||||
if (obj instanceof Referenceable) {
|
||||
return (new ReferenceWrapper(((Referenceable)obj).getReference()));
|
||||
}
|
||||
throw (new IllegalArgumentException(
|
||||
"RegistryContext: " +
|
||||
"object to bind must be Remote, Reference, or Referenceable"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes an object that has been retrieved from the registry.
|
||||
* First, if the object is a RemoteReference, the Reference is
|
||||
* unwrapped. Then, NamingManager.getObjectInstance() is invoked.
|
||||
*
|
||||
* @param name The object's name relative to this context.
|
||||
*/
|
||||
private Object decodeObject(Remote r, Name name) throws NamingException {
|
||||
try {
|
||||
Object obj = (r instanceof RemoteReference)
|
||||
? ((RemoteReference)r).getReference()
|
||||
: (Object)r;
|
||||
|
||||
/*
|
||||
* Classes may only be loaded from an arbitrary URL codebase when
|
||||
* the system property com.sun.jndi.rmi.object.trustURLCodebase
|
||||
* has been set to "true".
|
||||
*/
|
||||
|
||||
// Use reference if possible
|
||||
Reference ref = null;
|
||||
if (obj instanceof Reference) {
|
||||
ref = (Reference) obj;
|
||||
} else if (obj instanceof Referenceable) {
|
||||
ref = ((Referenceable)(obj)).getReference();
|
||||
}
|
||||
|
||||
if (ref != null && ref.getFactoryClassLocation() != null &&
|
||||
!trustURLCodebase) {
|
||||
throw new ConfigurationException(
|
||||
"The object factory is untrusted. Set the system property" +
|
||||
" 'com.sun.jndi.rmi.object.trustURLCodebase' to 'true'.");
|
||||
}
|
||||
return NamingManager.getObjectInstance(obj, name, this,
|
||||
environment);
|
||||
} catch (NamingException e) {
|
||||
throw e;
|
||||
} catch (RemoteException e) {
|
||||
throw (NamingException)
|
||||
wrapRemoteException(e).fillInStackTrace();
|
||||
} catch (Exception e) {
|
||||
NamingException ne = new NamingException();
|
||||
ne.setRootCause(e);
|
||||
throw ne;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A name parser for case-sensitive atomic names.
|
||||
*/
|
||||
class AtomicNameParser implements NameParser {
|
||||
private static final Properties syntax = new Properties();
|
||||
|
||||
public Name parse(String name) throws NamingException {
|
||||
return (new CompoundName(name, syntax));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* An enumeration of name / class-name pairs.
|
||||
*/
|
||||
class NameClassPairEnumeration implements NamingEnumeration<NameClassPair> {
|
||||
private final String[] names;
|
||||
private int nextName; // index into "names"
|
||||
|
||||
NameClassPairEnumeration(String[] names) {
|
||||
this.names = names;
|
||||
nextName = 0;
|
||||
}
|
||||
|
||||
public boolean hasMore() {
|
||||
return (nextName < names.length);
|
||||
}
|
||||
|
||||
public NameClassPair next() throws NamingException {
|
||||
if (!hasMore()) {
|
||||
throw (new java.util.NoSuchElementException());
|
||||
}
|
||||
// Convert name to a one-element composite name, so embedded
|
||||
// meta-characters are properly escaped.
|
||||
String name = names[nextName++];
|
||||
Name cname = (new CompositeName()).add(name);
|
||||
NameClassPair ncp = new NameClassPair(cname.toString(),
|
||||
"java.lang.Object");
|
||||
ncp.setNameInNamespace(name);
|
||||
return ncp;
|
||||
}
|
||||
|
||||
public boolean hasMoreElements() {
|
||||
return hasMore();
|
||||
}
|
||||
|
||||
public NameClassPair nextElement() {
|
||||
try {
|
||||
return next();
|
||||
} catch (NamingException e) { // should never happen
|
||||
throw (new java.util.NoSuchElementException(
|
||||
"javax.naming.NamingException was thrown"));
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
nextName = names.length;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* An enumeration of Bindings.
|
||||
*
|
||||
* The actual registry lookups are performed when next() is called. It would
|
||||
* be nicer to defer this until the object (or its class name) is actually
|
||||
* requested. The problem with that approach is that Binding.getObject()
|
||||
* cannot throw NamingException.
|
||||
*/
|
||||
class BindingEnumeration implements NamingEnumeration<Binding> {
|
||||
private RegistryContext ctx;
|
||||
private final String[] names;
|
||||
private int nextName; // index into "names"
|
||||
|
||||
BindingEnumeration(RegistryContext ctx, String[] names) {
|
||||
// Clone ctx in case someone closes it before we're through.
|
||||
this.ctx = new RegistryContext(ctx);
|
||||
this.names = names;
|
||||
nextName = 0;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
protected void finalize() {
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
public boolean hasMore() {
|
||||
if (nextName >= names.length) {
|
||||
ctx.close();
|
||||
}
|
||||
return (nextName < names.length);
|
||||
}
|
||||
|
||||
public Binding next() throws NamingException {
|
||||
if (!hasMore()) {
|
||||
throw (new java.util.NoSuchElementException());
|
||||
}
|
||||
// Convert name to a one-element composite name, so embedded
|
||||
// meta-characters are properly escaped.
|
||||
String name = names[nextName++];
|
||||
Name cname = (new CompositeName()).add(name);
|
||||
|
||||
Object obj = ctx.lookup(cname);
|
||||
String cnameStr = cname.toString();
|
||||
Binding binding = new Binding(cnameStr, obj);
|
||||
binding.setNameInNamespace(cnameStr);
|
||||
return binding;
|
||||
}
|
||||
|
||||
public boolean hasMoreElements() {
|
||||
return hasMore();
|
||||
}
|
||||
|
||||
public Binding nextElement() {
|
||||
try {
|
||||
return next();
|
||||
} catch (NamingException e) {
|
||||
throw (new java.util.NoSuchElementException(
|
||||
"javax.naming.NamingException was thrown"));
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void close () {
|
||||
finalize();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,178 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.jndi.rmi.registry;
|
||||
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.naming.*;
|
||||
import javax.naming.spi.*;
|
||||
|
||||
import com.sun.jndi.url.rmi.rmiURLContextFactory;
|
||||
|
||||
/**
|
||||
* A RegistryContextFactory takes an RMI registry reference, and
|
||||
* creates the corresponding RMI object or registry context. In
|
||||
* addition, it serves as the initial context factory when using an
|
||||
* RMI registry as an initial context.
|
||||
*<p>
|
||||
* When an initial context is being created, the environment
|
||||
* property "java.naming.provider.url" should contain the RMI URL of
|
||||
* the appropriate registry. Otherwise, the default URL "rmi:" is used.
|
||||
*<p>
|
||||
* An RMI registry reference contains one or more StringRefAddrs of
|
||||
* type "URL", each containing a single RMI URL. Other addresses
|
||||
* are ignored. Multiple URLs represent alternative addresses for the
|
||||
* same logical resource. The order of the addresses is not significant.
|
||||
*
|
||||
* @author Scott Seligman
|
||||
*/
|
||||
|
||||
|
||||
public class RegistryContextFactory
|
||||
implements ObjectFactory, InitialContextFactory
|
||||
{
|
||||
/**
|
||||
* The type of each address in an RMI registry reference.
|
||||
*/
|
||||
public final static String ADDRESS_TYPE = "URL";
|
||||
|
||||
public Context getInitialContext(Hashtable<?,?> env) throws NamingException {
|
||||
|
||||
if (env != null) {
|
||||
env = (Hashtable) env.clone();
|
||||
}
|
||||
return URLToContext(getInitCtxURL(env), env);
|
||||
}
|
||||
|
||||
public Object getObjectInstance(Object ref, Name name, Context nameCtx,
|
||||
Hashtable<?,?> env)
|
||||
throws NamingException
|
||||
{
|
||||
if (!isRegistryRef(ref)) {
|
||||
return null;
|
||||
}
|
||||
/*
|
||||
* No need to clone env here. If getObjectInstance()
|
||||
* returns something other than a RegistryContext (which
|
||||
* happens if you're looking up an object bound in the
|
||||
* registry, as opposed to looking up the registry itself),
|
||||
* then the context is GCed right away and there's no need to
|
||||
* clone the environment. If getObjectInstance() returns a
|
||||
* RegistryContext, then it still goes through
|
||||
* GenericURLContext, which calls RegistryContext.lookup()
|
||||
* with an empty name, which clones the environment.
|
||||
*/
|
||||
Object obj = URLsToObject(getURLs((Reference)ref), env);
|
||||
if (obj instanceof RegistryContext) {
|
||||
RegistryContext ctx = (RegistryContext)obj;
|
||||
ctx.reference = (Reference)ref;
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
private static Context URLToContext(String url, Hashtable<?,?> env)
|
||||
throws NamingException
|
||||
{
|
||||
rmiURLContextFactory factory = new rmiURLContextFactory();
|
||||
Object obj = factory.getObjectInstance(url, null, null, env);
|
||||
|
||||
if (obj instanceof Context) {
|
||||
return (Context)obj;
|
||||
} else {
|
||||
throw (new NotContextException(url));
|
||||
}
|
||||
}
|
||||
|
||||
private static Object URLsToObject(String[] urls, Hashtable<?,?> env)
|
||||
throws NamingException
|
||||
{
|
||||
rmiURLContextFactory factory = new rmiURLContextFactory();
|
||||
return factory.getObjectInstance(urls, null, null, env);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads environment to find URL of initial context.
|
||||
* The default URL is "rmi:".
|
||||
*/
|
||||
private static String getInitCtxURL(Hashtable<?,?> env) {
|
||||
|
||||
final String defaultURL = "rmi:";
|
||||
|
||||
String url = null;
|
||||
if (env != null) {
|
||||
url = (String)env.get(Context.PROVIDER_URL);
|
||||
}
|
||||
return ((url != null) ? url : defaultURL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if argument is an RMI registry reference.
|
||||
*/
|
||||
private static boolean isRegistryRef(Object obj) {
|
||||
|
||||
if (!(obj instanceof Reference)) {
|
||||
return false;
|
||||
}
|
||||
String thisClassName = RegistryContextFactory.class.getName();
|
||||
Reference ref = (Reference)obj;
|
||||
|
||||
return thisClassName.equals(ref.getFactoryClassName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URLs contained within an RMI registry reference.
|
||||
*/
|
||||
private static String[] getURLs(Reference ref) throws NamingException {
|
||||
|
||||
int size = 0; // number of URLs
|
||||
String[] urls = new String[ref.size()];
|
||||
|
||||
Enumeration<RefAddr> addrs = ref.getAll();
|
||||
while (addrs.hasMoreElements()) {
|
||||
RefAddr addr = addrs.nextElement();
|
||||
|
||||
if ((addr instanceof StringRefAddr) &&
|
||||
addr.getType().equals(ADDRESS_TYPE)) {
|
||||
|
||||
urls[size++] = (String)addr.getContent();
|
||||
}
|
||||
}
|
||||
if (size == 0) {
|
||||
throw (new ConfigurationException(
|
||||
"Reference contains no valid addresses"));
|
||||
}
|
||||
|
||||
// Trim URL array down to size.
|
||||
if (size == ref.size()) {
|
||||
return urls;
|
||||
}
|
||||
String[] urls2 = new String[size];
|
||||
System.arraycopy(urls, 0, urls2, 0, size);
|
||||
return urls2;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.jndi.rmi.registry;
|
||||
|
||||
|
||||
import java.rmi.*;
|
||||
|
||||
import javax.naming.*;
|
||||
|
||||
|
||||
/**
|
||||
* The RemoteReference interface wraps a Reference in a Remote wrapper.
|
||||
*
|
||||
* @author Scott Seligman
|
||||
*/
|
||||
|
||||
|
||||
public interface RemoteReference extends Remote {
|
||||
|
||||
Reference getReference() throws NamingException, RemoteException;
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.jndi.url.rmi;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.naming.*;
|
||||
import javax.naming.spi.ResolveResult;
|
||||
import com.sun.jndi.toolkit.url.GenericURLContext;
|
||||
import com.sun.jndi.rmi.registry.RegistryContext;
|
||||
|
||||
|
||||
/**
|
||||
* An RMI URL context resolves names that are URLs of the form
|
||||
* <pre>
|
||||
* rmi://[host][:port][/[object]]
|
||||
* or
|
||||
* rmi:[/][object]
|
||||
* </pre>
|
||||
* If an object is specified, the URL resolves to the named object.
|
||||
* Otherwise, the URL resolves to the specified RMI registry.
|
||||
*
|
||||
* @author Scott Seligman
|
||||
*/
|
||||
public class rmiURLContext extends GenericURLContext {
|
||||
|
||||
public rmiURLContext(Hashtable<?,?> env) {
|
||||
super(env);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the registry portion of "url" to the corresponding
|
||||
* RMI registry, and returns the atomic object name as the
|
||||
* remaining name.
|
||||
*/
|
||||
protected ResolveResult getRootURLContext(String url, Hashtable<?,?> env)
|
||||
throws NamingException
|
||||
{
|
||||
if (!url.startsWith("rmi:")) {
|
||||
throw (new IllegalArgumentException(
|
||||
"rmiURLContext: name is not an RMI URL: " + url));
|
||||
}
|
||||
|
||||
// Parse the URL.
|
||||
|
||||
String host = null;
|
||||
int port = -1;
|
||||
String objName = null;
|
||||
|
||||
int i = 4; // index into url, following the "rmi:"
|
||||
|
||||
if (url.startsWith("//", i)) { // parse "//host:port"
|
||||
i += 2; // skip past "//"
|
||||
int slash = url.indexOf('/', i);
|
||||
if (slash < 0) {
|
||||
slash = url.length();
|
||||
}
|
||||
if (url.startsWith("[", i)) { // at IPv6 literal
|
||||
int brac = url.indexOf(']', i + 1);
|
||||
if (brac < 0 || brac > slash) {
|
||||
throw new IllegalArgumentException(
|
||||
"rmiURLContext: name is an Invalid URL: " + url);
|
||||
}
|
||||
host = url.substring(i, brac + 1); // include brackets
|
||||
i = brac + 1; // skip past "[...]"
|
||||
} else { // at host name or IPv4
|
||||
int colon = url.indexOf(':', i);
|
||||
int hostEnd = (colon < 0 || colon > slash)
|
||||
? slash
|
||||
: colon;
|
||||
if (i < hostEnd) {
|
||||
host = url.substring(i, hostEnd);
|
||||
}
|
||||
i = hostEnd; // skip past host
|
||||
}
|
||||
if ((i + 1 < slash)) {
|
||||
if ( url.startsWith(":", i)) { // parse port
|
||||
i++; // skip past ":"
|
||||
port = Integer.parseInt(url.substring(i, slash));
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"rmiURLContext: name is an Invalid URL: " + url);
|
||||
}
|
||||
}
|
||||
i = slash;
|
||||
}
|
||||
if ("".equals(host)) {
|
||||
host = null;
|
||||
}
|
||||
if (url.startsWith("/", i)) { // skip "/" before object name
|
||||
i++;
|
||||
}
|
||||
if (i < url.length()) {
|
||||
objName = url.substring(i);
|
||||
}
|
||||
|
||||
// Represent object name as empty or single-component composite name.
|
||||
CompositeName remaining = new CompositeName();
|
||||
if (objName != null) {
|
||||
remaining.add(objName);
|
||||
}
|
||||
|
||||
// Debug
|
||||
//System.out.println("host=" + host + " port=" + port +
|
||||
// " objName=" + remaining.toString() + "\n");
|
||||
|
||||
// Create a registry context.
|
||||
Context regCtx = new RegistryContext(host, port, env);
|
||||
|
||||
return (new ResolveResult(regCtx, remaining));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.jndi.url.rmi;
|
||||
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.naming.*;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
|
||||
|
||||
/**
|
||||
* An RMI URL context factory creates contexts that can resolve names
|
||||
* that are RMI URLs as defined by rmiURLContext.
|
||||
* In addition, if given a specific RMI URL (or an array of them), the
|
||||
* factory will resolve all the way to the named registry or object.
|
||||
*
|
||||
* @author Scott Seligman
|
||||
*
|
||||
* @see rmiURLContext
|
||||
*/
|
||||
|
||||
|
||||
public class rmiURLContextFactory implements ObjectFactory {
|
||||
|
||||
public Object getObjectInstance(Object urlInfo, Name name,
|
||||
Context nameCtx, Hashtable<?,?> env)
|
||||
throws NamingException
|
||||
{
|
||||
if (urlInfo == null) {
|
||||
return (new rmiURLContext(env));
|
||||
} else if (urlInfo instanceof String) {
|
||||
return getUsingURL((String)urlInfo, env);
|
||||
} else if (urlInfo instanceof String[]) {
|
||||
return getUsingURLs((String[])urlInfo, env);
|
||||
} else {
|
||||
throw (new ConfigurationException(
|
||||
"rmiURLContextFactory.getObjectInstance: " +
|
||||
"argument must be an RMI URL String or an array of them"));
|
||||
}
|
||||
}
|
||||
|
||||
private static Object getUsingURL(String url, Hashtable<?,?> env)
|
||||
throws NamingException
|
||||
{
|
||||
rmiURLContext urlCtx = new rmiURLContext(env);
|
||||
try {
|
||||
return urlCtx.lookup(url);
|
||||
} finally {
|
||||
urlCtx.close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Try each URL until lookup() succeeds for one of them.
|
||||
* If all URLs fail, throw one of the exceptions arbitrarily.
|
||||
* Not pretty, but potentially more informative than returning null.
|
||||
*/
|
||||
private static Object getUsingURLs(String[] urls, Hashtable<?,?> env)
|
||||
throws NamingException
|
||||
{
|
||||
if (urls.length == 0) {
|
||||
throw (new ConfigurationException(
|
||||
"rmiURLContextFactory: empty URL array"));
|
||||
}
|
||||
rmiURLContext urlCtx = new rmiURLContext(env);
|
||||
try {
|
||||
NamingException ne = null;
|
||||
for (int i = 0; i < urls.length; i++) {
|
||||
try {
|
||||
return urlCtx.lookup(urls[i]);
|
||||
} catch (NamingException e) {
|
||||
ne = e;
|
||||
}
|
||||
}
|
||||
throw ne;
|
||||
} finally {
|
||||
urlCtx.close();
|
||||
}
|
||||
}
|
||||
}
|
44
src/jdk.naming.rmi/share/classes/module-info.java
Normal file
44
src/jdk.naming.rmi/share/classes/module-info.java
Normal file
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Provides the implementation of the RMI Java Naming provider.
|
||||
*
|
||||
* @provides javax.naming.spi.InitialContextFactory
|
||||
* @moduleGraph
|
||||
* @since 9
|
||||
*/
|
||||
module jdk.naming.rmi {
|
||||
requires java.naming;
|
||||
requires java.rmi;
|
||||
|
||||
// temporary export until NamingManager.getURLContext uses services
|
||||
exports com.sun.jndi.url.rmi to java.naming;
|
||||
exports com.sun.jndi.rmi.registry to java.rmi;
|
||||
|
||||
provides javax.naming.spi.InitialContextFactory with
|
||||
com.sun.jndi.rmi.registry.RegistryContextFactory;
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue