君子博学而日参省乎己 则知明而行无过矣

博客园 首页 新随笔 联系 订阅 管理

现在再次回顾企业搜索引擎开发之连接器connector(三)中UML图示中的servlet类

ConnectorManagerServlet类与ConnectorManagerGetServlet类均为抽象类,继承自HttpServlet类

它们均提供了抽象方法,供子类具体实现,本身的servlet的override方法里面调用了各自的抽象方法,这种处理方式类似于template method模式,在它们的子类override方法里面有些通过调度处理器来执行具体逻辑,具体源码我这里不粘贴出来了

这些servlet实现类部分用到了Manager类型的实例(ProductionManager实例),这个东东也就是对Instantiator类型的进一步封装

先熟悉一下Manager接口源码提供了哪些接口方法

/**
 * The main interface to the Connector Manager. Front ends such as servlets or
 * main() programs may be built using this.
 */
public interface Manager {

  /**
   * Gets the current configuration of the Connector Manager itself.
   *
   * @return the current configuration settings in a Properties object.  Keys
   *         to the Properties are the same as the keys in the property file.
   * @throws PersistentStoreException if there was a problem retrieving the
   *         configuration.
   */
  public Properties getConnectorManagerConfig() throws PersistentStoreException;

  /**
   * Stores configuration changes to the Connector Manager itself.
   *
   * @param feederGateHost The GSA host expressed as a String
   * @param feederGatePort The GSA feeder port number
   * @throws PersistentStoreException If there was a problem storing the
   *         configuration
   */
  public void setConnectorManagerConfig(String feederGateHost,
      int feederGatePort) throws PersistentStoreException;

  /**
   * Returns a list of connector types that this manager knows about.
   *
   * @return A Set of Strings - the name of each connector implementation.
   */
  public Set<String> getConnectorTypeNames();

  /**
   * Returns the ConnectorType that is associated with the supplied name.
   *
   * @param typeName a ConnectorType name.
   * @return an instance of ConnectorType associated with the typeName.
   * @throws ConnectorTypeNotFoundException if the connector type is not found.
   */
  public ConnectorType getConnectorType(String typeName)
      throws ConnectorTypeNotFoundException;

  /**
   * Returns a list of ConnectorStatus objects for each connector that this
   * manager knows about.
   *
   * @return A list of ConnectorStatus objects.
   */
  public List<ConnectorStatus> getConnectorStatuses();

  /**
   * Returns the status of a particular connector.
   *
   * @param connectorName the name of the connector instance
   * @return Document containing XML configuration - DTD TBD.
   */
  public ConnectorStatus getConnectorStatus(String connectorName);

  /**
   * Get initial configuration form snippet for a connector type.
   *
   * @param connectorTypeName The name of a connector implementation - it should
   *        be one that this manager knows about (one that would be returned by
   *        a call to getConnectorTypes()).
   * @param language A locale string, such as "en" or "fr_CA" which the
   *        implementation may use to produce appropriate descriptions and
   *        messages
   * @return a ConfigureResponse object, which may be null. If the return object
   *         is null or the form is null or empty, then the caller will use a
   *         default form.
   * @throws ConnectorTypeNotFoundException If the named connector type is not
   *         known to this manager.
   * @throws InstantiatorException
   */
  public ConfigureResponse getConfigForm(String connectorTypeName,
      String language) throws ConnectorTypeNotFoundException,
      InstantiatorException;

  /**
   * Get configuration data as a form snippet for an existing connnector. This
   * is different from getConfigForm because this is used to change the
   * configuration of a saved, configured Connector instance, not to configure a
   * new Connector instance.
   *
   * @param connectorName The connector for which to fetch configuration
   * @param language A locale string, such as "en" or "fr_CA" which the
   *        implementation may use to produce appropriate descriptions and
   *        messages
   * @return a ConfigureResponse object. As above, if the return object is null
   *         or the message and form are null or empty, then the caller will use
   *         a default form.
   * @throws ConnectorNotFoundException If the named connector is not known to
   *         this manager.
   * @throws InstantiatorException
   */
  public ConfigureResponse getConfigFormForConnector(String connectorName,
      String language) throws ConnectorNotFoundException, InstantiatorException;

  /**
   * Set config data for a new Connector or update config data for a running
   * Connector instance
   *
   * @param connectorName The connector to update
   * @param connectorTypeName The connector's type
   * @param configData A map of name, value pairs (String, String) of
   *        configuration data to submit
   * @param language A locale string, such as "en" or "fr_CA" which the
   *        implementation may use to produce appropriate descriptions and
   *        messages
   * @param update A boolean true to update the giving existing connector,
   *        false to create a new connector for the given connector name
   * @return a ConfigureResponse object. If the return object is null, then this
   *         means that the configuration was valid and has been successfully
   *         stored. If the object is non-null, then the caller should try
   *         again.
   * @throws ConnectorNotFoundException If the named connector is not known to
   *         this manager.
   * @throws PersistentStoreException If there was a problem storing the
   *         configuration
   * @throws InstantiatorException If the instantiator cannot store the
   *         configuration
   */
  public ConfigureResponse setConnectorConfig(String connectorName,
      String connectorTypeName, Map<String, String> configData,
      String language, boolean update)
      throws ConnectorNotFoundException, ConnectorExistsException,
      PersistentStoreException, InstantiatorException;

