📄 myjxta.java
字号:
/* * Copyright (c) 2001 Sun Microsystems, Inc. All rights * reserved. * * Redistribution and use in source and binary forms, with or withouta * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following discalimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Sun Microsystems, Inc. for Project JXTA." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" * must not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", * nor may "JXTA" appear in their name, without prior written * permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of Project JXTA. For more * information on Project JXTA, please see * <http://www.jxta.org/>. * * This license is based on the BSD license adopted by the Apache Foundation. * * $Id: MyJXTA.java,v 1.97 2006/08/05 11:01:26 nano Exp $ */package net.jxta.myjxta;import java.io.File;import java.lang.management.ManagementFactory;import java.net.URI;import java.net.URL;import java.util.ArrayList;import java.util.EventObject;import java.util.Iterator;import java.util.List;import java.util.ResourceBundle;import java.util.Timer;import java.util.TimerTask;import javax.management.InstanceAlreadyExistsException;import javax.management.MBeanRegistrationException;import javax.management.MalformedObjectNameException;import javax.management.NotCompliantMBeanException;import javax.management.ObjectName;import net.jxta.endpoint.Message;import net.jxta.exception.ConfiguratorException;import net.jxta.exception.PeerGroupException;import net.jxta.ext.config.ConversionException;import net.jxta.myjxta.dialog.Dialog;import net.jxta.myjxta.dialog.DialogFilter;import net.jxta.myjxta.dialog.DialogListener;import net.jxta.myjxta.dialog.DialogManager;import net.jxta.myjxta.dialog.DialogPipeListener;import net.jxta.myjxta.dialog.OneToOneCommandDialog;import net.jxta.myjxta.plugin.DefaultMyJxtaPluginContainer;import net.jxta.myjxta.plugin.PluginContainer;import net.jxta.myjxta.plugin.PluginPanel;import net.jxta.myjxta.search.SearchManager;import net.jxta.myjxta.search.SearchModifier;import net.jxta.myjxta.search.Searcher;import net.jxta.myjxta.share.ShareManager;import net.jxta.myjxta.util.AuthenticationUtil;import net.jxta.myjxta.util.Constants;import net.jxta.myjxta.util.Defaults;import net.jxta.myjxta.util.Env;import net.jxta.myjxta.util.Group;import net.jxta.myjxta.util.GroupListener;import net.jxta.myjxta.util.GroupNode;import net.jxta.myjxta.util.JxtaNode;import net.jxta.myjxta.util.MultipleInstanceUtil;import net.jxta.myjxta.util.MyJXTAConfigurator;import net.jxta.myjxta.util.MyJxtaObjectRepository;import net.jxta.myjxta.util.Peer;import net.jxta.myjxta.util.PeerGroupUtil;import net.jxta.myjxta.util.PeerNode;import net.jxta.myjxta.util.PipeUtil;import net.jxta.myjxta.util.Resources;import net.jxta.myjxta.util.Status;import net.jxta.myjxta.util.preferences.MyPreferenceManager;import net.jxta.peergroup.NetPeerGroupFactory;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupID;import net.jxta.peergroup.WorldPeerGroupFactory;import net.jxta.pipe.PipeService;import net.jxta.protocol.PeerGroupAdvertisement;import net.jxta.protocol.PipeAdvertisement;import net.jxta.util.JxtaBiDiPipe;import org.apache.log4j.Level;import org.apache.log4j.LogManager;import org.apache.log4j.Logger;/** * @author james todd [gonzo at jxta dot org] * @version $Id: MyJXTA.java,v 1.97 2006/08/05 11:01:26 nano Exp $ */public class MyJXTA implements GroupListener { public static final String GROUP_ID = "group.id"; public static final String GROUP_NAME = "group.name"; public static final String GROUP_DESCRIPTION = "group.description"; public static final String GROUP_AUTO_RENDEZVOUS_PERIOD = "group.autoRendezVousPeriod"; private static final ResourceBundle STRINGS = Resources.getStrings(); // not used right now --> commented out // private static final int CONFIGURATION_WAIT = 50; private static final Logger LOG = Logger.getLogger(MyJXTA.class); private final ArrayList<Group> joinedGroups = new ArrayList<Group>(); private String peerName = null; /** * The UI to use to display the data */ private View view = null; /** * A Timer that allows to schedule certain tasks */ private Timer timer = null; /** * The PreferenceManager object to use **/ private MyPreferenceManager myPreferenceManager = null; public MultipleInstanceUtil.AdministrationListener administrationListener = null; private PluginContainer m_pluginContainer; private ArrayList<String> administrationBuffer = null; public static final String NAME = Constants.getInstance().get( Constants.NAME); public static final String VERSION = NAME + " " + Constants.getInstance().get(Constants.VERSION) + " [" + Constants.getInstance().get(Constants.BUILD) + "]"; public static MyJXTA getTheInstance() { return g_theInstance; } private static MyJXTA g_theInstance; public static MyJXTA startMyJxta() { if (g_theInstance == null) { g_theInstance = new MyJXTA(); } return g_theInstance; } /** * Create a new instance of MyJxta and initializes it. This starts the * JXTA network if it was not already started */ private MyJXTA() { // only one Instance of MyJxta can be active inside the same working // directory if (!MultipleInstanceUtil.isOnlyInstance(this)) { destroy(); return; } g_theInstance = this; // only needed for the NetBean integration registerBeanStuff(); // initiate the Plugin / Module Container (will instantiate the plugins, // modules...) m_pluginContainer = new DefaultMyJxtaPluginContainer(this); // ViewFactory.setViewerClass(net.jxta.myjxta.ui.MyJXTALiteViewer.class); // initiate the view (right now only a single swing view is // implememented) this.view = ViewFactory.getView(this); //call the init methods of the plugins m_pluginContainer.init(); this.view.setVisible(true); boolean isConfigured = configureMyJxta(); if (isConfigured) { initialize(); } } private boolean configureMyJxta() { MyJXTAConfigurator c = new MyJXTAConfigurator(this.view); try { c.configure(); return true; } catch (ConfiguratorException ce) { if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("configuration error", ce); } } return false; } private void registerBeanStuff() { try { ManagementFactory.getPlatformMBeanServer().registerMBean( Status.getInstance(), new ObjectName(":type=net.jxta.myjxta")); } catch (MalformedObjectNameException mone) { mone.printStackTrace(); } catch (InstanceAlreadyExistsException iaee) { iaee.printStackTrace(); } catch (MBeanRegistrationException mbre) { mbre.printStackTrace(); } catch (NotCompliantMBeanException ncmbe) { ncmbe.printStackTrace(); } } public MyPreferenceManager getPreferenceManager() { return this.myPreferenceManager; } /** * Return the name of the peer * * @return the name of the peer */ public String getPeerName() { return this.peerName; } /** * Add a dialog that should be displayed in the UI * * @param dialog * the dialog to display */ public void addDialog(Dialog dialog) { this.view.addDialog(dialog); } public void removeDialog(Dialog dialog) { this.view.removeDialog(dialog); } /** * Return the ShareManager instance used for file sharing * * @return the ShareManager instance used for file sharing */ public ShareManager getShareManager() { return ShareManager.getShareManager(this, this.view.getGroup(), true); } /** * Part of group Lifecycle. Joins group. Starts group services and * associated resources. * * @modified 2005-04-24 jamoore added support for ViJxta */ public void joinGroup(final Group group, boolean useAutoRdvMode, boolean discover) { final PeerGroup peerGroup = group.getPeerGroup(); setStatus(STRINGS.getString("status.group.join") + " " + peerGroup.getPeerGroupName()); this.view.setTitle(peerGroup.getPeerName() + "@" + peerGroup.getPeerGroupName()); String dialogName = Dialog.getDialogNamer(OneToOneCommandDialog.class) .getDialogName(peerGroup.getPeerName()); PipeAdvertisement pipeAdvertisment = PipeUtil.getAdv(peerGroup, dialogName, PipeService.UnicastType, null, true); setStatus(STRINGS.getString("status.group.display") + " " + peerGroup.getPeerGroupName()); // add the one Group Panel to the navigation tree if (group.isVisible()) { view.createGroupNavigation(group); } GroupNode groupNode = new GroupNode(group); if (group.getParentGroup() != null) { groupNode.setParent(new GroupNode(group.getParentGroup())); } else { groupNode.setParent(MyJxtaObjectRepository.getRoot()); } addJxtaNode(groupNode); PeerNode ownPeerNode = new PeerNode(new Peer(pipeAdvertisment), group); group.setOwnCommandId(pipeAdvertisment.getPipeID()); addJxtaNode(ownPeerNode, true); restore(group); if (discover) { discover(group); } addToJoinedGroups(group); if (group.isVisible()) { PeerGroup cpg = AuthenticationUtil.getTLSPeerGroup(peerGroup); if (!AuthenticationUtil.isAuthenticated(cpg)) { if (LOG.isEnabledFor(Level.INFO)) { LOG.info("authenticating"); } AuthenticationUtil.authenticate(getView(), cpg); } if (!AuthenticationUtil.isAuthenticated(cpg)) { if (LOG.isEnabledFor(Level.INFO)) { LOG.info("not authenticated"); } } } // tell the plugins that we have joined a group m_pluginContainer.notifyAboutGroupJoin(group); // now lets add the different dialogs/pipe listeners if (group.isVisible()) { registerCommandDialogPipeListener(group); } if (group.isVisible()) { setStatus(STRINGS.getString("status.share.register")); try { // todo move to plugin ShareManager.getShareManager(this, group, true); } catch (Throwable t) { // Catch exception so that Main Initializer does not fail. if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("Caught unexpected Exception", t); } } } if (useAutoRdvMode) { if (shouldBeAutoRdv(group)) { setStatus(STRINGS.getString("status.group.rendezvous.auto") + ": " + group.getAutoRendezVousPeriod()); peerGroup.getRendezVousService().setAutoStart(useAutoRdvMode, group.getAutoRendezVousPeriod()); } } } private void registerCommandDialogPipeListener(final Group group) { String dialogName; // START 1to1Command setStatus(STRINGS.getString("status.dialog.command.register")); dialogName = Dialog.getDialogNamer(OneToOneCommandDialog.class) .getDialogName(group.getPeerGroup().getPeerName()); setStatus(STRINGS.getString("status.dialog.listener.add") + ": " + dialogName); DialogManager.getInstance(group, dialogName, PipeService.UnicastType) .addPipeListener(group.getPeerGroup(), new DialogPipeListener() { public void receive(@SuppressWarnings("unused") PeerGroup pg, JxtaBiDiPipe pipe) { // install a local command listener at the // incomming pipe // (each module registers its specific commands // via CommandFactory. DialogManager.installCommandListener( OneToOneCommandDialog.class, group, pipe, MyJXTA.this); } public void receive(PeerGroup pg, Message msg) { // commands are transported via BiDiPipes only - // no need to handle incomming messages LOG.warn("should never happen!"+pg+msg); } }); // END ONE2ONE Command } private void addToJoinedGroups(Group p_group) { joinedGroups.add(p_group); } private void removeFromJoinedGroups(Group p_group) { joinedGroups.remove(p_group); } /** * has this peer the needed tcp-ip connectivity to act as an auto-rdv? * * @param p_group * @return true if the peer should be configured for auto-rdv behaviour */ private boolean shouldBeAutoRdv(Group p_group) { if (p_group.isConnected()) { // comments on this one? are proxyed peers able to act as a rdv? String proxyHost = System.getProperty(Env.HTTP_PROXY_HOST); return proxyHost == null || proxyHost.trim().length() == 0; } return true; // not connected -->enable auto-rdv (not an optimal // solution, // because we may connect later, but the only one that can prevent // complete rdv-less groups right now) } /** * Part of group Lifecycle. Resigns from group. Dismesses all group * resources. */ public void resignGroup(GroupNode groupNode) { Group g = groupNode.getGroup(); boolean resigned = resignGroup(g); if (resigned) { // and update the JxtaTree for (Iterator n = groupNode.getChildren(); n.hasNext();) { removeJxtaNode((JxtaNode) n.next()); } if (g.isVisible()) { view.removeGroupNavigation(g); } } } private boolean resignGroup(Group g) { setStatus(STRINGS.getString("status.group.resign") + " " + g.getName()); // first notify the plugins that we are leaving a group m_pluginContainer.notifyAboutGroupResign(g); // now lets remove the core modules und listeners that are no longer // needed // in this group if (g.isJoined()) { PeerGroup peerGroup = g.getPeerGroup(); String peerNameInGroup = peerGroup.getPeerName(); String pn = Dialog.getDialogNamer(OneToOneCommandDialog.class) .getDialogName(peerNameInGroup);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -