乔占卫
6 years ago
committed by
GitHub
171 changed files with 5912 additions and 2619 deletions
@ -0,0 +1,169 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.api.controller; |
||||
|
||||
|
||||
import cn.escheduler.api.enums.Status; |
||||
import cn.escheduler.api.service.AccessTokenService; |
||||
import cn.escheduler.api.service.UsersService; |
||||
import cn.escheduler.api.utils.Constants; |
||||
import cn.escheduler.api.utils.Result; |
||||
import cn.escheduler.dao.model.User; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import static cn.escheduler.api.enums.Status.*; |
||||
|
||||
|
||||
/** |
||||
* user controller |
||||
*/ |
||||
@RestController |
||||
@RequestMapping("/access-token") |
||||
public class AccessTokenController extends BaseController{ |
||||
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AccessTokenController.class); |
||||
|
||||
|
||||
@Autowired |
||||
private AccessTokenService accessTokenService; |
||||
|
||||
/** |
||||
* create token |
||||
* @param loginUser |
||||
* @return |
||||
*/ |
||||
@PostMapping(value = "/create") |
||||
@ResponseStatus(HttpStatus.CREATED) |
||||
public Result createToken(@RequestAttribute(value = Constants.SESSION_USER) User loginUser, |
||||
@RequestParam(value = "userId") int userId, |
||||
@RequestParam(value = "expireTime") String expireTime, |
||||
@RequestParam(value = "token") String token){ |
||||
logger.info("login user {}, create token , userId : {} , token expire time : {} , token : {}", loginUser.getUserName(), |
||||
userId,expireTime,token); |
||||
|
||||
try { |
||||
Map<String, Object> result = accessTokenService.createToken(userId, expireTime, token); |
||||
return returnDataList(result); |
||||
}catch (Exception e){ |
||||
logger.error(CREATE_ACCESS_TOKEN_ERROR.getMsg(),e); |
||||
return error(CREATE_ACCESS_TOKEN_ERROR.getCode(), CREATE_ACCESS_TOKEN_ERROR.getMsg()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* create token |
||||
* @param loginUser |
||||
* @return |
||||
*/ |
||||
@PostMapping(value = "/generate") |
||||
@ResponseStatus(HttpStatus.CREATED) |
||||
public Result generateToken(@RequestAttribute(value = Constants.SESSION_USER) User loginUser, |
||||
@RequestParam(value = "userId") int userId, |
||||
@RequestParam(value = "expireTime") String expireTime){ |
||||
logger.info("login user {}, generate token , userId : {} , token expire time : {}",loginUser,userId,expireTime); |
||||
try { |
||||
Map<String, Object> result = accessTokenService.generateToken(userId, expireTime); |
||||
return returnDataList(result); |
||||
}catch (Exception e){ |
||||
logger.error(GENERATE_TOKEN_ERROR.getMsg(),e); |
||||
return error(GENERATE_TOKEN_ERROR.getCode(), GENERATE_TOKEN_ERROR.getMsg()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* query access token list paging |
||||
* |
||||
* @param loginUser |
||||
* @param pageNo |
||||
* @param searchVal |
||||
* @param pageSize |
||||
* @return |
||||
*/ |
||||
@GetMapping(value="/list-paging") |
||||
@ResponseStatus(HttpStatus.OK) |
||||
public Result queryAccessTokenList(@RequestAttribute(value = Constants.SESSION_USER) User loginUser, |
||||
@RequestParam("pageNo") Integer pageNo, |
||||
@RequestParam(value = "searchVal", required = false) String searchVal, |
||||
@RequestParam("pageSize") Integer pageSize){ |
||||
logger.info("login user {}, list access token paging, pageNo: {}, searchVal: {}, pageSize: {}", |
||||
loginUser.getUserName(),pageNo,searchVal,pageSize); |
||||
try{ |
||||
Map<String, Object> result = checkPageParams(pageNo, pageSize); |
||||
if(result.get(Constants.STATUS) != Status.SUCCESS){ |
||||
return returnDataListPaging(result); |
||||
} |
||||
result = accessTokenService.queryAccessTokenList(loginUser, searchVal, pageNo, pageSize); |
||||
return returnDataListPaging(result); |
||||
}catch (Exception e){ |
||||
logger.error(QUERY_ACCESSTOKEN_LIST_PAGING_ERROR.getMsg(),e); |
||||
return error(QUERY_ACCESSTOKEN_LIST_PAGING_ERROR.getCode(),QUERY_ACCESSTOKEN_LIST_PAGING_ERROR.getMsg()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* delete access token by id |
||||
* @param loginUser |
||||
* @param id |
||||
* @return |
||||
*/ |
||||
@PostMapping(value = "/delete") |
||||
@ResponseStatus(HttpStatus.OK) |
||||
public Result delAccessTokenById(@RequestAttribute(value = Constants.SESSION_USER) User loginUser, |
||||
@RequestParam(value = "id") int id) { |
||||
logger.info("login user {}, delete access token, id: {},", loginUser.getUserName(), id); |
||||
try { |
||||
Map<String, Object> result = accessTokenService.delAccessTokenById(loginUser, id); |
||||
return returnDataList(result); |
||||
}catch (Exception e){ |
||||
logger.error(DELETE_USER_BY_ID_ERROR.getMsg(),e); |
||||
return error(Status.DELETE_USER_BY_ID_ERROR.getCode(), Status.DELETE_USER_BY_ID_ERROR.getMsg()); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* update token |
||||
* @param loginUser |
||||
* @return |
||||
*/ |
||||
@PostMapping(value = "/update") |
||||
@ResponseStatus(HttpStatus.CREATED) |
||||
public Result updateToken(@RequestAttribute(value = Constants.SESSION_USER) User loginUser, |
||||
@RequestParam(value = "id") int id, |
||||
@RequestParam(value = "userId") int userId, |
||||
@RequestParam(value = "expireTime") String expireTime, |
||||
@RequestParam(value = "token") String token){ |
||||
logger.info("login user {}, update token , userId : {} , token expire time : {} , token : {}", loginUser.getUserName(), |
||||
userId,expireTime,token); |
||||
|
||||
try { |
||||
Map<String, Object> result = accessTokenService.updateToken(id,userId, expireTime, token); |
||||
return returnDataList(result); |
||||
}catch (Exception e){ |
||||
logger.error(CREATE_ACCESS_TOKEN_ERROR.getMsg(),e); |
||||
return error(CREATE_ACCESS_TOKEN_ERROR.getCode(), CREATE_ACCESS_TOKEN_ERROR.getMsg()); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,184 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.api.service; |
||||
|
||||
import cn.escheduler.api.enums.Status; |
||||
import cn.escheduler.api.utils.CheckUtils; |
||||
import cn.escheduler.api.utils.Constants; |
||||
import cn.escheduler.api.utils.PageInfo; |
||||
import cn.escheduler.api.utils.Result; |
||||
import cn.escheduler.common.enums.UserType; |
||||
import cn.escheduler.common.utils.*; |
||||
import cn.escheduler.dao.mapper.*; |
||||
import cn.escheduler.dao.model.*; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.transaction.annotation.Transactional; |
||||
|
||||
import java.util.*; |
||||
|
||||
/** |
||||
* user service |
||||
*/ |
||||
@Service |
||||
public class AccessTokenService extends BaseService { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AccessTokenService.class); |
||||
|
||||
@Autowired |
||||
private AccessTokenMapper accessTokenMapper; |
||||
|
||||
|
||||
/** |
||||
* query access token list |
||||
* |
||||
* @param loginUser |
||||
* @param searchVal |
||||
* @param pageNo |
||||
* @param pageSize |
||||
* @return |
||||
*/ |
||||
public Map<String, Object> queryAccessTokenList(User loginUser, String searchVal, Integer pageNo, Integer pageSize) { |
||||
Map<String, Object> result = new HashMap<>(5); |
||||
|
||||
if (check(result, !isAdmin(loginUser), Status.USER_NO_OPERATION_PERM, Constants.STATUS)) { |
||||
return result; |
||||
} |
||||
|
||||
Integer count = accessTokenMapper.countAccessTokenPaging(searchVal); |
||||
|
||||
PageInfo<AccessToken> pageInfo = new PageInfo<>(pageNo, pageSize); |
||||
|
||||
List<AccessToken> accessTokenList = accessTokenMapper.queryAccessTokenPaging(searchVal, pageInfo.getStart(), pageSize); |
||||
|
||||
pageInfo.setTotalCount(count); |
||||
pageInfo.setLists(accessTokenList); |
||||
result.put(Constants.DATA_LIST, pageInfo); |
||||
putMsg(result, Status.SUCCESS); |
||||
|
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* check |
||||
* |
||||
* @param result |
||||
* @param bool |
||||
* @param userNoOperationPerm |
||||
* @param status |
||||
* @return |
||||
*/ |
||||
private boolean check(Map<String, Object> result, boolean bool, Status userNoOperationPerm, String status) { |
||||
//only admin can operate
|
||||
if (bool) { |
||||
result.put(Constants.STATUS, userNoOperationPerm); |
||||
result.put(status, userNoOperationPerm.getMsg()); |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* create token |
||||
* |
||||
* @param userId |
||||
* @param expireTime |
||||
* @param token |
||||
* @return |
||||
*/ |
||||
public Map<String, Object> createToken(int userId, String expireTime, String token) { |
||||
Map<String, Object> result = new HashMap<>(5); |
||||
|
||||
AccessToken accessToken = new AccessToken(); |
||||
accessToken.setUserId(userId); |
||||
accessToken.setExpireTime(DateUtils.stringToDate(expireTime)); |
||||
accessToken.setToken(token); |
||||
accessToken.setCreateTime(new Date()); |
||||
accessToken.setUpdateTime(new Date()); |
||||
|
||||
// insert
|
||||
int insert = accessTokenMapper.insert(accessToken); |
||||
|
||||
if (insert > 0) { |
||||
putMsg(result, Status.SUCCESS); |
||||
} else { |
||||
putMsg(result, Status.CREATE_ALERT_GROUP_ERROR); |
||||
} |
||||
|
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* generate token |
||||
* @param userId |
||||
* @param expireTime |
||||
* @return |
||||
*/ |
||||
public Map<String, Object> generateToken(int userId, String expireTime) { |
||||
Map<String, Object> result = new HashMap<>(5); |
||||
String token = EncryptionUtils.getMd5(userId + expireTime + String.valueOf(System.currentTimeMillis())); |
||||
result.put(Constants.DATA_LIST, token); |
||||
putMsg(result, Status.SUCCESS); |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* delete access token |
||||
* @param loginUser |
||||
* @param id |
||||
* @return |
||||
*/ |
||||
public Map<String, Object> delAccessTokenById(User loginUser, int id) { |
||||
Map<String, Object> result = new HashMap<>(5); |
||||
//only admin can operate
|
||||
if (!isAdmin(loginUser)) { |
||||
putMsg(result, Status.USER_NOT_EXIST, id); |
||||
return result; |
||||
} |
||||
|
||||
accessTokenMapper.delete(id); |
||||
putMsg(result, Status.SUCCESS); |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* update token by id |
||||
* @param id |
||||
* @param userId |
||||
* @param expireTime |
||||
* @param token |
||||
* @return |
||||
*/ |
||||
public Map<String, Object> updateToken(int id,int userId, String expireTime, String token) { |
||||
Map<String, Object> result = new HashMap<>(5); |
||||
AccessToken accessToken = new AccessToken(); |
||||
accessToken.setId(id); |
||||
accessToken.setUserId(userId); |
||||
accessToken.setExpireTime(DateUtils.stringToDate(expireTime)); |
||||
accessToken.setToken(token); |
||||
accessToken.setUpdateTime(new Date()); |
||||
|
||||
accessTokenMapper.update(accessToken); |
||||
|
||||
putMsg(result, Status.SUCCESS); |
||||
return result; |
||||
} |
||||
} |
@ -0,0 +1,162 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.api; |
||||
|
||||
import java.io.File; |
||||
import java.net.URI; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.UUID; |
||||
|
||||
import cn.escheduler.common.utils.EncryptionUtils; |
||||
import org.apache.commons.io.FileUtils; |
||||
import org.apache.http.NameValuePair; |
||||
import org.apache.http.client.entity.UrlEncodedFormEntity; |
||||
import org.apache.http.client.methods.CloseableHttpResponse; |
||||
import org.apache.http.client.methods.HttpGet; |
||||
import org.apache.http.client.methods.HttpPost; |
||||
import org.apache.http.client.utils.URIBuilder; |
||||
import org.apache.http.impl.client.CloseableHttpClient; |
||||
import org.apache.http.impl.client.HttpClients; |
||||
import org.apache.http.message.BasicNameValuePair; |
||||
import org.apache.http.util.EntityUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
public class HttpClientTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(HttpClientTest.class); |
||||
|
||||
public static void main(String[] args) throws Exception { |
||||
// doGETParamPathVariableAndChinese();
|
||||
// doGETParam();
|
||||
// doPOSTParam();
|
||||
|
||||
String md5 = EncryptionUtils.getMd5(String.valueOf(System.currentTimeMillis()) + "张三"); |
||||
System.out.println(md5); |
||||
System.out.println(md5.length()); |
||||
} |
||||
|
||||
public static void doPOSTParam()throws Exception{ |
||||
// create Httpclient
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault(); |
||||
// 创建http POST请求
|
||||
HttpPost httpPost = new HttpPost("http://127.0.0.1:12345/escheduler/projects/create"); |
||||
httpPost.setHeader("token", "123"); |
||||
// set parameters
|
||||
List<NameValuePair> parameters = new ArrayList<NameValuePair>(); |
||||
parameters.add(new BasicNameValuePair("projectName", "qzw")); |
||||
parameters.add(new BasicNameValuePair("desc", "qzw")); |
||||
|
||||
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters); |
||||
httpPost.setEntity(formEntity); |
||||
|
||||
|
||||
CloseableHttpResponse response = null; |
||||
try { |
||||
// execute
|
||||
response = httpclient.execute(httpPost); |
||||
// eponse status code 200
|
||||
if (response.getStatusLine().getStatusCode() == 200) { |
||||
String content = EntityUtils.toString(response.getEntity(), "UTF-8"); |
||||
System.out.println(content); |
||||
} |
||||
} finally { |
||||
if (response != null) { |
||||
response.close(); |
||||
} |
||||
httpclient.close(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @throws Exception |
||||
*/ |
||||
public static void doGETParamPathVariableAndChinese()throws Exception{ |
||||
// create HttpClient
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault(); |
||||
|
||||
List<NameValuePair> parameters = new ArrayList<NameValuePair>(); |
||||
// parameters.add(new BasicNameValuePair("pageSize", "10"));
|
||||
|
||||
// define the parameters of the request
|
||||
URI uri = new URIBuilder("http://127.0.0.1:12345/escheduler/projects/%E5%85%A8%E9%83%A8%E6%B5%81%E7%A8%8B%E6%B5%8B%E8%AF%95/process/list") |
||||
.build(); |
||||
|
||||
// create http GET request
|
||||
HttpGet httpGet = new HttpGet(uri); |
||||
httpGet.setHeader("token","123"); |
||||
//response object
|
||||
CloseableHttpResponse response = null; |
||||
try { |
||||
// execute http get request
|
||||
response = httpclient.execute(httpGet); |
||||
// reponse status code 200
|
||||
if (response.getStatusLine().getStatusCode() == 200) { |
||||
String content = EntityUtils.toString(response.getEntity(), "UTF-8"); |
||||
logger.info("start--------------->"); |
||||
logger.info(content); |
||||
logger.info("end----------------->"); |
||||
} |
||||
} finally { |
||||
if (response != null) { |
||||
response.close(); |
||||
} |
||||
httpclient.close(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @throws Exception |
||||
*/ |
||||
public static void doGETParam()throws Exception{ |
||||
// create HttpClient
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault(); |
||||
|
||||
List<NameValuePair> parameters = new ArrayList<NameValuePair>(); |
||||
parameters.add(new BasicNameValuePair("processInstanceId", "41415")); |
||||
|
||||
// define the parameters of the request
|
||||
URI uri = new URIBuilder("http://127.0.0.1:12345/escheduler/projects/%E5%85%A8%E9%83%A8%E6%B5%81%E7%A8%8B%E6%B5%8B%E8%AF%95/instance/view-variables") |
||||
.setParameters(parameters) |
||||
.build(); |
||||
|
||||
// create http GET request
|
||||
HttpGet httpGet = new HttpGet(uri); |
||||
httpGet.setHeader("token","123"); |
||||
//response object
|
||||
CloseableHttpResponse response = null; |
||||
try { |
||||
// execute http get request
|
||||
response = httpclient.execute(httpGet); |
||||
// reponse status code 200
|
||||
if (response.getStatusLine().getStatusCode() == 200) { |
||||
String content = EntityUtils.toString(response.getEntity(), "UTF-8"); |
||||
logger.info("start--------------->"); |
||||
logger.info(content); |
||||
logger.info("end----------------->"); |
||||
} |
||||
} finally { |
||||
if (response != null) { |
||||
response.close(); |
||||
} |
||||
httpclient.close(); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,75 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.common.job.db; |
||||
|
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.sql.Connection; |
||||
import java.sql.DriverManager; |
||||
import java.sql.SQLException; |
||||
|
||||
/** |
||||
* data source of ClickHouse |
||||
*/ |
||||
public class ClickHouseDataSource extends BaseDataSource { |
||||
private static final Logger logger = LoggerFactory.getLogger(ClickHouseDataSource.class); |
||||
|
||||
/** |
||||
* gets the JDBC url for the data source connection |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public String getJdbcUrl() { |
||||
String jdbcUrl = getAddress(); |
||||
if (jdbcUrl.lastIndexOf("/") != (jdbcUrl.length() - 1)) { |
||||
jdbcUrl += "/"; |
||||
} |
||||
|
||||
jdbcUrl += getDatabase(); |
||||
|
||||
if (StringUtils.isNotEmpty(getOther())) { |
||||
jdbcUrl += "?" + getOther(); |
||||
} |
||||
|
||||
return jdbcUrl; |
||||
} |
||||
|
||||
/** |
||||
* test whether the data source can be connected successfully |
||||
* @throws Exception |
||||
*/ |
||||
@Override |
||||
public void isConnectable() throws Exception { |
||||
Connection con = null; |
||||
try { |
||||
Class.forName("ru.yandex.clickhouse.ClickHouseDriver"); |
||||
con = DriverManager.getConnection(getJdbcUrl(), getUser(), getPassword()); |
||||
} finally { |
||||
if (con != null) { |
||||
try { |
||||
con.close(); |
||||
} catch (SQLException e) { |
||||
logger.error("ClickHouse datasource try conn close conn error", e); |
||||
throw e; |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,75 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.common.job.db; |
||||
|
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.sql.Connection; |
||||
import java.sql.DriverManager; |
||||
import java.sql.SQLException; |
||||
|
||||
/** |
||||
* data source of Oracle |
||||
*/ |
||||
public class OracleDataSource extends BaseDataSource { |
||||
private static final Logger logger = LoggerFactory.getLogger(OracleDataSource.class); |
||||
|
||||
/** |
||||
* gets the JDBC url for the data source connection |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public String getJdbcUrl() { |
||||
String jdbcUrl = getAddress(); |
||||
if (jdbcUrl.lastIndexOf("/") != (jdbcUrl.length() - 1)) { |
||||
jdbcUrl += "/"; |
||||
} |
||||
|
||||
jdbcUrl += getDatabase(); |
||||
|
||||
if (StringUtils.isNotEmpty(getOther())) { |
||||
jdbcUrl += "?" + getOther(); |
||||
} |
||||
|
||||
return jdbcUrl; |
||||
} |
||||
|
||||
/** |
||||
* test whether the data source can be connected successfully |
||||
* @throws Exception |
||||
*/ |
||||
@Override |
||||
public void isConnectable() throws Exception { |
||||
Connection con = null; |
||||
try { |
||||
Class.forName("oracle.jdbc.driver.OracleDriver"); |
||||
con = DriverManager.getConnection(getJdbcUrl(), getUser(), getPassword()); |
||||
} finally { |
||||
if (con != null) { |
||||
try { |
||||
con.close(); |
||||
} catch (SQLException e) { |
||||
logger.error("Oracle datasource try conn close conn error", e); |
||||
throw e; |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,71 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.common.job.db; |
||||
|
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.sql.Connection; |
||||
import java.sql.DriverManager; |
||||
import java.sql.SQLException; |
||||
|
||||
/** |
||||
* data source of SQL Server |
||||
*/ |
||||
public class SQLServerDataSource extends BaseDataSource { |
||||
private static final Logger logger = LoggerFactory.getLogger(SQLServerDataSource.class); |
||||
|
||||
/** |
||||
* gets the JDBC url for the data source connection |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public String getJdbcUrl() { |
||||
String jdbcUrl = getAddress(); |
||||
jdbcUrl += ";databaseName=" + getDatabase(); |
||||
|
||||
if (StringUtils.isNotEmpty(getOther())) { |
||||
jdbcUrl += ";" + getOther(); |
||||
} |
||||
|
||||
return jdbcUrl; |
||||
} |
||||
|
||||
/** |
||||
* test whether the data source can be connected successfully |
||||
* @throws Exception |
||||
*/ |
||||
@Override |
||||
public void isConnectable() throws Exception { |
||||
Connection con = null; |
||||
try { |
||||
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); |
||||
con = DriverManager.getConnection(getJdbcUrl(), getUser(), getPassword()); |
||||
} finally { |
||||
if (con != null) { |
||||
try { |
||||
con.close(); |
||||
} catch (SQLException e) { |
||||
logger.error("SQL Server datasource try conn close conn error", e); |
||||
throw e; |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,104 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.common.utils; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.sql.*; |
||||
|
||||
public class MysqlUtil { |
||||
|
||||
public static final Logger logger = LoggerFactory.getLogger(MysqlUtil.class); |
||||
|
||||
private static MysqlUtil instance; |
||||
|
||||
MysqlUtil() { |
||||
} |
||||
|
||||
public static MysqlUtil getInstance() { |
||||
if (null == instance) { |
||||
syncInit(); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
private static synchronized void syncInit() { |
||||
if (instance == null) { |
||||
instance = new MysqlUtil(); |
||||
} |
||||
} |
||||
|
||||
public void release(ResultSet rs, Statement stmt, Connection conn) { |
||||
try { |
||||
if (rs != null) { |
||||
rs.close(); |
||||
rs = null; |
||||
} |
||||
} catch (SQLException e) { |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e); |
||||
} finally { |
||||
try { |
||||
if (stmt != null) { |
||||
stmt.close(); |
||||
stmt = null; |
||||
} |
||||
} catch (SQLException e) { |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e); |
||||
} finally { |
||||
try { |
||||
if (conn != null) { |
||||
conn.close(); |
||||
conn = null; |
||||
} |
||||
} catch (SQLException e) { |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
public static void realeaseResource(ResultSet rs, PreparedStatement ps, Connection conn) { |
||||
MysqlUtil.getInstance().release(rs,ps,conn); |
||||
if (null != rs) { |
||||
try { |
||||
rs.close(); |
||||
} catch (SQLException e) { |
||||
logger.error(e.getMessage(),e); |
||||
} |
||||
} |
||||
|
||||
if (null != ps) { |
||||
try { |
||||
ps.close(); |
||||
} catch (SQLException e) { |
||||
logger.error(e.getMessage(),e); |
||||
} |
||||
} |
||||
|
||||
if (null != conn) { |
||||
try { |
||||
conn.close(); |
||||
} catch (SQLException e) { |
||||
logger.error(e.getMessage(),e); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,150 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.common.utils; |
||||
|
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.IOException; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.Comparator; |
||||
import java.util.List; |
||||
import java.util.regex.Matcher; |
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* Metadata related common classes |
||||
* |
||||
*/ |
||||
public class SchemaUtils { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SchemaUtils.class); |
||||
private static Pattern p = Pattern.compile("\\s*|\t|\r|\n"); |
||||
|
||||
/** |
||||
* 获取所有upgrade目录下的可升级的schema |
||||
* Gets upgradable schemas for all upgrade directories |
||||
* @return |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
public static List<String> getAllSchemaList() { |
||||
List<String> schemaDirList = new ArrayList<>(); |
||||
File[] schemaDirArr = FileUtils.getAllDir("sql/upgrade"); |
||||
if(schemaDirArr == null || schemaDirArr.length == 0) { |
||||
return null; |
||||
} |
||||
|
||||
for(File file : schemaDirArr) { |
||||
schemaDirList.add(file.getName()); |
||||
} |
||||
|
||||
Collections.sort(schemaDirList , new Comparator() { |
||||
@Override |
||||
public int compare(Object o1 , Object o2){ |
||||
try { |
||||
String dir1 = String.valueOf(o1); |
||||
String dir2 = String.valueOf(o2); |
||||
String version1 = dir1.split("_")[0]; |
||||
String version2 = dir2.split("_")[0]; |
||||
if(version1.equals(version2)) { |
||||
return 0; |
||||
} |
||||
|
||||
if(SchemaUtils.isAGreatVersion(version1, version2)) { |
||||
return 1; |
||||
} |
||||
|
||||
return -1; |
||||
|
||||
} catch (Exception e) { |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
}); |
||||
|
||||
return schemaDirList; |
||||
} |
||||
|
||||
/** |
||||
* 判断schemaVersion是否比version版本高 |
||||
* Determine whether schemaVersion is higher than version |
||||
* @param schemaVersion |
||||
* @param version |
||||
* @return |
||||
*/ |
||||
public static boolean isAGreatVersion(String schemaVersion, String version) { |
||||
if(StringUtils.isEmpty(schemaVersion) || StringUtils.isEmpty(version)) { |
||||
throw new RuntimeException("schemaVersion or version is empty"); |
||||
} |
||||
|
||||
String[] schemaVersionArr = schemaVersion.split("\\."); |
||||
String[] versionArr = version.split("\\."); |
||||
int arrLength = schemaVersionArr.length < versionArr.length ? schemaVersionArr.length : versionArr.length; |
||||
for(int i = 0 ; i < arrLength ; i++) { |
||||
if(Integer.valueOf(schemaVersionArr[i]) > Integer.valueOf(versionArr[i])) { |
||||
return true; |
||||
}else if(Integer.valueOf(schemaVersionArr[i]) < Integer.valueOf(versionArr[i])) { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
// 说明直到第arrLength-1个元素,两个版本号都一样,此时谁的arrLength大,谁的版本号就大
|
||||
// If the version and schema version is the same from 0 up to the arrlength-1 element,whoever has a larger arrLength has a larger version number
|
||||
return schemaVersionArr.length > versionArr.length; |
||||
} |
||||
|
||||
/** |
||||
* Gets the current software version number of the system |
||||
* @return |
||||
*/ |
||||
public static String getSoftVersion() { |
||||
String soft_version; |
||||
try { |
||||
soft_version = FileUtils.readFile2Str(new FileInputStream(new File("sql/soft_version"))); |
||||
soft_version = replaceBlank(soft_version); |
||||
} catch (FileNotFoundException e) { |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException("Failed to get the product version description file. The file could not be found", e); |
||||
} catch (IOException e) { |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException("Failed to get product version number description file, failed to read the file", e); |
||||
} |
||||
return soft_version; |
||||
} |
||||
|
||||
/** |
||||
* 去掉字符串中的空格回车换行和制表符 |
||||
* Strips the string of space carriage returns and tabs |
||||
* @param str |
||||
* @return |
||||
*/ |
||||
public static String replaceBlank(String str) { |
||||
String dest = ""; |
||||
if (str!=null) { |
||||
|
||||
Matcher m = p.matcher(str); |
||||
dest = m.replaceAll(""); |
||||
} |
||||
return dest; |
||||
} |
||||
} |
@ -0,0 +1,317 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.common.utils; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.LineNumberReader; |
||||
import java.io.Reader; |
||||
import java.sql.*; |
||||
|
||||
/* |
||||
* Slightly modified version of the com.ibatis.common.jdbc.ScriptRunner class
|
||||
* from the iBATIS Apache project. Only removed dependency on Resource class
|
||||
* and a constructor |
||||
*/ |
||||
/* |
||||
* Copyright 2004 Clinton Begin |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
/** |
||||
* Tool to run database scripts |
||||
*/ |
||||
public class ScriptRunner { |
||||
|
||||
public static final Logger logger = LoggerFactory.getLogger(ScriptRunner.class); |
||||
|
||||
private static final String DEFAULT_DELIMITER = ";"; |
||||
|
||||
private Connection connection; |
||||
|
||||
private boolean stopOnError; |
||||
private boolean autoCommit; |
||||
|
||||
private String delimiter = DEFAULT_DELIMITER; |
||||
private boolean fullLineDelimiter = false; |
||||
|
||||
/** |
||||
* Default constructor |
||||
*/ |
||||
public ScriptRunner(Connection connection, boolean autoCommit, boolean stopOnError) { |
||||
this.connection = connection; |
||||
this.autoCommit = autoCommit; |
||||
this.stopOnError = stopOnError; |
||||
} |
||||
|
||||
public static void main(String[] args) { |
||||
String dbName = "db_mmu"; |
||||
String appKey = dbName.substring(dbName.lastIndexOf("_")+1, dbName.length()); |
||||
System.out.println(appKey); |
||||
} |
||||
|
||||
public void setDelimiter(String delimiter, boolean fullLineDelimiter) { |
||||
this.delimiter = delimiter; |
||||
this.fullLineDelimiter = fullLineDelimiter; |
||||
} |
||||
|
||||
/** |
||||
* Runs an SQL script (read in using the Reader parameter) |
||||
* |
||||
* @param reader |
||||
* - the source of the script |
||||
*/ |
||||
public void runScript(Reader reader) throws IOException, SQLException { |
||||
try { |
||||
boolean originalAutoCommit = connection.getAutoCommit(); |
||||
try { |
||||
if (originalAutoCommit != this.autoCommit) { |
||||
connection.setAutoCommit(this.autoCommit); |
||||
} |
||||
runScript(connection, reader); |
||||
} finally { |
||||
connection.setAutoCommit(originalAutoCommit); |
||||
} |
||||
} catch (IOException e) { |
||||
throw e; |
||||
} catch (SQLException e) { |
||||
throw e; |
||||
} catch (Exception e) { |
||||
throw new RuntimeException("Error running script. Cause: " + e, e); |
||||
} |
||||
} |
||||
|
||||
public void runScript(Reader reader, String dbName) throws IOException, SQLException { |
||||
try { |
||||
boolean originalAutoCommit = connection.getAutoCommit(); |
||||
try { |
||||
if (originalAutoCommit != this.autoCommit) { |
||||
connection.setAutoCommit(this.autoCommit); |
||||
} |
||||
runScript(connection, reader, dbName); |
||||
} finally { |
||||
connection.setAutoCommit(originalAutoCommit); |
||||
} |
||||
} catch (IOException e) { |
||||
throw e; |
||||
} catch (SQLException e) { |
||||
throw e; |
||||
} catch (Exception e) { |
||||
throw new RuntimeException("Error running script. Cause: " + e, e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Runs an SQL script (read in using the Reader parameter) using the connection |
||||
* passed in |
||||
* |
||||
* @param conn |
||||
* - the connection to use for the script |
||||
* @param reader |
||||
* - the source of the script |
||||
* @throws SQLException |
||||
* if any SQL errors occur |
||||
* @throws IOException |
||||
* if there is an error reading from the Reader |
||||
*/ |
||||
private void runScript(Connection conn, Reader reader) throws IOException, SQLException { |
||||
StringBuffer command = null; |
||||
try { |
||||
LineNumberReader lineReader = new LineNumberReader(reader); |
||||
String line = null; |
||||
while ((line = lineReader.readLine()) != null) { |
||||
if (command == null) { |
||||
command = new StringBuffer(); |
||||
} |
||||
String trimmedLine = line.trim(); |
||||
if (trimmedLine.startsWith("--")) { |
||||
logger.info(trimmedLine); |
||||
} else if (trimmedLine.length() < 1 || trimmedLine.startsWith("//")) { |
||||
// Do nothing
|
||||
} else if (trimmedLine.length() < 1 || trimmedLine.startsWith("--")) { |
||||
// Do nothing
|
||||
|
||||
} else if (trimmedLine.startsWith("delimiter")) { |
||||
String newDelimiter = trimmedLine.split(" ")[1]; |
||||
this.setDelimiter(newDelimiter, fullLineDelimiter); |
||||
|
||||
} else if (!fullLineDelimiter && trimmedLine.endsWith(getDelimiter()) |
||||
|| fullLineDelimiter && trimmedLine.equals(getDelimiter())) { |
||||
command.append(line.substring(0, line.lastIndexOf(getDelimiter()))); |
||||
command.append(" "); |
||||
Statement statement = conn.createStatement(); |
||||
|
||||
// logger.info(command.toString());
|
||||
|
||||
boolean hasResults = false; |
||||
logger.info("sql:"+command.toString()); |
||||
if (stopOnError) { |
||||
hasResults = statement.execute(command.toString()); |
||||
} else { |
||||
try { |
||||
statement.execute(command.toString()); |
||||
} catch (SQLException e) { |
||||
logger.error(e.getMessage(),e); |
||||
throw e; |
||||
} |
||||
} |
||||
|
||||
ResultSet rs = statement.getResultSet(); |
||||
if (hasResults && rs != null) { |
||||
ResultSetMetaData md = rs.getMetaData(); |
||||
int cols = md.getColumnCount(); |
||||
for (int i = 0; i < cols; i++) { |
||||
String name = md.getColumnLabel(i); |
||||
logger.info(name + "\t"); |
||||
} |
||||
logger.info(""); |
||||
while (rs.next()) { |
||||
for (int i = 0; i < cols; i++) { |
||||
String value = rs.getString(i); |
||||
logger.info(value + "\t"); |
||||
} |
||||
logger.info(""); |
||||
} |
||||
} |
||||
|
||||
command = null; |
||||
try { |
||||
statement.close(); |
||||
} catch (Exception e) { |
||||
// Ignore to workaround a bug in Jakarta DBCP
|
||||
} |
||||
Thread.yield(); |
||||
} else { |
||||
command.append(line); |
||||
command.append(" "); |
||||
} |
||||
} |
||||
|
||||
} catch (SQLException e) { |
||||
logger.error("Error executing: " + command.toString()); |
||||
throw e; |
||||
} catch (IOException e) { |
||||
e.fillInStackTrace(); |
||||
logger.error("Error executing: " + command.toString()); |
||||
throw e; |
||||
} |
||||
} |
||||
|
||||
private void runScript(Connection conn, Reader reader , String dbName) throws IOException, SQLException { |
||||
StringBuffer command = null; |
||||
String sql = ""; |
||||
String appKey = dbName.substring(dbName.lastIndexOf("_")+1, dbName.length()); |
||||
try { |
||||
LineNumberReader lineReader = new LineNumberReader(reader); |
||||
String line = null; |
||||
while ((line = lineReader.readLine()) != null) { |
||||
if (command == null) { |
||||
command = new StringBuffer(); |
||||
} |
||||
String trimmedLine = line.trim(); |
||||
if (trimmedLine.startsWith("--")) { |
||||
logger.info(trimmedLine); |
||||
} else if (trimmedLine.length() < 1 || trimmedLine.startsWith("//")) { |
||||
// Do nothing
|
||||
} else if (trimmedLine.length() < 1 || trimmedLine.startsWith("--")) { |
||||
// Do nothing
|
||||
|
||||
} else if (trimmedLine.startsWith("delimiter")) { |
||||
String newDelimiter = trimmedLine.split(" ")[1]; |
||||
this.setDelimiter(newDelimiter, fullLineDelimiter); |
||||
|
||||
} else if (!fullLineDelimiter && trimmedLine.endsWith(getDelimiter()) |
||||
|| fullLineDelimiter && trimmedLine.equals(getDelimiter())) { |
||||
command.append(line.substring(0, line.lastIndexOf(getDelimiter()))); |
||||
command.append(" "); |
||||
Statement statement = conn.createStatement(); |
||||
|
||||
// logger.info(command.toString());
|
||||
|
||||
sql = command.toString().replaceAll("\\{\\{APPDB\\}\\}", dbName); |
||||
boolean hasResults = false; |
||||
logger.info("sql:"+sql); |
||||
if (stopOnError) { |
||||
hasResults = statement.execute(sql); |
||||
} else { |
||||
try { |
||||
statement.execute(sql); |
||||
} catch (SQLException e) { |
||||
logger.error(e.getMessage(),e); |
||||
throw e; |
||||
} |
||||
} |
||||
|
||||
ResultSet rs = statement.getResultSet(); |
||||
if (hasResults && rs != null) { |
||||
ResultSetMetaData md = rs.getMetaData(); |
||||
int cols = md.getColumnCount(); |
||||
for (int i = 0; i < cols; i++) { |
||||
String name = md.getColumnLabel(i); |
||||
logger.info(name + "\t"); |
||||
} |
||||
logger.info(""); |
||||
while (rs.next()) { |
||||
for (int i = 0; i < cols; i++) { |
||||
String value = rs.getString(i); |
||||
logger.info(value + "\t"); |
||||
} |
||||
logger.info(""); |
||||
} |
||||
} |
||||
|
||||
command = null; |
||||
try { |
||||
statement.close(); |
||||
} catch (Exception e) { |
||||
// Ignore to workaround a bug in Jakarta DBCP
|
||||
} |
||||
Thread.yield(); |
||||
} else { |
||||
command.append(line); |
||||
command.append(" "); |
||||
} |
||||
} |
||||
|
||||
} catch (SQLException e) { |
||||
logger.error("Error executing: " + sql); |
||||
throw e; |
||||
} catch (IOException e) { |
||||
e.fillInStackTrace(); |
||||
logger.error("Error executing: " + sql); |
||||
throw e; |
||||
} |
||||
} |
||||
|
||||
private String getDelimiter() { |
||||
return delimiter; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,13 @@
|
||||
-- 用户指定队列 |
||||
alter table t_escheduler_user add queue varchar(64); |
||||
|
||||
-- 访问token |
||||
CREATE TABLE `t_escheduler_access_token` ( |
||||
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', |
||||
`user_id` int(11) DEFAULT NULL COMMENT '用户id', |
||||
`token` varchar(64) DEFAULT NULL COMMENT 'token令牌', |
||||
`expire_time` datetime DEFAULT NULL COMMENT 'token有效结束时间', |
||||
`create_time` datetime DEFAULT NULL COMMENT '创建时间', |
||||
`update_time` datetime DEFAULT NULL COMMENT '更新时间', |
||||
PRIMARY KEY (`id`) |
||||
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; |
@ -0,0 +1,88 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.dao.mapper; |
||||
|
||||
import cn.escheduler.common.enums.UserType; |
||||
import cn.escheduler.dao.model.AccessToken; |
||||
import cn.escheduler.dao.model.User; |
||||
import org.apache.ibatis.annotations.*; |
||||
import org.apache.ibatis.type.EnumOrdinalTypeHandler; |
||||
import org.apache.ibatis.type.JdbcType; |
||||
|
||||
import java.sql.Timestamp; |
||||
import java.util.List; |
||||
|
||||
public interface AccessTokenMapper { |
||||
|
||||
/** |
||||
* insert accessToken |
||||
* @param accessToken |
||||
* @return |
||||
*/ |
||||
@InsertProvider(type = AccessTokenMapperProvider.class, method = "insert") |
||||
@Options(useGeneratedKeys = true,keyProperty = "accessToken.id") |
||||
@SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "accessToken.id", before = false, resultType = int.class) |
||||
int insert(@Param("accessToken") AccessToken accessToken); |
||||
|
||||
|
||||
/** |
||||
* delete accessToken |
||||
* @param accessTokenId |
||||
* @return |
||||
*/ |
||||
@DeleteProvider(type = AccessTokenMapperProvider.class, method = "delete") |
||||
int delete(@Param("accessTokenId") int accessTokenId); |
||||
|
||||
|
||||
/** |
||||
* update accessToken |
||||
* |
||||
* @param accessToken |
||||
* @return |
||||
*/ |
||||
@UpdateProvider(type = AccessTokenMapperProvider.class, method = "update") |
||||
int update(@Param("accessToken") AccessToken accessToken); |
||||
|
||||
|
||||
/** |
||||
* query access token list paging |
||||
* @param searchVal |
||||
* @param offset |
||||
* @param pageSize |
||||
* @return |
||||
*/ |
||||
@Results(value = {@Result(property = "id", column = "id", id = true, javaType = Integer.class, jdbcType = JdbcType.INTEGER), |
||||
@Result(property = "userId", column = "user_id", javaType = Integer.class, jdbcType = JdbcType.INTEGER), |
||||
@Result(property = "token", column = "token", javaType = String.class, jdbcType = JdbcType.VARCHAR), |
||||
@Result(property = "userName", column = "user_name", javaType = String.class, jdbcType = JdbcType.VARCHAR), |
||||
@Result(property = "expireTime", column = "expire_time", javaType = Timestamp.class, jdbcType = JdbcType.DATE), |
||||
@Result(property = "createTime", column = "create_time", javaType = Timestamp.class, jdbcType = JdbcType.DATE), |
||||
@Result(property = "updateTime", column = "update_time", javaType = Timestamp.class, jdbcType = JdbcType.DATE) |
||||
}) |
||||
@SelectProvider(type = AccessTokenMapperProvider.class, method = "queryAccessTokenPaging") |
||||
List<AccessToken> queryAccessTokenPaging(@Param("searchVal") String searchVal, |
||||
@Param("offset") Integer offset, |
||||
@Param("pageSize") Integer pageSize); |
||||
|
||||
/** |
||||
* count access token by search value |
||||
* @param searchVal |
||||
* @return |
||||
*/ |
||||
@SelectProvider(type = AccessTokenMapperProvider.class, method = "countAccessTokenPaging") |
||||
Integer countAccessTokenPaging(@Param("searchVal") String searchVal); |
||||
} |
@ -0,0 +1,130 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.dao.mapper; |
||||
|
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.apache.ibatis.jdbc.SQL; |
||||
|
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* access token mapper provider |
||||
* |
||||
*/ |
||||
public class AccessTokenMapperProvider { |
||||
|
||||
private static final String TABLE_NAME = "t_escheduler_access_token"; |
||||
|
||||
/** |
||||
* insert accessToken |
||||
* |
||||
* @param parameter |
||||
* @return |
||||
*/ |
||||
public String insert(Map<String, Object> parameter) { |
||||
return new SQL() { |
||||
{ |
||||
INSERT_INTO(TABLE_NAME); |
||||
VALUES("`user_id`", "#{accessToken.userId}"); |
||||
VALUES("`token`", "#{accessToken.token}"); |
||||
VALUES("`expire_time`", "#{accessToken.expireTime}");; |
||||
VALUES("`create_time`", "#{accessToken.createTime}"); |
||||
VALUES("`update_time`", "#{accessToken.updateTime}"); |
||||
} |
||||
}.toString(); |
||||
} |
||||
|
||||
/** |
||||
* delete accessToken |
||||
* |
||||
* @param parameter |
||||
* @return |
||||
*/ |
||||
public String delete(Map<String, Object> parameter) { |
||||
return new SQL() { |
||||
{ |
||||
DELETE_FROM(TABLE_NAME); |
||||
|
||||
WHERE("`id`=#{accessTokenId}"); |
||||
} |
||||
}.toString(); |
||||
} |
||||
|
||||
/** |
||||
* update accessToken |
||||
* |
||||
* @param parameter |
||||
* @return |
||||
*/ |
||||
public String update(Map<String, Object> parameter) { |
||||
return new SQL() { |
||||
{ |
||||
UPDATE(TABLE_NAME); |
||||
|
||||
SET("`user_id`=#{accessToken.userId}"); |
||||
SET("`token`=#{accessToken.token}"); |
||||
SET("`expire_time`=#{accessToken.expireTime}"); |
||||
SET("`update_time`=#{accessToken.updateTime}"); |
||||
|
||||
WHERE("`id`=#{user.id}"); |
||||
} |
||||
}.toString(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* count user number by search value |
||||
* @param parameter |
||||
* @return |
||||
*/ |
||||
public String countAccessTokenPaging(Map<String, Object> parameter) { |
||||
return new SQL() {{ |
||||
SELECT("count(0)"); |
||||
FROM(TABLE_NAME + " t,t_escheduler_user u"); |
||||
Object searchVal = parameter.get("searchVal"); |
||||
WHERE("u.id = t.user_id"); |
||||
if(searchVal != null && StringUtils.isNotEmpty(searchVal.toString())){ |
||||
WHERE(" u.user_name like concat('%', #{searchVal}, '%')"); |
||||
} |
||||
}}.toString(); |
||||
} |
||||
|
||||
/** |
||||
* query user list paging |
||||
* @param parameter |
||||
* @return |
||||
*/ |
||||
public String queryAccessTokenPaging(Map<String, Object> parameter) { |
||||
return new SQL() { |
||||
{ |
||||
SELECT("t.*,u.user_name"); |
||||
FROM(TABLE_NAME + " t,t_escheduler_user u"); |
||||
Object searchVal = parameter.get("searchVal"); |
||||
WHERE("u.id = t.user_id"); |
||||
if(searchVal != null && StringUtils.isNotEmpty(searchVal.toString())){ |
||||
WHERE(" u.user_name like concat('%', #{searchVal}, '%') "); |
||||
} |
||||
ORDER_BY(" t.update_time desc limit #{offset},#{pageSize} "); |
||||
} |
||||
}.toString(); |
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
} |
@ -0,0 +1,126 @@
|
||||
package cn.escheduler.dao.model; |
||||
|
||||
import java.util.Date; |
||||
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
public class AccessToken { |
||||
|
||||
/** |
||||
* id |
||||
*/ |
||||
private int id; |
||||
|
||||
/** |
||||
* user id |
||||
*/ |
||||
private int userId; |
||||
|
||||
/** |
||||
* user name |
||||
*/ |
||||
private String userName; |
||||
|
||||
/** |
||||
* user token |
||||
*/ |
||||
private String token; |
||||
|
||||
/** |
||||
* token expire time |
||||
*/ |
||||
private Date expireTime; |
||||
|
||||
/** |
||||
* create time |
||||
*/ |
||||
private Date createTime; |
||||
|
||||
/** |
||||
* update time |
||||
*/ |
||||
private Date updateTime; |
||||
|
||||
public int getId() { |
||||
return id; |
||||
} |
||||
|
||||
public void setId(int id) { |
||||
this.id = id; |
||||
} |
||||
|
||||
public int getUserId() { |
||||
return userId; |
||||
} |
||||
|
||||
public void setUserId(int userId) { |
||||
this.userId = userId; |
||||
} |
||||
|
||||
public String getToken() { |
||||
return token; |
||||
} |
||||
|
||||
public void setToken(String token) { |
||||
this.token = token; |
||||
} |
||||
|
||||
public Date getExpireTime() { |
||||
return expireTime; |
||||
} |
||||
|
||||
public void setExpireTime(Date expireTime) { |
||||
this.expireTime = expireTime; |
||||
} |
||||
|
||||
public Date getCreateTime() { |
||||
return createTime; |
||||
} |
||||
|
||||
public void setCreateTime(Date createTime) { |
||||
this.createTime = createTime; |
||||
} |
||||
|
||||
public Date getUpdateTime() { |
||||
return updateTime; |
||||
} |
||||
|
||||
public void setUpdateTime(Date updateTime) { |
||||
this.updateTime = updateTime; |
||||
} |
||||
|
||||
public String getUserName() { |
||||
return userName; |
||||
} |
||||
|
||||
public void setUserName(String userName) { |
||||
this.userName = userName; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "AccessToken{" + |
||||
"id=" + id + |
||||
", userId=" + userId + |
||||
", userName='" + userName + '\'' + |
||||
", token='" + token + '\'' + |
||||
", expireTime=" + expireTime + |
||||
", createTime=" + createTime + |
||||
", updateTime=" + updateTime + |
||||
'}'; |
||||
} |
||||
} |
@ -0,0 +1,82 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.dao.upgrade; |
||||
|
||||
import cn.escheduler.common.utils.SchemaUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* upgrade manager |
||||
*/ |
||||
public class EschedulerManager { |
||||
private static final Logger logger = LoggerFactory.getLogger(EschedulerManager.class); |
||||
UpgradeDao upgradeDao = UpgradeDao.getInstance(); |
||||
|
||||
public void initEscheduler() { |
||||
this.initEschedulerSchema(); |
||||
} |
||||
|
||||
public void initEschedulerSchema() { |
||||
|
||||
logger.info("Start initializing the ark manager mysql table structure"); |
||||
upgradeDao.initEschedulerSchema(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* upgrade escheduler |
||||
*/ |
||||
public void upgradeEscheduler() throws Exception{ |
||||
|
||||
// Gets a list of all upgrades
|
||||
List<String> schemaList = SchemaUtils.getAllSchemaList(); |
||||
if(schemaList == null || schemaList.size() == 0) { |
||||
logger.info("There is no schema to upgrade!"); |
||||
}else { |
||||
|
||||
String version = ""; |
||||
// The target version of the upgrade
|
||||
String schemaVersion = ""; |
||||
for(String schemaDir : schemaList) { |
||||
// Gets the version of the current system
|
||||
if (upgradeDao.isExistsTable("t_escheduler_version")) { |
||||
version = upgradeDao.getCurrentVersion(); |
||||
}else { |
||||
version = "1.0.0"; |
||||
} |
||||
|
||||
schemaVersion = schemaDir.split("_")[0]; |
||||
if(SchemaUtils.isAGreatVersion(schemaVersion , version)) { |
||||
|
||||
logger.info("upgrade escheduler metadata version from " + version + " to " + schemaVersion); |
||||
|
||||
|
||||
logger.info("Begin upgrading escheduler's mysql table structure"); |
||||
upgradeDao.upgradeEscheduler(schemaDir); |
||||
|
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
// Assign the value of the version field in the version table to the version of the product
|
||||
upgradeDao.updateVersion(SchemaUtils.getSoftVersion()); |
||||
} |
||||
} |
@ -0,0 +1,299 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.dao.upgrade; |
||||
|
||||
import cn.escheduler.common.utils.MysqlUtil; |
||||
import cn.escheduler.common.utils.ScriptRunner; |
||||
import cn.escheduler.dao.AbstractBaseDao; |
||||
import cn.escheduler.dao.datasource.ConnectionFactory; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.io.*; |
||||
import java.sql.Connection; |
||||
import java.sql.PreparedStatement; |
||||
import java.sql.ResultSet; |
||||
import java.sql.SQLException; |
||||
|
||||
public class UpgradeDao extends AbstractBaseDao { |
||||
|
||||
public static final Logger logger = LoggerFactory.getLogger(UpgradeDao.class); |
||||
private static final String T_VERSION_NAME = "t_escheduler_version"; |
||||
|
||||
@Override |
||||
protected void init() { |
||||
|
||||
} |
||||
|
||||
private static class UpgradeDaoHolder { |
||||
private static final UpgradeDao INSTANCE = new UpgradeDao(); |
||||
} |
||||
|
||||
private UpgradeDao() { |
||||
} |
||||
|
||||
public static final UpgradeDao getInstance() { |
||||
return UpgradeDaoHolder.INSTANCE; |
||||
} |
||||
|
||||
|
||||
|
||||
public void initEschedulerSchema() { |
||||
|
||||
// Execute the escheduler DDL, it cannot be rolled back
|
||||
runInitEschedulerDDL(); |
||||
|
||||
// Execute the escheduler DML, it can be rolled back
|
||||
runInitEschedulerDML(); |
||||
|
||||
} |
||||
|
||||
private void runInitEschedulerDML() { |
||||
Connection conn = null; |
||||
try { |
||||
conn = ConnectionFactory.getDataSource().getConnection(); |
||||
conn.setAutoCommit(false); |
||||
// 执行escheduler_dml.sql脚本,导入escheduler相关的数据
|
||||
// Execute the ark_manager_dml.sql script to import the data related to escheduler
|
||||
|
||||
ScriptRunner initScriptRunner = new ScriptRunner(conn, false, true); |
||||
Reader initSqlReader = new FileReader(new File("sql/create/release-1.0.0_schema/mysql/escheduler_dml.sql")); |
||||
initScriptRunner.runScript(initSqlReader); |
||||
|
||||
conn.commit(); |
||||
} catch (IOException e) { |
||||
try { |
||||
conn.rollback(); |
||||
} catch (SQLException e1) { |
||||
logger.error(e1.getMessage(),e1); |
||||
} |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e.getMessage(),e); |
||||
} catch (Exception e) { |
||||
try { |
||||
conn.rollback(); |
||||
} catch (SQLException e1) { |
||||
logger.error(e1.getMessage(),e1); |
||||
} |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e.getMessage(),e); |
||||
} finally { |
||||
MysqlUtil.realeaseResource(null, null, conn); |
||||
|
||||
} |
||||
|
||||
} |
||||
|
||||
private void runInitEschedulerDDL() { |
||||
Connection conn = null; |
||||
try { |
||||
conn = ConnectionFactory.getDataSource().getConnection(); |
||||
// Execute the escheduler_ddl.sql script to create the table structure of escheduler
|
||||
ScriptRunner initScriptRunner = new ScriptRunner(conn, true, true); |
||||
Reader initSqlReader = new FileReader(new File("sql/create/release-1.0.0_schema/mysql/escheduler_ddl.sql")); |
||||
initScriptRunner.runScript(initSqlReader); |
||||
|
||||
} catch (IOException e) { |
||||
|
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e.getMessage(),e); |
||||
} catch (Exception e) { |
||||
|
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e.getMessage(),e); |
||||
} finally { |
||||
MysqlUtil.realeaseResource(null, null, conn); |
||||
|
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
public boolean isExistsTable(String tableName) { |
||||
Connection conn = null; |
||||
try { |
||||
conn = ConnectionFactory.getDataSource().getConnection(); |
||||
ResultSet rs = conn.getMetaData().getTables(null, null, tableName, null); |
||||
if (rs.next()) { |
||||
return true; |
||||
} else { |
||||
return false; |
||||
} |
||||
|
||||
} catch (SQLException e) { |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e.getMessage(),e); |
||||
} finally { |
||||
MysqlUtil.realeaseResource(null, null, conn); |
||||
|
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
public String getCurrentVersion() { |
||||
String sql = String.format("select version from %s",T_VERSION_NAME); |
||||
Connection conn = null; |
||||
ResultSet rs = null; |
||||
PreparedStatement pstmt = null; |
||||
String version = null; |
||||
try { |
||||
conn = ConnectionFactory.getDataSource().getConnection(); |
||||
pstmt = conn.prepareStatement(sql); |
||||
rs = pstmt.executeQuery(); |
||||
|
||||
if (rs.next()) { |
||||
version = rs.getString(1); |
||||
} |
||||
|
||||
return version; |
||||
|
||||
} catch (SQLException e) { |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException("sql: " + sql, e); |
||||
} finally { |
||||
MysqlUtil.realeaseResource(rs, pstmt, conn); |
||||
|
||||
} |
||||
} |
||||
|
||||
|
||||
public void upgradeEscheduler(String schemaDir) { |
||||
|
||||
upgradeEschedulerDDL(schemaDir); |
||||
|
||||
upgradeEschedulerDML(schemaDir); |
||||
|
||||
} |
||||
|
||||
private void upgradeEschedulerDML(String schemaDir) { |
||||
String schemaVersion = schemaDir.split("_")[0]; |
||||
String mysqlSQLFilePath = "sql/upgrade/" + schemaDir + "/mysql/escheduler_dml.sql"; |
||||
Connection conn = null; |
||||
PreparedStatement pstmt = null; |
||||
try { |
||||
conn = ConnectionFactory.getDataSource().getConnection(); |
||||
conn.setAutoCommit(false); |
||||
// Execute the upgraded escheduler dml
|
||||
ScriptRunner scriptRunner = new ScriptRunner(conn, false, true); |
||||
Reader sqlReader = new FileReader(new File(mysqlSQLFilePath)); |
||||
scriptRunner.runScript(sqlReader); |
||||
if (isExistsTable(T_VERSION_NAME)) { |
||||
// Change version in the version table to the new version
|
||||
String upgradeSQL = String.format("update %s set version = ?",T_VERSION_NAME); |
||||
pstmt = conn.prepareStatement(upgradeSQL); |
||||
pstmt.setString(1, schemaVersion); |
||||
pstmt.executeUpdate(); |
||||
} |
||||
conn.commit(); |
||||
} catch (FileNotFoundException e) { |
||||
try { |
||||
conn.rollback(); |
||||
} catch (SQLException e1) { |
||||
logger.error(e1.getMessage(),e1); |
||||
} |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException("sql file not found ", e); |
||||
} catch (IOException e) { |
||||
try { |
||||
conn.rollback(); |
||||
} catch (SQLException e1) { |
||||
logger.error(e1.getMessage(),e1); |
||||
} |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e.getMessage(),e); |
||||
} catch (SQLException e) { |
||||
try { |
||||
conn.rollback(); |
||||
} catch (SQLException e1) { |
||||
logger.error(e1.getMessage(),e1); |
||||
} |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e.getMessage(),e); |
||||
} catch (Exception e) { |
||||
try { |
||||
conn.rollback(); |
||||
} catch (SQLException e1) { |
||||
logger.error(e1.getMessage(),e1); |
||||
} |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e.getMessage(),e); |
||||
} finally { |
||||
MysqlUtil.realeaseResource(null, pstmt, conn); |
||||
} |
||||
|
||||
} |
||||
|
||||
private void upgradeEschedulerDDL(String schemaDir) { |
||||
String mysqlSQLFilePath = "sql/upgrade/" + schemaDir + "/mysql/escheduler_ddl.sql"; |
||||
Connection conn = null; |
||||
PreparedStatement pstmt = null; |
||||
try { |
||||
conn = ConnectionFactory.getDataSource().getConnection(); |
||||
String dbName = conn.getCatalog(); |
||||
logger.info(dbName); |
||||
conn.setAutoCommit(true); |
||||
// Execute the escheduler ddl.sql for the upgrade
|
||||
ScriptRunner scriptRunner = new ScriptRunner(conn, true, true); |
||||
Reader sqlReader = new FileReader(new File(mysqlSQLFilePath)); |
||||
scriptRunner.runScript(sqlReader); |
||||
|
||||
} catch (FileNotFoundException e) { |
||||
|
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException("sql file not found ", e); |
||||
} catch (IOException e) { |
||||
|
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e.getMessage(),e); |
||||
} catch (SQLException e) { |
||||
|
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e.getMessage(),e); |
||||
} catch (Exception e) { |
||||
|
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException(e.getMessage(),e); |
||||
} finally { |
||||
MysqlUtil.realeaseResource(null, pstmt, conn); |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
public void updateVersion(String version) { |
||||
// Change version in the version table to the new version
|
||||
String upgradeSQL = String.format("update %s set version = ?",T_VERSION_NAME); |
||||
PreparedStatement pstmt = null; |
||||
Connection conn = null; |
||||
try { |
||||
conn = ConnectionFactory.getDataSource().getConnection(); |
||||
pstmt = conn.prepareStatement(upgradeSQL); |
||||
pstmt.setString(1, version); |
||||
pstmt.executeUpdate(); |
||||
|
||||
} catch (SQLException e) { |
||||
logger.error(e.getMessage(),e); |
||||
throw new RuntimeException("sql: " + upgradeSQL, e); |
||||
} finally { |
||||
MysqlUtil.realeaseResource(null, pstmt, conn); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,44 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.dao.upgrade.shell; |
||||
|
||||
import cn.escheduler.dao.upgrade.EschedulerManager; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
/** |
||||
* init escheduler |
||||
* |
||||
*/ |
||||
public class CreateEscheduler { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CreateEscheduler.class); |
||||
|
||||
public static void main(String[] args) { |
||||
Thread.currentThread().setName("manager-CreateEscheduler"); |
||||
EschedulerManager eschedulerManager = new EschedulerManager(); |
||||
eschedulerManager.initEscheduler(); |
||||
logger.info("init escheduler finished"); |
||||
try { |
||||
eschedulerManager.upgradeEscheduler(); |
||||
logger.info("upgrade escheduler finished"); |
||||
} catch (Exception e) { |
||||
logger.error("upgrade escheduler failed",e); |
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,38 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.dao.upgrade.shell; |
||||
|
||||
import cn.escheduler.dao.upgrade.EschedulerManager; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
/** |
||||
* init escheduler |
||||
* |
||||
*/ |
||||
public class InitEscheduler { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(InitEscheduler.class); |
||||
|
||||
public static void main(String[] args) { |
||||
Thread.currentThread().setName("manager-InitEscheduler"); |
||||
EschedulerManager eschedulerManager = new EschedulerManager(); |
||||
eschedulerManager.initEscheduler(); |
||||
logger.info("init escheduler finished"); |
||||
|
||||
} |
||||
} |
@ -0,0 +1,47 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.dao.upgrade.shell; |
||||
|
||||
import cn.escheduler.dao.upgrade.EschedulerManager; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
/** |
||||
* upgrade escheduler database |
||||
*/ |
||||
public class UpgradeEscheduler { |
||||
private static final Logger logger = LoggerFactory.getLogger(UpgradeEscheduler.class); |
||||
|
||||
public static void main(String[] args) { |
||||
Thread.currentThread().setName("manager-UpgradeEscheduler"); |
||||
|
||||
EschedulerManager eschedulerManager = new EschedulerManager(); |
||||
try { |
||||
eschedulerManager.upgradeEscheduler(); |
||||
logger.info("upgrade escheduler finished"); |
||||
} catch (Exception e) { |
||||
logger.error(e.getMessage(),e); |
||||
logger.info("Upgrade escheduler failed"); |
||||
throw new RuntimeException(e); |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
} |
@ -0,0 +1,62 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package cn.escheduler.dao.mapper; |
||||
|
||||
import cn.escheduler.common.utils.EncryptionUtils; |
||||
import cn.escheduler.dao.datasource.ConnectionFactory; |
||||
import cn.escheduler.dao.model.AccessToken; |
||||
import org.junit.Assert; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
|
||||
import java.util.Date; |
||||
import java.util.List; |
||||
|
||||
|
||||
|
||||
public class AccessTokenMapperTest { |
||||
|
||||
|
||||
AccessTokenMapper accessTokenMapper; |
||||
|
||||
@Before |
||||
public void before(){ |
||||
accessTokenMapper = ConnectionFactory.getSqlSession().getMapper(AccessTokenMapper.class); |
||||
} |
||||
|
||||
@Test |
||||
public void testInsert(){ |
||||
AccessToken accessToken = new AccessToken(); |
||||
accessToken.setUserId(10); |
||||
accessToken.setExpireTime(new Date()); |
||||
accessToken.setToken("ssssssssssssssssssssssssss"); |
||||
accessToken.setCreateTime(new Date()); |
||||
accessToken.setUpdateTime(new Date()); |
||||
accessTokenMapper.insert(accessToken); |
||||
} |
||||
|
||||
@Test |
||||
public void testListPaging(){ |
||||
Integer count = accessTokenMapper.countAccessTokenPaging(""); |
||||
Assert.assertEquals(count, (Integer) 5); |
||||
|
||||
List<AccessToken> accessTokenList = accessTokenMapper.queryAccessTokenPaging("", 0, 2); |
||||
Assert.assertEquals(accessTokenList.size(), 5); |
||||
} |
||||
|
||||
|
||||
} |
@ -1,6 +1,6 @@
|
||||
|
||||
# 后端接口地址 |
||||
API_BASE = http://192.168.220.154:12345 |
||||
API_BASE = http://192.168.220.247:12345 |
||||
|
||||
# 本地开发如需ip访问项目把"#"号去掉 |
||||
#DEV_HOST = 192.168.xx.xx |
||||
|
@ -0,0 +1,141 @@
|
||||
<template> |
||||
<m-conditions> |
||||
<template slot="search-group"> |
||||
<div class="list"> |
||||
<x-button type="ghost" size="small" @click="_ckQuery" icon="fa fa-search"></x-button> |
||||
</div> |
||||
<div class="list"> |
||||
<x-datepicker |
||||
:value="[searchParams.startDate,searchParams.endDate]" |
||||
ref="datepicker" |
||||
@on-change="_onChangeStartStop" |
||||
type="daterange" |
||||
format="YYYY-MM-DD HH:mm:ss" |
||||
placement="bottom-end" |
||||
:panelNum="2"> |
||||
<x-input slot="input" readonly slot-scope="{value}" :value="value" style="width: 310px;" size="small" :placeholder="$t('Select date range')"> |
||||
<i slot="suffix" |
||||
@click.stop="_dateEmpty()" |
||||
class="ans-icon-fail-solid" |
||||
v-show="value" |
||||
style="font-size: 13px;cursor: pointer;margin-top: 1px;"> |
||||
</i> |
||||
</x-input> |
||||
</x-datepicker> |
||||
</div> |
||||
<div class="list"> |
||||
<x-input v-model="searchParams.destTable" style="width: 120px;" size="small" :placeholder="$t('Target Table')"></x-input> |
||||
</div> |
||||
<div class="list"> |
||||
<x-input v-model="searchParams.sourceTable" style="width: 120px;" size="small" :placeholder="$t('Source Table')"></x-input> |
||||
</div> |
||||
<div class="list"> |
||||
<x-select style="width: 90px;" @on-change="_onChangeState" :value="searchParams.state"> |
||||
<x-input slot="trigger" readonly :value="selectedModel ? selectedModel.label : ''" slot-scope="{ selectedModel }" style="width: 90px;" size="small" :placeholder="$t('State')" suffix-icon="ans-icon-arrow-down"></x-input> |
||||
<x-option |
||||
v-for="city in stateList" |
||||
:key="city.label" |
||||
:value="city.code" |
||||
:label="city.label"> |
||||
</x-option> |
||||
</x-select> |
||||
</div> |
||||
<div class="list"> |
||||
<x-datepicker |
||||
v-model="searchParams.taskDate" |
||||
@on-change="_onChangeDate" |
||||
format="YYYY-MM-DD" |
||||
:panelNum="1"> |
||||
<x-input slot="input" readonly slot-scope="{value}" style="width: 130px;" :value="value" size="small" :placeholder="$t('Date')"></x-input> |
||||
</x-datepicker> |
||||
</div> |
||||
<div class="list"> |
||||
<x-input v-model="searchParams.taskName" style="width: 130px;" size="small" :placeholder="$t('Task Name')"></x-input> |
||||
</div> |
||||
</template> |
||||
</m-conditions> |
||||
</template> |
||||
<script> |
||||
import _ from 'lodash' |
||||
import mConditions from '@/module/components/conditions/conditions' |
||||
export default { |
||||
name: 'conditions', |
||||
data () { |
||||
return { |
||||
stateList: [ |
||||
{ |
||||
label: `${this.$t('none')}`, |
||||
code: `` |
||||
}, |
||||
{ |
||||
label: `${this.$t('success')}`, |
||||
code: `成功` |
||||
}, |
||||
{ |
||||
label: `${this.$t('waiting')}`, |
||||
code: `等待` |
||||
}, |
||||
{ |
||||
label: `${this.$t('execution')}`, |
||||
code: `执行中` |
||||
}, |
||||
{ |
||||
label: `${this.$t('finish')}`, |
||||
code: `完成` |
||||
}, { |
||||
label: `${this.$t('failed')}`, |
||||
code: `失败` |
||||
} |
||||
], |
||||
searchParams: { |
||||
taskName: '', |
||||
state: '', |
||||
sourceTable: '', |
||||
destTable: '', |
||||
taskDate: '', |
||||
startDate: '', |
||||
endDate: '' |
||||
} |
||||
} |
||||
}, |
||||
props: {}, |
||||
methods: { |
||||
_ckQuery () { |
||||
this.$emit('on-query', this.searchParams) |
||||
}, |
||||
/** |
||||
* change times |
||||
*/ |
||||
_onChangeStartStop (val) { |
||||
this.searchParams.startDate = val[0] |
||||
this.searchParams.endDate = val[1] |
||||
}, |
||||
/** |
||||
* change state |
||||
*/ |
||||
_onChangeState (val) { |
||||
this.searchParams.state = val.value |
||||
}, |
||||
/** |
||||
* empty date |
||||
*/ |
||||
_dateEmpty () { |
||||
this.searchParams.startDate = '' |
||||
this.searchParams.endDate = '' |
||||
this.$refs.datepicker.empty() |
||||
}, |
||||
_onChangeDate (val) { |
||||
this.searchParams.taskDate = val.replace(/-/g, '') |
||||
} |
||||
}, |
||||
created () { |
||||
// Routing parameter merging |
||||
if (!_.isEmpty(this.$route.query)) { |
||||
this.searchParams = _.assign(this.searchParams, this.$route.query) |
||||
} |
||||
}, |
||||
mounted () { |
||||
}, |
||||
components: { mConditions } |
||||
} |
||||
</script> |
@ -0,0 +1,102 @@
|
||||
<template> |
||||
<div class="main-layout-box"> |
||||
<m-secondary-menu :type="'projects'"></m-secondary-menu> |
||||
<m-list-construction :title="config.title"> |
||||
<template slot="conditions"> |
||||
<m-conditions @on-query="_onQuery"></m-conditions> |
||||
</template> |
||||
<template slot="content"> |
||||
<template v-if="taskRecordList.length"> |
||||
<m-list :task-record-list="taskRecordList" @on-update="_onUpdate" :page-no="searchParams.pageNo" :page-size="searchParams.pageSize"> |
||||
</m-list> |
||||
<div class="page-box"> |
||||
<x-page :current="parseInt(searchParams.pageNo)" :total="total" show-elevator @on-change="_page"></x-page> |
||||
</div> |
||||
</template> |
||||
<template v-if="!taskRecordList.length"> |
||||
<m-no-data></m-no-data> |
||||
</template> |
||||
<m-spin :is-spin="isLoading"></m-spin> |
||||
</template> |
||||
</m-list-construction> |
||||
</div> |
||||
</template> |
||||
<script> |
||||
import _ from 'lodash' |
||||
import mList from './_source/list' |
||||
import store from '@/conf/home/store' |
||||
import mConditions from './_source/conditions' |
||||
import mSpin from '@/module/components/spin/spin' |
||||
import mNoData from '@/module/components/noData/noData' |
||||
import listUrlParamHandle from '@/module/mixin/listUrlParamHandle' |
||||
import mSecondaryMenu from '@/module/components/secondaryMenu/secondaryMenu' |
||||
import mListConstruction from '@/module/components/listConstruction/listConstruction' |
||||
|
||||
export default { |
||||
name: 'task-record-list', |
||||
data () { |
||||
return { |
||||
store, |
||||
total: null, |
||||
taskRecordList: [], |
||||
isLoading: true, |
||||
searchParams: { |
||||
taskName: '', |
||||
state: '', |
||||
sourceTable: '', |
||||
destTable: '', |
||||
taskDate: '', |
||||
startDate: '', |
||||
endDate: '', |
||||
pageSize: 10, |
||||
pageNo: 1 |
||||
} |
||||
} |
||||
}, |
||||
mixins: [listUrlParamHandle], |
||||
props: { |
||||
config: String |
||||
}, |
||||
methods: { |
||||
_onQuery (o) { |
||||
this.searchParams = _.assign(this.searchParams, o) |
||||
this.searchParams.pageNo = 1 |
||||
}, |
||||
_page (val) { |
||||
this.searchParams.pageNo = val |
||||
}, |
||||
/** |
||||
* get list data |
||||
*/ |
||||
_getList (flag) { |
||||
this.isLoading = !flag |
||||
this.store.dispatch(`dag/${this.config.apiFn}`, this.searchParams).then(res => { |
||||
this.taskRecordList = [] |
||||
this.taskRecordList = res.totalList |
||||
this.total = res.total |
||||
this.isLoading = false |
||||
}).catch(e => { |
||||
this.isLoading = false |
||||
}) |
||||
}, |
||||
_onUpdate () { |
||||
this._debounceGET() |
||||
} |
||||
}, |
||||
watch: { |
||||
// router |
||||
'$route' (a) { |
||||
// url no params get instance list |
||||
if (_.isEmpty(a.query)) { |
||||
this.searchParams.processInstanceId = '' |
||||
} |
||||
this.searchParams.pageNo = _.isEmpty(a.query) ? 1 : a.query.pageNo |
||||
} |
||||
}, |
||||
created () { |
||||
}, |
||||
mounted () { |
||||
}, |
||||
components: { mList, mConditions, mSpin, mListConstruction, mSecondaryMenu, mNoData } |
||||
} |
||||
</script> |
@ -0,0 +1,18 @@
|
||||
<template> |
||||
<m-list :config="config"></m-list> |
||||
</template> |
||||
<script> |
||||
import mList from '@/conf/home/pages/projects/pages/_source/taskRecordList' |
||||
export default { |
||||
name: 'history-task-record', |
||||
data () { |
||||
return { |
||||
config: { |
||||
title: `${this.$t('History task record')}`, |
||||
apiFn: 'getHistoryTaskRecordList' |
||||
} |
||||
} |
||||
}, |
||||
components: { mList } |
||||
} |
||||
</script> |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue