Github user GERey commented on a diff in the pull request:

    https://github.com/apache/incubator-usergrid/pull/160#discussion_r24765428
  
    --- Diff: 
stack/core/src/main/java/org/apache/usergrid/corepersistence/CpSetup.java ---
    @@ -61,151 +59,106 @@
     
         private static final Logger logger = LoggerFactory.getLogger( 
CpSetup.class );
     
    -    private static Injector injector = null;
    -    private static EntityManagerFactory emf;
    +
    +    private final Injector injector;
    +
     
         private final CassandraService cass;
     
    +    private final EntityManagerFactory emf;
    +
     
         /**
          * Instantiates a new setup object.
          *
          * @param emf the emf
          */
    -    public CpSetup( EntityManagerFactory emf, CassandraService cass ) {
    +    public CpSetup( final EntityManagerFactory emf, final CassandraService 
cassandraService, final Injector injector ) {
             this.emf = emf;
    -        this.cass = cass;
    -    }
    -
    -
    -    public static Injector getInjector() {
    -        if ( injector == null ) {
    -            injector = Guice.createInjector( new GuiceModule( emf ) ); 
    -        }
    -        return injector;
    +        this.cass = cassandraService;
    +        this.injector = injector;
         }
     
     
         @Override
         public void init() throws Exception {
    -        cass.init();
    -
    -        try {
    -            logger.info("Loading Core Persistence properties");
    -
    -            String hostsString = "";
    -            CassandraHost[] hosts = 
cass.getCassandraHostConfigurator().buildCassandraHosts();
    -            if ( hosts.length == 0 ) {
    -                throw new RuntimeException("Fatal error: no Cassandra 
hosts configured");
    -            }
    -            String sep = "";
    -            for ( CassandraHost host : hosts ) {
    -                if (StringUtils.isEmpty(host.getHost())) {
    -                    throw new RuntimeException("Fatal error: Cassandra 
hostname cannot be empty");
    -                }
    -                hostsString = hostsString + sep + host.getHost();
    -                sep = ",";
    -            }
    -
    -            logger.info("hostsString: " + hostsString);
    +        //a no op, creating the injector creates the connections
     
    -            Properties cpProps = new Properties();
    +    }
     
    -            // Some Usergrid properties must be mapped to Core Persistence 
properties
    -            cpProps.put("cassandra.hosts", hostsString);
    -            cpProps.put("cassandra.port", hosts[0].getPort());
    -            cpProps.put("cassandra.cluster_name", 
cass.getProperties().get("cassandra.cluster"));
     
    -            String cassRemoteString = 
(String)cass.getProperties().get("cassandra.use_remote"); 
    -            if ( cassRemoteString != null && 
cassRemoteString.equals("false")) {
    -                cpProps.put("cassandra.embedded", "true");
    -            } else {
    -                cpProps.put("cassandra.embedded", "false");
    -            }
    +    public void createDefaultApplications() throws Exception {
     
    -            cpProps.put("collections.keyspace.strategy.class", 
    -                    
cass.getProperties().get("cassandra.keyspace.strategy"));
    +        setupSystemKeyspace();
     
    -            cpProps.put("collections.keyspace.strategy.options",
    -                    
cass.getProperties().get("cassandra.keyspace.replication"));
    +        setupStaticKeyspace();
     
    +        //force the EMF creation of indexes before creating the default 
applications
    +        emf.refreshIndex();
     
    -            logger.debug("Set Cassandra properties for Core Persistence: " 
+ cpProps.toString() );
    +        injector.getInstance( DataMigrationManager.class ).migrate();
     
    -            // Make all Usergrid properties into Core Persistence config
    -            cpProps.putAll( cass.getProperties() );
    -            //logger.debug("All properties fed to Core Persistence: " + 
cpProps.toString() );
    +        logger.info( "Setting up default applications" );
     
    -            ConfigurationManager.loadProperties( cpProps );
    +        try {
    +            emf.initializeApplication( DEFAULT_ORGANIZATION, 
emf.getDefaultAppId(), DEFAULT_APPLICATION, null );
             }
    -        catch ( Exception e ) {
    -            throw new RuntimeException( "Fatal error loading 
configuration.", e );
    +        catch ( ApplicationAlreadyExistsException ex ) {
    +            logger.warn( "Application {}/{} already exists", 
DEFAULT_ORGANIZATION, DEFAULT_APPLICATION );
    +        }
    +        catch ( OrganizationAlreadyExistsException oaee ) {
    +            logger.warn( "Organization {} already exists", 
DEFAULT_ORGANIZATION );
             }
     
    -        Injector injector = CpSetup.getInjector();
    -        MigrationManager m = injector.getInstance( MigrationManager.class 
);
             try {
    -            m.migrate();
    -        } catch (MigrationException ex) {
    -            throw new RuntimeException("Error migrating Core Persistence", 
ex);
    +            emf.initializeApplication( DEFAULT_ORGANIZATION, 
emf.getManagementAppId(), MANAGEMENT_APPLICATION, null );
    +        }
    +        catch ( ApplicationAlreadyExistsException ex ) {
    +            logger.warn( "Application {}/{} already exists", 
DEFAULT_ORGANIZATION, MANAGEMENT_APPLICATION );
    +        }
    +        catch ( OrganizationAlreadyExistsException oaee ) {
    +            logger.warn( "Organization {} already exists", 
DEFAULT_ORGANIZATION );
             }
    -
    -        setupSystemKeyspace();
    -
    -        setupStaticKeyspace();
    -
    -        //force the EMF creation of indexes before creating the default 
applications
    -        emf.refreshIndex();
    -
    -        createDefaultApplications();
         }
     
     
    -    public void createDefaultApplications() throws Exception {
    -
    -        logger.info("Setting up default applications");
    -
    +    /**
    +     * Perform migration of the 2.0 code
    +     */
    +    private void migrate() {
    +        MigrationManager m = injector.getInstance( MigrationManager.class 
);
             try {
    -            emf.initializeApplication( DEFAULT_ORGANIZATION,
    -                    emf.getDefaultAppId(), DEFAULT_APPLICATION, null );
    -        } catch (ApplicationAlreadyExistsException ex) {
    -            logger.warn("Application {}/{} already exists", 
    -                    DEFAULT_ORGANIZATION, DEFAULT_APPLICATION);
    +            m.migrate();
             }
    -
    -        try {
    -            emf.initializeApplication( DEFAULT_ORGANIZATION,
    -                    emf.getManagementAppId(), MANAGEMENT_APPLICATION, null 
);
    -        } catch (ApplicationAlreadyExistsException ex) {
    -            logger.warn("Application {}/{} already exists", 
    -                    DEFAULT_ORGANIZATION, MANAGEMENT_APPLICATION);
    +        catch ( MigrationException ex ) {
    +            throw new RuntimeException( "Error migrating Core 
Persistence", ex );
             }
         }
     
     
    -
    -    
         @Override
         public void setupSystemKeyspace() throws Exception {
     
             logger.info( "Initialize system keyspace" );
     
    -        cass.createColumnFamily( SYSTEM_KEYSPACE, 
createColumnFamilyDefinition( 
    -                SYSTEM_KEYSPACE, APPLICATIONS_CF, ComparatorType.BYTESTYPE 
) );
    +        migrate();
     
    -        cass.createColumnFamily( SYSTEM_KEYSPACE, 
createColumnFamilyDefinition( 
    -                SYSTEM_KEYSPACE, PROPERTIES_CF, ComparatorType.BYTESTYPE ) 
);
    +        cass.createColumnFamily( SYSTEM_KEYSPACE,
    --- End diff --
    
    Is there a way to check that we've already created these keyspaces? That 
way it won't show the exception when we try to recreate already existing 
keyspaces?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to