Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/* LanguageTool, a natural language style checker
* Copyright (C) 2018 Daniel Naber (http://www.danielnaber.de)
*
* This library 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 library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package org.languagetool.server;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.languagetool.Language;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import static org.languagetool.server.ServerTools.print;
/**
* Encapsulate database access. Will do nothing if database access is not configured.
* @since 4.2
*/
class DatabaseAccess {
private static DatabaseAccess instance;
private static SqlSessionFactory sqlSessionFactory;
private final Cache> userDictCache = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(24, TimeUnit.HOURS)
.build();
private final Cache dbLoggingCache = CacheBuilder.newBuilder()
.expireAfterAccess(1, TimeUnit.HOURS)
.maximumSize(5000)
.build();
private DatabaseAccess(HTTPServerConfig config) {
if (config.getDatabaseDriver() != null) {
try {
print("Setting up database access, URL " + config.getDatabaseUrl() + ", driver: " + config.getDatabaseDriver() + ", user: " + config.getDatabaseUsername());
InputStream inputStream = Resources.getResourceAsStream("org/languagetool/server/mybatis-config.xml");
Properties properties = new Properties();
properties.setProperty("driver", config.getDatabaseDriver());
properties.setProperty("url", config.getDatabaseUrl());
properties.setProperty("username", config.getDatabaseUsername());
properties.setProperty("password", config.getDatabasePassword());
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, properties);
DatabaseLogger.init(sqlSessionFactory);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
print("Not setting up database access, dbDriver is not configured");
}
}
static synchronized void init(HTTPServerConfig config) {
if (instance == null) {
instance = new DatabaseAccess(config);
}
}
static synchronized DatabaseAccess getInstance() {
if (instance == null) {
throw new IllegalStateException("DatabaseAccess.init() has not been called yet");
}
return instance;
}
List getUserDictWords(Long userId) {
List dictEntries = new ArrayList<>();
if (sqlSessionFactory == null) {
return dictEntries;
}
try (SqlSession session = sqlSessionFactory.openSession()) {
try {
List dict = session.selectList("org.languagetool.server.UserDictMapper.selectWordList", userId);
for (UserDictEntry userDictEntry : dict) {
dictEntries.add(userDictEntry.getWord());
}
if (dict.size() <= 1000) { // make sure users with huge dict don't blow up the cache
userDictCache.put(userId, dict);
} else {
print("WARN: Large dict size " + dict.size() + " for user " + userId + " - will not put user's dict in cache");
}
} catch (Exception e) {
// try to be more robust when database is down, i.e. don't just crash but try to use cache:
List cachedDictOrNull = userDictCache.getIfPresent(userId);
if (cachedDictOrNull != null) {
print("ERROR: Could not get words from database for user " + userId + ": " + e.getMessage() + ", will use cached version (" + cachedDictOrNull.size() + " items). Full stack trace follows:", System.err);
for (UserDictEntry userDictEntry : cachedDictOrNull) {
dictEntries.add(userDictEntry.getWord());
}
} else {
print("ERROR: Could not get words from database for user " + userId + ": " + e.getMessage() + " - also, could not use version from cache, user id not found in cache, will use empty dict. Full stack trace follows:", System.err);
}
e.printStackTrace();
}
}
return dictEntries;
}
List getWords(Long userId, int offset, int limit) {
if (sqlSessionFactory == null) {
return new ArrayList<>();
}
try (SqlSession session = sqlSessionFactory.openSession(true)) {
Map