Example usage for java.io PrintStream print

List of usage examples for java.io PrintStream print

Introduction

In this page you can find the example usage for java.io PrintStream print.

Prototype

public void print(Object obj) 

Source Link

Document

Prints an object.

Usage

From source file:org.ejbca.util.CertTools.java

/**
 * Reads a certificate in PEM-format from an InputStream. The stream may contain other things,
 * the first certificate in the stream is read.
 *
 * @param certstream the input stream containing the certificate in PEM-format
 * @return Ordered Collection of Certificate, first certificate first, or empty Collection
 * @exception IOException if the stream cannot be read.
 * @exception CertificateException if the stream does not contain a correct certificate.
 *///  ww w  .  ja  va 2  s . c o m
public static Collection<Certificate> getCertsFromPEM(InputStream certstream)
        throws IOException, CertificateException {
    if (log.isTraceEnabled()) {
        log.trace(">getCertfromPEM");
    }
    ArrayList<Certificate> ret = new ArrayList<Certificate>();
    String beginKeyTrust = "-----BEGIN TRUSTED CERTIFICATE-----";
    String endKeyTrust = "-----END TRUSTED CERTIFICATE-----";
    BufferedReader bufRdr = null;
    ByteArrayOutputStream ostr = null;
    PrintStream opstr = null;
    try {
        bufRdr = new BufferedReader(new InputStreamReader(certstream));
        while (bufRdr.ready()) {
            ostr = new ByteArrayOutputStream();
            opstr = new PrintStream(ostr);
            String temp;
            while ((temp = bufRdr.readLine()) != null
                    && !(temp.equals(CertTools.BEGIN_CERTIFICATE) || temp.equals(beginKeyTrust))) {
                continue;
            }
            if (temp == null) {
                if (ret.isEmpty()) {
                    // There was no certificate in the file
                    throw new IOException("Error in " + certstream.toString() + ", missing "
                            + CertTools.BEGIN_CERTIFICATE + " boundary");
                } else {
                    // There were certificates, but some blank lines or something in the end
                    // anyhow, the file has ended so we can break here.
                    break;
                }
            }
            while ((temp = bufRdr.readLine()) != null
                    && !(temp.equals(CertTools.END_CERTIFICATE) || temp.equals(endKeyTrust))) {
                opstr.print(temp);
            }
            if (temp == null) {
                throw new IOException("Error in " + certstream.toString() + ", missing "
                        + CertTools.END_CERTIFICATE + " boundary");
            }
            opstr.close();

            byte[] certbuf = Base64.decode(ostr.toByteArray());
            ostr.close();
            // Phweeew, were done, now decode the cert from file back to Certificate object
            Certificate cert = getCertfromByteArray(certbuf);
            ret.add(cert);
        }
    } finally {
        if (bufRdr != null) {
            bufRdr.close();
        }
        if (opstr != null) {
            opstr.close();
        }
        if (ostr != null) {
            ostr.close();
        }
    }
    if (log.isTraceEnabled()) {
        log.trace("<getcertfromPEM:" + ret.size());
    }
    return ret;
}

From source file:gov.nasa.ensemble.dictionary.nddl.ParseInterpreter.java

/**
 * Function that writes out the passive compatibilities into the model file
 * specified by the given output stream and based on a given Activity
 * Dictionary that has already been parsed
 * //www . j a va  2s  .  c  o m
 * @param shortName
 *            of oStrm
 */

