Beispiel #1
0
 public static byte[] input(String fileName) {
   try {
     return input(Files.newInputStream(IO.path(fileName)));
   } catch (IOException e) {
     return Exceptions.handle(byte[].class, e);
   }
 }
Beispiel #2
0
  @Test
  public void testFileResources() throws Exception {
    String someResource = "/org/node/resource.txt";

    File file = new File("files/node-1.0-SNAPSHOT.jar");
    URL url1 = file.getAbsoluteFile().toURI().toURL();
    URL url2 = new File("files/invoke-1.0-SNAPSHOT.jar").getAbsoluteFile().toURI().toURL();

    URLClassLoader loader = new URLClassLoader(new URL[] {url1, url2});

    final List<String> resourcePaths = Classpaths.listFromClassLoader(loader, someResource);

    int fileCount = 0;
    int dirCount = 0;
    for (String path : resourcePaths) {
      if (!Files.isDirectory(IO.path(path))) {
        fileCount++;
      } else {
        dirCount++;
      }
    }

    boolean ok = true;

    ok |= dirCount == 0 || die();

    //        ok |= fileCount == 2 || die();

  }
Beispiel #3
0
  @Test
  public void testResourcesFromPath() throws Exception {
    final List<String> paths = Classpaths.resources(this.getClass(), "/org/node/file1.txt");

    String path = paths.get(0);

    boolean ok = true;

    ok |= Str.in("abc", IO.read(path)) || die();
  }
Beispiel #4
0
  @Test
  public void test2NoRoot() throws Exception {
    final List<URL> urls = Classpaths.classpathResources(this.getClass(), "org/node/file1.txt");

    URL url = urls.get(0);

    boolean ok = true;

    ok |= Str.in("abc", IO.read(url.openStream())) || die();
  }
Beispiel #5
0
  @Test
  public void testFileResources2() throws Exception {
    String someResource = "/org/node/resource.txt";

    File file = new File("files/node-1.0-SNAPSHOT.jar");
    URL url1 = file.getAbsoluteFile().toURI().toURL();
    URL url2 = new File("files/invoke-1.0-SNAPSHOT.jar").getAbsoluteFile().toURI().toURL();

    URLClassLoader loader = new URLClassLoader(new URL[] {url1, url2});

    final List<String> resourcePaths = Classpaths.listFromClassLoader(loader, someResource);

    List<Path> list = IO.paths("classpath://org/node/");

    // List<Path> list = Classpaths.pathsFromClassLoader(loader, someResource);

    puts(multiply('-', 10), "Path ");
    for (Path path : list) {

      puts(path, path.getFileSystem(), path.getClass().getName());
      if (path.toString().endsWith(".txt")) {
        puts(IO.readPath(path));
      }
    }

    puts(multiply('-', 10), "String Path ");
    List<String> slist = IO.list("classpath://org/node/");

    for (String spath : slist) {

      puts(spath);
      if (spath.toString().endsWith(".txt")) {
        puts(IO.readResource(spath));
      }
    }
  }
  public static DataStoreClientConfig load() {

    puts("Config for data store client", FILE_LOCATION);

    if (IO.exists(FILE_LOCATION)) {
      try {
        return new JsonParserFactory()
            .create()
            .parseFile(DataStoreClientConfig.class, FILE_LOCATION);
      } catch (Exception ex) {
        Exceptions.handle(
            ex, "Unable to read config file", FILE_LOCATION, "for data store client config");
        return null;
      }
    } else {

      puts("WARNING", FILE_LOCATION, "does not exist for data store client config!!!");
      return new DataStoreClientConfig();
    }
  }
Beispiel #7
0
  private static List<String> listFromDefaultClassLoader(String s) {
    List<String> result = new ArrayList<>();

    String newPath = s;

    final List<Path> resources = Classpaths.resources(IO.class, newPath);

    for (Path resourcePath : resources) {
      if (Files.isDirectory(resourcePath)) {
        result.addAll(IO.list(resourcePath));
      } else {
        result.add(resourcePath.toString());
      }
    }

    //        for ( int index = 0; index < result.size(); index++ ) {
    //            result.set( index, "classpath:" + result.get( index ) );
    //        }

    return result;
  }
