package com.sun.enterprise.shoal.memberdetailstest; import net.jxta.logging.Logging; import java.util.logging.Level; import java.util.logging.Logger; import java.util.UUID; import java.util.Properties; import java.util.Map; import java.io.Serializable; import com.sun.enterprise.ee.cms.core.*; import com.sun.enterprise.ee.cms.impl.client.JoinNotificationActionFactoryImpl; /** * Created by IntelliJ IDEA. * User: carryel * Date: 2008. 12. 17 * Time: ¿ÀÀü 9:47:31 * To change this template use File | Settings | File Templates. */ public class SimpleMemberDetailsTest { private final static Logger logger = Logger.getLogger( "SimpleMemberDetailsTest" ); private static final long DSC_SYNC_WAIT = 2000; // ms private static final int DSC_SYNC_MAX_COUNT = 5; private static String specialServerName; private final String group = "TestGroup"; public static void main( String[] args ) { if( args != null && args.length == 1 ) { specialServerName = args[0]; } else { specialServerName = null; } System.setProperty( Logging.JXTA_LOGGING_PROPERTY, Level.OFF.toString()); SimpleMemberDetailsTest check = new SimpleMemberDetailsTest(); try { check.runSimpleSample(); } catch( GMSException e ) { logger.log( Level.SEVERE, "Exception occured while joining group:" + e ); } } private void runSimpleSample() throws GMSException { logger.log( Level.INFO, "Starting SimpleJoinTest...." ); String serverName; if( specialServerName == null ) serverName = UUID.randomUUID().toString(); else serverName = specialServerName; // initialize Group Management Service GroupManagementService gms = initializeGMS( serverName, group ); // register for Group Events logger.log( Level.INFO, "Registering for group event notifications" ); gms.addActionFactory( new JoinNotificationActionFactoryImpl( new JoinNotificationCallBack( serverName ) ) ); // join group logger.log( Level.INFO, "Joining Group " + group ); gms.join(); // add user's data gms.updateMemberDetails( serverName, "UUID", "UUDI_" + serverName ); gms.updateMemberDetails( serverName, "PUBLIC_KEY", "PUBLIC_KEY_" + serverName ); // finish //leaveGroupAndShutdown( serverName, gms ); } private GroupManagementService initializeGMS( String serverName, String groupName ) { logger.log( Level.INFO, "Initializing Shoal for member: " + serverName + " group:" + groupName ); return (GroupManagementService)GMSFactory.startGMSModule( serverName, groupName, GroupManagementService.MemberType.CORE, new Properties() ); //null ); // or null } private void leaveGroupAndShutdown( String serverName, GroupManagementService gms ) { logger.log( Level.INFO, "Shutting down gms " + gms + "for server " + serverName ); gms.shutdown( GMSConstants.shutdownType.INSTANCE_SHUTDOWN ); } private class JoinNotificationCallBack implements CallBack { private String serverName; public JoinNotificationCallBack( String serverName ) { this.serverName = serverName; } public void processNotification( Signal notification ) { if ( !( notification instanceof JoinNotificationSignal ) ) logger.log( Level.SEVERE, "received unkown notification type:" + notification ); String targetServerName = notification.getMemberToken(); // skip own join notification if( serverName.equals( targetServerName ) ) return; int count = 0; Serializable uuid = null; Serializable publicKey = null; do { Map details = notification.getMemberDetails(); uuid = details.get( "UUID" ); publicKey = details.get( "PUBLIC_KEY" ); if( uuid != null && publicKey != null ) break; // wait for dsc's synchronization count++; System.out.println( "Waiting for DSC first Sync. count = (" + count + "/" + DSC_SYNC_MAX_COUNT + ")" ); try { Thread.sleep( DSC_SYNC_WAIT ); } catch( InterruptedException ignore ) { } } while( count <= DSC_SYNC_MAX_COUNT ); System.out.println( "## UUID=" + uuid ); System.out.println( "## PUBLIC_KEY=" + publicKey ); } } }