📄 cms.java
字号:
/* * Copyright (c) 2001 Sun Microsystems, Inc. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * 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 disclaimer 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: CMS.java,v 1.30 2006/02/07 20:43:49 bondolo Exp $ * */package net.jxta.share;import java.io.File;import java.io.InputStream;import java.io.IOException;import net.jxta.discovery.DiscoveryService;import net.jxta.document.Advertisement;import net.jxta.document.MimeMediaType;import net.jxta.endpoint.EndpointService;import net.jxta.endpoint.EndpointAddress;import net.jxta.endpoint.EndpointListener;import net.jxta.endpoint.Message;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.ByteArrayMessageElement;import net.jxta.exception.PeerGroupException;import net.jxta.id.ID;import net.jxta.peergroup.PeerGroup;import net.jxta.platform.Application;import net.jxta.protocol.ResolverQueryMsg;import net.jxta.protocol.ResolverResponseMsg;import net.jxta.resolver.ResolverService;import net.jxta.resolver.QueryHandler;import org.apache.log4j.Logger;import org.apache.log4j.Level;/** * This class implements the Content Management Service. * The class is started with one of the startApp methods. * The command pipe and persistent directory is passed to the startApp method, else * default values are used. * A remote client interacts with the service by finding the request PipeAdvertisement * and sending it LIST and GET requests. The requests are handled by the * ListMessageProcessor and GetMessageProcessor. */public class CMS implements Application, EndpointListener, QueryHandler { private final static transient Logger LOG = Logger.getLogger(CMS.class.getName()); public static final String DEFAULT_DIR = "cms"; // Message tag's public static final String LIST_REQUEST = "LIST_REQ"; public static final String LIST_RESULT = "LIST_RES"; public static final String GET_REQUEST = "GET_REQ"; public static final String GET_RESULT = "GET_RES"; public static final String MESSAGE_TYPE = "msgType"; public static final String REQUEST_ID = "RID"; public static final String CONTENT_ID = "CID"; public static final String RETURN_ADDRESS = "RTN_ADR"; public static final String CONTENT_ADVERTISEMENT = "CADV"; public static final String CONTENT_LENGTH = "CLEN"; public static final String ADVERTISEMENT_COUNT = "COUNT"; public static final String CHUNK_OFFSET = "OFFSET"; public static final String CHUNK_SIZE = "SIZE"; public static final String CHUNK_DATA = "DATA"; public static final String RANGE_BEGIN = "RANGE_BEGIN"; public static final String RANGE_END = "RANGE_END"; public static final String QUERY_SUBSTRING = "QSUBSTR"; public static String serviceName = "CMS"; public static MimeMediaType encodeAs = new MimeMediaType("text", "xml"); // The following two constants defines how long ContentAdvertisements are published // in Discovery. // FIXME: 11/03/2002 lomax@jxta.org We need to have a better lifecycle management // of the advertisements. Current settings are probably fine for usual usage of CMS // for the time being. private static long DEFAULT_LOCAL_ADV_LIFETIME = DiscoveryService.DEFAULT_LIFETIME; private static long DEFAULT_REMOTE_ADV_LIFETIME = 2 * 60 * 1000L; // 2 minutes private PeerGroup group = null; private ListMessageProcessor listQueue = null; private GetMessageProcessor getQueue = null; private ContentManager contentManager = null; private File cmsDir = null; public CMS() { // Set the default content manager directory to a subdirectory of the // JXTA_HOME directory. String homedir = System.getProperty("JXTA_HOME"); homedir = (homedir != null) ? homedir + DEFAULT_DIR : DEFAULT_DIR; cmsDir = new File(homedir); if (LOG.isEnabledFor(Level.INFO)) LOG.info("create new CMS"); // Makes sure the AdvertisementFactory knows about ContentAdvertisement ContentAdvertisementImpl.registerAdvertisement(); } /** * Initialize the application passing it ist peer group handle. * * @param group PeerGroup this application is started from * * @exception PeerGroupException failure to initialize the peer group application * */ public void init(PeerGroup group, ID assignedID, Advertisement impl) throws PeerGroupException { this.group = group; } /** * Start the service with the given parameters. * The parameters are in the form of name=value pairs. * There is one optional parameter, the directory for storing * persistent data about the shared content. * * @param arg String[] of parameters * * @return int status indication. 0 if ok, -1 if error */ public int startApp(String[] arg) { File cmsDir = null; for (int i=0; i<arg.length; i++) { // Directory parameter if (arg[i].startsWith("dir=")) { cmsDir = new File(arg[i].substring(4)); } } return startApp(cmsDir); } /** * Start the service with the given parameters. * * @param rPipeAdv PipeAdvertisement of the request pipe * @param cmsDir File pointing to the persistence directory * * @return int status indication. 0 if ok, -1 if error. */ public int startApp(File dir) { // check if a cmsDir was passed if(dir != null) cmsDir = dir; // Check if the cms directory exists if (false == cmsDir.exists()) { // create the directory cmsDir.mkdir(); } // Initialize the content manager try { contentManager = new ContentManagerImpl(cmsDir, this); } catch (IOException e) { if (LOG.isEnabledFor(Level.ERROR)) LOG.error("Could not initialize content manager"); return (-1); } // initialize the message processor queues listQueue = new ListMessageProcessor(this); getQueue = new GetMessageProcessor(this); // register the message listener group.getEndpointService().addIncomingMessageListener(this,serviceName, getGroupId(group)); // Publish the contentManager publishAllContents (); return 0; } /** * Stops the service */ public void stopApp() { // unregister the message listener group.getEndpointService().removeIncomingMessageListener(serviceName, getGroupId(group)); group.getResolverService().unregisterHandler(serviceName + getGroupId (group)); } /** * Add a SearchListener object to monitor the search queries recieved. * * @param sl the SearchListener to add * @see SearchListener * @see #removeSearchListener(SearchListener) */ public void addSearchListener(SearchListener sl) { if(listQueue != null) listQueue.addSearchListener(sl);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -