/** * Check for the existence of a given class * * @param pClassName class name to check * @return true if the class could be loaded by the thread's conext class loader, false otherwise */ public static boolean checkForClass(String pClassName) { return ClassUtil.classForName(pClassName,false) != null; }
/** * Lookup a class. See {@link ClassUtil#classForName(String, boolean,ClassLoader[])} for details. The class * gets initialized during lookup. * * @param pClassName name to lookup. * @return the class found or null if no class could be found. */ public static <T> Class<T> classForName(String pClassName, ClassLoader ... pClassLoaders) { return classForName(pClassName, true, pClassLoaders); }
private static Class lookupKeyGenClass() { Class keyGenClass = null; for (final String keyGenCandidate : new String[]{ KEYGEN_CLASS_JDK8_SUN, KEYGEN_CLASS_JDK7_SUN, KEYGEN_CLASS_JDK8_IBM, KEYGEN_CLASS_JDK7_IBM }) { keyGenClass = ClassUtil.classForName(keyGenCandidate); if (keyGenClass != null) { break; } } if (keyGenClass == null) { throw new IllegalStateException( "Cannot find any key-generator class: Tried Sun Java 8's " + KEYGEN_CLASS_JDK8_SUN + ", Sun Java 7's " + KEYGEN_CLASS_JDK7_SUN + ", IBM Java 8's " + KEYGEN_CLASS_JDK8_IBM + " and IBM Java 7's " + KEYGEN_CLASS_JDK7_IBM); } return keyGenClass; }
private Object convertByConstructor(String pType, String pValue) { Class<?> expectedClass = ClassUtil.classForName(pType); if (expectedClass != null) { for (Constructor<?> constructor : expectedClass.getConstructors()) { // only support only 1 constructor parameter if (constructor.getParameterTypes().length == 1 && constructor.getParameterTypes()[0].isAssignableFrom(String.class)) { try { return constructor.newInstance(pValue); } catch (Exception ignore) { } } } } return null; }
private static X509Certificate getSelfCertificate(Object keypair, Object x500Name, Date date, long l) { final Class<?> clazz; if (ClassUtil.checkForClass(X500_NAME_SUN)) { clazz = ClassUtil.classForName(X500_NAME_SUN, false); } else { clazz = ClassUtil.classForName(X500_NAME_IBM, false); } try { final Method selfCertMethod = keypair.getClass().getDeclaredMethod("getSelfCertificate", clazz, Date.class, long.class); selfCertMethod.setAccessible(true); return (X509Certificate) selfCertMethod.invoke(keypair, x500Name, date, l); } catch (final NoSuchMethodException e) { throw new IllegalStateException("Found no getSelfCertificate-method with the expected signature.", e); } catch (final IllegalAccessException e) { throw new IllegalStateException("Not allowed to access getSelfCertificate-method.", e); } catch (final InvocationTargetException e) { throw new IllegalStateException("The getSelfCertificate-method threw an error.", e); } }
/** * Instantiate an instance of the given class with its default constructor. The context class loader is used * to lookup the class * * @param pClassName name of class to instantiate * @param pArguments optional constructor arguments. Works only for objects with the same class as declared in * the constructor types (no subclasses) * @param <T> type object type * @return instantiated object * @throws IllegalArgumentException if the class could not be found or instantiated */ public static <T> T newInstance(String pClassName, Object ... pArguments) { Class<T> clazz = classForName(pClassName); if (clazz == null) { throw new IllegalArgumentException("Cannot find " + pClassName); } return newInstance(clazz, pArguments); }
/** {@inheritDoc} * @param servers*/ @Override public void addMBeanServers(Set<MBeanServerConnection> servers) { try { /* * this.mbeanServer = AdminServiceFactory.getMBeanFactory().getMBeanServer(); */ Class adminServiceClass = ClassUtil.classForName("com.ibm.websphere.management.AdminServiceFactory",getClass().getClassLoader()); if (adminServiceClass != null) { Method getMBeanFactoryMethod = adminServiceClass.getMethod("getMBeanFactory", new Class[0]); Object mbeanFactory = getMBeanFactoryMethod.invoke(null); if (mbeanFactory != null) { Method getMBeanServerMethod = mbeanFactory.getClass().getMethod("getMBeanServer", new Class[0]); servers.add((MBeanServer) getMBeanServerMethod.invoke(mbeanFactory)); } } } catch (InvocationTargetException ex) { // CNFE should be earlier throw new IllegalArgumentException(INTERNAL_ERROR_MSG,ex); } catch (IllegalAccessException ex) { throw new IllegalArgumentException(INTERNAL_ERROR_MSG,ex); } catch (NoSuchMethodException ex) { throw new IllegalArgumentException(INTERNAL_ERROR_MSG,ex); } }
private static Restrictor createCustomRestrictor(Configuration pConfig) { String restrictorClassName = pConfig.get(ConfigKey.RESTRICTOR_CLASS); if (restrictorClassName == null) { return null; } Class restrictorClass = ClassUtil.classForName(restrictorClassName); if (restrictorClass == null) { throw new IllegalArgumentException("No custom restrictor class " + restrictorClassName + " found"); } return lookupRestrictor(pConfig, restrictorClass); }
private static <T> void createOrRemoveService(Map<ServiceEntry, T> pExtractorMap, String pLine) throws ClassNotFoundException, InstantiationException, IllegalAccessException { if (pLine.length() > 0) { ServiceEntry entry = new ServiceEntry(pLine); if (entry.isRemove()) { // Removing is a bit complex since we need to find out // the proper key since the order is part of equals/hash // so we cant fetch/remove it directly Set<ServiceEntry> toRemove = new HashSet<ServiceEntry>(); for (ServiceEntry key : pExtractorMap.keySet()) { if (key.getClassName().equals(entry.getClassName())) { toRemove.add(key); } } for (ServiceEntry key : toRemove) { pExtractorMap.remove(key); } } else { Class<T> clazz = ClassUtil.classForName(entry.getClassName(),ServiceObjectFactory.class.getClassLoader()); if (clazz == null) { throw new ClassNotFoundException("Class " + entry.getClassName() + " could not be found"); } T ext = (T) clazz.newInstance(); pExtractorMap.put(entry, ext); } } }
Configuration pConfig) { try { Class clazz = ClassUtil.classForName(pDispatcherClass, getClass().getClassLoader()); if (clazz == null) { throw new IllegalArgumentException("Couldn't lookup dispatcher " + pDispatcherClass);
private Object convertToArray(String pType, String pValue) { // It's an array String t = pType.substring(1,2); Class valueType; if (t.equals("L")) { // It's an object-type String oType = pType.substring(2,pType.length()-1).replace('/','.'); valueType = ClassUtil.classForName(oType); if (valueType == null) { throw new IllegalArgumentException("No class of type " + oType + "found"); } } else { valueType = TYPE_SIGNATURE_MAP.get(t); if (valueType == null) { throw new IllegalArgumentException("Cannot convert to unknown array type " + t); } } String[] values = EscapeUtil.splitAsArray(pValue, EscapeUtil.PATH_ESCAPE, ","); Object ret = Array.newInstance(valueType,values.length); int i = 0; for (String value : values) { Array.set(ret,i++,value.equals("[null]") ? null : convertFromString(valueType.getCanonicalName(),value)); } return ret; }
/** {@inheritDoc} * @param pMBeanServerExecutor*/ public ServerHandle detect(MBeanServerExecutor pMBeanServerExecutor) { Class serverClass = ClassUtil.classForName("org.mortbay.jetty.Server",false); if (serverClass != null) { return new ServerHandle("Mortbay", "jetty", getVersion(serverClass), null); } serverClass = ClassUtil.classForName("org.eclipse.jetty.server.Server",false); if (serverClass != null) { return new ServerHandle("Eclipse", "jetty", getVersion(serverClass), null); } return null; }
/** * Prepare a value from a either a given object or its string representation. * If the value is already assignable to the given class name it is returned directly. * * @param pExpectedClassName type name of the expected type * @param pValue value to either take directly or to convert from its string representation. * @return the prepared / converted object */ public Object prepareValue(String pExpectedClassName, Object pValue) { if (pValue == null) { return null; } else { Class expectedClass = ClassUtil.classForName(pExpectedClassName); Object param = null; if (expectedClass != null) { param = prepareValue(expectedClass,pValue); } if (param == null) { // Ok, we try to convert it from a string // If expectedClass is null, it is probably a native type, so we // let happen the string conversion // later on (e.g. conversion of pArgument.toString()) which will throw // an exception at this point if conversion can not be done return convertFromString(pExpectedClassName, pValue.toString()); } return param; } }
/** * Check for the existence of a given class * * @param pClassName class name to check * @return true if the class could be loaded by the thread's conext class loader, false otherwise */ public static boolean checkForClass(String pClassName) { return ClassUtil.classForName(pClassName,false) != null; }
/** * Lookup a class. See {@link ClassUtil#classForName(String, boolean,ClassLoader[])} for details. The class * gets initialized during lookup. * * @param pClassName name to lookup. * @return the class found or null if no class could be found. */ public static <T> Class<T> classForName(String pClassName, ClassLoader ... pClassLoaders) { return classForName(pClassName, true, pClassLoaders); }
/** * Lookup a class. See {@link ClassUtil#classForName(String, boolean,ClassLoader[])} for details. The class * gets initialized during lookup. * * @param pClassName name to lookup. * @return the class found or null if no class could be found. */ public static <T> Class<T> classForName(String pClassName, ClassLoader ... pClassLoaders) { return classForName(pClassName, true, pClassLoaders); }
/** * Check for the existence of a given class * * @param pClassName class name to check * @return true if the class could be loaded by the thread's conext class loader, false otherwise */ public static boolean checkForClass(String pClassName) { return ClassUtil.classForName(pClassName,false) != null; }
private static Restrictor createCustomRestrictor(Configuration pConfig) { String restrictorClassName = pConfig.get(ConfigKey.RESTRICTOR_CLASS); if (restrictorClassName == null) { return null; } Class restrictorClass = ClassUtil.classForName(restrictorClassName); if (restrictorClass == null) { throw new IllegalArgumentException("No custom restrictor class " + restrictorClassName + " found"); } return lookupRestrictor(pConfig, restrictorClass); }
private static Restrictor createCustomRestrictor(Configuration pConfig) { String restrictorClassName = pConfig.get(ConfigKey.RESTRICTOR_CLASS); if (restrictorClassName == null) { return null; } Class restrictorClass = ClassUtil.classForName(restrictorClassName); if (restrictorClass == null) { throw new IllegalArgumentException("No custom restrictor class " + restrictorClassName + " found"); } return lookupRestrictor(pConfig, restrictorClass); }
/** {@inheritDoc} * @param pMBeanServerExecutor*/ public ServerHandle detect(MBeanServerExecutor pMBeanServerExecutor) { Class serverClass = ClassUtil.classForName("org.mortbay.jetty.Server",false); if (serverClass != null) { return new ServerHandle("Mortbay", "jetty", getVersion(serverClass), null); } serverClass = ClassUtil.classForName("org.eclipse.jetty.server.Server",false); if (serverClass != null) { return new ServerHandle("Eclipse", "jetty", getVersion(serverClass), null); } return null; }