private void parseSecurity(OperationContext context, ModelNode model, EditableDocument configDoc)
      throws OperationFailedException {
    EditableDocument security = configDoc.getOrCreateDocument(FieldName.SECURITY);

    // Anonymous ...
    EditableDocument anon = security.getOrCreateDocument(FieldName.ANONYMOUS);
    String anonUsername = attribute(context, model, ModelAttributes.ANONYMOUS_USERNAME).asString();
    boolean useAnonIfFailed =
        attribute(context, model, ModelAttributes.USE_ANONYMOUS_IF_AUTH_FAILED).asBoolean();
    anon.set(FieldName.ANONYMOUS_USERNAME, anonUsername);
    anon.set(FieldName.USE_ANONYMOUS_ON_FAILED_LOGINS, useAnonIfFailed);
    List<ModelNode> modelNodes =
        model.hasDefined(ModelKeys.ANONYMOUS_ROLES)
            ? model.get(ModelKeys.ANONYMOUS_ROLES).asList()
            : ModelAttributes.ANONYMOUS_ROLES.getDefaultValue().asList();
    for (ModelNode roleNode : modelNodes) {
      EditableArray anonymousRolesArray = anon.getOrCreateArray(FieldName.ANONYMOUS_ROLES);
      String roleName = roleNode.asString();
      if (!StringUtil.isBlank(roleName)) {
        anonymousRolesArray.addString(roleName);
      }
    }

    EditableArray providers = security.getOrCreateArray(FieldName.PROVIDERS);

    // JBoss authenticator ...
    String securityDomain = attribute(context, model, ModelAttributes.SECURITY_DOMAIN).asString();
    EditableDocument jboss = Schematic.newDocument();
    jboss.set(FieldName.CLASSNAME, JBossDomainAuthenticationProvider.class.getName());
    jboss.set(FieldName.SECURITY_DOMAIN, securityDomain);
    providers.add(jboss);

    // Servlet authenticator ...
    EditableDocument servlet = Schematic.newDocument();
    servlet.set(FieldName.CLASSNAME, "servlet");
    providers.add(servlet);
  }
  @Override
  public void start(StartContext arg0) throws StartException {
    JcrEngine jcr = getEngine();
    try {
      final String repositoryName = repositoryConfiguration.getName();

      // Get the index storage configuration ...
      IndexStorage indexStorageConfig = indexStorageConfigInjector.getValue();
      Document queryConfig = null;
      if (indexStorageConfig != null) {
        queryConfig = indexStorageConfig.getQueryConfiguration();
      } else {
        // We'll use the default index storage, but this will be overwritten by the *IndexStorageAdd
        // operation
        // (that we're dependent upon). The default for non-AS7 ModeShape repositories is to use
        // RAM index storage, but in AS7 we want to by default store the indexes on the filesystem
        // in the
        // AS7 data directory.
        // We'll do this by setting a path relative to the data directory, and then injecting
        // the "${jboss.server.data.dir}/modeshape" path into the repository service
        // (which will then update the configuration prior to deployment) ...
        EditableDocument query = Schematic.newDocument();
        EditableDocument indexing = query.getOrCreateDocument(FieldName.INDEXING);
        EditableDocument indexStorage = query.getOrCreateDocument(FieldName.INDEX_STORAGE);
        EditableDocument backend = indexing.getOrCreateDocument(FieldName.INDEXING_BACKEND);
        query.set(FieldName.REBUILD_UPON_STARTUP, "if_needed");
        backend.set(FieldName.TYPE, FieldValue.INDEXING_BACKEND_TYPE_LUCENE);
        indexStorage.set(FieldName.TYPE, FieldValue.INDEX_STORAGE_FILESYSTEM);
        String dataDirPath = dataDirectoryPathInjector.getValue();
        indexStorage.set(
            FieldName.INDEX_STORAGE_LOCATION, dataDirPath + "/" + repositoryName + "/indexes");
        queryConfig = query;
      }
      assert queryConfig != null;

      // Get the binary storage configuration ...
      Document binaryConfig = null;
      BinaryStorage binaryStorageConfig = binaryStorageInjector.getValue();
      if (binaryStorageConfig != null) {
        binaryConfig = binaryStorageConfig.getBinaryConfiguration();
      } else {
        // By default, store the binaries in the data directory ...
        EditableDocument binaries = Schematic.newDocument();
        binaries.set(FieldName.TYPE, FieldValue.BINARY_STORAGE_TYPE_FILE);
        String dataDirPath = dataDirectoryPathInjector.getValue();
        binaries.set(FieldName.DIRECTORY, dataDirPath + "/" + repositoryName + "/binaries");
        binaryConfig = binaries;
      }

      // Now update the configuration ...
      Editor editor = repositoryConfiguration.edit();
      editor.setDocument(FieldName.QUERY, queryConfig);
      editor
          .getOrCreateDocument(FieldName.STORAGE)
          .setDocument(FieldName.BINARY_STORAGE, binaryConfig);

      // Apply the changes to the configuration ...
      editor.apply(editor.getChanges());

      // Deploy the repository and use this as the environment ...
      jcr.deploy(repositoryConfiguration.with(this));
    } catch (ConfigurationException e) {
      throw new StartException(e);
    } catch (RepositoryException e) {
      throw new StartException(e);
    }
  }
  @Override
  protected void performRuntime(
      final OperationContext context,
      final ModelNode operation,
      final ModelNode model,
      final ServiceVerificationHandler verificationHandler,
      final List<ServiceController<?>> newControllers)
      throws OperationFailedException {

    final ServiceTarget target = context.getServiceTarget();
    final AddressContext addressContext = AddressContext.forOperation(operation);
    final String repositoryName = addressContext.repositoryName();
    final String cacheName = attribute(context, model, ModelAttributes.CACHE_NAME, repositoryName);
    String infinispanConfig = attribute(context, model, ModelAttributes.CACHE_CONFIG, null);
    String configRelativeTo =
        attribute(context, model, ModelAttributes.CONFIG_RELATIVE_TO).asString();
    final boolean enableMonitoring =
        attribute(context, model, ModelAttributes.ENABLE_MONITORING).asBoolean();
    final String gcThreadPool =
        attribute(context, model, ModelAttributes.GARBAGE_COLLECTION_THREAD_POOL, null);
    final String gcInitialTime =
        attribute(context, model, ModelAttributes.GARBAGE_COLLECTION_INITIAL_TIME, null);
    final int gcIntervalInHours =
        attribute(context, model, ModelAttributes.GARBAGE_COLLECTION_INTERVAL).asInt();
    final String optThreadPool =
        attribute(context, model, ModelAttributes.DOCUMENT_OPTIMIZATION_THREAD_POOL, null);
    final String optInitialTime =
        attribute(context, model, ModelAttributes.DOCUMENT_OPTIMIZATION_INITIAL_TIME, null);
    final int optIntervalInHours =
        attribute(context, model, ModelAttributes.DOCUMENT_OPTIMIZATION_INTERVAL).asInt();
    final Integer optTarget =
        intAttribute(
            context, model, ModelAttributes.DOCUMENT_OPTIMIZATION_CHILD_COUNT_TARGET, null);
    final Integer eventBusSize = intAttribute(context, model, ModelAttributes.EVENT_BUS_SIZE, null);
    final Integer optTolerance =
        intAttribute(
            context, model, ModelAttributes.DOCUMENT_OPTIMIZATION_CHILD_COUNT_TOLERANCE, null);

    // Create a document for the repository configuration ...
    EditableDocument configDoc = Schematic.newDocument();
    configDoc.set(FieldName.NAME, repositoryName);

    // Determine the JNDI name ...
    configDoc.set(
        FieldName.JNDI_NAME,
        ""); // always set to empty string, since we'll register in JNDI here ...
    final String jndiName = ModeShapeJndiNames.JNDI_BASE_NAME + repositoryName;
    String jndiAlias = ModeShapeJndiNames.jndiNameFrom(model, repositoryName);
    if (jndiName.equals(jndiAlias)) {
      jndiAlias = null;
    }

    if (eventBusSize != null) {
      configDoc.setNumber(FieldName.EVENT_BUS_SIZE, eventBusSize);
    }

    // Parse the cache configuration
    if (StringUtil.isBlank(infinispanConfig)) {
      infinispanConfig = "modeshape/" + repositoryName + "-cache-config.xml";
    } else {
      // check if it's a system property
      String infinispanConfigSystemProperty = System.getProperty(infinispanConfig);
      if (!StringUtil.isBlank(infinispanConfigSystemProperty)) {
        infinispanConfig = infinispanConfigSystemProperty;
      }
    }
    // Set the storage information (that was set on the repository ModelNode) ...
    setRepositoryStorageConfiguration(infinispanConfig, cacheName, configDoc);

    // Always set whether monitoring is enabled ...
    enableMonitoring(enableMonitoring, configDoc);

    // Initial node-types if configured
    parseCustomNodeTypes(model, configDoc);

    // Workspace information is on the repository model node (unlike the XML) ...
    EditableDocument workspacesDoc = parseWorkspaces(context, model, configDoc);

    // security
    parseSecurity(context, model, configDoc);

    // Now create the repository service that manages the lifecycle of the JcrRepository instance
    // ...
    RepositoryConfiguration repositoryConfig =
        new RepositoryConfiguration(configDoc, repositoryName);
    String configRelativeToSystemProperty = System.getProperty(configRelativeTo);
    if (!StringUtil.isBlank(configRelativeToSystemProperty)) {
      configRelativeTo = configRelativeToSystemProperty;
    }
    if (!configRelativeTo.endsWith("/")) {
      configRelativeTo = configRelativeTo + "/";
    }
    RepositoryService repositoryService =
        new RepositoryService(repositoryConfig, infinispanConfig, configRelativeTo);
    ServiceName repositoryServiceName = ModeShapeServiceNames.repositoryServiceName(repositoryName);

    // Sequencing
    parseSequencing(model, configDoc);

    // Text Extraction
    parseTextExtraction(model, configDoc);

    // Reindexing
    parseReindexing(model, configDoc);

    // Journaling
    parseJournaling(repositoryService, context, model, configDoc);

    // Add the EngineService's dependencies ...
    ServiceBuilder<JcrRepository> repositoryServiceBuilder =
        target.addService(repositoryServiceName, repositoryService);

    // Add dependency to the ModeShape engine service ...
    repositoryServiceBuilder.addDependency(
        ModeShapeServiceNames.ENGINE, ModeShapeEngine.class, repositoryService.getEngineInjector());
    repositoryServiceBuilder.setInitialMode(ServiceController.Mode.ACTIVE);

    // Add garbage collection information ...
    if (gcThreadPool != null) {
      configDoc
          .getOrCreateDocument(FieldName.GARBAGE_COLLECTION)
          .setString(FieldName.THREAD_POOL, gcThreadPool);
    }
    if (gcInitialTime != null) {
      configDoc
          .getOrCreateDocument(FieldName.GARBAGE_COLLECTION)
          .setString(FieldName.INITIAL_TIME, gcInitialTime);
    }
    configDoc
        .getOrCreateDocument(FieldName.GARBAGE_COLLECTION)
        .setNumber(FieldName.INTERVAL_IN_HOURS, gcIntervalInHours);

    // Add document optimization information ...
    if (optTarget != null) {
      EditableDocument docOpt =
          configDoc
              .getOrCreateDocument(FieldName.STORAGE)
              .getOrCreateDocument(FieldName.DOCUMENT_OPTIMIZATION);
      if (optThreadPool != null) {
        docOpt.setString(FieldName.THREAD_POOL, optThreadPool);
      }
      if (optInitialTime != null) {
        docOpt.setString(FieldName.INITIAL_TIME, optInitialTime);
      }
      docOpt.setNumber(FieldName.INTERVAL_IN_HOURS, optIntervalInHours);
      docOpt.setNumber(FieldName.OPTIMIZATION_CHILD_COUNT_TARGET, optTarget.intValue());
      if (optTolerance != null) {
        docOpt.setNumber(FieldName.OPTIMIZATION_CHILD_COUNT_TOLERANCE, optTolerance.intValue());
      }
    }

    // Add the dependency to the Security Manager
    repositoryServiceBuilder.addDependency(
        SecurityManagementService.SERVICE_NAME,
        ISecurityManagement.class,
        repositoryService.getSecurityManagementServiceInjector());

    // Add dependency, if necessary, to the workspaces cache container
    String workspacesInfinispanConfig =
        attribute(context, model, ModelAttributes.WORKSPACES_CACHE_CONTAINER, null);
    if (workspacesInfinispanConfig != null
        && !workspacesInfinispanConfig.toLowerCase().equalsIgnoreCase(infinispanConfig)) {
      workspacesDoc.set(FieldName.WORKSPACE_CACHE_CONFIGURATION, workspacesInfinispanConfig);
    }

    repositoryServiceBuilder.addDependency(
        Services.JBOSS_SERVICE_MODULE_LOADER,
        ModuleLoader.class,
        repositoryService.getModuleLoaderInjector());

    // Set up the JNDI binder service ...
    final ReferenceFactoryService<JcrRepository> referenceFactoryService =
        new ReferenceFactoryService<JcrRepository>();
    ServiceName referenceFactoryServiceName =
        ModeShapeServiceNames.referenceFactoryServiceName(repositoryName);
    final ServiceBuilder<?> referenceBuilder =
        target.addService(referenceFactoryServiceName, referenceFactoryService);
    referenceBuilder.addDependency(
        repositoryServiceName, JcrRepository.class, referenceFactoryService.getInjector());
    referenceBuilder.setInitialMode(ServiceController.Mode.ACTIVE);

    ContextNames.BindInfo bindInfo = ContextNames.bindInfoFor(jndiName);
    BinderService binder = new BinderService(bindInfo.getBindName());
    ServiceBuilder<?> binderBuilder = target.addService(bindInfo.getBinderServiceName(), binder);
    if (jndiAlias != null) {
      ContextNames.BindInfo aliasInfo = ContextNames.bindInfoFor(jndiAlias);
      ServiceName alias = aliasInfo.getBinderServiceName();
      binderBuilder.addAliases(alias);
      LOG.debugv(
          "Binding repository {0} to JNDI name {1} and {2}",
          repositoryName, bindInfo.getAbsoluteJndiName(), aliasInfo.getAbsoluteJndiName());
    } else {
      LOG.debugv(
          "Binding repository {0} to JNDI name {1}",
          repositoryName, bindInfo.getAbsoluteJndiName());
    }
    binderBuilder.addDependency(
        referenceFactoryServiceName,
        ManagedReferenceFactory.class,
        binder.getManagedObjectInjector());
    binderBuilder.addDependency(
        bindInfo.getParentContextServiceName(),
        ServiceBasedNamingStore.class,
        binder.getNamingStoreInjector());
    binderBuilder.setInitialMode(ServiceController.Mode.ACTIVE);

    // Add dependency to the data directory ...
    ServiceName dataDirServiceName = ModeShapeServiceNames.dataDirectoryServiceName(repositoryName);
    ServiceController<String> dataDirServiceController =
        RelativePathService.addService(
            dataDirServiceName,
            "modeshape/" + repositoryName,
            ModeShapeExtension.JBOSS_DATA_DIR_VARIABLE,
            target);
    newControllers.add(dataDirServiceController);
    repositoryServiceBuilder.addDependency(
        dataDirServiceName, String.class, repositoryService.getDataDirectoryPathInjector());

    // Add the default binary storage service which will provide the binary configuration
    BinaryStorageService defaultBinaryService = BinaryStorageService.createDefault();
    ServiceName defaultBinaryStorageServiceName =
        ModeShapeServiceNames.binaryStorageDefaultServiceName(repositoryName);
    ServiceBuilder<BinaryStorage> binaryStorageBuilder =
        target.addService(defaultBinaryStorageServiceName, defaultBinaryService);
    binaryStorageBuilder.setInitialMode(ServiceController.Mode.ACTIVE);
    // Add dependency to the binaries storage service, which captures the properties for the
    // binaries storage
    repositoryServiceBuilder.addDependency(
        defaultBinaryStorageServiceName,
        BinaryStorage.class,
        repositoryService.getBinaryStorageInjector());

    // Add monitor service
    final MonitorService monitorService = new MonitorService();
    final ServiceBuilder<RepositoryMonitor> monitorBuilder =
        target.addService(ModeShapeServiceNames.monitorServiceName(repositoryName), monitorService);
    monitorBuilder.addDependency(
        ModeShapeServiceNames.repositoryServiceName(repositoryName),
        JcrRepository.class,
        monitorService.getJcrRepositoryInjector());
    monitorBuilder.setInitialMode(ServiceController.Mode.ACTIVE);

    // Now add the controller for the RepositoryService ...
    newControllers.add(repositoryServiceBuilder.install());
    newControllers.add(referenceBuilder.install());
    newControllers.add(binderBuilder.install());
    newControllers.add(binaryStorageBuilder.install());
    newControllers.add(monitorBuilder.install());
  }