  /**
   * Authenticates an Identity against a named connector.
   *
   * @param connectorName
   * @param identity An AuthenticationIdentity object that encapsulates the
   *        user's identity
   * @return true for success.
   */
  public boolean authenticate(String connectorName, AuthenticationIdentity identity);

  /**
   * Gets authorization from a named connector for a set of documents by ID.
   *
   * @param connectorName
   * @param docidList The document set represented as a list of Strings: the
   *        docid for each document
   * @param identity An AuthenticationIdentity object that encapsulates the
   *        user's identity
   * @return A Set of String IDs indicating which documents the user can see.
   */
  public Set<String> authorizeDocids(String connectorName,
      List<String> docidList, AuthenticationIdentity identity);

  /**
   * Set schedule for a given Connector.
   *
   * @param connectorName
   * @param schedule stringified Schedule
   * @throws ConnectorNotFoundException If the named connector is not known to
   *         this manager.
   * @throws PersistentStoreException If there was a problem storing the
   *         configuration
   */
  public void setSchedule(String connectorName, String schedule)
      throws ConnectorNotFoundException, PersistentStoreException;

  /*
   * Remove a connector for a given Connector.
   *
   * @param connectorName
   * @throws ConnectorNotFoundException If the named connector is not known to
   *         this manager.
   * @throws PersistentStoreException If there was a problem storing the
   *         configuration
   */
  public void removeConnector(String connectorName)
      throws ConnectorNotFoundException, PersistentStoreException,
      InstantiatorException;

  /**
   * Restart the Traverser for the named connector.
   * This resets the Traverser, re-indexing the repository from scratch.
   *
   * @param connectorName
   * @throws ConnectorNotFoundException
   * @throws InstantiatorException
   */
  public void restartConnectorTraversal(String connectorName)
      throws ConnectorNotFoundException, InstantiatorException;

  /**
   * Get a connector's ConnectorType-specific configuration data
   *
   * @param connectorName the connector to look up
   * @return a Map&lt;String, String&gt; of its ConnectorType-specific
   * configuration data
   * @throws ConnectorNotFoundException if the named connector is not found
   */
  public Map<String, String> getConnectorConfig(String connectorName)
      throws ConnectorNotFoundException;

  /**
   * @return true if the manager is currently locked, false otherwise.
   */
  public boolean isLocked();
}

实现类ProductionManager源码如下: 

/**
 *
 */
public class ProductionManager implements Manager {
  private static final Logger LOGGER =
      Logger.getLogger(ProductionManager.class.getName());

  Instantiator instantiator;

  public ProductionManager() {
  }

  /**
   * @param instantiator the instantiator to set
   */
  public void setInstantiator(Instantiator instantiator) {
    this.instantiator = instantiator;
  }

  /* @Override */
  public boolean authenticate(String connectorName,
      AuthenticationIdentity identity) {
    boolean result = false;

    try {
      AuthenticationManager authnManager =
          instantiator.getAuthenticationManager(connectorName);
      AuthenticationResponse authenticationResponse;
      // Some connectors don't implement the AuthenticationManager interface so
      // we need to check.
      if (authnManager != null) {
        authenticationResponse = authnManager.authenticate(identity);
      } else {
        authenticationResponse = new AuthenticationResponse(false, null);
      }
      result = authenticationResponse.isValid();
    } catch (ConnectorNotFoundException e) {
      LOGGER.log(Level.WARNING, "Connector " + connectorName + " Not Found: ",
          e);
    } catch (InstantiatorException e) {
      LOGGER.log(Level.WARNING, "Instantiator: ", e);
    } catch (RepositoryLoginException e) {
      LOGGER.log(Level.WARNING, "Login: ", e);
    } catch (RepositoryException e) {
      LOGGER.log(Level.WARNING, "Repository: ", e);
    } catch (Exception e) {
      LOGGER.log(Level.WARNING, "Exception: ", e);
    }

    return result;
  }

  /* @Override */
  public Set<String> authorizeDocids(String connectorName,
      List<String> docidList, AuthenticationIdentity identity) {
    Set<String> result = new HashSet<String>();
    try {
      AuthorizationManager authzManager =
          instantiator.getAuthorizationManager(connectorName);
      if (authzManager == null) {
        // This is a bad situation.  This means the Connector has feed the
        // content in such a way that it is being asked to authorize access to
        // that content and yet it doesn't implement the AuthorizationManager
        // interface.  Log the situation and return the empty result.
        LOGGER.warning("Connector:" + connectorName
            + " is being asked to authorize documents but has not implemented"
            + " the AuthorizationManager interface.");
        return result;
      }
      Collection<AuthorizationResponse> results =
          authzManager.authorizeDocids(docidList, identity);
      for (AuthorizationResponse response : results) {
        if (response.isValid()) {
          result.add(response.getDocid());
        }
      }
    } catch (ConnectorNotFoundException e) {
      LOGGER.log(Level.WARNING, "Connector " + connectorName + " Not Found: ",
          e);
    } catch (InstantiatorException e) {
      LOGGER.log(Level.WARNING, "Instantiator: ", e);
    } catch (RepositoryException e) {
      LOGGER.log(Level.WARNING, "Repository: ", e);
    } catch (Exception e) {
      LOGGER.log(Level.WARNING, "Exception: ", e);
    }

    return result;
  }

