Ignore:
Timestamp:
08/28/10 21:55:08 (21 months ago)
Author:
kasper
Message:

Ticket #382: Added CLI for executing jobs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • AnalyzerBeans/trunk/src/main/java/org/eobjects/analyzer/connection/JdbcDatastore.java

    r947 r982  
    55import java.sql.DriverManager; 
    66import java.sql.SQLException; 
     7 
     8import javax.naming.InitialContext; 
     9import javax.sql.DataSource; 
     10 
     11import org.eobjects.analyzer.util.StringUtils; 
    712 
    813import dk.eobjects.metamodel.DataContext; 
     
    1419 
    1520        private String _name; 
    16         private String _url; 
     21        private String _jdbcUrl; 
    1722        private String _username; 
    1823        private String _password; 
    1924        private String _driverClass; 
     25        private String _datasourceJndiUrl; 
    2026        private transient DataContextProvider _dataContextProvider; 
    2127        private transient Connection _connection; 
    2228 
    23         public JdbcDatastore(String name, String url, String driverClass) { 
     29        public JdbcDatastore(String name, String jdbcUrl, String driverClass) { 
    2430                _name = name; 
    25                 _url = url; 
     31                _jdbcUrl = jdbcUrl; 
    2632                _driverClass = driverClass; 
    2733        } 
     
    3440        } 
    3541 
    36         public String getUrl() { 
    37                 return _url; 
     42        public JdbcDatastore(String name, String datasourceJndiUrl) { 
     43                _name = name; 
     44                _datasourceJndiUrl = datasourceJndiUrl; 
     45 
    3846        } 
    3947 
    40         public void setUrl(String url) { 
    41                 _url = url; 
     48        public String getJdbcUrl() { 
     49                return _jdbcUrl; 
    4250        } 
    4351 
     
    4654        } 
    4755 
    48         public void setUsername(String username) { 
    49                 synchronized (this) { 
    50                         _username = username; 
    51                 } 
    52         } 
    53  
    5456        public String getPassword() { 
    5557                return _password; 
    5658        } 
    5759 
    58         public void setPassword(String password) { 
    59                 synchronized (this) { 
    60                         _password = password; 
    61                 } 
    62         } 
    63  
    6460        public String getDriverClass() { 
    6561                return _driverClass; 
    66         } 
    67  
    68         public void setDriverClass(String driverClass) { 
    69                 _driverClass = driverClass; 
    7062        } 
    7163 
     
    7567        } 
    7668 
    77         public void setName(String name) { 
    78                 _name = name; 
     69        public String getDatasourceJndiUrl() { 
     70                return _datasourceJndiUrl; 
    7971        } 
    8072 
     
    8476                        synchronized (this) { 
    8577                                if (_dataContextProvider == null) { 
    86                                         try { 
    87                                                 Class.forName(_driverClass); 
    88                                         } catch (ClassNotFoundException e) { 
    89                                                 throw new IllegalStateException( 
    90                                                                 "Could not initialize JDBC driver", e); 
     78                                        if (StringUtils.isNullOrEmpty(_datasourceJndiUrl)) { 
     79                                                try { 
     80                                                        Class.forName(_driverClass); 
     81                                                } catch (ClassNotFoundException e) { 
     82                                                        throw new IllegalStateException( 
     83                                                                        "Could not initialize JDBC driver", e); 
     84                                                } 
     85                                                try { 
     86                                                        if (_username == null && _password == null) { 
     87                                                                _connection = DriverManager 
     88                                                                                .getConnection(_jdbcUrl); 
     89                                                        } else { 
     90                                                                _connection = DriverManager.getConnection( 
     91                                                                                _jdbcUrl, _username, _password); 
     92                                                        } 
     93                                                } catch (SQLException e) { 
     94                                                        throw new IllegalStateException( 
     95                                                                        "Could not establish JDBC connection", e); 
     96                                                } 
     97 
     98                                                DataContext dataContext = DataContextFactory 
     99                                                                .createJdbcDataContext(_connection); 
     100                                                _dataContextProvider = new SingleDataContextProvider( 
     101                                                                dataContext); 
     102                                        } else { 
     103                                                try { 
     104                                                        InitialContext initialContext = new InitialContext(); 
     105                                                        DataSource dataSource = (DataSource) initialContext 
     106                                                                        .lookup(_datasourceJndiUrl); 
     107                                                        _dataContextProvider = new DataSourceDataContextProvider( 
     108                                                                        dataSource); 
     109                                                } catch (Exception e) { 
     110                                                        throw new IllegalStateException(e); 
     111                                                } 
    91112                                        } 
    92                                         try { 
    93                                                 if (_username == null && _password == null) { 
    94                                                         _connection = DriverManager.getConnection(_url); 
    95                                                 } else { 
    96                                                         _connection = DriverManager.getConnection(_url, 
    97                                                                         _username, _password); 
    98                                                 } 
    99                                         } catch (SQLException e) { 
    100                                                 throw new IllegalStateException( 
    101                                                                 "Could not establish JDBC connection", e); 
    102                                         } 
    103  
    104                                         DataContext dataContext = DataContextFactory 
    105                                                         .createJdbcDataContext(_connection); 
    106                                         _dataContextProvider = new SingleDataContextProvider( 
    107                                                         dataContext); 
    108113                                } 
    109114                        } 
Note: See TracChangeset for help on using the changeset viewer.