⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gdataservice.java

📁 一套java版本的搜索引擎源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.lucene.gdata.server;import java.io.IOException;import java.util.Date;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.lucene.gdata.data.ServerBaseEntry;import org.apache.lucene.gdata.data.ServerBaseFeed;import org.apache.lucene.gdata.server.registry.ComponentType;import org.apache.lucene.gdata.server.registry.EntryEventMediator;import org.apache.lucene.gdata.server.registry.GDataServerRegistry;import org.apache.lucene.gdata.storage.ModificationConflictException;import org.apache.lucene.gdata.storage.ResourceNotFoundException;import org.apache.lucene.gdata.storage.Storage;import org.apache.lucene.gdata.storage.StorageController;import org.apache.lucene.gdata.storage.StorageException;import com.google.gdata.data.BaseEntry;import com.google.gdata.data.BaseFeed;import com.google.gdata.data.DateTime;import com.google.gdata.data.Generator;import com.google.gdata.data.Link;import com.google.gdata.util.ParseException;/** * default implementation of the {@link org.apache.lucene.gdata.server.Service} * interface. *  * @author Simon Willnauer *  */public class GDataService implements Service {    private static final Log LOG = LogFactory.getLog(GDataService.class);    protected Storage storage;    protected GDataServerRegistry registry = GDataServerRegistry.getRegistry();    private static final Generator generator;    private static final String generatorName = "Lucene GData-Server";    private static final String generatorURI = "http://lucene.apache.org";    private static final String XMLMIME = "application/atom+xml";        protected final EntryEventMediator entryEventMediator;    static {        generator = new Generator();        generator.setName(generatorName);        generator.setUri(generatorURI);        generator.setVersion("0.1");    }    protected GDataService() throws ServiceException {        try {            StorageController controller = GDataServerRegistry.getRegistry()                    .lookup(StorageController.class,                            ComponentType.STORAGECONTROLLER);            if (controller == null)                throw new StorageException(                        "StorageController is not registered");            this.storage = controller.getStorage();            this.entryEventMediator = GDataServerRegistry.getRegistry().getEntryEventMediator();        } catch (StorageException e) {            LOG                    .fatal(                            "Can't get Storage Instance -- can't serve any requests",                            e);            ServiceException ex = new ServiceException(                    "Can't get Storage instance" + e.getMessage(), e,GDataResponse.SERVER_ERROR);            ex.setStackTrace(e.getStackTrace());            throw ex;        }    }    /**     * @see org.apache.lucene.gdata.server.Service#createEntry(org.apache.lucene.gdata.server.GDataRequest,     *      org.apache.lucene.gdata.server.GDataResponse)     */    public BaseEntry createEntry(GDataRequest request, GDataResponse response)            throws ServiceException {        if (LOG.isInfoEnabled())            LOG.info("create Entry for feedId: " + request.getFeedId());        ServerBaseEntry entry = buildEntry(request, response);        entry.setFeedId(request.getFeedId());        entry.setServiceConfig(request.getConfigurator());        BaseEntry tempEntry = entry.getEntry();        tempEntry.setPublished(getCurrentDateTime());        tempEntry.setUpdated(getCurrentDateTime());        BaseEntry retVal = null;        removeDynamicElements(entry.getEntry());        try {            retVal = this.storage.storeEntry(entry);        } catch (Exception e) {                        ServiceException ex = new ServiceException("Could not store entry",                    e,GDataResponse.SERVER_ERROR);            ex.setStackTrace(e.getStackTrace());            throw ex;        }        this.entryEventMediator.entryAdded(entry);        return retVal;    }    /**     * @see org.apache.lucene.gdata.server.Service#deleteEntry(org.apache.lucene.gdata.server.GDataRequest,     *      org.apache.lucene.gdata.server.GDataResponse)     */    public BaseEntry deleteEntry(GDataRequest request, GDataResponse response)            throws ServiceException {        ServerBaseEntry entry = new ServerBaseEntry();        entry.setServiceConfig(request.getConfigurator());        entry.setFeedId(request.getFeedId());        entry.setId(request.getEntryId());        setVersionId(entry,request,response);        if (entry.getId() == null)            throw new ServiceException(                    "entry id is null -- can not delete null entry",GDataResponse.SERVER_ERROR);        try {            this.storage.deleteEntry(entry);                    } catch (ResourceNotFoundException e) {                        ServiceException ex = new ServiceException(                    "Could not delete entry", e,GDataResponse.BAD_REQUEST);            ex.setStackTrace(e.getStackTrace());            throw ex;        }catch (ModificationConflictException e) {                        ServiceException ex = new ServiceException(                    "Could not delete entry - version conflict",e, GDataResponse.CONFLICT);            ex.setStackTrace(e.getStackTrace());            throw ex;          }catch (StorageException e) {                        ServiceException ex = new ServiceException(                    "Could not delete entry", e,GDataResponse.SERVER_ERROR);            ex.setStackTrace(e.getStackTrace());            throw ex;        }        this.entryEventMediator.entryDeleted(entry);        //TODO change ret value        return null;    }    /**     * @see org.apache.lucene.gdata.server.Service#updateEntry(org.apache.lucene.gdata.server.GDataRequest,     *      org.apache.lucene.gdata.server.GDataResponse)     */    public BaseEntry updateEntry(GDataRequest request, GDataResponse response)            throws ServiceException {        ServerBaseEntry entry = buildEntry(request, response);        entry.setFeedId(request.getFeedId());        setVersionId(entry,request,response);        entry.setServiceConfig(request.getConfigurator());        if (LOG.isInfoEnabled())            LOG.info("update Entry" + entry.getId() + " for feedId: "                    + request.getFeedId());        if (entry.getId() == null) {            throw new ServiceException("Entry id is null can not update entry",GDataResponse.BAD_REQUEST);        }        if (!entry.getId().equals(request.getEntryId())) {            if (LOG.isInfoEnabled())                LOG                        .info("Entry id in the entry xml does not match the requested resource -- XML-ID:"                                + entry.getId()                                + "; Requested resource: "                                + request.getEntryId());            throw new ServiceException(                    "Entry id in the entry xml does not match the requested resource",GDataResponse.BAD_REQUEST);        }        BaseEntry tempEntry = entry.getEntry();        tempEntry.setUpdated(getCurrentDateTime());        removeDynamicElements(entry.getEntry());                BaseEntry retVal = null;             try {            retVal = this.storage.updateEntry(entry);        } catch (ResourceNotFoundException e) {                        ServiceException ex = new ServiceException(                    "Could not update entry", e,GDataResponse.BAD_REQUEST);            ex.setStackTrace(e.getStackTrace());            throw ex;        }catch (ModificationConflictException e) {            ServiceException ex = new ServiceException(                    "Could not update entry - version conflict", e,GDataResponse.CONFLICT);            ex.setStackTrace(e.getStackTrace());            throw ex;        }catch (StorageException e) {            ServiceException ex = new ServiceException(                    "Could not update entry", e,GDataResponse.SERVER_ERROR);            ex.setStackTrace(e.getStackTrace());            throw ex;        }        this.entryEventMediator.entryUpdated(entry);        return retVal;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -