Exemplo n.º 1
0
  static MailSessionConfig from(final OperationContext operationContext, final ModelNode model)
      throws OperationFailedException {
    MailSessionConfig cfg = new MailSessionConfig();

    cfg.setJndiName(
        MailSessionDefinition.JNDI_NAME.resolveModelAttribute(operationContext, model).asString());
    cfg.setDebug(
        MailSessionDefinition.DEBUG.resolveModelAttribute(operationContext, model).asBoolean());
    if (MailSessionDefinition.FROM.resolveModelAttribute(operationContext, model).isDefined()) {
      cfg.setFrom(
          MailSessionDefinition.FROM.resolveModelAttribute(operationContext, model).asString());
    }
    if (model.hasDefined(SERVER_TYPE)) {
      ModelNode server = model.get(SERVER_TYPE);
      if (server.hasDefined(SMTP)) {
        cfg.setSmtpServer(readServerConfig(operationContext, server.get(SMTP)));
      }
      if (server.hasDefined(POP3)) {
        cfg.setPop3Server(readServerConfig(operationContext, server.get(POP3)));
      }
      if (server.hasDefined(IMAP)) {
        cfg.setImapServer(readServerConfig(operationContext, server.get(IMAP)));
      }
    }
    if (model.hasDefined(CUSTOM)) {
      for (Property server : model.get(CUSTOM).asPropertyList()) {
        cfg.addCustomServer(
            readCustomServerConfig(server.getName(), operationContext, server.getValue()));
      }
    }
    return cfg;
  }
Exemplo n.º 2
0
  static void installRuntimeServices(
      OperationContext context,
      PathAddress address,
      ModelNode fullModel,
      ServiceVerificationHandler verificationHandler,
      List<ServiceController<?>> controllers)
      throws OperationFailedException {
    String name = address.getLastElement().getValue();

    final String jndiName = getJndiName(fullModel, context);
    final ServiceTarget serviceTarget = context.getServiceTarget();

    final MailSessionConfig config = from(context, fullModel);
    final MailSessionService service = new MailSessionService(config);
    final ServiceName serviceName = MAIL_SESSION_SERVICE_NAME.append(name);
    final ServiceBuilder<?> mailSessionBuilder = serviceTarget.addService(serviceName, service);
    addOutboundSocketDependency(service, mailSessionBuilder, config.getImapServer());
    addOutboundSocketDependency(service, mailSessionBuilder, config.getPop3Server());
    addOutboundSocketDependency(service, mailSessionBuilder, config.getSmtpServer());
    for (CustomServerConfig server : config.getCustomServers()) {
      if (server.getOutgoingSocketBinding() != null) {
        addOutboundSocketDependency(service, mailSessionBuilder, server);
      }
    }

    final ManagedReferenceFactory valueManagedReferenceFactory =
        new MailSessionManagedReferenceFactory(service);
    final ContextNames.BindInfo bindInfo = ContextNames.bindInfoFor(jndiName);
    final BinderService binderService = new BinderService(bindInfo.getBindName());
    final ServiceBuilder<?> binderBuilder =
        serviceTarget
            .addService(bindInfo.getBinderServiceName(), binderService)
            .addInjection(binderService.getManagedObjectInjector(), valueManagedReferenceFactory)
            .addDependency(
                bindInfo.getParentContextServiceName(),
                ServiceBasedNamingStore.class,
                binderService.getNamingStoreInjector())
            .addListener(
                new AbstractServiceListener<Object>() {
                  public void transition(
                      final ServiceController<? extends Object> controller,
                      final ServiceController.Transition transition) {
                    switch (transition) {
                      case STARTING_to_UP:
                        {
                          MailLogger.ROOT_LOGGER.boundMailSession(jndiName);
                          break;
                        }
                      case START_REQUESTED_to_DOWN:
                        {
                          MailLogger.ROOT_LOGGER.unboundMailSession(jndiName);
                          break;
                        }
                      case REMOVING_to_REMOVED:
                        {
                          MailLogger.ROOT_LOGGER.removedMailSession(jndiName);
                          break;
                        }
                    }
                  }
                });

    mailSessionBuilder
        .setInitialMode(ServiceController.Mode.ACTIVE)
        .addListener(verificationHandler);
    binderBuilder.setInitialMode(ServiceController.Mode.ACTIVE).addListener(verificationHandler);
    controllers.add(mailSessionBuilder.install());
    controllers.add(binderBuilder.install());
  }
