protected List<Field> getAllFieldsForClass(final Class<?> clazz) { List<Field> filteredFields = fieldsForCmdClass.get(clazz); // If list of fields was not cached yet if (filteredFields == null) { final List<Field> allFields = ReflectUtil.getAllFieldsForClass(this.getClass(), BaseCmd.class); filteredFields = new ArrayList<Field>(); for (final Field field : allFields) { final Parameter parameterAnnotation = field.getAnnotation(Parameter.class); if ((parameterAnnotation != null) && parameterAnnotation.expose()) { filteredFields.add(field); } } // Cache the prepared list for future use fieldsForCmdClass.put(clazz, filteredFields); } return filteredFields; }
public static List<String> flattenPropeties(final Object target, final Class<?> clazz, final String... excludedProperties) { return flattenProperties(target, clazz, ImmutableSet.copyOf(excludedProperties)); }
public static Pair<Class<?>, Field> getAnyField(Class<?> clazz, String fieldName) { try { return new Pair<Class<?>, Field>(clazz, clazz.getDeclaredField(fieldName)); } catch (SecurityException e) { throw new CloudRuntimeException("How the heck?", e); } catch (NoSuchFieldException e) { // Do I really want this? No I don't but what can I do? It only throws the NoSuchFieldException. Class<?> parent = clazz.getSuperclass(); if (parent != null) { return getAnyField(parent, fieldName); } return null; } }
boolean isAsync = ReflectUtil.isCmdClassAsync(clas, new Class<?>[] {BaseAsyncCmd.class, BaseAsyncCreateCmd.class}); Set<Field> fields = ReflectUtil.getAllFieldsForClass(clas, new Class<?>[] {BaseCmd.class, BaseAsyncCmd.class, BaseAsyncCreateCmd.class});
private static void configLog4j() { final ClassLoader loader = ReflectUtil.getClassLoaderForName("conf"); URL configUrl = loader.getResource("/conf/log4j-cloud.xml"); if (configUrl == null) configUrl = ClassLoader.getSystemResource("log4j-cloud.xml"); if (configUrl == null) configUrl = ClassLoader.getSystemResource("conf/log4j-cloud.xml"); if (configUrl != null) { try { System.out.println("Configure log4j using " + configUrl.toURI().toString()); } catch (URISyntaxException e1) { e1.printStackTrace(); } try { File file = new File(configUrl.toURI()); System.out.println("Log4j configuration from : " + file.getAbsolutePath()); DOMConfigurator.configureAndWatch(file.getAbsolutePath(), 10000); } catch (URISyntaxException e) { System.out.println("Unable to convert log4j configuration Url to URI"); } // DOMConfigurator.configure(configUrl); } else { System.out.println("Configure log4j with default properties"); } }
public static List<String> flattenProperties(final Object target, final Class<?> clazz) { return flattenPropeties(target, clazz, "class"); }
private ApiDiscoveryResponse getCmdRequestMap(Class<?> cmdClass, APICommand apiCmdAnnotation) { String apiName = apiCmdAnnotation.name(); ApiDiscoveryResponse response = new ApiDiscoveryResponse(); response.setName(apiName); response.setDescription(apiCmdAnnotation.description()); if (!apiCmdAnnotation.since().isEmpty()) { response.setSince(apiCmdAnnotation.since()); } Set<Field> fields = ReflectUtil.getAllFieldsForClass(cmdClass, new Class<?>[] {BaseCmd.class, BaseAsyncCmd.class, BaseAsyncCreateCmd.class}); boolean isAsync = ReflectUtil.isCmdClassAsync(cmdClass, new Class<?>[] {BaseAsyncCmd.class, BaseAsyncCreateCmd.class}); response.setAsync(isAsync); for (Field field : fields) { Parameter parameterAnnotation = field.getAnnotation(Parameter.class); if (parameterAnnotation != null && parameterAnnotation.expose() && parameterAnnotation.includeInApiDoc()) { ApiParameterResponse paramResponse = new ApiParameterResponse(); paramResponse.setName(parameterAnnotation.name()); paramResponse.setDescription(parameterAnnotation.description()); paramResponse.setType(parameterAnnotation.type().toString().toLowerCase()); paramResponse.setLength(parameterAnnotation.length()); paramResponse.setRequired(parameterAnnotation.required()); if (!parameterAnnotation.since().isEmpty()) { paramResponse.setSince(parameterAnnotation.since()); } paramResponse.setRelated(parameterAnnotation.entityType()[0].getName()); response.addParam(paramResponse); } } return response; }
private void launchConsoleProxy(final byte[] ksBits, final String ksPassword, final String encryptorPassword) { final Object resource = this; s_logger.info("Building class loader for com.cloud.consoleproxy.ConsoleProxy"); final ClassLoader loader = ReflectUtil.getClassLoaderForName("console-proxy"); if (_consoleProxyMain == null) { s_logger.info("Running com.cloud.consoleproxy.ConsoleProxy with encryptor password=" + encryptorPassword);
public static List<String> flattenProperties(final Object target, final Class<?> clazz) { return flattenPropeties(target, clazz, "class"); }
Class<?> cmdClass = getCmdClass(command[0]); if (cmdClass != null) { List<Field> fields = ReflectUtil.getAllFieldsForClass(cmdClass, BaseCmd.class); for (Field field : fields) { Parameter parameterAnnotation = field.getAnnotation(Parameter.class);
protected String backupSnapshotToS3(final Connection connection, final S3TO s3, final String srUuid, final String folder, final String snapshotUuid, final Boolean iSCSIFlag, final int wait) { final String filename = iSCSIFlag ? "VHD-" + snapshotUuid : snapshotUuid + ".vhd"; final String dir = (iSCSIFlag ? "/dev/VG_XenStorage-" : "/var/run/sr-mount/") + srUuid; final String key = folder + "/" + filename; // String.format("/snapshots/%1$s", snapshotUuid); try { final List<String> parameters = newArrayList(flattenProperties(s3, ClientOptions.class)); // https workaround for Introspector bug that does not // recognize Boolean accessor methods ... parameters.addAll(Arrays.asList("operation", "put", "filename", dir + "/" + filename, "iSCSIFlag", iSCSIFlag.toString(), "bucket", s3.getBucketName(), "key", key, "https", s3.isHttps() != null ? s3.isHttps().toString() : "null", "maxSingleUploadSizeInBytes", String.valueOf(s3.getMaxSingleUploadSizeInBytes()))); final String result = hypervisorResource.callHostPluginAsync(connection, "s3xenserver", "s3", wait, parameters.toArray(new String[parameters.size()])); if (result != null && result.equals("true")) { return key; } return null; } catch (final Exception e) { s_logger.error(String.format("S3 upload failed of snapshot %1$s due to %2$s.", snapshotUuid, e.toString()), e); } return null; }
public void addOrderBy(Class<?> clazz, String field, boolean ascending) { if (field == null) { return; } Field f; Pair<Class<?>, Field> pair = ReflectUtil.getAnyField(clazz, field); assert (pair != null) : "Can't find field " + field + " in " + clazz.getName(); clazz = pair.first(); f = pair.second(); Column column = f.getAnnotation(Column.class); String name = column != null ? column.name() : field; StringBuilder order = new StringBuilder(); if (column == null || column.table() == null || column.table().length() == 0) { order.append(DbUtil.getTableName(clazz)); } else { order.append(column.table()); } order.append(".").append(name).append(ascending ? " ASC " : " DESC "); if (_orderBy == null) { _orderBy = order.insert(0, " ORDER BY ").toString(); } else { _orderBy = order.insert(0, _orderBy + ", ").toString(); } }
ConsoleProxy.ksPassword = ksPassword; try { final ClassLoader loader = ReflectUtil.getClassLoaderForName("agent"); Class<?> contextClazz = loader.loadClass("com.cloud.agent.resource.consoleproxy.ConsoleProxyResource"); authMethod = contextClazz.getDeclaredMethod("authenticateConsoleAccess", String.class, String.class, String.class, String.class, String.class, Boolean.class);
@Test public void getAllFieldsForClassWithFoo() throws NoSuchFieldException, SecurityException { Set<Field> fooFields = ReflectUtil.getAllFieldsForClass(Foo.class, new Class<?>[] {}); Assert.assertNotNull(fooFields); Assert.assertTrue(fooFields.contains(Foo.class.getDeclaredField("fooField"))); Assert.assertTrue(fooFields.contains(Foo.class.getDeclaredField("fooIntField"))); }
@Test public void testFlattenPropertiesNullTarget() throws Exception { assertEquals(emptyList(), flattenProperties(null, Bean.class)); }
public static Pair<Class<?>, Field> getAnyField(final Class<?> clazz, final String fieldName) { try { return new Pair<>(clazz, clazz.getDeclaredField(fieldName)); } catch (final SecurityException e) { throw new CloudRuntimeException("How the heck?", e); } catch (final NoSuchFieldException e) { // Do I really want this? No I don't but what can I do? It only throws the NoSuchFieldException. final Class<?> parent = clazz.getSuperclass(); if (parent != null) { return getAnyField(parent, fieldName); } return null; } }
@Test public void getAllFieldsForClassWithBar() throws NoSuchFieldException, SecurityException { Set<Field> barFields = ReflectUtil.getAllFieldsForClass(Bar.class, new Class<?>[] {}); Assert.assertNotNull(barFields); Assert.assertTrue(barFields.contains(Foo.class.getDeclaredField("fooField"))); Assert.assertTrue(barFields.contains(Foo.class.getDeclaredField("fooIntField"))); Assert.assertTrue(barFields.contains(Bar.class.getDeclaredField("barField"))); Assert.assertTrue(barFields.contains(Bar.class.getDeclaredField("barIntField"))); }
@Test public void testFlattenNonNullProperties() throws Exception { final List<String> expectedResult = newArrayList("booleanProperty", TRUE.toString(), "intProperty", "1", "stringProperty", "foo"); final Bean bean = new Bean(1, true, "foo"); assertEquals(expectedResult, flattenProperties(bean, Bean.class)); }
public void addOrderBy(Class<?> clazz, final String field, final boolean ascending) { if (field == null) { return; } final Field f; final Pair<Class<?>, Field> pair = ReflectUtil.getAnyField(clazz, field); assert (pair != null) : "Can't find field " + field + " in " + clazz.getName(); clazz = pair.first(); f = pair.second(); final Column column = f.getAnnotation(Column.class); final String name = column != null ? column.name() : field; final StringBuilder order = new StringBuilder(); if (column == null || column.table() == null || column.table().length() == 0) { order.append(DbUtil.getTableName(clazz)); } else { order.append(column.table()); } order.append(".").append(name).append(ascending ? " ASC " : " DESC "); if (_orderBy == null) { _orderBy = order.insert(0, " ORDER BY ").toString(); } else { _orderBy = order.insert(0, _orderBy + ", ").toString(); } }
@Test public void getAllFieldsForClassWithBarWithoutFoo() throws NoSuchFieldException, SecurityException { Set<Field> barFields = ReflectUtil.getAllFieldsForClass(Bar.class, new Class<?>[] {Foo.class}); Assert.assertNotNull(barFields); Assert.assertTrue(barFields.contains(Bar.class.getDeclaredField("barField"))); Assert.assertTrue(barFields.contains(Bar.class.getDeclaredField("barIntField"))); }