public void writePassiveCompats(OutputStream oStrm) {
    PrintStream out = new PrintStream(oStrm);
    String startVar;
    String endVar;
    String qVar;
    String stateName;
    String state;
    String atStartValue;
    String atEndValue;
    String claimName;
    @SuppressWarnings("unused")
    String objrefName;
    String shareName;
    List<String> allowedValues;

    // first handle the incon activity, then all others
    out.print("InitialConds::incon {\n" + "  if (scheduled == true) {\n"
            + "    if (Enable_Passive_Checking == true) {\n" + "        eq(inconStart, start);\n\n");
    // PHM 05/10/2011 Declare the negated states as locals if they exist
    for (String stat : stateNames) {
        Set<String> stateNotValues = stateNotValuesMap.get(stat);
        if (stateNotValues != null && stateNotValues.size() > 0) {
            for (String val : stateNotValues) {
                out.printf("    float \t _not_%s_%s;\n", NDDLUtil.escape(stat), NDDLUtil.escape(val));
            }
        }
    }
    // PHM 05/10/2011 Set the negated state values
    for (String stat : stateNames) {
        Set<String> stateNotValues = stateNotValuesMap.get(stat);
        if (stateNotValues != null && stateNotValues.size() > 0) {
            for (String val : stateNotValues) {
                out.printf("    sum(_not_%s_%s, _%s_%s, STATE_COND_TRUE);\n", NDDLUtil.escape(stat),
                        NDDLUtil.escape(val), NDDLUtil.escape(stat), NDDLUtil.escape(val));
            }
        }
    }
    for (String claim : claimNames) {
        claimName = NDDLUtil.escape(claim);
        startVar = NDDLUtil.escape("i" + varN++);
        out.printf("\n" + "\t starts(%s.produce %s);\n" + "\t eq(%s.quantity, _%s);\n", claimName, startVar,
                startVar, claimName);
    }
    for (String share : shareNames) {
        startVar = NDDLUtil.escape("i" + varN++);
        shareName = NDDLUtil.escape(share);
        out.printf("\n" + "\t starts(%s.produce %s);\n" + "\t eq(%s.quantity, _%s);\n", shareName, startVar,
                startVar, shareName);
    }
    // note that we use state-value pairs
    // for each state, one and only one value should be TRUE
    for (String resource : stateNames) {
        List<String> stateValues = stateValuesMap.get(resource);
        if (stateValues != null && stateValues.size() > 0) {
            for (String val : stateValues) {
                String resourceName = NDDLUtil.escape(resource + "_" + val);
                startVar = NDDLUtil.escape("i" + varN++);
                out.printf("\n" + "\t starts(%s.produce %s);\n" + "\t eq(%s.quantity, _%s);\n", resourceName,
                        startVar, startVar, resourceName);
            }
        }
        // add in the negated values if they exist
        Set<String> stateNotValues = stateNotValuesMap.get(resource);
        if (stateNotValues != null && stateNotValues.size() > 0) {
            for (String val : stateNotValues) {
                String resourceName = NDDLUtil.escape("not_" + resource + "_" + val);
                startVar = NDDLUtil.escape("i" + varN++);
                out.printf("\n" + "\t starts(%s.produce %s);\n" + "\t eq(%s.quantity, _%s);\n", resourceName,
                        startVar, startVar, resourceName);
            }
        }
    }
    out.print("    }\n  }\n}\n\n");

    // handle passive compat for each dynamic object claim
    for (String objdef : objectDefNames) {
        startVar = "o" + varN++;
        endVar = "o" + varN++;
        qVar = "q" + varN++;
        out.printf(
                "\n%s::Assign_%s {\n" + "  if (isSingleton(object)) {\n" + "    if (scheduled == true) {\n"
                        + "      if (Enable_Passive_Checking == true) {\n"
                        + "          condleq(afterIncon, inconStart, start);\n" + "          float %s;\n"
                        + "          eq(%s, afterIncon);\n" + "          starts(object.passive.consume %s);\n"
                        + "          eq(%s.quantity, %s);\n\n" + "          ends(object.passive.produce %s);\n"
                        + "          eq(%s.quantity, 1.0);\n\n         }\n     }\n  }\n}\n\n",
                objdef, objdef, qVar, qVar, startVar, startVar, qVar, endVar, endVar);
    }

    // Due to the incon guard, have to handle the start transitions and end
    // transitions separately
    // ** Since we've eliminated the incon guard, this is no longer necessary,
    // but code 

    for (EActivityDef activityDef : activityDefs) {
        //         if ((activityDef.getExpansion() == null || activityDef
        //               .getExpansion().getSubActivities() == null)
        //               && (activityDef.getClaims() != null
        //                     || activityDef.getSharedReservations() != null
        //                     || (activityDef.getRequirements() != null && activityDef
        //                           .getRequirements().getStateRequirements() != null) || (activityDef
        //                     .getEffects() != null && activityDef.getEffects()
        //                     .getStateEffects() != null))) {

        if (!activityDef.getClaimableEffects().isEmpty() || !activityDef.getSharedEffects().isEmpty()
                || !activityDef.getStateRequirements().isEmpty() || !activityDef.getStateEffects().isEmpty()) {
            out.printf(
                    "%s::%s {\n" + "  if (scheduled == true) {\n"
                            + "    if (Enable_Passive_Checking == true) {\n\n",
                    activitySubsystemMap.get(NDDLUtil.escape(activityDef.getName())),
                    NDDLUtil.escape(activityDef.getName()));
            // first process only the start transitions for the activity

            // handle claims
            for (EClaimableEffect claim : activityDef.getClaimableEffects()) {
                claimName = NDDLUtil.escape(claim.getName());
                if (claimNames.contains(claimName)) {
                    startVar = "c" + varN++;
                    qVar = "q" + varN++;
                    out.printf(
                            "\n" + "          condleq(afterIncon, inconStart, start);\n"
                                    + "          float %s;\n" + "          eq(%s, afterIncon);\n"
                                    + "          starts(%s.consume %s);\n" + "          eq(%s.quantity, %s);\n",
                            qVar, qVar, claimName, startVar, startVar, qVar);
                } else {
                    System.err.print("\n* Undefined claim " + claimName + " in activity "
                            + NDDLUtil.escape(activityDef.getName()) + " *\n\n");
                }
            }
            // handle shared reservations
            for (ESharableResourceEffect share : activityDef.getSharedEffects()) {
                shareName = NDDLUtil.escape(share.getName());
                if (shareNames.contains(shareName)) {
                    startVar = "r" + varN++;
                    qVar = "q" + varN++;
                    int reservations = share.getReservations();
                    if (reservations > 0) {
                        out.printf("\n" + "          condleq(afterIncon, inconStart, start);\n"
                                + "          float %s;\n" + "          product(%s, %d, afterIncon);\n"
                                + "          starts(%s.consume %s);\n" + "          eq(%s.quantity, %s);\n",
                                qVar, qVar, reservations, shareName, startVar, startVar, qVar);
                    }
                } else {
                    System.err.print("\n* Undefined share " + shareName + " in activity "
                            + NDDLUtil.escape(activityDef.getName()) + " *\n\n");
                }
            }
            // handle state requirements
            for (EStateRequirement stateReq : activityDef.getStateRequirements()) {
                // period = 0 means RequiresThroughout; period = 1 means
                // RequiresBeforeStart
                // we only handle RequiresThroughout
                state = NDDLUtil.escape(stateReq.getName());
                if (stateNames.contains(state)) {
                    if (stateReq.getPeriod() == Period.REQUIRES_THROUGHOUT) {
                        // For requirements, Enum and Threshold states are no longer 
                        // handled identically due to negation and disjunction
                        if (stateTypesMap.get(state).equals("Enum")) {
                            if (stateReq.getRequiredState() != null) {
                                stateName = NDDLUtil.escape(state + "_" + stateReq.getRequiredState());
                                startVar = "r" + varN++;
                                qVar = "q" + varN++;
                                out.printf(
                                        "\n" + "          condleq(afterIncon, inconStart, start);\n"
                                                + "          float %s;\n" + "          eq(%s, afterIncon);\n"
                                                + "           starts(%s.consume %s);\n"
                                                + "          eq(%s.quantity, %s);\n",
                                        qVar, qVar, stateName, startVar, startVar, qVar);
                            } else if (stateReq.getDisallowedState() != null) {
                                stateName = NDDLUtil
                                        .escape("not_" + state + "_" + stateReq.getDisallowedState());
                                startVar = "r" + varN++;
                                qVar = "q" + varN++;
                                out.printf(
                                        "\n" + "          condleq(afterIncon, inconStart, start);\n"
                                                + "          float %s;\n" + "          eq(%s, afterIncon);\n"
                                                + "           starts(%s.consume %s);\n"
                                                + "          eq(%s.quantity, %s);\n",
                                        qVar, qVar, stateName, startVar, startVar, qVar);
                            } else if (stateReq.getAllowedStates() != null
                                    && stateReq.getAllowedStates().size() > 0) {
                                allowedValues = stateReq.getAllowedStates();
                                for (String val : stateValuesMap.get(state)) {
                                    if (!allowedValues.contains(val)) {
                                        stateName = NDDLUtil.escape("not_" + state + "_" + val);
                                        startVar = "r" + varN++;
                                        qVar = "q" + varN++;
                                        out.printf("\n" + "          condleq(afterIncon, inconStart, start);\n"
                                                + "          float %s;\n" + "          eq(%s, afterIncon);\n"
                                                + "           starts(%s.consume %s);\n"
                                                + "          eq(%s.quantity, %s);\n", qVar, qVar, stateName,
                                                startVar, startVar, qVar);
                                    }
                                }
                            } else {
                                System.err.print(
                                        "*Required resource " + state + " did not have a value specified*\n\n");
                            }
                        } else if (stateTypesMap.get(state).equals("Threshold")) {
                            stateName = NDDLUtil.escape(state + "_" + stateReq.getRequiredState());
                            startVar = "r" + varN++;
                            qVar = "q" + varN++;
                            out.printf(
                                    "\n" + "          condleq(afterIncon, inconStart, start);\n"
                                            + "          float %s;\n" + "          eq(%s, afterIncon);\n"
                                            + "           starts(%s.consume %s);\n"
                                            + "          eq(%s.quantity, %s);\n",
                                    qVar, qVar, stateName, startVar, startVar, qVar);
                        } else {
                            System.err.print("*Required resource " + state
                                    + " is not of type Enum nor ThresholdEnum*\n\n");
                        }
                    }
                } else {
                    System.err.print("\n* Undefined state " + state + " in activity "
                            + NDDLUtil.escape(activityDef.getName()) + " *\n\n");
                }
            }

            // handle state effects
            for (EStateResourceEffect<?> effect : activityDef.getStateEffects()) {
                state = NDDLUtil.escape(effect.getName());
                atStartValue = effect.getStartEffect();
                // for effects, Enum and Threshold states are handled
                // differently
                String stateTypeName = stateTypesMap.get(state);
                if (stateTypeName != null && stateTypeName.equals("Enum")) {
                    if (atStartValue != null) {

                        writeEnumStateEffectSection(out, state, atStartValue, "starts");

                    }
                } else if (stateTypeName != null && stateTypeName.equals("Threshold")) {
                    // we assume that there is an atStart value
                    // and that atEnd we retract the effect

                    // make all LOWER values False at start and True at
                    // end
                    for (String val : stateValuesMap.get(state)) {
                        if (val.equals(atStartValue)) {
                            break;
                        }
                        stateName = state + "_" + val;
                        startVar = "s" + varN++;
                        qVar = "q" + varN++;
                        out.printf("\n" + "          condleq(afterIncon, inconStart, start);\n"
                                + "          float %s;\n"
                                + "          product(%s, STATE_COND_FALSE, afterIncon);\n"
                                + "          starts(%s.consume %s);\n" + "          eq(%s.quantity, %s);\n",
                                qVar, qVar, stateName, startVar, startVar, qVar);
                        out.println();
                    }
                }
            }

            // now process the end transitions for the activity

            // handle claims
            for (EClaimableEffect claim : activityDef.getClaimableEffects()) {
                claimName = NDDLUtil.escape(claim.getName());
                if (claimNames.contains(claimName)) {
                    endVar = "c" + varN++;
                    out.printf("\n\t\tends(%s.produce %s);\n" + "\t\teq(%s.quantity, 1.0);\n",
                            NDDLUtil.escape(claimName), NDDLUtil.escape(endVar), NDDLUtil.escape(endVar));
                } else {
                    System.err.print("\n* Undefined claim " + claimName + " in activity "
                            + NDDLUtil.escape(activityDef.getName()) + " *\n\n");
                }
            }
            // handle shared reservations
            for (ESharableResourceEffect share : activityDef.getSharedEffects()) {
                shareName = NDDLUtil.escape(share.getName());
                if (shareNames.contains(shareName)) {
                    endVar = "r" + varN++;
                    int reservations = share.getReservations();
                    if (reservations > 0) {
                        out.printf("\n\t\tends(%s.produce %s);\n" + "\t\teq(%s.quantity, %d);\n",
                                NDDLUtil.escape(shareName), NDDLUtil.escape(endVar), NDDLUtil.escape(endVar),
                                reservations);
                    }
                } else {
                    System.err.print("\n* Undefined share " + shareName + " in activity "
                            + NDDLUtil.escape(activityDef.getName()) + " *\n\n");
                }
            }

            // handle state requirements
            for (EStateRequirement stateReq : activityDef.getStateRequirements()) {
                // period = 0 means RequiresThroughout; period = 1 means
                // RequiresBeforeStart
                // we only handle RequiresThroughout
                state = NDDLUtil.escape(stateReq.getName());
                if (stateNames.contains(state)) {
                    if (stateReq.getPeriod() == Period.REQUIRES_THROUGHOUT) {
                        // For requirements, Enum and Threshold states
                        // are no longer handled identically due to negation and disjunction
                        if (stateTypesMap.get(state).equals("Enum")) {
                            if (stateReq.getRequiredState() != null) {
                                stateName = state + "_" + stateReq.getRequiredState();
                                endVar = NDDLUtil.escape("r" + varN++);
                                out.printf("\n\t\tends(%s.produce %s);\n" + "\t\teq(%s.quantity, 1.0);\n",
                                        NDDLUtil.escape(stateName), endVar, endVar);
                                // if stateName is the CPUwindow predicate, then
                                // add the Window capability
                                if (stateName.equals(CPUwindow)) {
                                    startVar = NDDLUtil.escape("w" + varN++);
                                    out.printf(
                                            "\n\t\tany(CPU_Windows.produce %s);\n"
                                                    + "\t\teq(%s.quantity, 1.0);\n"
                                                    + "\t\ttemporalDistance(%s.time, CPU_BOOT_DUR, start);\n",
                                            startVar, startVar, startVar);
                                    endVar = NDDLUtil.escape("w" + varN++);
                                    out.printf(
                                            "\t\tany(CPU_Windows.consume %s);\n" + "\t\teq(%s.quantity, 1.0);\n"
                                                    + "\t\ttemporalDistance(end, POST_CPU_WINDOW, %s.time);\n",
                                            endVar, endVar, endVar);
                                }
                            } else if (stateReq.getDisallowedState() != null) {
                                stateName = "not_" + state + "_" + stateReq.getDisallowedState();
                                endVar = NDDLUtil.escape("r" + varN++);
                                out.printf("\n\t\tends(%s.produce %s);\n" + "\t\teq(%s.quantity, 1.0);\n",
                                        NDDLUtil.escape(stateName), endVar, endVar);
                            } else if (stateReq.getAllowedStates() != null
                                    && stateReq.getAllowedStates().size() > 0) {
                                allowedValues = stateReq.getAllowedStates();
                                for (String val : stateValuesMap.get(state)) {
                                    if (!allowedValues.contains(val)) {
                                        stateName = "not_" + state + "_" + val;
                                        endVar = NDDLUtil.escape("r" + varN++);
                                        out.printf(
                                                "\n\t\tends(%s.produce %s);\n" + "\t\teq(%s.quantity, 1.0);\n",
                                                NDDLUtil.escape(stateName), endVar, endVar);
                                    }
                                }
                            } else {
                                System.err.print(
                                        "*Required resource " + state + " did not have a value specified*\n\n");
                            }
                        } else if (stateTypesMap.get(state).equals("Threshold")) {
                            stateName = state + "_" + stateReq.getRequiredState();
                            endVar = NDDLUtil.escape("r" + varN++);
                            out.printf("\n\t\tends(%s.produce %s);\n" + "\t\teq(%s.quantity, 1.0);\n",
                                    NDDLUtil.escape(stateName), endVar, endVar);
                        }
                    }
                } else {
                    System.err.print("\n* Undefined state " + state + " in activity "
                            + NDDLUtil.escape(activityDef.getName()) + " *\n\n");
                }
            }

            // handle state effects
            for (EStateResourceEffect<?> effect : activityDef.getStateEffects()) {
                state = NDDLUtil.escape(effect.getName());
                atEndValue = effect.getEndEffect();
                // for effects, Enum and Threshold states are handled
                // differently
                String stateTypeName = stateTypesMap.get(state);
                if (stateTypeName != null && stateTypeName.equals("Enum")) {
                    if (atEndValue != null) {
                        writeEnumStateEffectSection(out, state, atEndValue, "ends");
                    }
                } else if (stateTypeName != null && stateTypeName.equals("Threshold")) {
                    // we assume that there is an atStart value
                    // and that atEnd we retract the effect

                    // make all LOWER values False at start and True at
                    // end
                    for (String val : stateValuesMap.get(state)) {
                        if (val.equals(atEndValue)) {
                            break;
                        }
                        stateName = NDDLUtil.escape(state + "_" + val);
                        endVar = NDDLUtil.escape("e" + varN++);
                        out.printf("\t\tends(%s.produce %s);\n\t\teq(%s.quantity, STATE_COND_TRUE);\n",
                                stateName, endVar, endVar);
                        out.println();
                    }
                }
            }
            out.print("\n    }\n  }\n}\n\n");
        }
    }
}

