📄 ireleaseservice.java
字号:
/*
* Created on 2007-9-6
* Last modified on 2007-9-16
* Powered by YeQiangWei.com
*/
package com.yeqiangwei.club.release;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.log4j.Logger;
import com.yeqiangwei.club.param.TopicParameter;
import com.yeqiangwei.club.release.cfg.ReleaseProvider;
import com.yeqiangwei.club.release.exception.ReleaseException;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.service.model.TopicModel;
import com.yeqiangwei.club.service.search.Constants;
import com.yeqiangwei.club.service.topic.ReplyService;
import com.yeqiangwei.club.service.topic.TopicService;
import com.yeqiangwei.io.File;
import com.yeqiangwei.net.HttpWrapper;
import com.yeqiangwei.util.Validator;
public class IReleaseService implements ReleaseService {
private static final Logger logger = Logger.getLogger(IReleaseService.class);
private static String MINID_FILE = null;
static{
if(MINID_FILE == null)
MINID_FILE = Constants.REAL_PATH+"WEB-INF"+File.separator+"release"+File.separator+"lastReleaseId.txt";
}
private String contentFilter(String content){
if(!Validator.isEmpty(ReleaseProvider.getReleaseConfig().getFolder())){
content = content.replace("club_release/",ReleaseProvider.getReleaseConfig().getFolder()+"/");
}else{
content = content.replace("club_release/","");
}
return content;
}
public String getState() {
StringBuffer sb = new StringBuffer();
sb.append("{\"indexRunning\":");
sb.append(Utils.getIndexRunning());
sb.append(",\"listRunning\":");
sb.append(Utils.getListRunning());
sb.append(",\"pageRunning\":");
sb.append(Utils.getPageRunning());
sb.append(",\"lastUrl\":\"");
sb.append(Utils.getLastUrl());
sb.append("\"");
sb.append("}");
return sb.toString();
}
public void createIndex() throws ReleaseException {
if(!Utils.getIndexRunning()){
Utils.setIndexRunning(true);
String url = ReleaseProvider.getReleaseConfig().getUrl(); //
if(!url.endsWith("/")){
url = url+"/";
}
url = url+"club_release/index.jsp";
String path = ReleaseProvider.getReleaseConfig().getPath();
if(!path.endsWith(File.separator)){
path = path+File.separator;
}
path = path+ReleaseProvider.getReleaseConfig().getFolder();
if(!path.endsWith(File.separator)){
path = path+File.separator;
}
path = path+"index."+ReleaseProvider.getReleaseConfig().getFileExt();
logger.info("url: "+url);
logger.info("path: "+path);
byte b[] = HttpWrapper.getBytes(url);
Utils.setLastUrl(url);
try {
String content = new String(b,"utf-8");
content = this.contentFilter(content);
File.createFile(path,content,"utf-8",true);
} catch (UnsupportedEncodingException e) {
logger.error(e.toString());
throw new ReleaseException(e.getMessage());
} catch (IOException e) {
logger.error(e.toString());
throw new ReleaseException(e.getMessage());
}
Utils.setIndexRunning(false);
}else{
throw new ReleaseException("首页创建任务正在运行!");
}
}
public void createList(int forumId) throws ReleaseException {
if(!Utils.getListRunning()){
Utils.setListRunning(true);
if(Utils.getStopAll()){
return;
}
String url = ReleaseProvider.getReleaseConfig().getUrl(); //
if(!url.endsWith("/")){
url = url+"/";
}
url = url+"club_release/list.jsp?forumId="+forumId;
TopicParameter param = new TopicParameter();
param.setForumId(forumId);
param.setIsDeleted(new Boolean(false));
param.setRows(60);
long total = this.getTopicService().countByParameter(param);
logger.info("total:"+total);
int pages = (int) (total/param.getRows()); //列表总页数
if(total%param.getRows()!=0){
pages++;
}
for(int i=1; i<=pages; i++){
if(Utils.getStopAll()){
break;
}
String url_ = url+"&page="+i;
logger.info("url_"+url_);
try {
byte b[] = HttpWrapper.getBytes(url_);
Utils.setLastUrl(url_);
String content = new String(b,"utf-8");
content = this.contentFilter(content);
if(content.trim().endsWith("<!-- Powered by www.YeQiangWei.com -->")){
File.createFile(Utils.getListPath(param.getForumId(),i),content,"utf-8",true);
}else{
logger.error("read error "+url);
}
} catch (UnsupportedEncodingException e) {
logger.error(e.toString());
throw new ReleaseException(e.getMessage());
} catch (IOException e) {
logger.error(e.toString());
throw new ReleaseException(e.getMessage());
}
}
Utils.setListRunning(false);
}else{
throw new ReleaseException("列表页创建任务正在运行");
}
}
public void createPages(int forumId, boolean createAll) throws ReleaseException {
if(Utils.getPageRunning()){
throw new ReleaseException("内容页创建任务正在运行");
}
Utils.setPageRunning(true);
if(Utils.getStopAll()){
return ;
}
int minId = 0;
/*
if(createAll){
minId = 0;
}else{
try {
//minId = TypeChange.stringToInt(File.readText(MINID_FILE,"utf-8"));
logger.debug("minId = "+minId);
} catch (IOException e1) {
logger.error(e1.toString());
}
}
*/
TopicParameter param = new TopicParameter();
if(minId>0){
param.setMinId(minId);
}
if(forumId>0){
param.setForumId(forumId);
}
long total = this.getTopicService().countByParameter(param);
int pages = (int) (total/1000);
if(total%1000!=0){
pages = pages+1;
}
for(int i=1; i<=pages; i++){
if(Utils.getStopAll()){
break;
}
param.setPage(i);
param.setRows(1000);
List<TopicModel> list = this.getTopicService().findByParameter(param);
if(!Validator.isEmpty(list)){
for(int n=0; n<list.size(); n++){
if(Utils.getStopAll()){
return ;
}
TopicModel topicModel = list.get(n);
try {
this.createPage(topicModel);
File.createFile(MINID_FILE,String.valueOf(topicModel.getTopicId()),"utf-8",true);
} catch (ReleaseException e) {
logger.error(e.toString());
} catch (IOException e) {
logger.error(e.toString());
}
}
}
Utils.setPageRunning(false);
}
}
public void createPage(TopicModel topicModel) throws ReleaseException {
if(Utils.getStopAll()){
return ;
}
int rows = ReleaseProvider.getReleaseConfig().getRows();
String url = ReleaseProvider.getReleaseConfig().getUrl(); //
if(!url.endsWith("/")){
url = url+"/";
}
url = url+"club_release/page.jsp?topicId="+topicModel.getTopicId();
logger.info(url);
byte b[] = HttpWrapper.getBytes(url);
Utils.setLastUrl(url);
try {
String content = new String(b,"utf-8");
content = this.contentFilter(content);
if(content.trim().endsWith("<!-- Powered by www.YeQiangWei.com -->")){
File.createFile(Utils.getPagePath(topicModel.getForumId()
,topicModel.getTopicId(),0),content,"utf-8",true);
}else{
logger.error("read error "+url);
}
} catch (UnsupportedEncodingException e) {
logger.error(e.toString());
} catch (IOException e) {
logger.error(e.toString());
}
/*
* 回复数
*/
TopicParameter param = new TopicParameter();
param.setOrderBy((byte)0);
param.setTopicId(topicModel.getTopicId());
long total = this.getReplyService().countByParameter(param);
if(total>0){
int pages = (int)total/rows;
if(total%rows!=0){pages=pages+1;}
for(int i=1; i<=pages;i++){
if(Utils.getStopAll()){
break;
}
String reUrl = url+"&page="+i;
byte r[] = HttpWrapper.getBytes(reUrl);
Utils.setLastUrl(reUrl);
try {
String content = new String(r,"utf-8");
content = this.contentFilter(content);
if(content.endsWith("<!-- Powered by www.YeQiangWei.com -->")){
File.createFile(Utils.getPagePath(topicModel.getForumId()
,topicModel.getTopicId(),i),content,"utf-8",true);
}else{
logger.error("read error "+reUrl);
}
} catch (UnsupportedEncodingException e) {
logger.error(e.toString());
} catch (IOException e) {
logger.error(e.toString());
}
}
}
}
private TopicService getTopicService() {
return ServiceWrapper.<TopicService>getSingletonInstance(ServiceLocator.TOPIC);
}
private ReplyService getReplyService() {
return ServiceWrapper.<ReplyService>getSingletonInstance(ServiceLocator.REPLY);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -