📄 protocolfactory.java
字号:
/** * Copyright 2005 The Apache Software Foundation * * Licensed 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.nutch.protocol;import java.net.URL;import java.net.MalformedURLException;// Commons Logging importsimport org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.nutch.plugin.*;import org.apache.hadoop.conf.Configuration;/** * Creates and caches {@link Protocol} plugins. Protocol plugins should * define the attribute "protocolName" with the name of the protocol that they * implement. Configuration object is used for caching. Cache key is * constructed from appending protocol name (eg. http) to * constant {@link Protocol#X_POINT_ID). */public class ProtocolFactory { public static final Log LOG = LogFactory.getLog(ProtocolFactory.class); private ExtensionPoint extensionPoint; private Configuration conf; public ProtocolFactory(Configuration conf) { this.conf = conf; this.extensionPoint = PluginRepository.get(conf) .getExtensionPoint(Protocol.X_POINT_ID); if (this.extensionPoint == null) { throw new RuntimeException("x-point " + Protocol.X_POINT_ID + " not found."); } } /** * Returns the appropriate {@link Protocol} implementation for a url. * @param urlString Url String * @return * @throws ProtocolNotFound when Protocol can not be found for urlString */ public Protocol getProtocol(String urlString) throws ProtocolNotFound { try { URL url = new URL(urlString); String protocolName = url.getProtocol(); String cacheId=Protocol.X_POINT_ID + protocolName; if (protocolName == null) throw new ProtocolNotFound(urlString); if (conf.getObject(cacheId) != null) { return (Protocol) conf.getObject(cacheId); } else { Extension extension = findExtension(protocolName); if (extension == null) { throw new ProtocolNotFound(protocolName); } Protocol protocol = (Protocol) extension.getExtensionInstance(); conf.setObject(cacheId, protocol); return protocol; } } catch (MalformedURLException e) { throw new ProtocolNotFound(urlString, e.toString()); } catch (PluginRuntimeException e) { throw new ProtocolNotFound(urlString, e.toString()); } } private Extension findExtension(String name) throws PluginRuntimeException { Extension[] extensions = this.extensionPoint.getExtensions(); for (int i = 0; i < extensions.length; i++) { Extension extension = extensions[i]; if (name.equals(extension.getAttribute("protocolName"))) return extension; } return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -