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.commons.lang3.NotImplementedException;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.languagetool.Language;
import org.languagetool.Premium;
import org.languagetool.rules.Rule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
/**
* Encapsulate database access. Will do nothing if database access is not configured.
* @since 4.2
*/
class DatabaseAccessOpenSource extends DatabaseAccess {
private static final Logger logger = LoggerFactory.getLogger(DatabaseAccessOpenSource.class);
private static final String NON_PREMIUM_MSG = "This server does not support username/password";
private static final Pattern WHITESPACE_PATTERN = Pattern.compile(".*\\s.*");
private final Cache dbLoggingCache = CacheBuilder.newBuilder()
.expireAfterAccess(1, TimeUnit.HOURS)
.maximumSize(5000)
.build();
public DatabaseAccessOpenSource(HTTPServerConfig config) {
super(config);
if (config.getDatabaseDriver() != null) {
try {
logger.info("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());
properties.setProperty("premium", Premium.isPremiumVersion() ? "Premium" : "OpenSource");
properties.setProperty("timeout", String.valueOf(config.getDbTimeoutSeconds()));
properties.setProperty("poolMaximumActiveConnections", String.valueOf(config.getDbMaxConnections()));
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, properties);
// try to close connections even on hard restart
// workaround as described in https://github.com/mybatis/mybatis-3/issues/821
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (sqlSessionFactory != null) {
((PooledDataSource) sqlSessionFactory
.getConfiguration().getEnvironment().getDataSource()).forceCloseAll();
}
}));
DatabaseLogger.init(sqlSessionFactory);
if (!config.getDatabaseLogging()) {
logger.info("dbLogging not set to true, turning off logging");
DatabaseLogger.getInstance().disableLogging();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
logger.info("Not setting up database access, dbDriver is not configured");
}
}
@Override
void invalidateCaches() {
}
@Override
boolean addWord(String word, Long userId, String groupName) {
return addWord(word, userId);
}
@Override
boolean deleteWord(String word, Long userId, String groupName) {
return deleteWord(word, userId);
}
@Override
boolean deleteWordBatch(List words, Long userId, String groupName) {
boolean deleted = false;
for (String word : words) {
if (deleteWord(word, userId)) {
deleted = true;
}
}
return deleted;
}
@Override
void addWordBatch(List words, Long userId, String groupName) {
words.forEach(w -> addWord(w, userId));
}
@Override
UserInfoEntry getUserInfoWithPassword(String username, String password) {
throw new NotImplementedException(NON_PREMIUM_MSG);
}
@Override
ExtendedUserInfo getExtendedUserInfo(String user) {
throw new NotImplementedException(NON_PREMIUM_MSG);
}
@Override
ExtendedUserInfo getExtendedUserInfo(long userId) {
throw new NotImplementedException(NON_PREMIUM_MSG);
}
@Override
UserInfoEntry getUserInfoWithApiKey(String username, String apiKey) {
Long userId = getUserId(username, apiKey);
UserInfoEntry user = new UserInfoEntry(userId, username, null, null, null, null, null, null, null, null, apiKey, null, null, null);
return user;
}
@Override
UserInfoEntry getUserInfoWithAddonToken(String username, String apiKey) {
throw new NotImplementedException(NON_PREMIUM_MSG);
}
@Override
void invalidateUserInfoCache(String user) {
throw new NotImplementedException(NON_PREMIUM_MSG);
}
@Override
Long getUserRequestCount(Long userId) {
return null;
}
List getWords(Long userId, int offset, int limit) {
if (sqlSessionFactory == null) {
return new ArrayList<>();
}
try (SqlSession session = sqlSessionFactory.openSession(true)) {
Map