From source file:com.netscape.cms.servlet.csadmin.ConfigurationUtils.java

public static void importLDIFS(String param, LDAPConnection conn, boolean suppressErrors)
        throws IOException, EPropertyNotFound, EBaseException {
    IConfigStore cs = CMS.getConfigStore();

    logger.debug("importLDIFS: param=" + param);
    String v = cs.getString(param);

    String baseDN = cs.getString("internaldb.basedn");
    String database = cs.getString("internaldb.database");
    String instancePath = cs.getString("instanceRoot");
    String instanceId = cs.getString("instanceId");
    String cstype = cs.getString("cs.type");
    String dbuser = cs.getString("preop.internaldb.dbuser", "uid=" + DBUSER + ",ou=people," + baseDN);

    String configDir = instancePath + File.separator + cstype.toLowerCase() + File.separator + "conf";

    StringTokenizer tokenizer = new StringTokenizer(v, ",");
    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken().trim();
        int index = token.lastIndexOf("/");
        String name = token;/*from  w  w  w .  ja  va2s  .c om*/

        if (index != -1) {
            name = token.substring(index + 1);
        }

        logger.debug("importLDIFS(): ldif file = " + token);
        String filename = configDir + File.separator + name;

        logger.debug("importLDIFS(): ldif file copy to " + filename);
        PrintStream ps = null;
        BufferedReader in = null;

        in = new BufferedReader(new InputStreamReader(new FileInputStream(token), "UTF-8"));
        ps = new PrintStream(filename, "UTF-8");
        while (in.ready()) {
            String s = in.readLine();
            int n = s.indexOf("{");

            if (n == -1) {
                ps.println(s);
            } else {
                boolean endOfline = false;

                while (n != -1) {
                    ps.print(s.substring(0, n));
                    int n1 = s.indexOf("}");
                    String tok = s.substring(n + 1, n1);

                    if (tok.equals("instanceId")) {
                        ps.print(instanceId);
                    } else if (tok.equals("rootSuffix")) {
                        ps.print(baseDN);
                    } else if (tok.equals("database")) {
                        ps.print(database);
                    } else if (tok.equals("dbuser")) {
                        ps.print(dbuser);
                    }
                    if ((s.length() + 1) == n1) {
                        endOfline = true;
                        break;
                    }
                    s = s.substring(n1 + 1);
                    n = s.indexOf("{");
                }

                if (!endOfline) {
                    ps.println(s);
                }
            }
        }
        in.close();
        ps.close();

        ArrayList<String> errors = new ArrayList<String>();
        LDAPUtil.importLDIF(conn, filename, errors);
        if (!errors.isEmpty()) {
            logger.error("importLDIFS(): LDAP Errors in importing " + filename);
            for (String error : errors) {
                logger.error(error);
            }
            if (!suppressErrors) {
                throw new EBaseException("LDAP Errors in importing " + filename);
            }
        }
    }
}

From source file:com.microfocus.application.automation.tools.srf.run.RunFromSrfBuilder.java

public static JSONObject getSrfConnectionData(AbstractBuild<?, ?> build, PrintStream logger) {
    try {// www . j  a  v a  2s  . co  m
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        String path = build.getProject().getParent().getRootDir().toString();
        path = path.concat(
                "/com.microfocus.application.automation.tools.srf.settings.SrfServerSettingsBuilder.xml");
        File file = new File(path);
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(file);
        // This also shows how you can consult the global configuration of the builder
        JSONObject connectionData = new JSONObject();

        String credentialsId = document.getElementsByTagName("credentialsId").item(0).getTextContent();
        UsernamePasswordCredentials credentials = CredentialsProvider.findCredentialById(credentialsId,
                StandardUsernamePasswordCredentials.class, build, URIRequirementBuilder.create().build());

        String app = credentials.getUsername();
        String tenant = app.substring(1, app.indexOf('_'));
        String secret = credentials.getPassword().getPlainText();
        String server = document.getElementsByTagName("srfServerName").item(0).getTextContent();

        // Normalize SRF server URL string if needed
        if (server.substring(server.length() - 1).equals("/")) {
            server = server.substring(0, server.length() - 1);
        }

        boolean https = true;
        if (!server.startsWith("https://")) {
            if (!server.startsWith("http://")) {
                String tmp = server;
                server = "https://";
                server = server.concat(tmp);
            } else
                https = false;
        }
        URL urlTmp = new URL(server);
        if (urlTmp.getPort() == -1) {
            if (https)
                server = server.concat(":443");
            else
                server = server.concat(":80");
        }
        String srfProxy = "";
        String srfTunnel = "";
        try {
            srfProxy = document.getElementsByTagName("srfProxyName").item(0) != null
                    ? document.getElementsByTagName("srfProxyName").item(0).getTextContent().trim()
                    : null;
            srfTunnel = document.getElementsByTagName("srfTunnelPath").item(0) != null
                    ? document.getElementsByTagName("srfTunnelPath").item(0).getTextContent()
                    : null;
        } catch (Exception e) {
            throw e;
        }
        connectionData.put("app", app);
        connectionData.put("tunnel", srfTunnel);
        connectionData.put("secret", secret);
        connectionData.put("server", server);
        connectionData.put("https", (https) ? "True" : "False");
        connectionData.put("proxy", srfProxy);
        connectionData.put("tenant", tenant);
        return connectionData;
    } catch (ParserConfigurationException e) {
        logger.print(e.getMessage());
        logger.print("\n\r");
    } catch (SAXException | IOException e) {
        logger.print(e.getMessage());
    }
    return null;
}

From source file:de.juwimm.cms.remote.ViewServiceSpringImpl.java

/**
 * //from   w  w w.j  a va  2  s .c  o  m
 * @param doc
 * @param out
 * @param tagString can be picture or document
 * @param attribute description for picture or src for document
 * @throws Exception
 */
private void getMediaXML(Document doc, PrintStream out, String tagString, String attribute) throws Exception {
    try {
        Iterator it = XercesHelper.findNodes(doc, "//" + tagString);
        while (it.hasNext()) {
            Node node = (Node) it.next();
            int itemId = Integer.parseInt(node.getAttributes().getNamedItem(attribute).getNodeValue());
            if (itemId != 0) {
                if (tagString.equals("picture")) {
                    PictureHbm picture = getPictureHbmDao().load(itemId);
                    out.print(picture.toXml(0));
                } else if (tagString.equals("document")) {
                    out.print(getDocumentHbmDao().toXml(itemId, 0, true));
                }
            }
        }

    } catch (Exception e) {
        if (log.isDebugEnabled())
            log.debug("Error at getting " + tagString + " xml");
        throw new UserException(e.getMessage());
    }
}

From source file:gov.nasa.ensemble.dictionary.nddl.ParseInterpreter.java

/**
 * Function that writes out the objects.nddl model file to the given output
 * stream based on a given Activity Dictionary that has already been parsed
 * //  w  w w . j  a  v  a2s . c  o  m
 * @param shortName
 *            of oStrm
 */