  /* @Override */
  public ConfigureResponse getConfigForm(String connectorTypeName,
      String language)
      throws ConnectorTypeNotFoundException, InstantiatorException {
    ConnectorType connectorType =
        instantiator.getConnectorType(connectorTypeName);
    Locale locale = I18NUtil.getLocaleFromStandardLocaleString(language);
    try {
      return connectorType.getConfigForm(locale);
    } catch (Exception e) {
      throw new InstantiatorException("Failed to get configuration form.", e);
    }
  }

  /* @Override */
  public ConfigureResponse getConfigFormForConnector(String connectorName,
      String language)
      throws ConnectorNotFoundException, InstantiatorException {
    String connectorTypeName = instantiator.getConnectorTypeName(connectorName);
    Locale locale = I18NUtil.getLocaleFromStandardLocaleString(language);
    ConfigureResponse response =
        instantiator.getConfigFormForConnector(connectorName,
            connectorTypeName, locale);
    return response;
  }

  /* @Override */
  public ConnectorStatus getConnectorStatus(String connectorName) {
    String connectorTypeName = null;
    try {
      connectorTypeName = instantiator.getConnectorTypeName(connectorName);
      String schedule = instantiator.getConnectorSchedule(connectorName);
      // TODO: resolve the third parameter - we need to give status a meaning
      return new ConnectorStatus(connectorName, connectorTypeName, 0, schedule);
    } catch (ConnectorNotFoundException e) {
      // TODO: this should become part of the signature - so we should just
      // let this exception bubble up
      LOGGER.log(Level.WARNING, "Connector type " + connectorTypeName
          + " Not Found: ", e);
      throw new IllegalArgumentException();
    }
  }

  /* @Override */
  public List<ConnectorStatus> getConnectorStatuses() {
    List<ConnectorStatus> result = new ArrayList<ConnectorStatus>();
    for (String connectorName : instantiator.getConnectorNames()) {
      result.add(getConnectorStatus(connectorName));
    }
    return result;
  }

  /* @Override */
  public Set<String> getConnectorTypeNames() {
    return instantiator.getConnectorTypeNames();
  }

  /* @Override */
  public ConnectorType getConnectorType(String typeName)
      throws ConnectorTypeNotFoundException {
    return instantiator.getConnectorType(typeName);
  }

  /* @Override */
  public ConfigureResponse setConnectorConfig(String connectorName,
      String connectorTypeName, Map<String, String> configData,
      String language, boolean update) throws ConnectorNotFoundException,
      PersistentStoreException, InstantiatorException {
    Locale locale = I18NUtil.getLocaleFromStandardLocaleString(language);
    return instantiator.setConnectorConfig(connectorName, connectorTypeName,
        configData, locale, update);
  }

  /* @Override */
  public Properties getConnectorManagerConfig()
      throws PersistentStoreException {
    try {
      return Context.getInstance().getConnectorManagerConfig();
    } catch (InstantiatorException e) {
      throw new PersistentStoreException(e);
    }
  }

  /* @Override */
  public void setConnectorManagerConfig(String feederGateHost,
      int feederGatePort) throws PersistentStoreException {
    try {
      Context.getInstance().setConnectorManagerConfig(feederGateHost,
          feederGatePort);
    } catch (InstantiatorException e) {
      throw new PersistentStoreException(e);
    }
  }

  /* @Override */
  public void setSchedule(String connectorName, String schedule)
      throws ConnectorNotFoundException, PersistentStoreException {
    instantiator.setConnectorSchedule(connectorName, schedule);
  }

  /* @Override */
  public void removeConnector(String connectorName)
      throws InstantiatorException {
    instantiator.removeConnector(connectorName);
  }

  /* @Override */
  public void restartConnectorTraversal(String connectorName)
      throws ConnectorNotFoundException, InstantiatorException {
    instantiator.restartConnectorTraversal(connectorName);
  }

  /* @Override */
  public Map<String, String> getConnectorConfig(String connectorName)
      throws ConnectorNotFoundException {
    return instantiator.getConnectorConfig(connectorName);
  }

  /* @Override */
  public boolean isLocked() {
    return Context.getInstance().getIsManagerLocked();

写到本文我也有点倦意,以后如有心得再作补充

---------------------------------------------------------------------------

本系列企业搜索引擎开发之连接器connector系本人原创

转载请注明出处 博客园 刺猬的温驯

本文链接 http://www.cnblogs.com/chenying99/archive/2013/03/20/2970385.html

posted on 2013-03-20 01:47  刺猬的温驯  阅读(398)  评论(0编辑  收藏  举报