Beispiel #4
0
  @Override
  public void start(StartContext arg0) throws StartException {
    ModeShapeEngine engine = getEngine();

    try {
      final String repositoryName = repositoryName();

      // Get the index storage configuration ...
      IndexStorage indexStorageConfig = indexStorageConfigInjector.getValue();
      Document queryConfig = null;
      if (indexStorageConfig != null) {
        queryConfig = indexStorageConfig.getQueryConfiguration();
      } else {
        // We'll use the default index storage, but this will be overwritten by the *IndexStorageAdd
        // operation
        // (that we're dependent upon). The default for non-AS7 ModeShape repositories is to use
        // RAM index storage, but in AS7 we want to by default store the indexes on the filesystem
        // in the
        // AS7 data directory.
        // We'll do this by setting a path relative to the data directory, and then injecting
        // the "${jboss.server.data.dir}/modeshape" path into the repository service
        // (which will then update the configuration prior to deployment) ...
        EditableDocument query = Schematic.newDocument();
        EditableDocument indexing = query.getOrCreateDocument(FieldName.INDEXING);
        EditableDocument indexStorage = query.getOrCreateDocument(FieldName.INDEX_STORAGE);
        EditableDocument backend = indexing.getOrCreateDocument(FieldName.INDEXING_BACKEND);
        query.set(FieldName.REBUILD_UPON_STARTUP, QueryRebuild.IF_MISSING.toString().toLowerCase());
        backend.set(FieldName.TYPE, FieldValue.INDEXING_BACKEND_TYPE_LUCENE);
        indexStorage.set(FieldName.TYPE, FieldValue.INDEX_STORAGE_FILESYSTEM);
        String dataDirPath = dataDirectoryPathInjector.getValue();
        indexStorage.set(
            FieldName.INDEX_STORAGE_LOCATION, dataDirPath + "/" + repositoryName + "/indexes");
        queryConfig = query;
      }
      assert queryConfig != null;

      // Get the binary storage configuration ...
      Document binaryConfig = null;
      BinaryStorage binaryStorageConfig = binaryStorageInjector.getValue();
      if (binaryStorageConfig != null) {
        binaryConfig = binaryStorageConfig.getBinaryConfiguration();
      } else {
        // By default, store the binaries in the data directory ...
        EditableDocument binaries = Schematic.newDocument();
        binaries.set(FieldName.TYPE, FieldValue.BINARY_STORAGE_TYPE_FILE);
        String dataDirPath = dataDirectoryPathInjector.getValue();
        binaries.set(FieldName.DIRECTORY, dataDirPath + "/" + repositoryName + "/binaries");
        binaryConfig = binaries;
      }

      // Create a new configuration document ...
      EditableDocument config = Schematic.newDocument(repositoryConfiguration.getDocument());
      config.setDocument(FieldName.QUERY, queryConfig);
      config
          .getOrCreateDocument(FieldName.STORAGE)
          .setDocument(FieldName.BINARY_STORAGE, binaryConfig);

      if (LOG.isDebugEnabled()) {
        LOG.debugv("ModeShape configuration for '{0}' repository: {1}", repositoryName, config);
        Problems problems = repositoryConfiguration.validate();
        if (problems.isEmpty()) {
          LOG.debugv(
              "Problems with configuration for '{0}' repository: {1}", repositoryName, problems);
        }
      }

      // Create a new (updated) configuration ...
      repositoryConfiguration = new RepositoryConfiguration(config, repositoryName);

      // Deploy the repository and use this as the environment ...
      engine.deploy(repositoryConfiguration.with(this));
    } catch (ConfigurationException e) {
      throw new StartException(e);
    } catch (RepositoryException e) {
      throw new StartException(e);
    }
  }