public void writeObjects(OutputStream oStrm) {
    PrintStream out = new PrintStream(oStrm);

    // For now, global declarations in the AD are ignored
    // We first define globals used in all models
    // #include \"SaturatedResource.nddl\"\n\n"
    out.printf("string \t AD_VERSION = \t %s;\n" + "float \t STATE_COND_TRUE = \t 1000.0;\n"
            + "float \t STATE_COND_FALSE = \t 1000.0;\n" + "int \t inconStart;\n"
            + "int \t CPU_BOOT_DUR = \t %d;\n" + "int \t CPU_SHUTDOWN_DUR = \t %d;\n"
            + "int \t POST_CPU_WINDOW = \t %d;\n\n", version, CPUpre, CPUpost, CPUpost + CPUgap);

    // Define globals for active enforcement and passive checking of flight
    // rules and set each one to true to enable it by default
    out.print("\nbool \t Enable_Passive_Checking;\nEnable_Passive_Checking.specify(true);\n"
            + "bool \t Enable_Active_Enforcement;\nEnable_Active_Enforcement.specify(true);\n\n");
    for (String objdef : objectDefNames) {
        out.printf("bool \t Enforce_%s_claim;\nEnforce_%s_claim.specify(true);\n\n", objdef, objdef);
    }
    out.println();
    for (String claim : claimNames) {
        out.printf("bool \t Enforce_%s;\nEnforce_%s.specify(true);\n\n", claim, claim);
    }
    out.println();
    // PHM 12/05/2011 Different guards for resource solving
    if (ModelGenerator.getPreferences().useResourceSolvingForStateConstraints()) {
        // PHM 04/05/2012 Extra guards for resource solving
        for (String share : shareNames) {
            out.printf("bool \t Enforce_sx_%s;\nEnforce_sx_%s.specify(true);\n\n", share, share);
        }
        out.println();
        for (String state : enumActMap.keySet()) {
            for (String actName : enumActMap.get(state)) {
                out.printf("bool \t Enforce_mx_%s__%s;\nEnforce_mx_%s__%s.specify(true);\n\n", actName, state,
                        actName, state);
            }
        }
    } else { // The old timeline solving code
        for (String mutex : exclusiveActsStatesMap.keySet()) {
            for (String state : exclusiveActsStatesMap.get(mutex)) {
                out.printf("bool \t Enforce_%s__%s;\nEnforce_%s__%s.specify(true);\n\n", mutex, state, mutex,
                        state);
            }
        }
    }
    out.println();
    // globals for threshold state enforcement
    for (String state : stateNames) {
        String stateTypeName = stateTypesMap.get(state);
        if (stateTypeName != null && stateTypeName.equals("Threshold")) {
            out.printf("bool \t Enforce_%s;\nEnforce_%s.specify(true);\n\n", state, state);
        }
    }
    out.println();
    // Define class used for instance-based enable/disable of active enforcement
    // Each Activity predicate will have a parameter of this class
    out.print("\n" + "class Active_Enforcer extends Object {\n");
    for (String objdef : objectDefNames) {
        out.printf("\t bool \t Enforce_%s_claim;\n", objdef);
    }
    for (String claim : claimNames) {
        out.printf("\t bool \t Enforce_%s;\n", claim);
    }
    // PHM 12/07/2011 Different guards for resource solving
    if (ModelGenerator.getPreferences().useResourceSolvingForStateConstraints()) {
        // PHM 04/05/2012 Extra guards for resource solving
        for (String share : shareNames) {
            out.printf("\t bool \t Enforce_sx_%s;\n", share);
        }
        for (String state : enumActMap.keySet()) {
            for (String actName : enumActMap.get(state)) {
                out.printf("\t bool \t Enforce_mx_%s__%s;\n", actName, state);
            }
        }
    } else { // The old timeline solving code
        for (String mutex : exclusiveActsStatesMap.keySet()) {
            for (String state : exclusiveActsStatesMap.get(mutex)) {
                out.printf("\t bool \t Enforce_%s__%s;\n", mutex, state);
            }
        }
    }
    for (String state : stateNames) {
        String stateTypeName = stateTypesMap.get(state);
        if (stateTypeName != null && stateTypeName.equals("Threshold")) {
            out.printf("\t bool \t Enforce_%s;\n", state);
        }
    }
    out.println();
    out.print("    Active_Enforcer() {}\n}\n\n");
    // Define Claimable resources
    out.print("class Unit_Capacity_Resource extends Reservoir {" + "\n  string profileType;"
            + "\n  string detectorType;" + "\n Unit_Capacity_Resource(float initCap) { \n"
            + "\n\t super(initCap, 0.0, 10.0);" + "\n\t profileType = \"AddGroundedProfile\";"
            + "\n\t detectorType = \"PassiveFVDetector\";" + "\n\t}\n}\n");
    for (String objdef : objectDefNames) {
        out.printf(
                "class %s_claim extends Unit_Capacity_Resource {\n    %s_claim(float initCap) {\n\t super(initCap);\n\t}\n}\n",
                objdef, objdef);
    }
    for (String claim : claimNames) {
        out.printf(
                "class %s extends Unit_Capacity_Resource {\n    %s(float initCap) {\n\t super(initCap);\n\t}\n}\n",
                claim, claim);
    }
    // Define Sharable resources
    // PHM 12/08/2011 Different Flaw detector for Resource Solving
    if (ModelGenerator.getPreferences().useResourceSolvingForStateConstraints()) {
        out.print("\n\n\nclass Multi_Capacity_Resource extends Reservoir {" + "\n  string profileType;"
                + "\n  string detectorType;" + "\n    Multi_Capacity_Resource(float initCap, float maxCap) { \n"
                + "\t super(initCap, 0.0, maxCap);" + "\n\t profileType = \"AddGroundedProfile\";"
                + "\n\t detectorType = \"GroundedFlawDetector\";" + "\n\t}\n}\n");
    } else {
        out.print("\n\n\nclass Multi_Capacity_Resource extends Reservoir {" + "\n  string profileType;"
                + "\n  string detectorType;" + "\n    Multi_Capacity_Resource(float initCap, float maxCap) { \n"
                + "\t super(initCap, 0.0, maxCap);" + "\n\t profileType = \"AddGroundedProfile\";"
                + "\n\t detectorType = \"PassiveFVDetector\";" + "\n\t}\n}\n");
    }
    for (ESharableResourceDef share : allShares) {
        out.printf(
                "class %s extends Multi_Capacity_Resource {\n    %s(float initCap, float maxCap) {\n\t super(initCap, maxCap);\n\t}\n}\n",
                NDDLUtil.escape(share.getName()), NDDLUtil.escape(share.getName()));
    }

    // Define Enum and Threshold Enum state conditions
    // Note that Enum has been renamed EnumType
    // First print part of resource defintions common to all models
    // PHM 12/08/2011 Different Flaw detector for Resource Solving
    if (ModelGenerator.getPreferences().useResourceSolvingForStateConstraints()) {
        out.print("\nclass State_Condition extends Reservoir {" + "\n  string profileType;"
                + "\n  string detectorType;" + "\n    State_Condition(float initCap) {\n"
                + "\t super(initCap, 0.0, +inff);" + "\n\t profileType = \"SatGroundedProfile\";"
                + "\n\t detectorType = \"GroundedFlawDetector\";" + "\n\t}\n}\n\n"
                + "class Threshold_Condition extends Reservoir {" + "\n  string profileType;"
                + "\n  string detectorType;" + "    Threshold_Condition(float initCap) {\n"
                + "\t super(initCap, 0.0, +inff);" + "\n\t profileType = \"AddGroundedProfile\";"
                + "\n\t detectorType = \"GroundedFlawDetector\";" + "\n\t}\n}\n\n");
    } else {
        out.print("\nclass State_Condition extends Reservoir {" + "\n  string profileType;"
                + "\n  string detectorType;" + "\n    State_Condition(float initCap) {\n"
                + "\t super(initCap, 0.0, +inff);" + "\n\t profileType = \"SatGroundedProfile\";"
                + "\n\t detectorType = \"PassiveFVDetector\";" + "\n\t}\n}\n\n"
                + "class Threshold_Condition extends Reservoir {" + "\n  string profileType;"
                + "\n  string detectorType;" + "    Threshold_Condition(float initCap) {\n"
                + "\t super(initCap, 0.0, +inff);" + "\n\t profileType = \"AddGroundedProfile\";"
                + "\n\t detectorType = \"PassiveFVDetector\";" + "\n\t}\n}\n\n");
    }
    // No longer using Deleted_Precondition distinction
    // "class Deleted_Precondition extends Resource {\n" +
    // " Deleted_Precondition(float initCap) {\n" +
    // "\t super(initCap, 0.0, +inff, +inff, +inff, -inff,
    // -inff);\n\t}\n}\n\n"

    if (CPUwindow != null) {
        out.print("class CPU_Windows extends Reservoir {\n" + "    string profileType;\n"
                + "    string detectorType;\n" + "    CPU_Windows() {\n" + "\t super(0.0, -inff, +inff);\n"
                + "\t profileType = \"AddGroundedProfile\";\n" + "\t detectorType = \"PassiveFVDetector\";\n"
                + "\t}\n}\n\n");
    }

    for (String state : stateTypesMap.keySet()) {
        String name;
        if (stateTypesMap.get(state).equals("Enum")) {
            // distinguish the enum states that appear as a deleted
            // precondition
            // We cannot use this distinction because the mod-filter of
            // flaws is needed for all state conditions
            // due to the way the state effects are implemented, a
            // stateVar-value can be made false twice
            // if (deletedPreconds.contains(state)) {
            // for (String value : stateValuesMap.get(state)) {
            // name = state + "_" + value;
            // out.printf("class %s extends Deleted_Precondition {\n
            // %s(float initCap) {\n\t super(initCap);\n\t }\n}\n\n", name,
            // name);
            // }
            // } else {
            // for (String value : stateValuesMap.get(state)) {
            // name = state + "_" + value;
            // out.printf("class %s extends State_Condition {\n %s(float
            // initCap) {\n\t super(initCap);\n\t }\n}\n\n", name, name);
            // }
            // }

            for (String value : stateValuesMap.get(state)) {
                name = state + "_" + value;
                String correctName = NDDLUtil.escape(name);
                out.printf(
                        "class %s extends State_Condition {\n    %s(float initCap) {\n\t super(initCap);\n\t }\n}\n\n",
                        correctName, correctName);
            }
            // handle the states with negated values
            if (stateNotValuesMap.get(state) != null) {
                for (String value : stateNotValuesMap.get(state)) {
                    name = "not_" + state + "_" + value;
                    String correctName = NDDLUtil.escape(name);
                    out.printf(
                            "class %s extends State_Condition {\n    %s(float initCap) {\n\t super(initCap);\n\t }\n}\n\n",
                            correctName, correctName);
                }
            }
        } else if (stateTypesMap.get(state).equals("Threshold")) {
            for (String value : stateValuesMap.get(state)) {
                name = state + "_" + value;
                name = state + "_" + value;
                out.printf(
                        "class %s extends Threshold_Condition {\n    %s(float initCap) {\n\t super(initCap);\n\t }\n}\n\n",
                        name, name);
            }
        }
    }

    // define mutual exclusion timelines for claims
    for (String objdef : objectDefNames) {
        out.print("class Active_" + objdef + "_claim extends Timeline {\n" + "  Active_" + objdef
                + "_claim() {}\n" + "    predicate " + objdef + "_claim_MX {}\n}\n\n");
    }
    for (String claimNam : claimNames) {
        out.print("class Active_" + claimNam + " extends Timeline {\n" + "  Active_" + claimNam + "() {}\n"
                + "    predicate " + claimNam + "_MX {}\n}\n\n");
    }
    // define mutual exclusion timelines for threshold states
    for (EStateResourceDef state : allStates) {
        String stateName = NDDLUtil.escape(state.getName());
        String stateTypeName = stateTypesMap.get(stateName);
        if (stateTypeName != null && stateTypeName.equals("Threshold")) {
            for (String val : stateValuesMap.get(stateName)) {
                stateName = NDDLUtil.escape(state.getName()) + "_" + val;
                out.print("class Active_" + stateName + " extends Timeline {\n" + "  Active_" + stateName
                        + "() {}\n" + "    predicate GT_" + stateName + " {}\n" + "    predicate LE_"
                        + stateName + " {}\n}\n\n");
            }
        }
    }
    // PHM 12/07/2011 Will be empty for resource solving
    // define mutual exclusion timelines for enumeration state conflicts
    for (String mutex : exclusiveActsStatesMap.keySet()) {
        out.printf(
                "class Active_%s extends Timeline {\n" + "  Active_%s() {}\n" + "    predicate %s_MX {}\n}\n\n",
                mutex, mutex, mutex);
    }
    // for (String mutex : exclusiveActsStatesMap2.keySet()) {
    // out.printf("class Active_%s extends Timeline {\n" +
    // " Active_%s() {}\n" +
    // " predicate %s_MX {}\n}\n\n",
    // mutex, mutex, mutex);
    // }

    // Define Activity Types
    // All activity types must have a subsystem specified, as this is the
    // class they will belong to

    // Print the subsytem class definitions with a predicate for each
    // activity
    // Also include classes that are common to all models

    // Create a HIDDEN (sub-activity) class for each dynamic object reference
    for (String objdef : objectDefNames) {
        out.printf(
                "\nclass %s extends Object {\n" + "    %s_claim passive;\n" + "    Active_%s_claim active; \n"
                        + "  %s() {\n" + "    passive = new %s_claim(1.0);\n"
                        + "    active = new Active_%s_claim();\n" + "  } \n" + "  predicate Assign_%s {\n"
                        + "    int \t priority;\n" + "    int \t reftime;\n" + "    bool \t enforced;\n"
                        + "    bool \t scheduled;\n" + "    bool \t solved;\n" + "    bool \t subSolved;\n"
                        + "    int \t offset;\n" + "    float \t container_id;\n"
                        + "    Active_Enforcer \t myEnforce;\n" + "    bool \t afterIncon;\n" + "  }\n}\n\n",
                objdef, objdef, objdef, objdef, objdef, objdef, objdef);
    }

    out.print("\n" + "class Misc extends Object { \n" + "  Misc() {} \n\n" + "  predicate GENERIC_ACTIVITY {\n"
            + "    int \t priority;\n" + "    int \t reftime;\n" + "    bool \t enforced;\n"
            + "    bool \t scheduled;\n" + "    bool \t solved;\n" + "    bool \t subSolved;\n"
            + "    int \t offset;\n" + "    float \t container_id;\n  }\n\n" + "}\n\n"
            + "class ContainerObj extends Object { \n" + "  ContainerObj() {} \n\n"
            + "  predicate CONTAINER {\n" + "    int \t priority;\n" + "    int \t reftime;\n"
            + "    bool \t enforced;\n" + "    bool \t scheduled;\n" + "    bool \t solved;\n"
            + "    bool \t subSolved;\n" + "    int \t offset;\n" + "    float \t container_id;\n"
            + "    float \t name; \n  }\n\n" + "}\n");
    // define Incon activity type
    // we assume all claimable & sharable resources and all states are in
    // the Incon
    out.print("\n" + "class InitialConds extends Object {\n" + "  InitialConds() {}\n\n"
            + "  predicate incon {\n" + "    int \t priority;\n" + "    int \t reftime;\n"
            + "    bool \t enforced;\n" + "    bool \t scheduled;\n" + "    bool \t solved;\n"
            + "    bool \t subSolved;\n" + "    int \t offset;\n" + "    float \t container_id;\n");
    for (String claim : claimNames) {
        out.printf("    float \t _%s;\n", NDDLUtil.escape(claim));
    }
    for (String share : shareNames) {
        out.printf("    float \t _%s;\n", NDDLUtil.escape(share));
    }
    // note that we use state-value pairs
    // for each state, one and only one value should be TRUE
    for (String state : stateNames) {
        List<String> stateValues = stateValuesMap.get(state);
        if (stateValues != null && stateValues.size() > 0) {
            for (String val : stateValues) {
                out.printf("    float \t _%s_%s;\n", NDDLUtil.escape(state), NDDLUtil.escape(val));
            }
        }
    }
    for (ADTranslator translator : adTranslators)
        if (translator instanceof NumericResourceTranslator) {
            ((NumericResourceTranslator) translator).writeExtraInconParameters(out);
        }
    // PHM 08/08/2013 Give Incon highest (lowest number) priority
    out.print("\n" + "\teq(priority, -1000000);");
    out.print("\n" + "\teq(duration, 1);\n" + "  }\n}\n\n");
    // define a class for each subsystem
    for (String subsystem : subsystemActivitiesMap.keySet()) {
        List<EActivityDef> activities = subsystemActivitiesMap.get(subsystem);
        out.printf("\nclass %s extends Object {\n  %s() {} \n\n", subsystem, subsystem);
        for (EActivityDef activity : activities) {
            out.print("  predicate " + NDDLUtil.escape(activity.getName()) + " {\n" + "    int \t priority;\n"
                    + "    int \t reftime;\n" + "    bool \t enforced;\n" + "    bool \t scheduled;\n"
                    + "    bool \t solved;\n" + "    bool \t subSolved;\n" + "    int \t offset;\n"
                    + "    float \t container_id;\n" + "    Active_Enforcer \t myEnforce;\n"
                    + "    bool \t afterIncon;\n");
            writeStateParameters(out, activity);
            for (ADTranslator translator : adTranslators)
                if (translator instanceof NumericResourceTranslator) {
                    ((NumericResourceTranslator) translator).writeExtraParameters(out, activity);
                }
            out.print(" }\n\n");
        }
        out.print("}\n\n");
    }

    // print out for debugging purposes
    if (DEBUG) {
        System.out.println("** Subsystems **");
        for (String subsys : subsystemActivitiesMap.keySet()) {
            System.out.printf("%s:\t{", subsys);
            List<EActivityDef> activities = subsystemActivitiesMap.get(subsys);
            for (EActivityDef act : activities) {
                System.out.printf("%s ", NDDLUtil.escape(act.getName()));
            }
            System.out.print("}\n");
        }
        System.out.print("\n\n** Defined States **\n\n");
        for (String stateName : stateValuesMap.keySet()) {
            System.out.printf("%s:\t{", stateName);
            for (String val : stateValuesMap.get(stateName)) {
                System.out.printf("%s ", val);
            }
            System.out.print("}\n");
        }
        System.out.print("\n\n** Defined Negated States **\n\n");
        if (stateNotValuesMap.keySet() != null) {
            for (String stateName : stateNotValuesMap.keySet()) {
                System.out.printf("%s:\t{", stateName);
                for (String val : stateNotValuesMap.get(stateName)) {
                    System.out.printf("%s ", val);
                }
                System.out.print("}\n");
            }
        }
        System.out.print("\n\n** Enumeration State Resource Effects by state **");
        for (String state : enumActEffectMap.keySet()) {
            System.out.printf("\n\n%s:  ", state);
            for (String[] entry : enumActEffectMap.get(state)) {
                System.out.printf("(%s, %s) ", entry[0], entry[1]);
            }
        }
        System.out.print("\n\n** Threshold State Resource Effects by act **");
        for (String act : actThresholdEffectMap.keySet()) {
            System.out.printf("\n\n%s:  ", act);
            for (String[] entry : actThresholdEffectMap.get(act)) {
                System.out.printf("(%s, %s) ", entry[0], entry[1]);
            }
        }
        System.out.print("\n\n** Enumeration State Resource Requirements by state **");
        for (String state : enumActRequireMap.keySet()) {
            System.out.printf("\n\n%s:  ", state);
            for (String[] entry : enumActRequireMap.get(state)) {
                System.out.printf("(%s, %s) ", entry[0], entry[1]);
            }
        }
        System.out.print("\n\n** Enumeration Negated State Resource Requirements by state **");
        if (enumActNotRequireMap.keySet() != null) {
            for (String state : enumActNotRequireMap.keySet()) {
                System.out.printf("\n\n%s:  ", state);
                if (enumActNotRequireMap.get(state) != null) {
                    for (String[] entry : enumActNotRequireMap.get(state)) {
                        System.out.printf("(%s, %s) ", entry[0], entry[1]);
                    }
                }
                System.out.print("\n\n** Threshold State Resource Requirements by act **");
                if (actThresholdRequireMap.keySet() != null) {
                    for (String act : actThresholdRequireMap.keySet()) {
                        System.out.printf("\n\n%s:  ", act);
                        if (actThresholdRequireMap.get(act) != null) {
                            for (String[] entry : actThresholdRequireMap.get(act)) {
                                System.out.printf("(%s, %s) ", entry[0], entry[1]);
                            }
                        }
                    }
                }
                System.out.print("\n\n");
            }
        }
    }

    for (ADTranslator translator : adTranslators)
        translator.writeObjects(out);
}

