Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2011, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.as.connector.subsystems.datasources;
 
 import static org.jboss.as.connector.ConnectorLogger.SUBSYSTEM_DATASOURCES_LOGGER;
 import static org.jboss.as.connector.ConnectorMessages.MESSAGES;
 import static org.jboss.as.connector.subsystems.datasources.Constants.JNDINAME;
 import static org.jboss.as.connector.subsystems.datasources.DataSourceModelNodeUtil.from;
 import static org.jboss.as.connector.subsystems.datasources.DataSourceModelNodeUtil.xaFrom;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ENABLED;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.PERSISTENT;
 
 import java.util.List;
 
Operation handler responsible for enabling an existing data-source.

Author(s):
John Bailey
 
 public class DataSourceEnable implements OperationStepHandler {
     static final DataSourceEnable LOCAL_INSTANCE = new DataSourceEnable(false);
     static final DataSourceEnable XA_INSTANCE = new DataSourceEnable(true);
 
     private final boolean xa;
 
     public DataSourceEnable(boolean xa) {
         super();
         this. = xa;
     }
 
     public void execute(OperationContext contextModelNode operation) {
 
         final ModelNode model = context.readResourceForUpdate(.).getModel();
 
         // On boot we invoke this op but we may not want to store a default value the model
         boolean persist = operation.get().asBoolean(true);
         if (persist) {
             model.get().set(true);
         } else if (model.hasDefined() && !model.get().asBoolean()) {
             // Just clear the "false" value that gets stored by default
             model.get().set(new ModelNode());
         }
 
         if (context.getType() == ..) {
 
             context.addStep(new OperationStepHandler() {
                 public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
                     final ServiceTarget serviceTarget = context.getServiceTarget();
                     final ServiceVerificationHandler verificationHandler = new ServiceVerificationHandler();
 
                     final ModelNode address = operation.require();
                     final String dsName = PathAddress.pathAddress(address).getLastElement().getValue();
                     final String jndiName = model.get(.getName()).asString();
                     final ServiceRegistry registry = context.getServiceRegistry(true);
 
                     final List<ServiceNameserviceNames = registry.getServiceNames();
 
 
                     if (isXa()) {
                         final ModifiableXaDataSource dataSourceConfig;
                        try {
                            dataSourceConfig = xaFrom(contextmodeldsName);
                        } catch (ValidateException e) {
                            throw new OperationFailedException(enew ModelNode().set(.failedToCreate("XaDataSource"operatione.getLocalizedMessage())));
                        }
                        final ServiceName xaDataSourceConfigServiceName = ..append(dsName);
                        final XADataSourceConfigService xaDataSourceConfigService = new XADataSourceConfigService(dataSourceConfig);
                        final ServiceBuilder<?> builder = serviceTarget.addService(xaDataSourceConfigServiceNamexaDataSourceConfigService);
                        builder.addListener(verificationHandler);
                        for (ServiceName name : serviceNames) {
                            if (xaDataSourceConfigServiceName.append("xa-datasource-properties").isParentOf(name)) {
                                final ServiceController<?> xaConfigProperyController = registry.getService(name);
                                XaDataSourcePropertiesService xaPropService = (XaDataSourcePropertiesServicexaConfigProperyController.getService();
                                if (xaConfigProperyController != null) {
                                    if (! ...equals(xaConfigProperyController.getState())) {
                                        xaConfigProperyController.setMode(..);
                                        builder.addDependency(nameString.classxaDataSourceConfigService.getXaDataSourcePropertyInjector(xaPropService.getName()));
                                    } else {
                                        throw new OperationFailedException(new ModelNode().set(.serviceAlreadyStarted("Data-source.xa-config-property"name)));
                                    }
                                } else {
                                    throw new OperationFailedException(new ModelNode().set(.serviceNotAvailable("Data-source.xa-config-property"name)));
                                }
                            }
                        }
                        builder.install();
                    } else {
                        final ModifiableDataSource dataSourceConfig;
                        try {
                            dataSourceConfig = from(contextmodel,dsName);
                        } catch (ValidateException e) {
                            throw new OperationFailedException(enew ModelNode().set(.failedToCreate("DataSource"operatione.getLocalizedMessage())));
                        }
                        final ServiceName dataSourceCongServiceName = ..append(dsName);
                        final DataSourceConfigService configService = new DataSourceConfigService(dataSourceConfig);
                        final ServiceBuilder<?> builder = serviceTarget.addService(dataSourceCongServiceNameconfigService);
                        builder.addListener(verificationHandler);
                        for (ServiceName name : serviceNames) {
                            if (dataSourceCongServiceName.append("connetion-properties").isParentOf(name)) {
                                final ServiceController<?> dataSourceController = registry.getService(name);
                                ConnectionPropertiesService connPropService = (ConnectionPropertiesServicedataSourceController.getService();
                                if (dataSourceController != null) {
                                    if (!...equals(dataSourceController.getState())) {
                                        dataSourceController.setMode(..);
                                        builder.addDependency(nameString.classconfigService.getConnectionPropertyInjector(connPropService.getName()));
                                    } else {
                                        throw new OperationFailedException(new ModelNode().set(.serviceAlreadyStarted("Data-source.connectionProperty"name)));
                                    }
                                } else {
                                    throw new OperationFailedException(new ModelNode().set(.serviceNotAvailable("Data-source.connectionProperty"name)));
                                }
                            }
                        }
                        builder.install();
                    }
                    context.completeStep();
                }
            }, ..);
            context.addStep(new OperationStepHandler() {
                public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
                    final ServiceTarget serviceTarget = context.getServiceTarget();
                    final ServiceVerificationHandler verificationHandler = new ServiceVerificationHandler();
                    final ModelNode address = operation.require();
                    final String dsName = PathAddress.pathAddress(address).getLastElement().getValue();
                    final String jndiName = model.get(.getName()).asString();
                    final ServiceRegistry registry = context.getServiceRegistry(true);
                    final ServiceName dataSourceServiceName = ..append(jndiName);
                    final List<ServiceNameserviceNames = registry.getServiceNames();
                    final ServiceController<?> dataSourceController = registry.getService(dataSourceServiceName);
                    if (dataSourceController != null) {
                        if (!...equals(dataSourceController.getState())) {
                            dataSourceController.setMode(..);
                        } else {
                            throw new OperationFailedException(new ModelNode().set(.serviceAlreadyStarted("Data-source"dsName)));
                        }
                    } else {
                        throw new OperationFailedException(new ModelNode().set(.serviceNotAvailable("Data-source"dsName)));
                    }
                    final DataSourceReferenceFactoryService referenceFactoryService = new DataSourceReferenceFactoryService();
                    final ServiceName referenceFactoryServiceName = .
                            .append(dsName);
                    final ServiceBuilder<?> referenceBuilder = serviceTarget.addService(referenceFactoryServiceName,
                            referenceFactoryService).addDependency(dataSourceServiceNamejavax.sql.DataSource.class,
                            referenceFactoryService.getDataSourceInjector());
                    referenceBuilder.install();
                    final ContextNames.BindInfo bindInfo = ContextNames.bindInfoFor(jndiName);
                    final BinderService binderService = new BinderService(bindInfo.getBindName());
                    final ServiceBuilder<?> binderBuilder = serviceTarget
                            .addService(bindInfo.getBinderServiceName(), binderService)
                            .addDependency(referenceFactoryServiceNameManagedReferenceFactory.classbinderService.getManagedObjectInjector())
                            .addDependency(bindInfo.getParentContextServiceName(), ServiceBasedNamingStore.classbinderService.getNamingStoreInjector()).addListener(new AbstractServiceListener<Object>() {
                                public void transition(final ServiceController<? extends Objectcontrollerfinal ServiceController.Transition transition) {
                                    switch (transition) {
                                        case : {
                                            .boundDataSource(jndiName);
                                            break;
                                        }
                                        case : {
                                            .unboundDataSource(jndiName);
                                            break;
                                        }
                                        case : {
                                            .debugf("Removed JDBC Data-source [%s]"jndiName);
                                            break;
                                        }
                                    }
                                }
                            });
                    binderBuilder.setInitialMode(..)
                            .addListener(verificationHandler);
                    binderBuilder.install();
                    context.completeStep();
                }
            }, ..);
        }
        context.completeStep();
    }
    public static DataSourceEnable getLocalInstance() {
        return ;
    }
    public boolean isXa() {
        return ;
    }
New to GrepCode? Check out our FAQ X