[solved ] Re: Initializing DataSourceInfo

From: Borut Bolčin (cayenn..mail.si)
Date: Fri Feb 18 2005 - 05:59:20 EST

  • Next message: Mike Kienenberger: "Re: Modeller Question"

    As usualy I was complicating simple things. Looking at Cayenne debug
    logs and Mike's info I figured it out.

          DataMap dataMap = null;
          try {
             DbAdapter mySqlDbAdapter = new MySQLAdapter();
            
             List maps = (List) dataContext.getDataMaps();
             for (Iterator iter = maps.iterator(); iter.hasNext();) {
                DataMap element = (DataMap) iter.next();
                if(element.getName().equals("MyDataMap"))
                   dataMap = element;
             }
            
             DataNode dataNode = dataContext.lookupDataNode(dataMap);
             DataSource dataSource = dataNode.getDataSource();
            
             DbGenerator mySqlDbGenerator = new DbGenerator(mySqlDbAdapter,
    dataMap);
            
             mySqlDbGenerator.setShouldCreatePKSupport(false);
             mySqlDbGenerator.setShouldDropTables(true);
             mySqlDbGenerator.runGenerator(dataSource);
                 
          } catch (Exception e) {
             logger.fatal("Error running generator.", e);
          }

    On 17.2.2005 16:02, Mike Kienenberger wrote:

    >=?UTF-8?B?Qm9ydXQgQm9sxI1pbmE=?= <cayenn..mail.si> wrote:
    >
    >
    >>I am having trouble initializing DataSourceInfo programaticaly. At
    >>application startup I want to drop tables, create them and then do so
    >>the usual staff. How can I obtain an initialized DataSourceInfo from
    >>DomainNode driver file?
    >>
    >>
    >
    >You can always look at the source for examples. :) There's a variety of
    >ways to do it, depending how much behavior you want to hardcode in, and how
    >much pre-initialization you've already got.
    >
    >And I'd stick with DataSource rather than DataSourceInfo. DataSourceInfo is
    >an internal Cayenne class. DataSource is javax.sql.DataSource.
    >runGenerator(DataSourceInfo dsi) simply calls runGenerator(DataSource ds)
    >after creating a DataSource.
    >
    >
    >org.objectstyle.cayenne.conf.RuntimeLoadDelegate.java:
    >
    > // ie, factory = "DriverDataSourceFactory";
    > DataSourceFactory localFactory = (DataSourceFactory)
    >Class.forName(factory).newInstance();
    > localFactory.initializeWithParentConfiguration(config);
    > DataSource ds = localFactory.getDataSource(dataSourceURL);
    >
    >Or, you could bypass all of that if you want to work with a specific
    >DataSourceFactory (like DriverDataSourceFactory).
    >
    > FileConfiguration conf = new FileConfiguration("cayenne.xml");
    > conf.addFilesystemPath(new File("yourConfigFileDirectory"));
    > Configuration.initializeSharedConfiguration(conf);
    >
    > DriverDataSourceFactory dataSourceFactory = new
    >DriverDataSourceFactory();
    > dataSourceFactory.initializeWithParentConfiguration(config);
    > DataSource dataSource =
    >dataSourceFactory.getDataSource("yourDriver.xml");
    >
    >Or you could load your project as normal, and just read the information from
    >dataNode:
    >
    > ObjEntity objEntity =
    >entityResolver.lookupObjEntity(OneOfYourDataObjectClasses.class);
    > DataMap dataMap = objEntity.getDataMap();
    > DataNode dataNode = dataDomain.lookupDataNode(dataMap);
    > DataSource dataSource = dataNode.getDataSource();
    >
    >
    >
    >
    >>private DataContext ctxt;
    >>
    >>this.ctxt = createContext();
    >>
    >>
    >> private void createTables() {
    >> try {
    >> DbAdapter mySqlDbAdapter = new MySQLAdapter();
    >>
    >> MapLoader mapLoader = new MapLoader();
    >> InputStream is =
    >>this.getClass().getClassLoader().getResourceAsStream("MyDataMap.map.xml");
    >>
    >>
    >
    >
    >
    >> DataMap dataMap = mapLoader.loadDataMap(new InputSource(is));
    >>
    >> DbGenerator mySqlDbGenerator = new DbGenerator(mySqlDbAdapter,
    >>dataMap);
    >>
    >>// instead of this (don't want to be hardcoded)
    >>// DataSourceInfo *dataSourceInfo *= new DataSourceInfo();
    >>//
    >>
    >>
    >>
    >dataSourceInfo.setAdapterClassName("org.objectstyle.cayenne.dba.JdbcAdapter");
    >
    >
    >>// dataSourceInfo.setDataSourceUrl(connection);
    >>// dataSourceInfo.setJdbcDriver("com.mysql.jdbc.Driver");
    >>// dataSourceInfo.setPassword(password);
    >>// dataSourceInfo.setUserName(username);
    >>
    >>// *where do I get already initialized dataSourceInfo need by
    >>**mySqlDbGenerator.runGenerator*?
    >>
    >> logger.info("DSI getDataSourceUrl(): " +
    >>dataSourceInfo.getDataSourceUrl());
    >> logger.info("DSI getJdbcDriver() : " +
    >>dataSourceInfo.getJdbcDriver());
    >> logger.info("DSI getPassword() : " +
    >>dataSourceInfo.getPassword());
    >> logger.info("DSI getUserName() : " +
    >>dataSourceInfo.getUserName());
    >>
    >> mySqlDbGenerator.setShouldCreatePKSupport(false);
    >> mySqlDbGenerator.setShouldDropTables(true);
    >> mySqlDbGenerator.runGenerator(*dataSourceInfo*);
    >> } catch (DataMapException e) {
    >> logger.fatal("Error in NewsDataMap.", e);
    >> } catch (Exception e) {
    >> logger.fatal("Error running generator.", e);
    >> }
    >> }
    >>
    >>



    This archive was generated by hypermail 2.0.0 : Fri Feb 18 2005 - 05:59:27 EST