xmlSerializer.setOutputByteStream(System.out); xmlSerializer.serialize(dataDoc);
XMLUtils.outputDOM(dataDoc,System.out);
    private void startServer() {        logger.debug("Starting the server...");        try {            // First load or create the Module class advertisement associated with the service            // We build the module class advertisement using the advertisement            // Factory class by passing it the type of the advertisement we            // want to construct. The Module class advertisement is to be used            // to simply advertise the existence of the service. This is a            // a very small advertisement that only advertise the existence            // of service. In order to access the service, a peer will            // have to discover the associated module spec advertisement.            ModuleClassAdvertisement moduleClassAd;            ModuleClassID mcID = null;            File moduleAdFile = new File(MODULE_ADV_FILE);            if (!moduleAdFile.exists()) {                // create new module                moduleClassAd = (ModuleClassAdvertisement)                        AdvertisementFactory.newAdvertisement(                                ModuleClassAdvertisement.getAdvertisementType());                moduleClassAd.setName("JXTAMOD:" + SERVICE_NAME);                moduleClassAd.setDescription("Discus UDDI service");                mcID = IDFactory.newModuleClassID();                moduleClassAd.setModuleClassID(mcID);                // Ok the Module Class advertisement was created, now publish                // it in the local cache and to the NetPeerGroup                discoveryService.publish(moduleClassAd, DiscoveryService.ADV);                // and save it to file                FileOutputStream fout = new FileOutputStream(moduleAdFile);                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fout));                StructuredTextDocument doc = (StructuredTextDocument)                        moduleClassAd.getDocument(xmlMimeMediaType);                doc.sendToWriter(out);                out.flush();                out.close();                logger.debug("saved new module class advertisment");            } else {                // load module class ad from file                logger.debug("reading in module advertisement file");                try {                    FileInputStream is = new FileInputStream(moduleAdFile);                    moduleClassAd = (ModuleClassAdvertisement)                            AdvertisementFactory.newAdvertisement(                                    xmlMimeMediaType, is);                    is.close();                } catch (Exception e) {                    logger.fatal("failed to read/parse module class advertisement");                    return;                }            }            discoveryService.remotePublish(moduleClassAd, DiscoveryService.ADV);            // Create the Module Spec advertisement associated with the service            // We build the module Spec Advertisement using the advertisement            // Factory class by passing in the type of the advertisement we            // want to construct. The Module Spec advertisement will contain            // all the information necessary for a client to contact the service            // for example a pipe advertisement to be used to contact the service            ModuleSpecAdvertisement moduleSpecAd;            PipeAdvertisement pipeAd;            File moduleSpecAdFile = new File(MODULE_SPEC_ADV_FILE);            if (!moduleSpecAdFile.exists()) {                // create a new module spec ad                moduleSpecAd = (ModuleSpecAdvertisement)                        AdvertisementFactory.newAdvertisement(                                ModuleSpecAdvertisement.getAdvertisementType());                // Setup some of the information field about the servive.                moduleSpecAd.setName("JXTASPEC:" + SERVICE_NAME);                moduleSpecAd.setVersion("Version 1.0");                moduleSpecAd.setCreator("discus");                moduleSpecAd.setModuleSpecID(IDFactory.newModuleSpecID(mcID));                moduleSpecAd.setSpecURI("http://discus/");                //moduleSpecAd.setParam();                // Create a pipe advertisement for the Service. The client MUST use                // the same pipe advertisement to talk to the server. When the client                // discovers the module advertisement it will extract the pipe                // advertisement to create its pipe.                File advFile = new File(PIPE_ADV_FILE);                if (!advFile.exists()) {                    // we need to create a new pipe advertisement                    pipeAd = (PipeAdvertisement)                            AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());                    pipeAd.setName(SERVICE_NAME);                    pipeAd.setType(PipeService.UnicastType);                    pipeAd.setPipeID(IDFactory.newPipeID(group.getPeerGroupID()));                    logger.debug("created a new pipe with id " + pipeAd.getID());                    // save this advertisement                    FileOutputStream fout = new FileOutputStream(advFile);                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fout));                    StructuredTextDocument doc = (StructuredTextDocument) pipeAd.getDocument(xmlMimeMediaType);                    doc.sendToWriter(out);                    out.flush();                    out.close();                    logger.debug("saved new pipe advertisment");                } else {                    logger.debug("reading in pipe advertisement file");                    try {                        FileInputStream is = new FileInputStream(advFile);                        pipeAd = (PipeAdvertisement)                                AdvertisementFactory.newAdvertisement(                                        xmlMimeMediaType, is);                        is.close();                    } catch (Exception e) {                        logger.fatal("failed to read/parse pipe advertisement");                        return;                    }                }                // add the pipe advertisement to the ModuleSpecAdvertisement                moduleSpecAd.setPipeAdvertisement(pipeAd);                // We need to add a signature and/or public key to this advertisement                // Since we can't sign the whole advertisement (because then it wouldn't be a normal                // JXTA advertisement), we add as a parameter the signed advertisement ID and pipe ID                // Additionally, we wrap the IDs in a "data" tag that is signed.                // Change: for now, Base64-encode signed data! The reason for this is that                // JXTA changes the indenting/spacing, and the signature is broken when                // it gets to the client...                {                    org.w3c.dom.Document dataDoc = db.newDocument();                    Element data = dataDoc.createElement(DATA_TAG);                    Element msid = dataDoc.createElement(MODULE_SPEC_ID_TAG);                    msid.appendChild(dataDoc.createTextNode(moduleSpecAd.getID().toString()));                    data.appendChild(msid);                    Element pipeid = dataDoc.createElement(PIPE_ID_TAG);                    pipeid.appendChild(dataDoc.createTextNode(pipeAd.getID().toString()));                    data.appendChild(pipeid);                    dataDoc.appendChild(data);                    dataDoc = signatureManager.signDocument(dataDoc);                    // debugging                    //XMLUtils.outputDOMc14nWithComments(dataDoc,System.out);                    xmlSerializer.setOutputByteStream(System.out);                    xmlSerializer.serialize(dataDoc);                    ByteArrayOutputStream out = new ByteArrayOutputStream();                    //XMLUtils.outputDOMc14nWithComments(dataDoc,out);                    xmlSerializer.setOutputByteStream(out);                    xmlSerializer.serialize(dataDoc);                    out.close();                    // it's sad, but we have to encode it because otherwise signature gets mangled                    // and cannot be verified.... sort of defeats the whole point of XML signatures...                    String encoded = Base64.encodeBase64(out.toByteArray());                    StructuredTextDocument signatureDoc = (StructuredTextDocument)                            StructuredDocumentFactory.newStructuredDocument(xmlMimeMediaType, PARAM_TAG, encoded);                    moduleSpecAd.setParam(signatureDoc);                }                // debugging: display the advertisement as a plain text document.                {                    logger.debug("Created service advertisement:");                    StructuredTextDocument doc = (StructuredTextDocument)                            moduleSpecAd.getDocument(xmlMimeMediaType);                    StringWriter str_out = new StringWriter();                    doc.sendToWriter(str_out);                    logger.debug(str_out.toString());                }                // Ok the Module advertisement was created, just publish                // it in my local cache and into the NetPeerGroup.                discoveryService.publish(moduleSpecAd, DiscoveryService.ADV);                // and save it to file                {                    FileOutputStream fout = new FileOutputStream(moduleSpecAdFile);                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fout));                    StructuredTextDocument doc = (StructuredTextDocument)                            moduleSpecAd.getDocument(xmlMimeMediaType);                    doc.sendToWriter(out);                    out.flush();                    out.close();                    logger.debug("saved new module spec advertisment");                }            } else {                // load module class ad from file                logger.debug("reading in module spec ad file");                try {                    FileInputStream is = new FileInputStream(moduleSpecAdFile);                    moduleSpecAd = (ModuleSpecAdvertisement)                            AdvertisementFactory.newAdvertisement(                                    xmlMimeMediaType, is);                    is.close();                } catch (Exception e) {                    logger.fatal("failed to read/parse module class advertisement");                    return;                }                pipeAd = moduleSpecAd.getPipeAdvertisement();            }            discoveryService.remotePublish(moduleSpecAd, DiscoveryService.ADV);            // we are now ready to start the service            // create the input pipe endpoint clients will            // use to connect to the service            pipeService.createInputPipe(pipeAd, this);        } catch (Exception ex) {            ex.printStackTrace();            logger.debug("Server: Error publishing the module");        }    }