Beispiel #8
0
 public static void output(Path file, byte[] bytes) {
   IO.write(file, bytes);
 }
Beispiel #9
0
 @Override
 public Path apply(String s) {
   return IO.path(s);
 }
Beispiel #10
0
  public static <T> T coerce(TypeType coerceTo, Class<T> clz, Object value) {
    if (value == null) {
      if (coerceTo != TypeType.INSTANCE && !clz.isPrimitive()) {

        return null;
      } else if (clz.isPrimitive()) {
        if (clz == boolean.class) {
          return (T) (Boolean) false;
        }
        return (T) (Number) 0;
      }
    }

    switch (coerceTo) {
      case STRING:
      case CHAR_SEQUENCE:
        return (T) value.toString();

      case NUMBER:
        if (value instanceof Number) {
          return (T) value;
        } else {
          Double d = toDouble(value);
          return (T) d;
        }

      case INT:
      case INTEGER_WRAPPER:
        Integer i = toInt(value);
        return (T) i;

      case SHORT:
      case SHORT_WRAPPER:
        Short s = toShort(value);
        return (T) s;

      case BYTE:
      case BYTE_WRAPPER:
        Byte by = toByte(value);
        return (T) by;

      case CHAR:
      case CHAR_WRAPPER:
        Character ch = toChar(value);
        return (T) ch;

      case LONG:
      case LONG_WRAPPER:
        Long l = toLong(value);
        return (T) l;

      case DOUBLE:
      case DOUBLE_WRAPPER:
        Double d = toDouble(value);
        return (T) d;

      case FLOAT:
      case FLOAT_WRAPPER:
        Float f = toFloat(value);
        return (T) f;

      case DATE:
        return (T) toDate(value);

      case BIG_DECIMAL:
        return (T) toBigDecimal(value);

      case BIG_INT:
        return (T) toBigInteger(value);

      case CALENDAR:
        return (T) toCalendar(toDate(value));

      case BOOLEAN:
      case BOOLEAN_WRAPPER:
        return (T) (Boolean) toBoolean(value);

      case MAP:
        return (T) toMap(value);

      case ARRAY:
      case ARRAY_INT:
      case ARRAY_BYTE:
      case ARRAY_SHORT:
      case ARRAY_FLOAT:
      case ARRAY_DOUBLE:
      case ARRAY_LONG:
      case ARRAY_STRING:
      case ARRAY_OBJECT:
        return toPrimitiveArrayIfPossible(clz, value);

      case LIST:
      case SET:
      case COLLECTION:
        return toCollection(clz, value);

      case INSTANCE:
        if (value instanceof Value) {
          value = ((Value) value).toValue();
        }
        if (value instanceof Map) {
          return MapObjectConversion.fromMap((Map<String, Object>) value, clz);
        } else if (value instanceof List) {
          return MapObjectConversion.fromList((List<Object>) value, clz);
        } else if (clz.isInstance(value)) {
          return (T) value;
        } else {
          return createFromArg(clz, value);
        }

      case ENUM:
        return (T) toEnum((Class<? extends Enum>) clz, value);

      case PATH:
        return (T) IO.path(value.toString());

      case CLASS:
        return (T) toClass(value);

      case TIME_ZONE:
        return (T) toTimeZone(value);

      case UUID:
        return (T) toUUID(value);

      case CURRENCY:
        return (T) toCurrency(value);

      case OBJECT:
        return (T) value;

      case HANDLER:
        return (T) value;

      default:
        return createFromArg(clz, value);
    }
  }
  private Object parse(String str) throws Exception {

    try (InputStream inputStream = Files.newInputStream(IO.path(str))) {
      return new JSONParser(JSONParser.DEFAULT_PERMISSIVE_MODE).parse(inputStream);
    }
  }