From source file:it.unimi.di.big.mg4j.tool.Scan.java

/**
 * Runs in parallel a number of instances.
 * //from www .  j ava2 s.com
 * <p>This commodity method takes care of instantiating one instance per indexed field, and to
 * pass the right information to each instance. All options are common to all fields, except for
 * the number of occurrences in a batch, which can be tuned for each field separately.
 * 
 * @param ioFactory the factory that will be used to perform I/O.
 * @param basename the index basename.
 * @param documentSequence a document sequence.
 * @param completeness the completeness level of this run.
 * @param termProcessor the term processor for this index.
 * @param builder if not {@code null}, a builder that will be used to create new collection built using <code>documentSequence</code>.
 * @param bufferSize the buffer size used in all I/O.
 * @param documentsPerBatch the number of documents that we should try to put in each segment.
 * @param maxTerms the maximum number of overall (i.e., cross-field) terms in a batch.
 * @param indexedField the fields that should be indexed, in increasing order.
 * @param virtualDocumentResolver the array of virtual document resolvers to be used, parallel
 * to <code>indexedField</code>: it can safely contain anything (even {@code null})
 * in correspondence to non-virtual fields, and can safely be {@code null} if no fields
 * are virtual.
 * @param virtualGap the array of virtual field gaps to be used, parallel to
 * <code>indexedField</code>: it can safely contain anything in correspondence to non-virtual
 * fields, and can safely be {@code null} if no fields are virtual.
 * @param mapFile the name of a file containing a map to be applied to document indices.
 * @param logInterval the minimum time interval between activity logs in milliseconds.
 * @param tempDirName a directory for temporary files.
 * @throws IOException
 * @throws ConfigurationException
 */
