📄 main.java
字号:
// Initialize the Root private key. PSE_SAMPLE_GROUP_ROOT_CERT = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(cert_der)); byte[] key_der = PSEUtils.base64Decode(new StringReader(PSE_SAMPLE_GROUP_ROOT_ENCRYPTED_KEY_BASE64)); PSE_SAMPLE_GROUP_ROOT_ENCRYPTED_KEY = new EncryptedPrivateKeyInfo(key_der); } catch (IOException failure) { IllegalStateException failed = new IllegalStateException("Could not read certificate or key."); failed.initCause(failure); throw failed; } catch (CertificateException failure) { IllegalStateException failed = new IllegalStateException("Could not process certificate."); failed.initCause(failure); throw failed; } } /** * Creates a new instance of Main */ public Main() throws Exception { // Start JXTA, instantiate the net peer group. final PeerGroup npg = PeerGroupFactory.newNetPeerGroup(); // This will cause the JXTA Shell to start if enabled. if (START_NETGROUP) { npg.startApp(new String[0]); } // Build the Module Impl Advertisemet we will use for our group. ModuleImplAdvertisement pseImpl = build_psegroup_impl_adv(npg); // Publish the Module Impl Advertisement to the group where the // peergroup will be advertised. This should be done in every peer // group in which the Peer Group is also advertised. // We use the same expiration and lifetime that the Peer Group Adv // will use (the default). DiscoveryService disco = npg.getDiscoveryService(); disco.publish(pseImpl, PeerGroup.DEFAULT_LIFETIME, PeerGroup.DEFAULT_EXPIRATION); PeerGroupAdvertisement pse_pga = null; // Options for the Option Pane Dialog final Object[] options = { "Create self-invitation", "Use invitation..." }; while (null == pse_pga) { /* We use a JOptionPane to quickly choose between the choices * available for starting the PSE Sample Peer Group. * * The choices are: * - Create a self-invitation * - Use an invitation from a file * * "Self Invite" - If you know the password for the hard coded * root certificate/private key pair then you can create a group * advertisement which enables you to create and instantiate the * peer group with owner privleges. Only one peer ever needs to do * this as the res of the peers can join using generated invitations. * * "Use Invitation" - Use a peer group advertisement provided by * another peer in the group in order to join the peergroup. */ int optionPicked = JOptionPane.showOptionDialog(null , "To start the PSE Sample Peer Group you must choose a source for the Peer Group Advertisement." , "Start PSE Sample Peer Group", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options , options[1]); if (JOptionPane.CLOSED_OPTION == optionPicked) { // User cancelled dialog. They want to quit. break; } switch (optionPicked) { case 0: // Create Self-Invitation. X509Certificate[] invitationCertChain = { PSE_SAMPLE_GROUP_ROOT_CERT}; pse_pga = build_psegroup_adv(pseImpl, invitationCertChain, PSE_SAMPLE_GROUP_ROOT_ENCRYPTED_KEY); break; case 1: default: // Use an invitation from a file. final JFileChooser fc = new JFileChooser(); // In response to a button click: int returnVal = fc.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { FileReader invitation = new FileReader(fc.getSelectedFile()); XMLDocument advDoc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8 , invitation); pse_pga = (PeerGroupAdvertisement) AdvertisementFactory.newAdvertisement(advDoc); invitation.close(); } break; } } // If we have a Peer Group Advertisement then instantiate the group // and run the main application. if (null != pse_pga) { // Publish the Peer Group. This will also be done when the group is // instantiated, but we do it here because we want to ensure // what lifetime and expiration to use. disco.publish(pse_pga, PeerGroup.DEFAULT_LIFETIME, PeerGroup.DEFAULT_EXPIRATION); // The invokeLater inner class needs it to be "final". So dupe it. final PeerGroupAdvertisement ui_pga = pse_pga; // `Invoke the Swing based user interface. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new tutorial.psesample.SwingUI(npg, ui_pga).setVisible(true); } }); } } /** * Start of the PSE peer group sample. * * @param args the command line arguments. Unused by this sample. */ public static void main(String[] args) { // Give the Thread we are running on a name for debuggers. Thread.currentThread().setName(Main.class.getName() + ".main()"); try { new Main(); } catch (Throwable all) { // Something bad happened, print out the failure and quit. System.err.println("Uncaught Throwable in main() :"); all.printStackTrace(System.err); System.exit(1); } } /** * Build a Module Implementation Advertisement suitable for the PSE Sample * Peer Group. The <tt>ModuleImplAdvertisement</tt> is built using the * result of <tt>base.getAllPurposePeerGroupImplAdvertisement()</tt> to * ensure that the result will be appropriate for running as a child * peer group of <tt>base</tt>. * <p/> * <p/>The default advertisement is modified to use the PSE Membership * Service as it's membership service replacing whatever membership * service was originally specified. * <p/> * <p/>The Module Spec ID of the ModuleImplAdvertisement is set to the * hard-coded id <tt>PSE_SAMPLE_MSID</tt> so that all peer group instances * have a consistent (and compatible) specification. * * @param base The Peer Group from which we will retrieve the default * Module Implementation Advertisement. * @return The Module Implementation Advertisement for the PSE Sample * Peer Group. */ static ModuleImplAdvertisement build_psegroup_impl_adv(PeerGroup base) { ModuleImplAdvertisement newGroupImpl; try { newGroupImpl = base.getAllPurposePeerGroupImplAdvertisement(); } catch (Exception unlikely) { // getAllPurposePeerGroupImplAdvertisement() doesn't really throw expections. throw new IllegalStateException("Could not get All Purpose Peer Group Impl Advertisement."); } newGroupImpl.setModuleSpecID(PSE_SAMPLE_MSID); newGroupImpl.setDescription("PSE Sample Peer Group Implementation"); // FIXME bondolo Use something else to edit the params. StdPeerGroupParamAdv params = new StdPeerGroupParamAdv(newGroupImpl.getParam()); Map services = params.getServices(); ModuleImplAdvertisement aModuleAdv = (ModuleImplAdvertisement) services.get(PeerGroup.membershipClassID); services.remove(PeerGroup.membershipClassID); ModuleImplAdvertisement implAdv = (ModuleImplAdvertisement) AdvertisementFactory.newAdvertisement( ModuleImplAdvertisement.getAdvertisementType()); implAdv.setModuleSpecID(PSEMembershipService.pseMembershipSpecID); implAdv.setCompat(aModuleAdv.getCompat()); implAdv.setCode(PSEMembershipService.class.getName()); implAdv.setUri(aModuleAdv.getUri()); implAdv.setProvider(aModuleAdv.getProvider()); implAdv.setDescription("PSE Membership Service"); // Add our selected membership service to the peer group service as the // group's default membership service. services.put(PeerGroup.membershipClassID, implAdv); // Save the group impl parameters newGroupImpl.setParam((Element) params.getDocument(MimeMediaType.XMLUTF8)); return newGroupImpl; } /** * Build the Peer Group Advertisement for the PSE Sample Peer Group. * <p/> * <p/>The Peer Group Advertisement will be generated to contain an * invitation certificate chain and encrypted private key. Peers which * know the password for the Peer Group Root Certificate Key can generate * their own invitation otherwise peers must get an invitation from * another group member. * <p/> * <p/>The invitation certificate chain appears in two forms: * <ul> * <li>Self Invitation : PSE Sample Group Root Certificate + Encrypted Private Key</li> * <li>Regular Invitation : * <ul> * <li>Invitation Certificate + Encrpyted Private Key</li> * <li>Peer Group Member Certificate</li> * <li>Peer Group Administrator Certificate</li> * <li>PSE Sample Group Root Certificate</li> * </ul></li> * </ul> * <p/> * <p/>Invitations are provided to prospective peer group members. You can * use a unique invitation for each prospective member or a single * static invitation for every prospective member. If you use a static * invitation certificate keep in mind that every copy will use the same * shared password and thus the invitation will provide only very limited * security. * <p/> * <p/>In some applications the invitation password will be built in to the * application and the human user will never have to know of it's use. * This can be useful if you wish your PSE Peer Group used only by a single * application. * * @param pseImpl The Module Impl Advertisement which the Peer Group * Advertisement will reference for its Module Spec ID. * @param invitationCertChain The certificate chain which comprises the * PeerGroup Invitation. * @param invitationPrivateKey The private key of the invitation. * @return The Peer Group Advertisement. */ static PeerGroupAdvertisement build_psegroup_adv(ModuleImplAdvertisement pseImpl, X509Certificate[] invitationCertChain, EncryptedPrivateKeyInfo invitationPrivateKey) { PeerGroupAdvertisement newPGAdv = (PeerGroupAdvertisement) AdvertisementFactory.newAdvertisement( PeerGroupAdvertisement.getAdvertisementType()); newPGAdv.setPeerGroupID(PSE_SAMPLE_PGID); newPGAdv.setModuleSpecID(pseImpl.getModuleSpecID()); newPGAdv.setName("PSE Sample Peer Group"); newPGAdv.setDescription("Created by PSE Sample!"); PSEConfigAdv pseConf = (PSEConfigAdv) AdvertisementFactory.newAdvertisement(PSEConfigAdv.getAdvertisementType()); pseConf.setCertificateChain(invitationCertChain); pseConf.setEncryptedPrivateKey(invitationPrivateKey, invitationCertChain[0].getPublicKey().getAlgorithm()); XMLDocument pseDoc = (XMLDocument) pseConf.getDocument(MimeMediaType.XMLUTF8); newPGAdv.putServiceParam(PeerGroup.membershipClassID, pseDoc); return newPGAdv; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -