Example usage for javax.json JsonReader close

List of usage examples for javax.json JsonReader close

Introduction

In this page you can find the example usage for javax.json JsonReader close.

Prototype

@Override
void close();

Source Link

Document

Closes this reader and frees any resources associated with the reader.

Usage

From source file:org.apache.johnzon.maven.plugin.ExampleToModelMojo.java

private void generate(final JsonReaderFactory readerFactory, final File source, final Writer writer,
        final String javaName) throws MojoExecutionException {
    JsonReader reader = null;
    try {/*  ww w  . ja  va2 s .  c  om*/
        reader = readerFactory.createReader(new FileReader(source));
        final JsonStructure structure = reader.read();
        if (JsonArray.class.isInstance(structure) || !JsonObject.class.isInstance(structure)) { // quite redundant for now but to avoid surprises in future
            throw new MojoExecutionException(
                    "This plugin doesn't support array generation, generate the model (generic) and handle arrays in your code");
        }

        final JsonObject object = JsonObject.class.cast(structure);
        final Collection<String> imports = new TreeSet<String>();

        // while we browse the example tree just store imports as well, avoids a 2 passes processing duplicating imports logic
        final StringWriter memBuffer = new StringWriter();
        generateFieldsAndMethods(memBuffer, object, "    ", imports);

        if (header != null) {
            writer.write(header);
            writer.write('\n');
        }

        writer.write("package " + packageBase + ";\n\n");

        if (!imports.isEmpty()) {
            for (final String imp : imports) {
                writer.write("import " + imp + ";\n");
            }
            writer.write('\n');
        }

        writer.write("public class " + javaName + " {\n");
        writer.write(memBuffer.toString());
        writer.write("}\n");
    } catch (final IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:fr.ortolang.diffusion.core.CoreServiceBean.java

@TransactionAttribute(TransactionAttributeType.SUPPORTS)
private void builtPublicationMap(String key, Map<String, Map<String, List<String>>> map,
        Map<String, List<String>> current, Map<String, String> params)
        throws KeyNotFoundException, CoreServiceException, OrtolangException {
    Object object = readObject(key);
    if (object instanceof MetadataSource) {
        MetadataElement mde = ((MetadataSource) object).findMetadataByName(MetadataFormat.ACL);
        if (mde != null) {
            LOGGER.log(Level.FINE, "ACL metadata found, load json, find policy template and render it...");
            MetadataObject md = (MetadataObject) readObject(mde.getKey());
            try {
                JsonReader reader = Json.createReader(binarystore.get(md.getStream()));
                JsonObject json = reader.readObject();
                String template = json.getString("template");
                reader.close();
                AuthorisationPolicyTemplate policy = authorisation.getPolicyTemplate(template);
                Map<String, List<String>> rules = authorisation.getPolicyRules(policy.getTemplate());
                Map<String, List<String>> filtered = new HashMap<String, List<String>>();
                for (Entry<String, List<String>> entry : rules.entrySet()) {
                    if (params.containsKey(entry.getKey())) {
                        filtered.put(params.get(entry.getKey()), entry.getValue());
                    } else {
                        filtered.put(entry.getKey(), entry.getValue());
                    }// w w w.j a  v  a 2  s  .  c o m
                }
                current = filtered;
            } catch (AuthorisationServiceException | BinaryStoreServiceException | DataNotFoundException e) {
                LOGGER.log(Level.SEVERE, "unable to read acl metadata", e);
            }
        }
    }
    map.put(key, current);
    if (object instanceof MetadataSource) {
        for (MetadataElement element : ((MetadataSource) object).getMetadatas()) {
            map.put(element.getKey(), current);
        }
    }
    if (object instanceof Collection) {
        for (CollectionElement element : ((Collection) object).getElements()) {
            builtPublicationMap(element.getKey(), map, current, params);
        }
    }
}

From source file:fr.ortolang.diffusion.core.CoreServiceBean.java

@TransactionAttribute(TransactionAttributeType.SUPPORTS)
private void buildHandleList(String wsalias, String tag, String key, Set<OrtolangObjectPid> pids,
        PathBuilder path, String apiUrlBase, String marketUrlBase)
        throws CoreServiceException, KeyNotFoundException, OrtolangException, InvalidPathException {
    try {/*from ww  w.j  a  v  a2  s  .c om*/
        OrtolangObject object = findObject(key);
        LOGGER.log(Level.FINE, "Generating pid for key: " + key);
        String target = ((path.isRoot()) ? marketUrlBase : apiUrlBase) + "/" + wsalias + "/" + tag
                + ((path.isRoot()) ? "" : path.build());
        String dynHandle = OrtolangConfig.getInstance().getProperty(OrtolangConfig.Property.HANDLE_PREFIX) + "/"
                + wsalias + ((path.isRoot()) ? "" : path.build());
        String staticHandle = OrtolangConfig.getInstance().getProperty(OrtolangConfig.Property.HANDLE_PREFIX)
                + "/" + wsalias + "/" + tag + ((path.isRoot()) ? "" : path.build());
        OrtolangObjectPid dpid = new OrtolangObjectPid(OrtolangObjectPid.Type.HANDLE, dynHandle, key, target,
                false);
        boolean adddpid = true;
        for (OrtolangObjectPid pid : pids) {
            if (pid.getName().equals(dpid.getName()) && pid.isUserbased()) {
                adddpid = false;
                break;
            }
        }
        if (adddpid) {
            pids.add(dpid);
        }
        OrtolangObjectPid spid = new OrtolangObjectPid(OrtolangObjectPid.Type.HANDLE, staticHandle, key, target,
                false);
        boolean addspid = true;
        for (OrtolangObjectPid pid : pids) {
            if (pid.getName().equals(spid.getName()) && pid.isUserbased()) {
                addspid = false;
                break;
            }
        }
        if (addspid) {
            pids.add(spid);
        }
        if (object instanceof MetadataSource) {
            MetadataElement mde = ((MetadataSource) object).findMetadataByName(MetadataFormat.PID);
            if (mde != null) {
                LOGGER.log(Level.FINE, "PID metadata found, load json and generate corresponding pids");
                MetadataObject md = readMetadataObject(mde.getKey());
                try {
                    JsonReader reader = Json.createReader(binarystore.get(md.getStream()));
                    JsonObject json = reader.readObject();
                    if (json.containsKey("pids")) {
                        JsonArray jpids = json.getJsonArray("pids");
                        for (int i = 0; i < jpids.size(); i++) {
                            JsonObject jpid = jpids.getJsonObject(i);
                            LOGGER.log(Level.FINE, "Generating metadata based pid for key: " + key);
                            String ctarget = ((path.isRoot()) ? marketUrlBase : apiUrlBase) + "/" + wsalias
                                    + "/" + tag + ((path.isRoot()) ? "" : path.build());
                            OrtolangObjectPid upid = new OrtolangObjectPid(OrtolangObjectPid.Type.HANDLE,
                                    jpid.getString("value"), key, ctarget, true);
                            Iterator<OrtolangObjectPid> iter = pids.iterator();
                            while (iter.hasNext()) {
                                OrtolangObjectPid pid = iter.next();
                                if (pid.getName().equals(upid.getName())) {
                                    iter.remove();
                                }
                            }
                            pids.add(upid);
                        }
                    }
                    reader.close();
                } catch (BinaryStoreServiceException | DataNotFoundException e) {
                    LOGGER.log(Level.SEVERE, "unable to read pid metadata", e);
                }
            }
        }
        if (object instanceof Collection) {
            for (CollectionElement element : ((Collection) object).getElements()) {
                buildHandleList(wsalias, tag, element.getKey(), pids, path.clone().path(element.getName()),
                        apiUrlBase, marketUrlBase);
            }
        }
    } catch (AccessDeniedException e) {
        LOGGER.log(Level.INFO, "Unable to generate a PID for an object that has been set private.");
    }
}