/**
   * Returns the appropriate {@link Protocol} implementation for a url.
   *
   * @param urlString Url String
   * @return The appropriate {@link Protocol} implementation for a given {@link URL}.
   * @throws ProtocolNotFound when Protocol can not be found for urlString
   */
  public ProtocolHbase getProtocol(String urlString) throws ProtocolNotFound {
    ObjectCache objectCache = ObjectCache.get(conf);
    try {
      URL url = new URL(urlString);
      String protocolName = url.getProtocol();
      String cacheId = ProtocolHbase.X_POINT_ID + protocolName;
      if (protocolName == null) throw new ProtocolNotFound(urlString);

      if (objectCache.getObject(cacheId) != null) {
        return (ProtocolHbase) objectCache.getObject(cacheId);
      } else {
        Extension extension = findExtension(protocolName);
        if (extension == null) {
          throw new ProtocolNotFound(protocolName);
        }

        ProtocolHbase protocol = (ProtocolHbase) extension.getExtensionInstance();

        objectCache.setObject(cacheId, protocol);

        return protocol;
      }

    } catch (MalformedURLException e) {
      throw new ProtocolNotFound(urlString, e.toString());
    } catch (PluginRuntimeException e) {
      throw new ProtocolNotFound(urlString, e.toString());
    }
  }
Exemple #2
0
  /**
   * Constructor that configures the cache of ResponseWriter objects.
   *
   * @param conf The Nutch configuration object.
   */
  public ResponseWriters(Configuration conf) {

    // get the cache and the cache key
    String cacheKey = ResponseWriter.class.getName();
    ObjectCache objectCache = ObjectCache.get(conf);
    this.responseWriters = (Map<String, ResponseWriter>) objectCache.getObject(cacheKey);

    // if already populated do nothing
    if (this.responseWriters == null) {

      try {

        // get the extension point and all ResponseWriter extensions
        ExtensionPoint point =
            PluginRepository.get(conf).getExtensionPoint(ResponseWriter.X_POINT_ID);
        if (point == null) {
          throw new RuntimeException(ResponseWriter.X_POINT_ID + " not found.");
        }

        // populate content type on the ResponseWriter classes, each response
        // writer can handle more than one response type
        Extension[] extensions = point.getExtensions();
        Map<String, ResponseWriter> writers = new HashMap<String, ResponseWriter>();
        for (int i = 0; i < extensions.length; i++) {
          Extension extension = extensions[i];
          ResponseWriter writer = (ResponseWriter) extension.getExtensionInstance();
          String[] responseTypes = extension.getAttribute("responseType").split(",");
          String contentType = extension.getAttribute("contentType");
          writer.setContentType(contentType);
          for (int k = 0; k < responseTypes.length; k++) {
            writers.put(responseTypes[k], writer);
          }
        }

        // set null object if no writers, otherwise set the writers
        if (writers == null) {
          objectCache.setObject(cacheKey, new HashMap<String, ResponseWriter>());
        } else {
          objectCache.setObject(cacheKey, writers);
        }
      } catch (PluginRuntimeException e) {
        throw new RuntimeException(e);
      }

      // set the response writers map
      this.responseWriters = (Map<String, ResponseWriter>) objectCache.getObject(cacheKey);
    }
  }