
jakarta.activation.CommandInfo Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package jakarta.activation;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* @version $Rev$ $Date$
*/
public class CommandInfo {
private final String commandName;
private final String commandClass;
/**
* Constructor for a CommandInfo
*
* @param commandName the command name
* @param commandClass the name of the command's implementation class
*/
public CommandInfo(String commandName, String commandClass) {
this.commandName = commandName;
this.commandClass = commandClass;
}
/**
* Return the command name.
*
* @return the command name
*/
public String getCommandName() {
return commandName;
}
/**
* Return the implementation class name.
*
* @return the name of the command's implementation class; may be null
*/
public String getCommandClass() {
return commandClass;
}
/**
* Instantiate and return a command JavaBean.
*
* If the current runtime environment supports {@code Beans.instantiate},
* the bean is created using Beans.instantiate(loader, commandClass).
*
* Otherwise, use {@link java.lang.Class#forName Class.forName}.
*
* If the new bean implements CommandObject then its setCommandContext(String, DataHandler)
* method is called.
* Otherwise if it implements Externalizable and the supplied DataHandler is not null
* then its readExternal(ObjectInputStream) method is called with a stream obtained from
* DataHandler.getInputStream().
*
* @param dh a DataHandler that provides the data to be passed to the command
* @param loader the ClassLoader to be used to instantiate the command
* @return a new command instance
* @throws IOException if there was a problem initializing the command
* @throws ClassNotFoundException if the command class could not be found
*/
public Object getCommandObject(DataHandler dh, ClassLoader loader) throws IOException, ClassNotFoundException {
Object bean = Beans.instantiate(loader, commandClass);
if (bean instanceof CommandObject) {
((CommandObject) bean).setCommandContext(commandName, dh);
} else if (bean instanceof Externalizable && dh != null) {
((Externalizable) bean).readExternal(new ObjectInputStream(dh.getInputStream()));
}
return bean;
}
/**
* Helper class to invoke Beans.instantiate if java.desktop is not available.
*/
private static final class Beans {
private static final Method instantiateMethod;
static {
Method m;
try {
Class> c = Class.forName("java.beans.Beans");
m = c.getDeclaredMethod("instantiate", ClassLoader.class, String.class);
} catch (ClassNotFoundException | NoSuchMethodException e) {
m = null;
}
instantiateMethod = m;
}
/**
* Equivalent to invoking java.beans.Beans.instantiate(loader, cn)
*/
static Object instantiate(ClassLoader loader, String cn)
throws IOException, ClassNotFoundException {
if (instantiateMethod != null) {
// invoke Beans.instantiate
try {
return instantiateMethod.invoke(null, loader, cn);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof ClassNotFoundException) {
throw (ClassNotFoundException) e.getCause();
}
} catch (IllegalAccessException e) {
//ignored
}
} else {
SecurityManager security = System.getSecurityManager();
if (security != null) {
// if it's ok with the SecurityManager, it's ok with me.
String cname = cn.replace('/', '.');
if (cname.startsWith("[")) {
int b = cname.lastIndexOf('[') + 2;
if (b > 1 && b < cname.length()) {
cname = cname.substring(b);
}
}
int i = cname.lastIndexOf('.');
if (i != -1) {
security.checkPackageAccess(cname.substring(0, i));
}
}
// Beans.instantiate specified to use SCL when loader is null
if (loader == null) {
loader = (ClassLoader)
AccessController.doPrivileged((PrivilegedAction) () -> {
ClassLoader cl = null;
try {
cl = ClassLoader.getSystemClassLoader();
} catch (SecurityException ex) {
}
return cl;
});
}
final Class> beanClass = Class.forName(cn, true, loader);
try {
return beanClass.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
throw new ClassNotFoundException(beanClass + ": " + ex, ex);
}
}
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy