use of java.lang.reflect.Constructor in project XobotOS by xamarin.
the class GenericInflater method createItem.
/**
* Low-level function for instantiating by name. This attempts to
* instantiate class of the given <var>name</var> found in this
* inflater's ClassLoader.
*
* <p>
* There are two things that can happen in an error case: either the
* exception describing the error will be thrown, or a null will be
* returned. You must deal with both possibilities -- the former will happen
* the first time createItem() is called for a class of a particular name,
* the latter every time there-after for that class name.
*
* @param name The full name of the class to be instantiated.
* @param attrs The XML attributes supplied for this instance.
*
* @return The newly instantied item, or null.
*/
public final T createItem(String name, String prefix, AttributeSet attrs) throws ClassNotFoundException, InflateException {
Constructor constructor = (Constructor) sConstructorMap.get(name);
try {
if (null == constructor) {
// Class not found in the cache, see if it's real,
// and try to add it
Class clazz = mContext.getClassLoader().loadClass(prefix != null ? (prefix + name) : name);
constructor = clazz.getConstructor(mConstructorSignature);
sConstructorMap.put(name, constructor);
}
Object[] args = mConstructorArgs;
args[1] = attrs;
return (T) constructor.newInstance(args);
} catch (NoSuchMethodException e) {
InflateException ie = new InflateException(attrs.getPositionDescription() + ": Error inflating class " + (prefix != null ? (prefix + name) : name));
ie.initCause(e);
throw ie;
} catch (ClassNotFoundException e) {
// If loadClass fails, we should propagate the exception.
throw e;
} catch (Exception e) {
InflateException ie = new InflateException(attrs.getPositionDescription() + ": Error inflating class " + constructor.getClass().getName());
ie.initCause(e);
throw ie;
}
}
use of java.lang.reflect.Constructor in project otertool by wuntee.
the class JarSigner15 method findConstructor.
private static Constructor findConstructor(Class c, Class... argTypes) throws NoSuchMethodException {
Constructor ct = c.getDeclaredConstructor(argTypes);
if (ct == null) {
throw new RuntimeException(c.getName());
}
ct.setAccessible(true);
return ct;
}
use of java.lang.reflect.Constructor in project OpenAM by OpenRock.
the class EntitlementConfiguration method getInstance.
/**
* Returns an instance of entitlement configuration.
*
* @param adminSubject Admin Subject who has rights to query and modify
* configuration datastore.
* @param realm Realm name.
* @return an instance of entitlement configuration.
*/
public static EntitlementConfiguration getInstance(Subject adminSubject, String realm) {
final Class clazz;
try {
//RFE: load different configuration plugin
clazz = Class.forName("com.sun.identity.entitlement.opensso.EntitlementService");
} catch (ClassNotFoundException e) {
PolicyConstants.DEBUG.error("EntitlementConfiguration.<init>", e);
return null;
}
Class[] parameterTypes = { String.class };
try {
Constructor constructor = clazz.getConstructor(parameterTypes);
Object[] args = { realm };
EntitlementConfiguration impl = (EntitlementConfiguration) constructor.newInstance(args);
impl.adminSubject = adminSubject;
return impl;
} catch (InstantiationException ex) {
PolicyConstants.DEBUG.error("PrivilegeIndexStore.getInstance", ex);
} catch (IllegalAccessException ex) {
PolicyConstants.DEBUG.error("PrivilegeIndexStore.getInstance", ex);
} catch (IllegalArgumentException ex) {
PolicyConstants.DEBUG.error("PrivilegeIndexStore.getInstance", ex);
} catch (InvocationTargetException ex) {
PolicyConstants.DEBUG.error("PrivilegeIndexStore.getInstance", ex);
} catch (NoSuchMethodException ex) {
PolicyConstants.DEBUG.error("PrivilegeIndexStore.getInstance", ex);
} catch (SecurityException ex) {
PolicyConstants.DEBUG.error("PrivilegeIndexStore.getInstance", ex);
}
return null;
}
use of java.lang.reflect.Constructor in project OpenAM by OpenRock.
the class LogManager method readConfiguration.
/**
* This method overrides the <code>readConfiguration</code> method in
* JDK <code>LogManager</code> class.
* The base class method resets the loggers in memory. This method
* must add handlers to the loggers in memory according to the
* new configuration.
*
* @throws IOException if there are IO problems reading the configuration.
* @throws SecurityException if a security manager exists and if the caller
* does not have <code>LoggingPermission("control")</code>.
*/
public final void readConfiguration() throws IOException, SecurityException {
String[] xlogData = null;
try {
/*
* This writeLock ensures that no logging threads will execute
* a logger.log call after this point since they request for
* a readLock.
*/
Logger.rwLock.writeRequest();
/*
* This sync is for avoiding this thread snathing away
* time slice from a thread executing getLogger() method
* which is also sync on Logger.class
* which may lead to two handlers being added to the same logger.
*/
synchronized (Logger.class) {
Enumeration loggerNames = getLoggerNames();
LogManagerUtil.setupEnv();
if (didFirstReadConfig && SystemProperties.isServerMode()) {
oldLocation = getProperty(LogConstants.LOG_LOCATION);
oldLevel = getProperty(LogConstants.LOGGING_LEVEL);
oldSecurityStatus = getProperty(LogConstants.SECURITY_STATUS);
oldBackend = getProperty(LogConstants.BACKEND);
oldStatus = getProperty(LogConstants.LOG_STATUS_ATTR);
}
try {
/*
* This change is done for deploying AM as a single
* war. In server mode we will always use our
* LogConfigReader. On the client side the
* the JVM property will define whether to use the
* LogConfigReader or the remote handlers. If no
* JVM property is set, the remote handlers will
* be used.
*/
if (SystemProperties.isServerMode()) {
LogConfigReader logConfigReader = new LogConfigReader();
} else {
super.readConfiguration();
}
didFirstReadConfig = true;
} catch (Exception ex) {
/* no debug since our debugging system is not up. */
} finally {
LogManagerUtil.resetEnv();
}
if (isLocal) {
securityStatus = false;
readAllFields();
readSelectedFieldSet();
if (SystemProperties.isServerMode()) {
newLocation = getProperty(LogConstants.LOG_LOCATION);
newLevel = getProperty(LogConstants.LOGGING_LEVEL);
newSecurityStatus = getProperty(LogConstants.SECURITY_STATUS);
newBackend = getProperty(LogConstants.BACKEND);
newStatus = getProperty(LogConstants.LOG_STATUS_ATTR);
}
/*
* give all the pertinent values to decide why
* logging to the file was terminated. still
* have to check that one of the attributes
* that changed would cause a "real" termination
* of logging to the files/tables in the current
* location (as opposed to just a buffer timer change,
* for instance).
*/
String[] logData = { oldLocation, newLocation, oldBackend, newBackend, oldSecurityStatus, newSecurityStatus, oldStatus, newStatus, oldLevel, newLevel };
if (getProperty(LogConstants.BACKEND).equals("DB")) {
HANDLER = getProperty(LogConstants.DB_HANDLER);
FORMATTER = getProperty(LogConstants.DB_FORMATTER);
String driver = getProperty(LogConstants.DB_DRIVER);
} else if (getProperty(LogConstants.BACKEND).equals("Syslog")) {
HANDLER = getProperty(LogConstants.SYSLOG_HANDLER);
FORMATTER = getProperty(LogConstants.SYSLOG_FORMATTER);
} else if (getProperty(LogConstants.SECURITY_STATUS).equalsIgnoreCase("ON")) {
securityStatus = true;
HANDLER = getProperty(LogConstants.SECURE_FILE_HANDLER);
FORMATTER = getProperty(LogConstants.SECURE_ELF_FORMATTER);
} else {
HANDLER = getProperty(LogConstants.FILE_HANDLER);
FORMATTER = getProperty(LogConstants.ELF_FORMATTER);
}
if (getProperty(LogConstants.BACKEND).equals("File")) {
if (SystemProperties.isServerMode() && (newLocation != null) && (oldLocation != null) && !oldLocation.equals(newLocation)) {
File dir = new File(newLocation);
if (!dir.exists()) {
if (!dir.mkdirs()) {
Debug.error("LogManager:readConfiguration:" + "Unable to create the new log " + "directory. Verify that the " + "process has necessary permissions");
}
}
}
}
boolean loggingInactive = (getProperty(LogConstants.LOG_STATUS_ATTR).equals(inactive));
String strLogLevel = getProperty(LogConstants.LOGGING_LEVEL);
try {
loggingLevel = Level.parse(strLogLevel);
} catch (IllegalArgumentException iaex) {
// default
loggingLevel = Level.INFO;
Debug.error("LogManager:readConfiguration:" + "Log level '" + strLogLevel + "' unknown; setting to Level.INFO.");
}
if (loggingInactive) {
loggingLevel = Level.OFF;
}
xlogData = logData;
} else {
HANDLER = getProperty(LogConstants.REMOTE_HANDLER);
if (HANDLER == null) {
HANDLER = LogConstants.DEFAULT_REMOTE_HANDER;
}
FORMATTER = getProperty(LogConstants.REMOTE_FORMATTER);
if (FORMATTER == null) {
FORMATTER = LogConstants.DEFAULT_REMOTE_FORMATTER;
}
}
Logger.resolveHostName = Boolean.valueOf(getProperty(LogConstants.LOG_RESOLVE_HOSTNAME_ATTR)).booleanValue();
/*
* modify existing loggers in memory according
* to the new configuration
*/
loggerNames = getLoggerNames();
while (loggerNames.hasMoreElements()) {
String curEl = (String) loggerNames.nextElement();
/* avoid root logger */
if (!curEl.isEmpty() && !curEl.equals("global")) {
if (Debug.messageEnabled()) {
Debug.message("LogManager:readConfiguration:" + "Processing Logger: " + curEl);
}
/*
* remove all handlers and add new handlers for
* this logger
*/
Logger l = (Logger) Logger.getLogger(curEl);
Handler[] handlers = l.getHandlers();
for (int i = 0; i < handlers.length; i++) {
handlers[i].close();
l.removeHandler(handlers[i]);
}
String handlerClass = LogManager.HANDLER;
Class clz = null;
Class[] parameters = { String.class };
Object[] parameterObjects = { l.getName() };
Constructor cons = null;
Handler h = null;
try {
clz = Class.forName(handlerClass);
} catch (Exception e) {
Debug.error("LogManager.readConfiguration:could not load " + handlerClass, e);
}
try {
cons = clz.getDeclaredConstructor(parameters);
} catch (Exception e) {
Debug.error("LogManager.readConfiguration:could not" + " instantiate" + handlerClass, e);
}
try {
h = (Handler) cons.newInstance(parameterObjects);
} catch (Exception e) {
Debug.error("LogManager.readConfiguration:could not" + " instantiate" + handlerClass, e);
}
String formatterClass = LogManager.FORMATTER;
Formatter f = null;
try {
f = (Formatter) Class.forName(formatterClass).newInstance();
} catch (Exception e) {
Debug.error("LogManager.readConfiguration:could not" + " instantiate Formatter " + formatterClass, e);
}
h.setFormatter(f);
l.addHandler(h);
/*
* get the "iplanet-am-logging.<logfilename>.level
* value for this file, if it's been added on the
* server's advanced config page.
*
* BUT: logging status set to Inactive means
* all logging is turned Level.OFF
*/
Level tlevel = loggingLevel;
if (loggingLevel != Level.OFF) {
String levelProp = LogConstants.LOG_PROP_PREFIX + "." + l.getName() + ".level";
String lvlStr = SystemProperties.get(levelProp);
if ((lvlStr != null) && (lvlStr.length() > 0)) {
try {
tlevel = Level.parse(lvlStr);
} catch (IllegalArgumentException iaex) {
// use value for all others
}
}
}
if (loggingLevel != null) {
// only if isLocal
// update logging level
l.setLevel(tlevel);
}
}
/* end of avoid rootlogger */
}
/* end of while(loggerNames.hasMoreElements) */
}
/* end of synchronized(Logger.class) */
} finally {
Logger.rwLock.writeDone();
}
if (SystemProperties.isServerMode() && isLocal) {
checkStartLogs(xlogData);
//Update the new configuration info in Monitoring handle also
updateMonitConfigForLogService();
}
}
use of java.lang.reflect.Constructor in project OpenAM by OpenRock.
the class Logger method processNewLoggerObject.
/**
* To add handlers and formatters to the new logger object
*/
private static void processNewLoggerObject(Logger result) {
Formatter formatter = null;
String handlerClass = LogManager.HANDLER;
String formatterClass = LogManager.FORMATTER;
String levelProp = LogConstants.LOG_PROP_PREFIX + "." + result.logName + ".level";
/*
* see if logging level for this file already defined.
* if not, then check AMConfig.properties.
* if not, then use Logging service config value.
* if not, then use default ("INFO")
*/
String levelString = lm.getProperty(levelProp);
if ((levelString == null) || !(levelString.length() > 0)) {
levelString = SystemProperties.get(levelProp);
if ((levelString == null) || !(levelString.length() > 0)) {
levelString = lm.getProperty(LogConstants.LOGGING_LEVEL);
if ((levelString == null) || !(levelString.length() > 0)) {
levelString = LogConstants.DEFAULT_LOGGING_LEVEL_STR;
}
}
}
Level logLevel = null;
try {
logLevel = Level.parse(levelString);
} catch (IllegalArgumentException iaex) {
logLevel = LogConstants.DEFAULT_LOGGING_LEVEL;
}
result.setLevel(logLevel);
// but disabled logging in AMConfig.properties takes precedence
String logStatus = lm.getProperty(LogConstants.LOG_STATUS_ATTR);
if (logStatus != null && logStatus.startsWith("INACTIVE")) {
logLevel = Level.OFF;
}
result.setLevel(logLevel);
Class clz = null;
Class[] parameters = { String.class };
Object[] parameterObjects = { result.logName };
Constructor cons = null;
Handler handler = null;
if (handlerClass == null) {
Debug.error("Logger:processNewLoggerObject:" + "HandlerClass not in classpath ");
return;
}
try {
clz = Class.forName(handlerClass);
} catch (Exception e) {
Debug.error("Logger:processNewLoggerObject:" + "HandlerClass not in classpath: " + handlerClass, e);
return;
}
try {
if (clz != null) {
cons = clz.getDeclaredConstructor(parameters);
}
} catch (Exception e) {
Debug.error("Logger:processNewLoggerObject:" + "constructor parameter mismatch ", e);
return;
}
try {
if (cons != null) {
handler = (Handler) cons.newInstance(parameterObjects);
}
} catch (Exception e) {
Debug.error("Logger:processNewLoggerObject:" + "Could not instantiate handler: " + handlerClass, e);
return;
}
if (formatterClass == null) {
Debug.error("Logger:processNewLoggerObject:" + "formatterClass not in classpath ");
return;
}
try {
clz = Thread.currentThread().getContextClassLoader().loadClass(formatterClass);
} catch (Exception e) {
Debug.error("Logger:processNewLoggerObject:" + "Could not load Formatter Class: " + formatterClass, e);
return;
}
try {
if (clz != null) {
formatter = (Formatter) clz.newInstance();
}
} catch (Exception e) {
Debug.error("Logger:processNewLoggerObject:" + "Could not get Formatter instance " + formatterClass, e);
return;
}
try {
handler.setFormatter(formatter);
result.addHandler(handler);
} catch (Exception e) {
Debug.error("Logger:processNewLoggerObject:" + "Unable to add Handler", e);
return;
}
String filterClassName = lm.getProperty(LogConstants.FILTER_CLASS_NAME);
try {
if (filterClassName != null) {
Filter filter = (Filter) Class.forName(filterClassName).newInstance();
result.setFilter(filter);
}
} catch (Exception e) {
Debug.error("Logger:processNewLoggerObject:" + "Could not set Filter: " + filterClassName, e);
}
result.setUseParentHandlers(false);
resolveHostName = Boolean.valueOf(lm.getProperty(LogConstants.LOG_RESOLVE_HOSTNAME_ATTR)).booleanValue();
}
Aggregations