/** * Appends the contents of one or more files or resources to an transaction. * * @param containers The comma-separated list of files or resources to load * the container configurations from. * @param host The hostname where Shindig is running. * @param port The port number where Shindig is receiving requests. * @param transaction The transaction to add the contents of the file to. * @return The transaction, to allow chaining. * @throws ContainerConfigException If there was a problem reading the files. */ public static Transaction addToTransactionFromFile( String containers, String host, String port, String contextRoot, Transaction transaction) throws ContainerConfigException { List<Map<String, Object>> config = loadContainers(containers); addHostAndPortToDefaultContainer(config, host, port,contextRoot); addContainersToTransaction(transaction, config); return transaction; }
/** * Parses a container in JSON notation. * * @param json The container configuration in JSON notation. * @return A parsed container configuration. */ public static Map<String, Object> parseJsonContainer(JSONObject json) { return jsonToMap(json); }
/** * Creates a new configuration from files. * @throws ContainerConfigException */ @Inject public JsonContainerConfig(@Named("shindig.containers.default") String containers, @Nullable @Named("shindig.host") String host, @Nullable @Named("shindig.port") String port, @Nullable @Named("shindig.contextroot") String contextRoot, Expressions expressions) throws ContainerConfigException { super(expressions); JsonContainerConfigLoader.getTransactionFromFile(containers, host, port, contextRoot, this).commit(); }
/** * Convert a JSON value to a configuration value. */ private static Object jsonToConfig(Object json) { if (JSONObject.NULL.equals(json)) { return null; } else if (json instanceof CharSequence) { return json.toString(); } else if (json instanceof JSONArray) { JSONArray jsonArray = (JSONArray) json; ImmutableList.Builder<Object> values = ImmutableList.builder(); for (int i = 0, j = jsonArray.length(); i < j; ++i) { values.add(jsonToConfig(jsonArray.opt(i))); } return values.build(); } else if (json instanceof JSONObject) { return jsonToMap((JSONObject) json); } // A (boxed) primitive. return json; }
loadFiles(file.listFiles(), all); } else if (file.getName().toLowerCase(Locale.ENGLISH).endsWith(".js") || file.getName().toLowerCase(Locale.ENGLISH).endsWith(".json")) { "The file '" + file.getAbsolutePath() + "' doesn't exist."); all.add(loadFromString(ResourceLoader.getContent(file))); } else { if (LOG.isLoggable(Level.FINEST))
loadResources(CRLF_SPLITTER.split(ResourceLoader.getContent(location)), all); } else { loadResources(ImmutableList.of(location), all); loadFiles(new File[] {file}, all);
/** * Creates a transaction to append the contents of one or more files or * resources to an existing configuration. * * @param containers The comma-separated list of files or resources to load * the container configurations from. * @param host The hostname where Shindig is running. * @param port The port number where Shindig is receiving requests. * @param contextRoot contextRoot where Shindig module is deployed * @param containerConfig The container configuration to add the contents of * the file to. * @return A transaction to add the new containers to the configuration. * @throws ContainerConfigException If there was a problem reading the files. */ public static Transaction getTransactionFromFile( String containers, String host, String port, String contextRoot,ContainerConfig containerConfig) throws ContainerConfigException { return addToTransactionFromFile(containers, host, port, contextRoot,containerConfig.newTransaction()); }
/** * Loads resources recursively. * * @param files The base paths to look for container.xml * @throws ContainerConfigException when IO errors occur */ private static void loadResources(Iterable<String> files, List<Map<String, Object>> all) throws ContainerConfigException { try { for (String entry : files) { if (LOG.isLoggable(Level.INFO)) { LOG.logp(Level.INFO, classname, "loadResources", MessageKeys.READING_CONFIG, new Object[] {entry}); } String content = ResourceLoader.getContent(entry); if (content == null || content.length() == 0) throw new IOException("The file " + entry + "is empty"); all.add(loadFromString(content)); } } catch (IOException e) { throw new ContainerConfigException(e); } }
private static Map<String, Object> jsonToMap(JSONObject json) { String[] keys = JSONObject.getNames(json); if (keys == null) { return ImmutableMap.of(); } Map<String, Object> values = new HashMap<String, Object>(json.length(), 1); for (String key : keys) { Object val = jsonToConfig(json.opt(key)); //If this is a string see if its a pointer to an external resource, and if so, load the resource if (val instanceof String) { String stringVal = (String) val; if (stringVal.startsWith(ResourceLoader.RESOURCE_PREFIX) || stringVal.startsWith(ResourceLoader.FILE_PREFIX)) { try { val = IOUtils.toString(ResourceLoader.open(stringVal), "UTF-8"); } catch (IOException e) { if (LOG.isLoggable(Level.WARNING)) { LOG.logp(Level.WARNING, classname, "jsonToMap", MessageKeys.READING_CONFIG, e); } } } } values.put(key, val); } return Collections.unmodifiableMap(values); }
/** * Creates a new configuration from files. * @throws ContainerConfigException */ @Inject public JsonContainerConfig(@Named("shindig.containers.default") String containers, @Nullable @Named("shindig.host") String host, @Nullable @Named("shindig.port") String port, @Nullable @Named("shindig.contextroot") String contextRoot, Expressions expressions) throws ContainerConfigException { super(expressions); JsonContainerConfigLoader.getTransactionFromFile(containers, host, port, contextRoot, this).commit(); }
/** * Convert a JSON value to a configuration value. */ private static Object jsonToConfig(Object json) { if (JSONObject.NULL.equals(json)) { return null; } else if (json instanceof CharSequence) { return json.toString(); } else if (json instanceof JSONArray) { JSONArray jsonArray = (JSONArray) json; ImmutableList.Builder<Object> values = ImmutableList.builder(); for (int i = 0, j = jsonArray.length(); i < j; ++i) { values.add(jsonToConfig(jsonArray.opt(i))); } return values.build(); } else if (json instanceof JSONObject) { return jsonToMap((JSONObject) json); } // A (boxed) primitive. return json; }
loadFiles(file.listFiles(), all); } else if (file.getName().toLowerCase(Locale.ENGLISH).endsWith(".js") || file.getName().toLowerCase(Locale.ENGLISH).endsWith(".json")) { "The file '" + file.getAbsolutePath() + "' doesn't exist."); all.add(loadFromString(ResourceLoader.getContent(file))); } else { if (LOG.isLoggable(Level.FINEST))
loadResources(CRLF_SPLITTER.split(ResourceLoader.getContent(location)), all); } else { loadResources(ImmutableList.of(location), all); loadFiles(new File[] {file}, all);
/** * Creates a transaction to append the contents of one or more files or * resources to an existing configuration. * * @param containers The comma-separated list of files or resources to load * the container configurations from. * @param host The hostname where Shindig is running. * @param port The port number where Shindig is receiving requests. * @param contextRoot contextRoot where Shindig module is deployed * @param containerConfig The container configuration to add the contents of * the file to. * @return A transaction to add the new containers to the configuration. * @throws ContainerConfigException If there was a problem reading the files. */ public static Transaction getTransactionFromFile( String containers, String host, String port, String contextRoot,ContainerConfig containerConfig) throws ContainerConfigException { return addToTransactionFromFile(containers, host, port, contextRoot,containerConfig.newTransaction()); }
/** * Loads resources recursively. * * @param files The base paths to look for container.xml * @throws ContainerConfigException when IO errors occur */ private static void loadResources(Iterable<String> files, List<Map<String, Object>> all) throws ContainerConfigException { try { for (String entry : files) { if (LOG.isLoggable(Level.INFO)) { LOG.logp(Level.INFO, classname, "loadResources", MessageKeys.READING_CONFIG, new Object[] {entry}); } String content = ResourceLoader.getContent(entry); if (content == null || content.length() == 0) throw new IOException("The file " + entry + "is empty"); all.add(loadFromString(content)); } } catch (IOException e) { throw new ContainerConfigException(e); } }
private static Map<String, Object> jsonToMap(JSONObject json) { String[] keys = JSONObject.getNames(json); if (keys == null) { return ImmutableMap.of(); } Map<String, Object> values = new HashMap<String, Object>(json.length(), 1); for (String key : keys) { Object val = jsonToConfig(json.opt(key)); //If this is a string see if its a pointer to an external resource, and if so, load the resource if (val instanceof String) { String stringVal = (String) val; if (stringVal.startsWith(ResourceLoader.RESOURCE_PREFIX) || stringVal.startsWith(ResourceLoader.FILE_PREFIX)) { try { val = IOUtils.toString(ResourceLoader.open(stringVal), "UTF-8"); } catch (IOException e) { if (LOG.isLoggable(Level.WARNING)) { LOG.logp(Level.WARNING, classname, "jsonToMap", MessageKeys.READING_CONFIG, e); } } } } values.put(key, val); } return Collections.unmodifiableMap(values); }
/** * Appends the contents of one or more files or resources to an transaction. * * @param containers The comma-separated list of files or resources to load * the container configurations from. * @param host The hostname where Shindig is running. * @param port The port number where Shindig is receiving requests. * @param transaction The transaction to add the contents of the file to. * @return The transaction, to allow chaining. * @throws ContainerConfigException If there was a problem reading the files. */ public static Transaction addToTransactionFromFile( String containers, String host, String port, String contextRoot, Transaction transaction) throws ContainerConfigException { List<Map<String, Object>> config = loadContainers(containers); addHostAndPortToDefaultContainer(config, host, port,contextRoot); addContainersToTransaction(transaction, config); return transaction; }
/** * Creates a new configuration from files. * @throws ContainerConfigException */ @Inject public JsonContainerConfig(@Named("shindig.containers.default") String containers, @Nullable @Named("shindig.host") String host, @Nullable @Named("shindig.port") String port, @Nullable @Named("shindig.contextroot") String contextRoot, Expressions expressions) throws ContainerConfigException { super(expressions); JsonContainerConfigLoader.getTransactionFromFile(containers, host, port, contextRoot, this).commit(); }
/** * Convert a JSON value to a configuration value. */ private static Object jsonToConfig(Object json) { if (JSONObject.NULL.equals(json)) { return null; } else if (json instanceof CharSequence) { return json.toString(); } else if (json instanceof JSONArray) { JSONArray jsonArray = (JSONArray) json; ImmutableList.Builder<Object> values = ImmutableList.builder(); for (int i = 0, j = jsonArray.length(); i < j; ++i) { values.add(jsonToConfig(jsonArray.opt(i))); } return values.build(); } else if (json instanceof JSONObject) { return jsonToMap((JSONObject) json); } // A (boxed) primitive. return json; }
loadFiles(file.listFiles(), all); } else if (file.getName().toLowerCase(Locale.ENGLISH).endsWith(".js") || file.getName().toLowerCase(Locale.ENGLISH).endsWith(".json")) { "The file '" + file.getAbsolutePath() + "' doesn't exist."); all.add(loadFromString(ResourceLoader.getContent(file))); } else { if (LOG.isLoggable(Level.FINEST))