@SuppressWarnings("unchecked")
public static void run(final IOFactory ioFactory, final String basename,
        final DocumentSequence documentSequence, final Completeness completeness,
        final TermProcessor termProcessor, final DocumentCollectionBuilder builder, final int bufferSize,
        final int documentsPerBatch, final int maxTerms, final int[] indexedField,
        final VirtualDocumentResolver[] virtualDocumentResolver, final int[] virtualGap, final String mapFile,
        final long logInterval, final String tempDirName) throws ConfigurationException, IOException {

    final boolean building = builder != null;
    final int numberOfIndexedFields = indexedField.length;
    if (numberOfIndexedFields == 0)
        throw new IllegalArgumentException("You must specify at least one field");
    final DocumentFactory factory = documentSequence.factory();
    final File tempDir = tempDirName == null ? null : new File(tempDirName);
    for (int i = 0; i < indexedField.length; i++)
        if (factory.fieldType(indexedField[i]) == DocumentFactory.FieldType.VIRTUAL
                && (virtualDocumentResolver == null || virtualDocumentResolver[i] == null))
            throw new IllegalArgumentException(
                    "No resolver was associated with virtual field " + factory.fieldName(indexedField[i]));

    if (mapFile != null && ioFactory != IOFactory.FILESYSTEM_FACTORY)
        throw new IllegalStateException("Remapped indices currently do not support I/O factories");
    final int[] map = mapFile != null ? BinIO.loadInts(mapFile) : null;

    final Scan[] scan = new Scan[numberOfIndexedFields]; // To scan textual content
    final PayloadAccumulator[] accumulator = new PayloadAccumulator[numberOfIndexedFields]; // To accumulate
    // document data

    final ProgressLogger pl = new ProgressLogger(LOGGER, logInterval, TimeUnit.MILLISECONDS, "documents");
    if (documentSequence instanceof DocumentCollection)
        pl.expectedUpdates = ((DocumentCollection) documentSequence).size();

    final PrintStream titleStream = new PrintStream(basename + TITLES_EXTENSION, "UTF-8");

    for (int i = 0; i < numberOfIndexedFields; i++) {
        final String fieldName = factory.fieldName(indexedField[i]);
        switch (factory.fieldType(indexedField[i])) {
        case TEXT:
            scan[i] = new Scan(ioFactory, basename + '-' + fieldName, fieldName, completeness, termProcessor,
                    map != null ? IndexingType.REMAPPED : IndexingType.STANDARD, 0, 0, bufferSize, builder,
                    tempDir);
            break;
        case VIRTUAL:
            scan[i] = new Scan(ioFactory, basename + '-' + fieldName, fieldName, completeness, termProcessor,
                    IndexingType.VIRTUAL, virtualDocumentResolver[i].numberOfDocuments(), virtualGap[i],
                    bufferSize, builder, tempDir);
            break;

        case DATE:
            accumulator[i] = new PayloadAccumulator(ioFactory, basename + '-' + fieldName, new DatePayload(),
                    fieldName, map != null ? IndexingType.REMAPPED : IndexingType.STANDARD, documentsPerBatch,
                    tempDir);
            break;
        case INT:
            accumulator[i] = new PayloadAccumulator(ioFactory, basename + '-' + fieldName, new IntegerPayload(),
                    fieldName, map != null ? IndexingType.REMAPPED : IndexingType.STANDARD, documentsPerBatch,
                    tempDir);
            break;
        default:

        }
    }

    if (building)
        builder.open("@0"); // First batch

    pl.displayFreeMemory = true;
    pl.start("Indexing documents...");

    DocumentIterator iterator = documentSequence.iterator();
    Reader reader;
    WordReader wordReader;
    List<VirtualDocumentFragment> fragments;
    Document document;

    int documentPointer = 0, documentsInBatch = 0;
    long batchStartTime = System.currentTimeMillis();
    boolean outOfMemoryError = false;
    final MutableString title = new MutableString();

    while ((document = iterator.nextDocument()) != null) {

        long overallTerms = 0;
        if (document.title() != null) {
            title.replace(document.title());
            title.replace(ScanMetadata.LINE_TERMINATORS, ScanMetadata.SPACES);
            titleStream.print(title);
        }
        titleStream.println();
        if (building)
            builder.startDocument(document.title(), document.uri());
        for (int i = 0; i < numberOfIndexedFields; i++) {
            switch (factory.fieldType(indexedField[i])) {
            case TEXT:
                reader = (Reader) document.content(indexedField[i]);
                wordReader = document.wordReader(indexedField[i]);
                wordReader.setReader(reader);
                if (building)
                    builder.startTextField();
                scan[i].processDocument(map != null ? map[documentPointer] : documentPointer, wordReader);
                if (building)
                    builder.endTextField();
                overallTerms += scan[i].numTerms;
                break;
            case VIRTUAL:
                fragments = (List<VirtualDocumentFragment>) document.content(indexedField[i]);
                wordReader = document.wordReader(indexedField[i]);
                virtualDocumentResolver[i].context(document);
                for (VirtualDocumentFragment fragment : fragments) {
                    long virtualDocumentPointer = virtualDocumentResolver[i]
                            .resolve(fragment.documentSpecifier());
                    if (virtualDocumentPointer < 0)
                        continue;
                    // ALERT: we must rewrite remapping to work with long-sized document pointers.
                    if (map != null)
                        virtualDocumentPointer = map[(int) virtualDocumentPointer];
                    wordReader.setReader(new FastBufferedReader(fragment.text()));
                    scan[i].processDocument((int) virtualDocumentPointer, wordReader);
                }
                if (building)
                    builder.virtualField(fragments);
                overallTerms += scan[i].numTerms;
                break;
            default:
                Object o = document.content(indexedField[i]);
                accumulator[i].processData(map != null ? map[documentPointer] : documentPointer, o);
                if (building)
                    builder.nonTextField(o);
                break;
            }

            if (scan[i] != null && scan[i].outOfMemoryError)
                outOfMemoryError = true;
        }
        if (building)
            builder.endDocument();
        documentPointer++;
        documentsInBatch++;
        document.close();
        pl.update();

        long percAvailableMemory = 100;
        boolean compacted = false;
        if ((documentPointer & 0xFF) == 0) {
            // We try compaction if we detect less than PERC_AVAILABLE_MEMORY_CHECK memory available
            percAvailableMemory = Util.percAvailableMemory();
            if (!outOfMemoryError && percAvailableMemory < PERC_AVAILABLE_MEMORY_CHECK) {
                LOGGER.info("Starting compaction... (" + percAvailableMemory + "% available)");
                compacted = true;
                Util.compactMemory();
                percAvailableMemory = Util.percAvailableMemory();
                LOGGER.info("Compaction completed (" + percAvailableMemory + "% available)");
            }
        }

        if (outOfMemoryError || overallTerms >= maxTerms || documentsInBatch == documentsPerBatch
                || (compacted && percAvailableMemory < PERC_AVAILABLE_MEMORY_DUMP)) {
            if (outOfMemoryError)
                LOGGER.warn("OutOfMemoryError during buffer reallocation: writing a batch of "
                        + documentsInBatch + " documents");
            else if (overallTerms >= maxTerms)
                LOGGER.warn("Too many terms (" + overallTerms + "): writing a batch of " + documentsInBatch
                        + " documents");
            else if (compacted && percAvailableMemory < PERC_AVAILABLE_MEMORY_DUMP)
                LOGGER.warn("Available memory below " + PERC_AVAILABLE_MEMORY_DUMP + "%: writing a batch of "
                        + documentsInBatch + " documents");

            long occurrences = 0;
            for (int i = 0; i < numberOfIndexedFields; i++) {
                switch (factory.fieldType(indexedField[i])) {
                case TEXT:
                case VIRTUAL:
                    occurrences += scan[i].dumpBatch();
                    scan[i].openSizeBitStream();
                    break;
                default:
                    accumulator[i].writeData();
                }
            }

            if (building) {
                builder.close();
                builder.open("@" + scan[0].batch);
            }

            LOGGER.info("Last set of batches indexed at "
                    + Util.format((1000. * occurrences) / (System.currentTimeMillis() - batchStartTime))
                    + " occurrences/s");
            batchStartTime = System.currentTimeMillis();
            documentsInBatch = 0;
            outOfMemoryError = false;
        }
    }

    iterator.close();
    titleStream.close();
    if (builder != null)
        builder.close();

    for (int i = 0; i < numberOfIndexedFields; i++) {
        switch (factory.fieldType(indexedField[i])) {
        case TEXT:
        case VIRTUAL:
            scan[i].close();
            break;
        default:
            accumulator[i].close();
            break;
        }

    }

    documentSequence.close();

    pl.done();

    if (building) {
        final String name = new File(builder.basename()).getName();
        final String[] collectionName = new String[scan[0].batch];
        for (int i = scan[0].batch; i-- != 0;)
            collectionName[i] = name + "@" + i + DocumentCollection.DEFAULT_EXTENSION;
        IOFactories.storeObject(ioFactory, new ConcatenatedDocumentCollection(collectionName),
                builder.basename() + DocumentCollection.DEFAULT_EXTENSION);
    }

    if (map != null && documentPointer != map.length)
        LOGGER.warn("The document sequence contains " + documentPointer + " documents, but the map contains "
                + map.length + " integers");
}

From source file:com.rapid.core.Application.java

