P. 1
Practical JXTA II

Practical JXTA II


|Views: 15,610|Likes:
Published by Jérôme Verstrynge
This book is an introduction to the JXTA P2P protocol. It is written for software developers and architects willing to understand what P2P is all about, and those looking for code examples explaining how to use JXSE, step-by-step.
This book is an introduction to the JXTA P2P protocol. It is written for software developers and architects willing to understand what P2P is all about, and those looking for code examples explaining how to use JXSE, step-by-step.

More info:

Published by: Jérôme Verstrynge on Jan 25, 2011
Copyright:Traditional Copyright: All rights reserved


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF or read online from Scribd
See more
See less


The following code example describes how to create custom PSE peer group and to join it:


public class _620_Creating_A_Custom_PSE_PeerGroup_Example {

public static final String Name = "Example 620";

public static final PeerID PID = IDFactory.newPeerID(PeerGroupID.defaultNetPeerGroupID,


public static final File ConfigurationFile = new File("." +

System.getProperty("file.separator") + Name);

public static final String MyPrincipalName = "Principal - " + Name;

public static final String MyPrivateKeyPassword = "PrivateKey Password - " + Name;

public static final String MyKeyStoreFileName = "MyKeyStoreFile";

public static final String MyKeyStoreLocation = "." + System.getProperty("file.separator") +

Name + File.separator + "MyKeyStoreLocation";

public static final String MyKeyStorePassword = "KeyStore Password - " + Name;

public static final String MyKeyStoreProvider = "KeyStore Provider - " + Name;

public static final File MyKeyStoreDirectory = new File(MyKeyStoreLocation);

public static final File MyKeyStoreFile = new File(MyKeyStoreLocation + File.separator

+ MyKeyStoreFileName);

public static final X509Certificate TheX509Certificate;

public static final PrivateKey ThePrivateKey;

public static final String PsePeerGroupName = "PSE peer group name";

public static final PeerGroupID PsePeerGroupID =

IDFactory.newPeerGroupID(PeerGroupID.defaultNetPeerGroupID, PsePeerGroupName.getBytes());

static {

// Static initialization of certificates

PSEUtils.IssuerInfo ForPSE = PSEUtils.genCert(Name, null);

Using JXSE Private Keys, Certificates & KeyStores - 207

TheX509Certificate = ForPSE.cert;

ThePrivateKey = ForPSE.issuerPkey;


public static void main(String[] args) {

try {

// Removing any existing configuration?

Tools.CheckForExistingConfigurationDeletion(Name, ConfigurationFile);

// Preparing data


// Creating the key store

FileKeyStoreManager MyFileKeyStoreManager = new FileKeyStoreManager(

(String)null, MyKeyStoreProvider, MyKeyStoreFile);


if (!MyFileKeyStoreManager.isInitialized()) {

Tools.PopInformationMessage(Name, "Keystore is NOT initialized");

} else {

Tools.PopInformationMessage(Name, "Keystore is initialized");


// Loading the (empty) keystore

KeyStore MyKeyStore =


// Setting data

X509Certificate[] Temp = { TheX509Certificate };

MyKeyStore.setKeyEntry(PID.toString(), ThePrivateKey,

MyPrivateKeyPassword.toCharArray(), Temp);

// Saving the data

MyFileKeyStoreManager.saveKeyStore(MyKeyStore, MyKeyStorePassword.toCharArray());

// Reloading the KeyStore

MyKeyStore = MyFileKeyStoreManager.loadKeyStore(MyKeyStorePassword.toCharArray());

// Retrieving Certificate

X509Certificate MyCertificate = (X509Certificate)


if (MyCertificate==null) {

Tools.PopInformationMessage(Name, "X509 Certificate CANNOT be retrieved");

} else {

Tools.PopInformationMessage(Name, "X509 Certificate can be retrieved");



// Retrieving private key

PrivateKey MyPrivateKey = (PrivateKey) MyKeyStore.getKey(PID.toString(),


if (MyPrivateKey==null) {

Tools.PopInformationMessage(Name, "Private key CANNOT be retrieved");

} else {

Tools.PopInformationMessage(Name, "Private key can be retrieved");



// Creation of the network manager

NetworkManager MyNetworkManager = new NetworkManager(NetworkManager.ConfigMode.EDGE,

Name, ConfigurationFile.toURI());

// Retrieving the network configurator

NetworkConfigurator MyNetworkConfigurator = MyNetworkManager.getConfigurator();

// Setting the keystore



Using JXSE Private Keys, Certificates & KeyStores - 208

// Starting the network

PeerGroup MyNetPeerGroup = MyNetworkManager.startNetwork();

// Checking membership implementation

MembershipService NPGMembership = MyNetPeerGroup.getMembershipService();

Tools.PopInformationMessage(Name, "NetPeerGroup membership implementation:\n"

+ NPGMembership.getClass().getSimpleName());

// Creating a child group with PSE

PeerGroup ChildPeerGroup = MyNetPeerGroup.newGroup(




"Checking PSE..."


if (Module.START_OK != ChildPeerGroup.startApp(new String[0]))

System.err.println("Cannot start PSE peergroup");

// Checking membership implementation

MembershipService ChildGroupMembership = ChildPeerGroup.getMembershipService();

Tools.PopInformationMessage(Name, "Child group membership implementation:\n"

+ ChildGroupMembership.getClass().getSimpleName());

// Joining the peer group

AuthenticationCredential MyAuthenticationCredit = new

AuthenticationCredential( MyNetPeerGroup, "StringAuthentication", null );

StringAuthenticator MyStringAuthenticator = (StringAuthenticator)





Credential MyCredential = null;

if (MyStringAuthenticator.isReadyForJoin()) {

MyCredential = ChildGroupMembership.join(MyStringAuthenticator);


if (MyCredential!=null) {

Tools.PopInformationMessage(Name, "Credentials created successfully");

} else {

Tools.PopInformationMessage(Name, "Credentials NOT created successfully");


// Stopping the network

Tools.PopInformationMessage(Name, "Stop the JXTA network");


} catch (PeerGroupException Ex) {

Tools.PopErrorMessage(Name, Ex.toString());

} catch (NoSuchAlgorithmException Ex) {

Tools.PopErrorMessage(Name, Ex.toString());

} catch (UnrecoverableKeyException Ex) {

Tools.PopErrorMessage(Name, Ex.toString());

} catch (NoSuchProviderException Ex) {

Tools.PopErrorMessage(Name, Ex.toString());

} catch (KeyStoreException Ex) {

Tools.PopErrorMessage(Name, Ex.toString());

} catch (IOException Ex) {

Using JXSE Private Keys, Certificates & KeyStores - 209

Tools.PopErrorMessage(Name, Ex.toString());

} catch (ProtocolNotSupportedException Ex) {

Tools.PopErrorMessage(Name, Ex.toString());



public static ModuleImplAdvertisement createAllPurposePeerGroupWithPSEModuleImplAdv() {

ModuleImplAdvertisement implAdv = CompatibilityUtils.createModuleImplAdvertisement(

PeerGroup.allPurposePeerGroupSpecID, StdPeerGroup.class.getName(),

"General Purpose Peer Group with PSE Implementation");

// Create the service list for the group.

StdPeerGroupParamAdv paramAdv = new StdPeerGroupParamAdv();

// set the services

paramAdv.addService(PeerGroup.endpointClassID, PeerGroup.refEndpointSpecID);

paramAdv.addService(PeerGroup.resolverClassID, PeerGroup.refResolverSpecID);

paramAdv.addService(PeerGroup.membershipClassID, PSEMembershipService.pseMembershipSpecID);

paramAdv.addService(PeerGroup.accessClassID, PSEAccessService.PSE_ACCESS_SPEC_ID);

// standard services

paramAdv.addService(PeerGroup.discoveryClassID, PeerGroup.refDiscoverySpecID);

paramAdv.addService(PeerGroup.rendezvousClassID, PeerGroup.refRendezvousSpecID);

paramAdv.addService(PeerGroup.pipeClassID, PeerGroup.refPipeSpecID);

paramAdv.addService(PeerGroup.peerinfoClassID, PeerGroup.refPeerinfoSpecID);

paramAdv.addService(PeerGroup.contentClassID, ContentServiceImpl.MODULE_SPEC_ID);

// Insert the newParamAdv in implAdv

XMLElement paramElement = (XMLElement) paramAdv.getDocument(MimeMediaType.XMLUTF8);


return implAdv;



Example 620 – Custom PSE Peer Group & Joining

This example is performing the following:

1.Just like in the previous example, we create a keystore and load it with a X509 certificate
and a private key.

2.Then, we create a custom PSE peer group using the same method as for a non-PSE peer
group. Notice that the module implementation advertisement references the PSE mem-
bership ID and the PSE access service.

3.Next, we run the process to join the PSE peergroup.


Warning: the PSE access service implementation is still in experimental mode.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->