java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
branchv_0
changeset 73 d32fd50d3c2c
parent 72 fc9fc1f26b88
child 74 a8444f6a54f3
equal deleted inserted replaced
72:fc9fc1f26b88 73:d32fd50d3c2c
   130 		}
   130 		}
   131 
   131 
   132 		printTable(formatter, header, data);
   132 		printTable(formatter, header, data);
   133 	}
   133 	}
   134 
   134 
   135 	public void testConnection() {
   135 	public void testConnection() throws FormatterException, ConfigurationException {
   136 		boolean connectionTestResult = false;
   136 		ColumnsHeader header = constructHeader(
       
   137 				new HeaderField("database_name", SQLType.VARCHAR),
       
   138 				new HeaderField("configured", SQLType.VARCHAR),
       
   139 				new HeaderField("connected", SQLType.VARCHAR));
       
   140 		List<Object[]> data = new ArrayList<>();
       
   141 
       
   142 		/** TODO: support multiple DB to test */
   137 		String dbName = options.getDatabaseNameToTest();
   143 		String dbName = options.getDatabaseNameToTest();
       
   144 
       
   145 		data.add(testConnection(dbName));
       
   146 
       
   147 		printTable(formatter, header, data);
       
   148 	}
       
   149 
       
   150 	public Object[] testConnection(String dbName) {
   138 		log.log(Level.FINE, "Testing connection to database: {0}", dbName);
   151 		log.log(Level.FINE, "Testing connection to database: {0}", dbName);
       
   152 
       
   153 		boolean succesfullyConnected = false;
       
   154 		boolean succesfullyConfigured = false;
       
   155 
   139 		try {
   156 		try {
   140 			DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
   157 			DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
   141 			if (dd == null) {
   158 			if (dd == null) {
   142 				log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName);
   159 				log.log(Level.FINE, "No database with this name is configured: {0}", dbName);
   143 			} else {
   160 			} else {
   144 				log.log(Level.FINE, "Database definition was loaded from configuration");
   161 				log.log(Level.FINE, "Database definition was loaded from configuration");
   145 				DatabaseConnection dc = dd.connect();
   162 				succesfullyConfigured = true;
   146 				connectionTestResult = dc.test();
   163 				try (DatabaseConnection dc = dd.connect()) {
       
   164 					succesfullyConnected = dc.test();
       
   165 				}
       
   166 				log.log(Level.FINE, "Database connection test was successful");
   147 			}
   167 			}
   148 		} catch (ConfigurationException | SQLException e) {
   168 		} catch (ConfigurationException | SQLException e) {
   149 			log.log(Level.SEVERE, "Error during testing connection", e);
   169 			log.log(Level.SEVERE, "Error during testing connection", e);
   150 		}
   170 		}
   151 		log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure");
   171 
       
   172 		return new Object[]{dbName, succesfullyConfigured, succesfullyConnected};
   152 	}
   173 	}
   153 
   174 
   154 	public void printResource(String fileName) {
   175 	public void printResource(String fileName) {
   155 		try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
   176 		try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
   156 			while (true) {
   177 			while (true) {
   260 				infoLister.listDatabases();
   281 				infoLister.listDatabases();
   261 			}
   282 			}
   262 		},
   283 		},
   263 		CONNECTION {
   284 		CONNECTION {
   264 			@Override
   285 			@Override
   265 			public void showInfo(InfoLister infoLister) {
   286 			public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
   266 				infoLister.testConnection();
   287 				infoLister.testConnection();
   267 			}
   288 			}
   268 		};
   289 		};
   269 
   290 
   270 		public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;
   291 		public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;