public void initialise(ServletContext servletContext, boolean createResources) throws JSONException,
        InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException,
        InvocationTargetException, SecurityException, NoSuchMethodException, IOException {

    // get the logger
    Logger logger = (Logger) servletContext.getAttribute("logger");

    // trace log that we're initialising
    logger.trace("Initialising application " + _name + "/" + _version);

    // initialise the security adapter 
    setSecurityAdapter(servletContext, _securityAdapterType);

    // initialise the form adapter 
    setFormAdapter(servletContext, _formAdapterType);

    // initialise the resource includes collection
    _resources = new Resources();

    // if there is any app JavaScript functions - this is for backwards compatibility as _functions have been moved to JavaScript resources
    if (_functions != null) {
        // initialise app resources if need be
        if (_appResources == null)
            _appResources = new Resources();
        // add _functions as JavaScript resource to top of list
        _appResources.add(0, new Resource("Application functions", 1, _functions));
        // remove the _functions
        _functions = null;/*from www .j  av  a2s  . com*/
    }

    // if the created date is null set to today
    if (_createdDate == null)
        _createdDate = new Date();

    // when importing an application we need to initialise but don't want the resource folders made in the old applications name
    if (createResources) {

        // get the jsonControls
        JSONArray jsonControls = (JSONArray) servletContext.getAttribute("jsonControls");

        // get the jsonActions
        JSONArray jsonActions = (JSONArray) servletContext.getAttribute("jsonActions");

        // string builders for the different sections in our rapid.js file
        StringBuilder resourceJS = new StringBuilder();
        StringBuilder initJS = new StringBuilder();
        StringBuilder dataJS = new StringBuilder();
        StringBuilder actionJS = new StringBuilder();

        // string builder for our rapid.css file
        StringBuilder resourceCSS = new StringBuilder();

        // check controls
        if (jsonControls != null) {

            // check control types
            if (_controlTypes != null) {

                // remove the page control (if it's there)
                _controlTypes.remove("page");
                // add it to the top of the list
                _controlTypes.add(0, "page");

                // collection of dependent controls that need adding
                ArrayList<String> dependentControls = new ArrayList<String>();

                // loop control types used by this application
                for (String controlType : _controlTypes) {

                    // loop all available controls
                    for (int i = 0; i < jsonControls.length(); i++) {

                        // get the control
                        JSONObject jsonControl = jsonControls.getJSONObject(i);

                        // check if we're on the type we need
                        if (controlType.equals(jsonControl.optString("type"))) {

                            // look for any dependent control types
                            JSONObject dependantTypes = jsonControl.optJSONObject("dependentTypes");
                            // if we got some
                            if (dependantTypes != null) {
                                // look for an array
                                JSONArray dependantTypesArray = dependantTypes.optJSONArray("dependentType");
                                // if we got one
                                if (dependantTypesArray != null) {
                                    // loop the array
                                    for (int j = 0; j < dependantTypesArray.length(); j++) {
                                        String dependantType = dependantTypesArray.getString(j);
                                        if (!_controlTypes.contains(dependantType)
                                                && !dependentControls.contains(dependantType))
                                            dependentControls.add(dependantType);
                                    }
                                } else {
                                    // just use the object
                                    String dependantType = dependantTypes.getString("dependentType");
                                    if (!_controlTypes.contains(dependantType)
                                            && !dependentControls.contains(dependantType))
                                        dependentControls.add(dependantType);
                                }
                            }

                            // we're done
                            break;
                        } // available control type check

                    } // available control types loop

                } // application control types loop

                // now add all of the dependent controls
                _controlTypes.addAll(dependentControls);

                // loop control types used by this application
                for (String controlType : _controlTypes) {

                    // loop all available controls
                    for (int i = 0; i < jsonControls.length(); i++) {

                        // get the control
                        JSONObject jsonControl = jsonControls.getJSONObject(i);

                        // check if we're on the type we need
                        if (controlType.equals(jsonControl.optString("type"))) {

                            // add any resources (actions can have them too)
                            addResources(jsonControl, "control", resourceJS, resourceCSS);

                            // get any initJavaScript
                            String js = jsonControl.optString("initJavaScript", "");
                            // check we got some
                            if (js.length() > 0) {
                                initJS.append("\nfunction Init_" + jsonControl.getString("type")
                                        + "(id, details) {\n");
                                initJS.append("  " + js.trim().replace("\n", "\n  "));
                                initJS.append("\n}\n");
                            }

                            // check for a getData method
                            String getDataFunction = jsonControl.optString("getDataFunction");
                            // if there was something
                            if (getDataFunction != null) {
                                // clean and print! (if not an empty string)
                                if (getDataFunction.trim().length() > 0)
                                    dataJS.append("\nfunction getData_" + controlType
                                            + "(ev, id, field, details) {\n  "
                                            + getDataFunction.trim().replace("\n", "\n  ") + "\n}\n");

                            }

                            // check for a setData method
                            String setDataFunction = jsonControl.optString("setDataJavaScript");
                            // if there was something
                            if (setDataFunction != null) {
                                // clean and print! (if not an empty string)
                                if (setDataFunction.trim().length() > 0)
                                    dataJS.append("\nfunction setData_" + controlType
                                            + "(ev, id, field, details, data, changeEvents) {\n  "
                                            + setDataFunction.trim().replace("\n", "\n  ") + "\n}\n");
                            }

                            // retrieve any runtimeProperties
                            JSONObject jsonRuntimePropertyCollection = jsonControl
                                    .optJSONObject("runtimeProperties");
                            // check we got some
                            if (jsonRuntimePropertyCollection != null) {

                                // get the first one
                                JSONObject jsonRuntimeProperty = jsonRuntimePropertyCollection
                                        .optJSONObject("runtimeProperty");
                                // get an array
                                JSONArray jsonRunTimeProperties = jsonRuntimePropertyCollection
                                        .optJSONArray("runtimeProperty");

                                // initialise counters
                                int index = 0;
                                int count = 0;

                                // if we got an array
                                if (jsonRunTimeProperties != null) {
                                    // retain the first entry in the object
                                    jsonRuntimeProperty = jsonRunTimeProperties.getJSONObject(0);
                                    // retain the size
                                    count = jsonRunTimeProperties.length();
                                }

                                do {

                                    // get the type
                                    String type = jsonRuntimeProperty.getString("type");

                                    // get the get function
                                    String getFunction = jsonRuntimeProperty.optString("getPropertyFunction",
                                            null);
                                    // print the get function if there was one
                                    if (getFunction != null)
                                        dataJS.append("\nfunction getProperty_" + controlType + "_" + type
                                                + "(ev, id, field, details) {\n  "
                                                + getFunction.trim().replace("\n", "\n  ") + "\n}\n");

                                    // get the set function
                                    String setFunction = jsonRuntimeProperty.optString("setPropertyJavaScript",
                                            null);
                                    // print the get function if there was one
                                    if (setFunction != null)
                                        dataJS.append("\nfunction setProperty_" + controlType + "_" + type
                                                + "(ev, id, field, details, data, changeEvents) {\n  "
                                                + setFunction.trim().replace("\n", "\n  ") + "\n}\n");

                                    // increment index
                                    index++;

                                    // get the next one
                                    if (index < count)
                                        jsonRuntimeProperty = jsonRunTimeProperties.getJSONObject(index);

                                } while (index < count);

                            }

                            // we're done with this jsonControl
                            break;
                        }

                    } // jsonControls loop

                } // control types loop

            } // control types check

        } // jsonControls check

        // check  actions
        if (jsonActions != null) {

            // check action types
            if (_actionTypes != null) {

                // collection of dependent controls that need adding
                ArrayList<String> dependentActions = new ArrayList<String>();

                // loop control types used by this application
                for (String actionType : _actionTypes) {

                    // loop all available controls
                    for (int i = 0; i < jsonActions.length(); i++) {

                        // get the action
                        JSONObject jsonAction = jsonActions.getJSONObject(i);

                        // check if we're on the type we need
                        if (actionType.equals(jsonAction.optString("type"))) {

                            // look for any dependant control types
                            JSONObject dependantTypes = jsonAction.optJSONObject("dependentTypes");
                            // if we got some
                            if (dependantTypes != null) {
                                // look for an array
                                JSONArray dependantTypesArray = dependantTypes.optJSONArray("dependentType");
                                // if we got one
                                if (dependantTypesArray != null) {
                                    // loop the array
                                    for (int j = 0; j < dependantTypesArray.length(); j++) {
                                        String dependantType = dependantTypesArray.getString(j);
                                        if (!_actionTypes.contains(dependantType)
                                                && !dependentActions.contains(dependantType))
                                            dependentActions.add(dependantType);
                                    }
                                } else {
                                    // just use the object
                                    String dependantType = dependantTypes.getString("dependentType");
                                    if (!_actionTypes.contains(dependantType)
                                            && !dependentActions.contains(dependantType))
                                        dependentActions.add(dependantType);
                                }
                            }

                            // we're done
                            break;
                        }

                    }

                }

                // now add all of the dependent controls
                _controlTypes.addAll(dependentActions);

                // loop action types used by this application
                for (String actionType : _actionTypes) {

                    // loop jsonActions
                    for (int i = 0; i < jsonActions.length(); i++) {

                        // get action
                        JSONObject jsonAction = jsonActions.getJSONObject(i);

                        // check the action is the one we want
                        if (actionType.equals(jsonAction.optString("type"))) {

                            // add any resources (controls can have them too)
                            addResources(jsonAction, "action", resourceJS, resourceCSS);

                            // get action JavaScript
                            String js = jsonAction.optString("actionJavaScript");
                            // only produce rapid action is this is rapid app
                            if (js != null && ("rapid".equals(_id) || !"rapid".equals(actionType))) {
                                // clean and print! (if not an empty string)
                                if (js.trim().length() > 0)
                                    actionJS.append("\n" + js.trim() + "\n");
                            }

                            // move onto the next action type
                            break;
                        }

                    } // jsonActions loop

                } // action types loop

            } // action types check

        } // jsonAction check

        // assume no theme css
        String themeCSS = null;
        // assume no theme name
        String themeName = null;

        // check the theme type
        if (_themeType != null) {
            // get the themes
            List<Theme> themes = (List<Theme>) servletContext.getAttribute("themes");
            // check we got some
            if (themes != null) {
                // loop them
                for (Theme theme : themes) {
                    // check type
                    if (_themeType.equals(theme.getType())) {
                        // retain the theme CSS
                        themeCSS = theme.getCSS();
                        // retain the name
                        themeName = theme.getName();
                        // get any resources
                        addResources(theme.getResources(), "theme", themeName, null, resourceJS, resourceCSS);
                        // we're done
                        break;
                    }
                }
            }
        }

        // put the appResources at the end so they can be overrides
        if (_appResources != null) {
            for (Resource resource : _appResources) {
                // create new resource based on this one (so that the dependancy doesn't get written back to the application.xml file)
                Resource appResource = new Resource(resource.getType(), resource.getContent(),
                        ResourceDependency.RAPID);
                // if the type is a file or link prefix with the application folder
                switch (resource.getType()) {
                case Resource.JAVASCRIPTFILE:
                case Resource.CSSFILE:
                    // files are available on the local file system so we prefix with the webfolder
                    appResource.setContent(getWebFolder(this)
                            + (resource.getContent().startsWith("/") ? "" : "/") + resource.getContent());
                    break;
                case Resource.JAVASCRIPTLINK:
                case Resource.CSSLINK:
                    // links are not so go in as-is
                    appResource.setContent(resource.getContent());
                    break;
                }
                // add new resource based on this one but with Rapid dependency
                _resources.add(appResource);
            }
        }

        // create folders to write the rapid.js file
        String applicationPath = getWebFolder(servletContext);
        File applicationFolder = new File(applicationPath);
        if (!applicationFolder.exists())
            applicationFolder.mkdirs();

        // write the rapid.js file
        FileOutputStream fos = new FileOutputStream(applicationPath + "/rapid.js");
        PrintStream ps = new PrintStream(fos);

        // write the rapid.min.js file
        FileOutputStream fosMin = new FileOutputStream(applicationPath + "/rapid.min.js");
        PrintWriter pw = new PrintWriter(fosMin);

        // file header
        ps.print(
                "\n/* This file is auto-generated on application load and save - it is minified when the application status is live */\n");
        // check functions
        if (_functions != null) {
            if (_functions.length() > 0) {
                // header (this is removed by minify)
                ps.print("\n\n/* Application functions JavaScript */\n\n");
                // insert params
                String functionsParamsInserted = insertParameters(servletContext, _functions);
                // print
                ps.print(functionsParamsInserted);
                // print minify 
                Minify.toWriter(functionsParamsInserted, pw, Minify.JAVASCRIPT);
            }
        }
        // check resource js
        if (resourceJS.length() > 0) {
            // header
            ps.print("\n\n/* Control and Action resource JavaScript */\n\n");
            // insert params
            String resourceJSParamsInserted = insertParameters(servletContext, resourceJS.toString());
            // print
            ps.print(resourceJS.toString());
            // print minify 
            Minify.toWriter(resourceJSParamsInserted, pw, Minify.JAVASCRIPT);
        }
        // check init js
        if (initJS.length() > 0) {
            // header
            ps.print("\n\n/* Control initialisation methods */\n\n");
            // insert params
            String initJSParamsInserted = insertParameters(servletContext, initJS.toString());
            // print
            ps.print(initJS.toString());
            // print minify 
            Minify.toWriter(initJSParamsInserted, pw, Minify.JAVASCRIPT);
        }
        // check datajs
        if (dataJS.length() > 0) {
            // header
            ps.print("\n\n/* Control getData and setData methods */\n\n");
            // insert params
            String dataJSParamsInserted = insertParameters(servletContext, dataJS.toString());
            // print
            ps.print(dataJS.toString());
            // print minify 
            Minify.toWriter(dataJSParamsInserted, pw, Minify.JAVASCRIPT);
        }
        // check action js
        if (actionJS.length() > 0) {
            // header
            ps.print("\n\n/* Action methods */\n\n");
            // insert params
            String actionParamsInserted = insertParameters(servletContext, actionJS.toString());
            // print
            ps.print(actionJS.toString());
            // print minify 
            Minify.toWriter(actionParamsInserted, pw, Minify.JAVASCRIPT);
        }

        // close debug writer and stream
        ps.close();
        fos.close();
        // close min writer and stream
        pw.close();
        fosMin.close();

        // get the rapid CSS into a string and insert parameters
        String resourceCSSWithParams = insertParameters(servletContext, resourceCSS.toString());
        String appThemeCSSWithParams = insertParameters(servletContext, themeCSS);
        String appCSSWithParams = insertParameters(servletContext, _styles);

        // write the rapid.css file
        fos = new FileOutputStream(applicationPath + "/rapid.css");
        ps = new PrintStream(fos);
        ps.print(
                "\n/* This file is auto-generated on application load and save - it is minified when the application status is live */\n\n");
        if (resourceCSSWithParams != null) {
            ps.print(resourceCSSWithParams.trim());
        }
        if (appThemeCSSWithParams != null) {
            ps.print("\n\n/* " + themeName + " theme styles */\n\n");
            ps.print(appThemeCSSWithParams.trim());
        }
        if (appCSSWithParams != null) {
            ps.print("\n\n/* Application styles */\n\n");
            ps.print(appCSSWithParams.trim());
        }
        ps.close();
        fos.close();

        // minify it to a rapid.min.css file
        Minify.toFile(resourceCSSWithParams + appCSSWithParams, applicationPath + "/rapid.min.css", Minify.CSS);

        // check the status
        if (_status == STATUS_LIVE) {
            // add the application js min file as a resource
            _resources.add(new Resource(Resource.JAVASCRIPTFILE, getWebFolder(this) + "/rapid.min.js",
                    ResourceDependency.RAPID));
            // add the application css min file as a resource          
            _resources.add(new Resource(Resource.CSSFILE, getWebFolder(this) + "/rapid.min.css",
                    ResourceDependency.RAPID));
        } else {
            // add the application js file as a resource
            _resources.add(new Resource(Resource.JAVASCRIPTFILE, getWebFolder(this) + "/rapid.js",
                    ResourceDependency.RAPID));
            // add the application css file as a resource          
            _resources.add(new Resource(Resource.CSSFILE, getWebFolder(this) + "/rapid.css",
                    ResourceDependency.RAPID));
        }

        // loop all resources and minify js and css files
        for (Resource resource : _resources) {
            // get the content (which is the filename)
            String fileName = resource.getContent();
            // only interested in js and css files
            switch (resource.getType()) {
            case Resource.JAVASCRIPTFILE:
                // get a file for this
                File jsFile = new File(
                        servletContext.getRealPath("/") + (fileName.startsWith("/") ? "" : "/") + fileName);
                // if the file exists, and it's in the scripts folder and ends with .js
                if (jsFile.exists() && fileName.startsWith("scripts/") && fileName.endsWith(".js")) {
                    // derive the min file name by modifying the start and end
                    String fileNameMin = "scripts_min/" + fileName.substring(8, fileName.length() - 3)
                            + ".min.js";
                    // get a file for minifying 
                    File jsFileMin = new File(servletContext.getRealPath("/") + "/" + fileNameMin);
                    // if this file does not exist
                    if (!jsFileMin.exists()) {
                        // make any dirs it may need
                        jsFileMin.getParentFile().mkdirs();
                        // minify to it
                        Minify.toFile(jsFile, jsFileMin, Minify.JAVASCRIPT);
                    }
                    // if this application is live, update the resource to the min file
                    if (_status == STATUS_LIVE)
                        resource.setContent(fileNameMin);
                }
                break;
            case Resource.CSSFILE:
                // get a file for this
                File cssFile = new File(
                        servletContext.getRealPath("/") + (fileName.startsWith("/") ? "" : "/") + fileName);
                // if the file exists, and it's in the scripts folder and ends with .js
                if (cssFile.exists() && fileName.startsWith("styles/") && fileName.endsWith(".css")) {
                    // derive the min file name by modifying the start and end
                    String fileNameMin = "styles_min/" + fileName.substring(7, fileName.length() - 4)
                            + ".min.css";
                    // get a file for minifying 
                    File cssFileMin = new File(servletContext.getRealPath("/") + "/" + fileNameMin);
                    // if this file does not exist
                    if (!cssFileMin.exists()) {
                        // make any dirs it may need
                        cssFileMin.getParentFile().mkdirs();
                        // minify to it
                        Minify.toFile(cssFile, cssFileMin, Minify.CSS);
                    }
                    // if this application is live, update the resource to the min file
                    if (_status == STATUS_LIVE)
                        resource.setContent(fileNameMin);
                }
                break;
            }

        } // loop resources

        // a list for all of the style classes we're going to send up with
        _styleClasses = new ArrayList<String>();

        // populate the list of style classes by scanning the global styles
        scanStyleClasses(_styles, _styleClasses);
        // and any theme
        scanStyleClasses(appThemeCSSWithParams, _styleClasses);

        // remove any that have the same name as controls
        if (jsonControls != null) {
            // loop them
            for (int i = 0; i < jsonControls.length(); i++) {
                // remove any classes with this controls type
                _styleClasses.remove(jsonControls.getJSONObject(i).getString("type"));
            }
        }

    } // create resources

    // empty the list of page variables so it's regenerated
    _pageVariables = null;

    // debug log that we initialised
    logger.debug(
            "Initialised application " + _name + "/" + _version + (createResources ? "" : " (no resources)"));

}