Exemplo n.º 3
0
  /**
   * Make any runtime changes necessary to effect the changes indicated by the given {@code
   * operation}. E
   *
   * <p>It constructs a MailSessionService that provides mail session and registers it to Naming
   * service.
   *
   * @param context the operation context
   * @param operation the operation being executed
   * @param model persistent configuration model node that corresponds to the address of {@code
   *     operation}
   * @param verificationHandler step handler that can be added as a listener to any new services
   *     installed in order to validate the services installed correctly during the {@link
   *     org.jboss.as.controller.OperationContext.Stage#VERIFY VERIFY stage}
   * @param controllers holder for the {@link org.jboss.msc.service.ServiceController} for any new
   *     services installed by the method. The method should add the {@code ServiceController} for
   *     any new services to this list. If the overall operation needs to be rolled back, the list
   *     will be used in {@link #rollbackRuntime(org.jboss.as.controller.OperationContext,
   *     org.jboss.dmr.ModelNode, org.jboss.dmr.ModelNode, java.util.List)} to automatically removed
   *     the newly added services
   * @throws org.jboss.as.controller.OperationFailedException if {@code operation} is invalid or
   *     updating the runtime otherwise fails
   */
  @Override
  protected void performRuntime(
      OperationContext context,
      ModelNode operation,
      ModelNode model,
      ServiceVerificationHandler verificationHandler,
      List<ServiceController<?>> controllers)
      throws OperationFailedException {
    final String jndiName = getJndiName(operation);
    final ServiceTarget serviceTarget = context.getServiceTarget();

    ModelNode fullTree = Resource.Tools.readModel(context.readResource(PathAddress.EMPTY_ADDRESS));
    final MailSessionConfig config = from(context, fullTree);
    final MailSessionService service = new MailSessionService(config);
    final ServiceName serviceName = SERVICE_NAME_BASE.append(jndiName);
    final ServiceBuilder<?> mailSessionBuilder = serviceTarget.addService(serviceName, service);
    addOutboundSocketDependency(service, mailSessionBuilder, config.getImapServer());
    addOutboundSocketDependency(service, mailSessionBuilder, config.getPop3Server());
    addOutboundSocketDependency(service, mailSessionBuilder, config.getSmtpServer());
    for (CustomServerConfig server : config.getCustomServers()) {
      if (server.getOutgoingSocketBinding() != null) {
        addOutboundSocketDependency(service, mailSessionBuilder, server);
      }
    }

    final ManagedReferenceFactory valueManagedReferenceFactory =
        new ContextListAndJndiViewManagedReferenceFactory() {

          @Override
          public String getJndiViewInstanceValue() {
            return String.valueOf(getReference().getInstance());
          }

          @Override
          public String getInstanceClassName() {
            final Object value = getReference().getInstance();
            return value != null
                ? value.getClass().getName()
                : ContextListManagedReferenceFactory.DEFAULT_INSTANCE_CLASS_NAME;
          }

          @Override
          public ManagedReference getReference() {
            return new ValueManagedReference(new ImmediateValue<Object>(service.getValue()));
          }
        };
    final ContextNames.BindInfo bindInfo = ContextNames.bindInfoFor(jndiName);
    final BinderService binderService = new BinderService(bindInfo.getBindName());
    final ServiceBuilder<?> binderBuilder =
        serviceTarget
            .addService(bindInfo.getBinderServiceName(), binderService)
            .addInjection(binderService.getManagedObjectInjector(), valueManagedReferenceFactory)
            .addDependency(
                bindInfo.getParentContextServiceName(),
                ServiceBasedNamingStore.class,
                binderService.getNamingStoreInjector())
            .addListener(
                new AbstractServiceListener<Object>() {
                  public void transition(
                      final ServiceController<? extends Object> controller,
                      final ServiceController.Transition transition) {
                    switch (transition) {
                      case STARTING_to_UP:
                        {
                          MailLogger.ROOT_LOGGER.boundMailSession(jndiName);
                          break;
                        }
                      case START_REQUESTED_to_DOWN:
                        {
                          MailLogger.ROOT_LOGGER.unboundMailSession(jndiName);
                          break;
                        }
                      case REMOVING_to_REMOVED:
                        {
                          MailLogger.ROOT_LOGGER.removedMailSession(jndiName);
                          break;
                        }
                    }
                  }
                });

    mailSessionBuilder
        .setInitialMode(ServiceController.Mode.ACTIVE)
        .addListener(verificationHandler);
    binderBuilder.setInitialMode(ServiceController.Mode.ACTIVE).addListener(verificationHandler);
    controllers.add(mailSessionBuilder.install());
    controllers.add(binderBuilder.install());
  }