예제 #1
0
  @Test
  public void shouldGetAllPropertiesOnJavaBean() throws Exception {
    Status status = Status.INFO;
    int code = 121;
    I18n msg = CommonI18n.argumentMayNotBeEmpty;
    Object[] params = new Object[] {"argName"};
    String resource = "The source";
    String location = "The place to be";
    Throwable throwable = null;
    Problem problem = new Problem(status, code, msg, params, resource, location, throwable);
    Reflection reflection = new Reflection(Problem.class);
    List<Property> props = reflection.getAllPropertiesOn(problem);
    Map<String, Property> propsByName = reflection.getAllPropertiesByNameOn(problem);

    assertThat(props.size(), is(8));
    assertThat(propsByName.size(), is(8));
    assertThat(props.containsAll(propsByName.values()), is(true));

    Property property = propsByName.remove("status");
    assertThat(property.getName(), is("status"));
    assertThat(property.getLabel(), is("Status"));
    assertThat(property.getType().equals(Status.class), is(true));
    assertThat(property.isReadOnly(), is(true));
    assertThat(property, is(findProperty(property.getName(), props)));
    assertValue(reflection, problem, property, status);

    property = propsByName.remove("code");
    assertThat(property.getName(), is("code"));
    assertThat(property.getLabel(), is("Code"));
    assertThat(property.getType().equals(Integer.TYPE), is(true));
    assertThat(property.isReadOnly(), is(true));
    assertValue(reflection, problem, property, code);

    property = propsByName.remove("message");
    assertThat(property.getName(), is("message"));
    assertThat(property.getLabel(), is("Message"));
    assertThat(property.getType().equals(I18n.class), is(true));
    assertThat(property.isReadOnly(), is(true));
    assertValue(reflection, problem, property, msg);

    property = propsByName.remove("messageString");
    assertThat(property.getName(), is("messageString"));
    assertThat(property.getLabel(), is("Message String"));
    assertThat(property.getType().equals(String.class), is(true));
    assertThat(property.isReadOnly(), is(true));
    assertValue(reflection, problem, property, msg.text(params));

    property = propsByName.remove("parameters");
    assertThat(property.getName(), is("parameters"));
    assertThat(property.getLabel(), is("Parameters"));
    assertThat(property.getType().equals(Object[].class), is(true));
    assertThat(property.isReadOnly(), is(true));
    assertValue(reflection, problem, property, params);

    property = propsByName.remove("resource");
    assertThat(property.getName(), is("resource"));
    assertThat(property.getLabel(), is("Resource"));
    assertThat(property.getType().equals(String.class), is(true));
    assertThat(property.isReadOnly(), is(true));
    assertValue(reflection, problem, property, resource);

    property = propsByName.remove("location");
    assertThat(property.getName(), is("location"));
    assertThat(property.getLabel(), is("Location"));
    assertThat(property.getType().equals(String.class), is(true));
    assertThat(property.isReadOnly(), is(true));
    assertValue(reflection, problem, property, location);

    property = propsByName.remove("throwable");
    assertThat(property.getName(), is("throwable"));
    assertThat(property.getLabel(), is("Throwable"));
    assertThat(property.getType().equals(Throwable.class), is(true));
    assertThat(property.isReadOnly(), is(true));
    assertValue(reflection, problem, property, throwable);

    assertThat(propsByName.isEmpty(), is(true));
  }
    SelfContained(
        ExecutionContext context,
        String nameOfSourceToBeSearchable,
        RepositoryConnectionFactory connectionFactory,
        Observable observable,
        RepositoryNodeTypeManager nodeTypeManager,
        String indexDirectory,
        boolean updateIndexesSynchronously,
        int maxDepthPerRead)
        throws RepositoryException {
      super(nameOfSourceToBeSearchable);

      this.context = context;
      this.sourceName = nameOfSourceToBeSearchable;
      this.connectionFactory = connectionFactory;
      this.maxDepthPerRead = maxDepthPerRead;
      // Define the configuration ...
      TextEncoder encoder = new UrlEncoder();
      if (indexDirectory != null) {
        File indexDir = new File(indexDirectory);
        if (indexDir.exists()) {
          // The location does exist ...
          if (!indexDir.isDirectory()) {
            // The path is not a directory ...
            I18n msg = JcrI18n.searchIndexDirectoryOptionSpecifiesFileNotDirectory;
            throw new RepositoryException(msg.text(indexDirectory, sourceName));
          }
          if (!indexDir.canWrite()) {
            // But we cannot write to it ...
            I18n msg = JcrI18n.searchIndexDirectoryOptionSpecifiesDirectoryThatCannotBeWrittenTo;
            throw new RepositoryException(msg.text(indexDirectory, sourceName));
          }
          if (!indexDir.canRead()) {
            // But we cannot write to it ...
            I18n msg = JcrI18n.searchIndexDirectoryOptionSpecifiesDirectoryThatCannotBeRead;
            throw new RepositoryException(msg.text(indexDirectory, sourceName));
          }
          // The directory is usable
        } else {
          // The location doesn't exist,so try to make it ...
          if (!indexDir.mkdirs()) {
            I18n msg = JcrI18n.searchIndexDirectoryOptionSpecifiesDirectoryThatCannotBeCreated;
            throw new RepositoryException(msg.text(indexDirectory, sourceName));
          }
          // We successfully create the dirctory (or directories)
        }
        configuration = LuceneConfigurations.using(indexDir, encoder, encoder);
      } else {
        // Use in-memory as a fall-back ...
        configuration = LuceneConfigurations.inMemory();
      }
      assert configuration != null;

      // Set up the indexing rules ...
      IndexRules indexRules = nodeTypeManager.getRepositorySchemata().getIndexRules();

      // Set up the search engine ...
      org.apache.lucene.analysis.Analyzer analyzer = new EnglishAnalyzer(Version.LUCENE_30);
      boolean verifyWorkspaces = false;
      searchEngine =
          new LuceneSearchEngine(
              nameOfSourceToBeSearchable,
              connectionFactory,
              verifyWorkspaces,
              maxDepthPerRead,
              configuration,
              indexRules,
              analyzer);

      // Set up an original source observer to keep the index up to date ...
      if (updateIndexesSynchronously) {
        this.service = null;
        this.searchObserver =
            new Observer() {
              @SuppressWarnings("synthetic-access")
              public void notify(Changes changes) {
                if (changes.getSourceName().equals(sourceName)) {
                  process(changes);
                }
              }
            };
      } else {
        // It's asynchronous, so create a single-threaded executor and an observer that enqueues the
        // results
        ThreadFactory threadFactory =
            new ThreadFactory() {
              /**
               * {@inheritDoc}
               *
               * @see java.util.concurrent.ThreadFactory#newThread(java.lang.Runnable)
               */
              public Thread newThread(Runnable r) {
                Thread thread = new Thread("modeshape-indexing");
                thread.setPriority(Thread.NORM_PRIORITY + 3);
                return thread;
              }
            };
        // this.service = Executors.newCachedThreadPool(threadFactory);
        this.service = Executors.newSingleThreadExecutor(threadFactory);
        this.searchObserver =
            new Observer() {
              @SuppressWarnings("synthetic-access")
              public void notify(final Changes changes) {
                if (changes.getSourceName().equals(sourceName)) {
                  service.submit(
                      new Runnable() {
                        public void run() {
                          process(changes);
                        }
                      });
                }
              }
            };
      }
      observable.register(this.searchObserver);

      // Set up the query engine ...
      Planner planner = new CanonicalPlanner();

      // Create a custom optimizer that has our rules first ...
      Optimizer optimizer =
          new RuleBasedOptimizer() {
            /**
             * {@inheritDoc}
             *
             * @see
             *     org.modeshape.graph.query.optimize.RuleBasedOptimizer#populateRuleStack(java.util.LinkedList,
             *     org.modeshape.graph.query.plan.PlanHints)
             */
            @Override
            protected void populateRuleStack(LinkedList<OptimizerRule> ruleStack, PlanHints hints) {
              super.populateRuleStack(ruleStack, hints);
              ruleStack.addFirst(RewritePseudoColumns.INSTANCE);
            }
          };

      // Create a custom processor that knows how to submit the access query requests ...
      Processor processor =
          new QueryProcessor() {

            /**
             * {@inheritDoc}
             *
             * @see
             *     org.modeshape.graph.query.process.QueryProcessor#createAccessComponent(org.modeshape.graph.query.model.QueryCommand,
             *     org.modeshape.graph.query.QueryContext, org.modeshape.graph.query.plan.PlanNode,
             *     org.modeshape.graph.query.QueryResults.Columns,
             *     org.modeshape.graph.query.process.SelectComponent.Analyzer)
             */
            @Override
            protected ProcessingComponent createAccessComponent(
                QueryCommand originalQuery,
                QueryContext context,
                PlanNode accessNode,
                Columns resultColumns,
                Analyzer analyzer) {
              return new AccessQueryProcessor(
                  (GraphQueryContext) context, resultColumns, accessNode);
            }
          };
      this.queryEngine = new QueryEngine(planner, optimizer, processor);

      // Index any existing content ...
      reindexContent();
    }