?? updatecms.java
字號:
package com.sams.jxta.updateCMS;
// Gui Classes
import javax.swing.*;
import javax.swing.tree.*;
// standard Java classes
import java.io.*;
import java.util.*;
import java.net.*;
// JXTA classes
import net.jxta.membership.MembershipService;
import net.jxta.id.IDFactory;
import net.jxta.rendezvous.*;
import net.jxta.platform.Application;
import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.StructuredTextDocument;
import net.jxta.document.StructuredDocument;
import net.jxta.document.MimeMediaType;
import net.jxta.document.Document;
import java.lang.reflect.InvocationTargetException;
import net.jxta.endpoint.Message;
import net.jxta.pipe.PipeService;
import net.jxta.pipe.PipeID;
import net.jxta.pipe.OutputPipe;
import net.jxta.peergroup.PeerGroup;
import net.jxta.protocol.*;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.id.ID;
import net.jxta.discovery.*;
import net.jxta.exception.PeerGroupException;
// For input
import net.jxta.pipe.InputPipe;
import net.jxta.peergroup.PeerGroupID;
// For feedback
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import net.jxta.share.*;
import net.jxta.share.client.*;
import net.jxta.impl.id.UUID.*;
public class UpdateCMS implements Application{
/**
* This is the directory where CMS will store data
* about shared data.
*/
public static final String CMS_DATA_DIRNAME = "CMS_DATA";
protected PeerGroup group;
protected PeerGroup oldGroup;// used if we resign
protected DiscoveryService disco; // Discovery service
protected ContentManager contentManager;
protected MirrorLibrary mirrorGUI;
protected String baseDir = "shareOut\\";
//protected String shareDir = "C:\\DirShareCMS\\share";
protected String inputDir = "shareIn\\";
protected DefaultMutableTreeNode remoteFiles;
protected Advertisement adv;
protected net.jxta.impl.id.UUID.UUID appGID = new net.jxta.impl.id.UUID.UUID(0x43C41CB682064423L,
0x798AA490FF914510L);
public void UpdateCMS(){
System.out.println(this.getClass().getName()+".UpdateCMS()");
}
public PeerGroup getPeerGroup(){
return group;
}
/**
* Initialize the module, passing it its peer group and advertisement.
*
* @param group PeerGroup this Module is started from.
* @param assignedID Identity of Module within group.
* modules can use it as a the root of their namespace to create
* names that are unique within the group but predictible by the
* same module on another peer. This is normaly the ModuleClassID
* which is also the name under which the module is known by other
* modules. For a group it is the PeerGroupID itself.
* @param adv The implementation advertisement for this Module.
* parameters under its assigned ID.
* @exception PeerGroupException failure to initialize this Module.
*
*/
public void init(PeerGroup group, ID assignedID, Advertisement implAdv) throws PeerGroupException{
System.out.println(this.getClass().getName()+".init()");
this.group = group;
this.adv = implAdv;
// Let's initialize the client
disco = group.getDiscoveryService();
CMS cms = getCMS(group);
contentManager = cms.getContentManager();
mirrorGUI = new MirrorLibrary(this);
mirrorGUI.setVisible(true);
}// End of init
/*
* Starts the application.
*/
public int startApp(String[] args) {
System.out.println(this.getClass().getName()+".startApp()");
// get the cms for this group
//Share a default Directory
DefaultMutableTreeNode root = (DefaultMutableTreeNode)mirrorGUI.getLocalContentTreeModel().getRoot();
remoteFiles = new DefaultMutableTreeNode("Remote");
root.add(remoteFiles);
DefaultMutableTreeNode localFiles = new DefaultMutableTreeNode("Local");
root.add(localFiles);
File base = new File(baseDir);
if(!base.exists()){
base.mkdir();
}
shareDir(base,localFiles);
JTree tree = mirrorGUI.getLocalContentTree();
tree.treeDidChange();
((DefaultTreeModel)tree.getModel()).reload() ;
// Using non-event method.
PeriodicDocSearch periodicDocSearch = new PeriodicDocSearch(2*60*1000);
Thread thread = new Thread(periodicDocSearch);
thread.start();
return 0;
}
private void shareDir(File file,DefaultMutableTreeNode parent){
System.out.println(file.getName()+" shareing");
File files[] = null;
try {
if (file.isDirectory()){
files = file.listFiles();
//Share a default Directory
for (int i = 0; i < files.length;i++){
if (!files[i].isDirectory()){
DefaultMutableTreeNode fileNode = new DefaultMutableTreeNode(files[i].getName());
parent.add(fileNode);
// this is where we actually share the file
contentManager.share(files[i]);
System.out.println(files[i].getName()+" shared");
}else{
// It is a directory so add it to the list too
DefaultMutableTreeNode dirNode = new DefaultMutableTreeNode(file.getName());
parent.add(dirNode);
shareDir(files[i],parent);
}
}// end for
}else{
DefaultMutableTreeNode fileNode = new DefaultMutableTreeNode(file.getName());
parent.add(fileNode);
// this is where we actually share the file
contentManager.share(file);
System.out.println(file.getName()+" shared");
}
} catch (IOException e) {
System.out.println("ERROR: could not share ");
e.printStackTrace();
return;
}
}// end of shareDir()
public void stopApp() {
}
/**
* Note that this is where we create the
* group type and credentials.
*/
public void newGroupContext(String groupName){
/* Future implementation
try{
oldGroup = group;
GroupManager groupManager = new GroupManager(group);
PeerGroup newGroup = groupManager.addGroup(groupName,null);
if (newGroup == null){
System.out.println("group is null");
return;
}
}catch(Exception e){
e.printStackTrace();
}
*/
}
public void leaveGroup(String groupName){
/* Future implementation
try {
// Resign our current identity.
VisitorMembership membership = (VisitorMembership) group.getMembershipService();
membership.resign();
group = oldGroup;
}catch( Exception e ) {
e.printStackTrace();
}
*/
}
public PeerGroupAdvertisement getPeerGroupAdvertisement() {
return group.getPeerGroupAdvertisement();
}
private static Hashtable cmsSessions = new Hashtable();
public static CMS getCMS(PeerGroup group) {
// check if the cms is already started
CMS cms = (CMS)cmsSessions.get(getShortName(group));
if (null == cms) {
// create a new cms for this peergroup
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -