Esempio n. 1
0
 @Override
 public Iterable<Object> getInstances(Object instance, String property) {
   List<Object> result = new ArrayList<Object>();
   for (Value entity : graph.values(getValue(instance), Values.iri(property)))
     result.add(getInstance(entity));
   return result;
 }
Esempio n. 2
0
 @Override
 public Iterable<Object> getInstances(String property, String value) {
   List<Object> result = new ArrayList<Object>();
   for (Value entity : graph.project(Values.iri(property), Values.iri(value)))
     result.add(getInstance(entity));
   return result;
 }
Esempio n. 3
0
 @Override
 public String getValue(Object instance, String property) {
   Value value = graph.first(getValue(instance), Values.iri(property));
   if (value == null) return null;
   if (value.getString() != null) return value.getString();
   return value.toIri();
 }
Esempio n. 4
0
 @Override
 public Iterable<String> getValues(Object instance, String property) {
   List<String> result = new ArrayList<String>();
   for (Value value : graph.values(getValue(instance), Values.iri(property))) {
     if (value.getString() != null) result.add(value.getString());
     else result.add(value.toIri());
   }
   return result;
 }
Esempio n. 5
0
 private Object getInstance(Value entity) {
   Object instance = instances.get(entity);
   if (instance != null) return instance;
   Value implementation = null;
   for (Value impl :
       graph.values(entity, Values.iri("http://purl.org/openapp/server/implementation")))
     if ("http://purl.org/openapp/server/javaFQName".equals(impl.getType())) implementation = impl;
   try {
     if (implementation == null) {
       instance = new Object();
     } else {
       logger.info("Instantiating " + implementation.getString());
       instance = Class.forName(implementation.getString()).newInstance();
     }
     instances.put(entity, instance);
     entities.put(instance, entity);
     if (entity == applicationEntity) applicationInstance = instance;
     if (instance instanceof Bindable) ((Bindable) instance).bind(this);
     return instance;
   } catch (Exception e) {
     throw new Error("Error instantiating " + implementation.getString(), e);
   }
 }
Esempio n. 6
0
  public GraphBinder(String applicationClass, Object instantiator) {
    this.instantiator = instantiator;

    String home = null;
    try {
      home = System.getProperty("openapp.home");
      if (home != null)
        logger.info(
            "Binder implementation is set to value of system property openapp.home: " + home);
      else logger.info("System property openapp.home has not been set");
    } catch (SecurityException e) {
      logger.info("Getting system property openapp.home failed: " + e);
    }
    if (home == null) {
      try {
        Context ctx = new InitialContext();
        home = (String) ctx.lookup("java:comp/env/openapp/home");
        logger.info(
            "OpenApp home directory is set to value of environment variable java:comp/env/openapp/home: "
                + home);
      } catch (NamingException e) {
        logger.info("Lookup of environment variable java:comp/env/openapp/home failed: " + e);
      }
    }
    if (home == null) {
      File homeDirectoryCheck = new File("openapp");
      homeDirectory = homeDirectoryCheck.isDirectory() ? homeDirectoryCheck : null;
      if (homeDirectory != null)
        logger.info(
            "OpenApp home directory found in working directory: "
                + homeDirectory.getAbsolutePath());
    } else {
      homeDirectory = new File(home);
      if (homeDirectory.mkdirs())
        logger.info("Created OpenApp home directory: " + this.homeDirectory);
    }
    if (homeDirectory == null) {
      logger.info("OpenApp home directory is not set - will continue without a home directory");
    }

    if (homeDirectory != null) {
      File configuration = new File(homeDirectory, "application.jsonld");
      if (!configuration.exists()) {
        logger.warn(
            "There is no file named application.jsonld in the home directory - a file with default configuration will be created");
        InputStream is = null;
        OutputStream os = null;
        try {
          is = getClass().getClassLoader().getResourceAsStream("application.jsonld");
          os = new FileOutputStream(configuration);
          int read;
          while ((read = is.read()) != -1) os.write(read);
        } catch (Exception e) {
          logger.error(
              "Error copying default configuration to file application.jsonld in the home directory",
              e);
        } finally {
          try {
            if (is != null) is.close();
            if (os != null) os.close();
          } catch (IOException e) {
            logger.error(
                "Error copying default configuration to file application.jsonld in the home directory",
                e);
          }
        }
      }
      InputStream in = null;
      try {
        logger.info("Reading configuration from file application.jsonld in the home directory");
        in = new FileInputStream(configuration);
        JSONLD.parse(in, graph.toReader());
      } catch (Exception e) {
        logger.error("Could not read file application.jsonld in the home directory", e);
      } finally {
        if (in != null)
          try {
            in.close();
          } catch (IOException e) {
          }
      }
    } else {
      InputStream in = null;
      try {
        logger.info("Reading default configuration");
        in = getClass().getClassLoader().getResourceAsStream("application.jsonld");
        JSONLD.parse(in, graph.toReader());
      } catch (Exception e) {
        logger.error("Could not read default configuration file", e);
      } finally {
        if (in != null)
          try {
            in.close();
          } catch (IOException e) {
          }
      }
    }

    try {
      for (Enumeration<URL> moduleUrls =
              getClass().getClassLoader().getResources("META-INF/openapp.jsonld");
          moduleUrls.hasMoreElements(); ) {
        URL moduleUrl = moduleUrls.nextElement();
        InputStream in = null;
        try {
          in = moduleUrl.openStream();
          JSONLD.parse(in, graph.toReader());
        } catch (Exception e) {
          logger.error("Could not read classpath resource " + moduleUrl, e);
        } finally {
          if (in != null)
            try {
              in.close();
            } catch (IOException e) {
            }
        }
      }
    } catch (Exception e) {
      logger.error("Error finding resources named META-INF/openapp.jsonld in classpath", e);
    }

    for (Value entity :
        graph.project(
            Values.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
            Values.iri(applicationClass))) {
      applicationEntity = entity;
      if (homeDirectory != null)
        graph.add(
            Collections.singleton(
                Values.triple(
                    applicationEntity,
                    Values.iri("http://purl.org/openapp/server/homeDirectory"),
                    Values.string(homeDirectory.getPath()))));
      getInstance(applicationEntity);
      return;
    }
    throw new Error("Subject definition of application not found");
  }
Esempio n. 7
0
 @Override
 public Object getInstance(String property, String value) {
   for (Value entity : graph.project(Values.iri(property), Values.iri(value)))
     return getInstance(entity);
   return null;
 }
Esempio n. 8
0
 @Override
 public Object getInstance(Object instance, String property) {
   Value entity = graph.first(getValue(instance), Values.iri(property));
   return entity == null ? null : getInstance(entity);
 }
Esempio n. 9
0
 @Override
 public Iterable<String> getProperties(Object instance) {
   List<String> result = new ArrayList<String>();
   for (Value property : graph.properties(getValue(instance))) result.add(property.toIri());
   return result;
 }