From source file:com.datatorrent.stram.cli.DTCli.java

private void printHelp(String command, CommandSpec commandSpec, PrintStream os) {
    if (consolePresent) {
        os.print("\033[0;93m");
        os.print(command);/*from w  ww .  ja  v a 2s .  co m*/
        os.print("\033[0m");
    } else {
        os.print(command);
    }
    if (commandSpec instanceof OptionsCommandSpec) {
        OptionsCommandSpec ocs = (OptionsCommandSpec) commandSpec;
        if (ocs.options != null) {
            os.print(" [options]");
        }
    }
    if (commandSpec.requiredArgs != null) {
        for (Arg arg : commandSpec.requiredArgs) {
            if (consolePresent) {
                os.print(" \033[3m" + arg + "\033[0m");
            } else {
                os.print(" <" + arg + ">");
            }
        }
    }
    if (commandSpec.optionalArgs != null) {
        for (Arg arg : commandSpec.optionalArgs) {
            if (consolePresent) {
                os.print(" [\033[3m" + arg + "\033[0m");
            } else {
                os.print(" [<" + arg + ">");
            }
            if (arg instanceof VarArg) {
                os.print(" ...");
            }
            os.print("]");
        }
    }
    os.println("\n\t" + commandSpec.description);
    if (commandSpec instanceof OptionsCommandSpec) {
        OptionsCommandSpec ocs = (OptionsCommandSpec) commandSpec;
        if (ocs.options != null) {
            os.println("\tOptions:");
            HelpFormatter formatter = new HelpFormatter();
            PrintWriter pw = new PrintWriter(os);
            formatter.printOptions(pw, 80, ocs.options, 12, 4);
            pw.flush();
        }
    }
}

From source file:com.opengamma.analytics.financial.model.volatility.local.LocalVolatilityPDEGreekCalculator.java

/**
 * bumped each input volatility by 1bs and record the effect on the representative point by following the chain
 * of refitting the implied volatility surface, the local volatility surface and running the forward PDE solver
 * @param ps Print Stream//from  www. ja va  2s . c  om
 * @param option test option
 */
public void bucketedVegaForwardPDE(final PrintStream ps, final EuropeanVanillaOption option) {

    final int n = _marketData.getNumExpiries();
    final double[][] strikes = _marketData.getStrikes();
    final double forward = _marketData.getForwardCurve().getForward(option.getTimeToExpiry());
    final double maxT = option.getTimeToExpiry();
    final double maxProxyDelta = 0.4;
    final double x = option.getStrike() / forward;

    final PDEFullResults1D pdeRes = runForwardPDESolver(_localVolatilityMoneyness, _isCall, _theta, maxT,
            maxProxyDelta, _timeSteps, _spaceSteps, _timeGridBunching, _spaceGridBunching, 1.0);

    final double[] xNodes = pdeRes.getGrid().getSpaceNodes();
    int index = getLowerBoundIndex(xNodes, x);
    if (index >= 1) {
        index--;
    }
    if (index >= _spaceSteps - 1) {
        index--;
        if (index >= _spaceSteps - 1) {
            index--;
        }
    }
    final double[] vols = new double[4];
    final double[] moneyness = new double[4];
    System.arraycopy(xNodes, index, moneyness, 0, 4);
    for (int i = 0; i < 4; i++) {
        vols[i] = BlackFormulaRepository.impliedVolatility(pdeRes.getFunctionValue(index + i), 1.0,
                moneyness[i], option.getTimeToExpiry(), option.isCall());
    }
    Interpolator1DDoubleQuadraticDataBundle db = INTERPOLATOR_1D.getDataBundle(moneyness, vols);
    final double exampleVol = INTERPOLATOR_1D.interpolate(db, x);

    final double shiftAmount = 1e-4; //1bps

    final double[][] res = new double[n][];

    for (int i = 0; i < n; i++) {
        final int m = strikes[i].length;
        res[i] = new double[m];
        for (int j = 0; j < m; j++) {
            final BlackVolatilitySurfaceMoneyness bumpedSurface = _surfaceFitter
                    .getBumpedVolatilitySurface(_marketData, i, j, shiftAmount);
            final LocalVolatilitySurfaceMoneyness bumpedLV = DUPIRE.getLocalVolatility(bumpedSurface);
            final PDEFullResults1D pdeResBumped = runForwardPDESolver(bumpedLV, _isCall, _theta, maxT,
                    maxProxyDelta, _timeSteps, _spaceSteps, _timeGridBunching, _spaceGridBunching, 1.0);
            for (int k = 0; k < 4; k++) {
                vols[k] = BlackFormulaRepository.impliedVolatility(pdeResBumped.getFunctionValue(index + k),
                        1.0, moneyness[k], option.getTimeToExpiry(), option.isCall());
            }
            db = INTERPOLATOR_1D.getDataBundle(moneyness, vols);
            final double vol = INTERPOLATOR_1D.interpolate(db, x);
            res[i][j] = (vol - exampleVol) / shiftAmount;
        }
    }

    for (int i = 0; i < n; i++) {
        //  System.out.print(TENORS[i] + "\t");
        final int m = strikes[i].length;
        for (int j = 0; j < m; j++) {
            ps.print(res[i][j] + "\t");
        }
        ps.print("\n");
    }
    ps.print("\n");
}