📄 studentdemo.java
字号:
package com.sams.jxta.groups.student;
import net.jxta.discovery.DiscoveryEvent;
import net.jxta.discovery.DiscoveryListener;
import net.jxta.discovery.DiscoveryService;
import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredDocument;
import net.jxta.document.TextElement;
import net.jxta.exception.PeerGroupException;
import net.jxta.exception.ProtocolNotSupportedException;
import net.jxta.id.ID;
import net.jxta.id.IDFactory;
import net.jxta.impl.document.PlainTextDocument;
import net.jxta.impl.peergroup.StdPeerGroup;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.platform.Module;
import net.jxta.platform.ModuleClassID;
import net.jxta.platform.ModuleSpecID;
import net.jxta.protocol.DiscoveryResponseMsg;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.protocol.PeerGroupAdvertisement;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.impl.peergroup.Platform;
import net.jxta.impl.peergroup.GenericPeerGroup;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.net.*;
import java.io.IOException;
import com.sams.jxta.groups.*;
/**
* This class demonstrates how to use the Group Manager.
*
*/
public class StudentDemo implements Module{
private static final org.apache.log4j.Category LOG =
org.apache.log4j.Category.getInstance(StudentDemo.class.getName());
protected PeerGroup netPeerGroup = null;
/**
* we need to understand jxta protocols. This is used if you use "Java Web Start"
* or if you launch via a non-forked JVM in NetBeans or other IDE.
*/
static {
URL.setURLStreamHandlerFactory( new URLStreamHandlerFactory() {
public URLStreamHandler createURLStreamHandler(
String protocol ) {
if( "urn".equals( protocol ) ||
"jxta".equals( protocol )) {
return new URLStreamHandler() {
protected URLConnection openConnection(
URL u )
throws IOException {
return null;
}
};
}
else
return null;
}
} );
}
/*
* The main method which serves as the entry point
*/
public static void main(String args[]){
StudentDemo sample = new StudentDemo();
sample.runJXTA();
}
/*
* This method starts the JXTA platform.
*
*/
private void runJXTA(){
// Get an instance of the net peer group
try {
netPeerGroup = PeerGroupFactory.newNetPeerGroup();
} catch (PeerGroupException peerGroupException) {
//could not instanciate the group, print the stack and exit
LOG.error("Group Creation Failure !",peerGroupException);
System.exit(1);
}
// Create a module impl advertisement by using the factory.
// Set the appropriate fields.
ModuleClassID mID = IDFactory.newModuleClassID();
ModuleSpecID moduleSpecID = PeerGroup.refMembershipSpecID;
ModuleImplAdvertisement adv = (ModuleImplAdvertisement)
AdvertisementFactory.newAdvertisement(ModuleImplAdvertisement.getAdvertisementType());
adv.setModuleSpecID(moduleSpecID);
adv.setCompat(StdPeerGroup.stdCompatStatement);
adv.setCode("com.sams.jxta.groups.student.StudentDemo");
adv.setUri("http://www.jxta.org/download/jxta.jar");
// This module is loaded by the net peer group.
try{
netPeerGroup.loadModule(mID,adv);
} catch (ProtocolNotSupportedException protocolException) {
LOG.error("Could not load Module !",protocolException);
System.exit(1);
} catch (PeerGroupException peerGroupException) {
LOG.error("Could not load Module !",peerGroupException);
System.exit(1);
}
}
/*
* This method is called when the module is initialized.
* This is when we use the Group Manager.
*/
public void init(PeerGroup parentGroup,ID assignedID,Advertisement implAdv)
throws PeerGroupException{
GroupManager manager = new GroupManager(parentGroup/*netPeerGroup*/,parentGroup);
// This is to create a new University Admissions group
// to deal with the admissions of a particular university
PeerGroup p =manager.addGroup("UniversityStudents",
"com.sams.jxta.groups.student.UniversityAdmissionsService",
"To represent all graduate students in a University",null,false);
System.out.println("Successfully created Admissions Service");
// We try to submit a Pre-Application(Pre App) Form and join.
// Initially we try to join the department of management
try{
StructuredDocument preAppForm =
PreApplicationForm.createPreAppForm(PreApplicationForm.DEPARTMENT_MANAGEMENT);
manager.joinGroup(p,preAppForm,"PreApplication");
} catch(GroupManagerAuthenticationException authenticationException) {
LOG.debug("Exception :Expected behavior. University has no admissions for this dpt");
LOG.debug("Message is : "+authenticationException.getMessage());
} catch(ProtocolNotSupportedException protocolException) {
LOG.error("Error in join !",protocolException);
}
// Try to submit a Pre-Application for the Department of computer science
// Must be success !
// Applications will be accepted if GPA >3 and name != null
try{
StructuredDocument preAppForm =
PreApplicationForm.createPreAppForm(PreApplicationForm.DEPARTMENT_COMPUTER_SCIENCE);
manager.joinGroup(p,preAppForm,"PreApplication");
} catch(GroupManagerAuthenticationException authenticationException) {
LOG.error("Error in join !",authenticationException);
} catch(ProtocolNotSupportedException protocolException) {
LOG.error("Error in join !",protocolException);
}
}
/*
* Methods to be implemented by virtue of being a module.
* Not used in this example
*/
public int startApp(String[] args){return 0;}
public void stopApp(){};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -