55 */
66package com .example .controller ;
77
8+ import cn .hutool .core .io .FileUtil ;
89import cn .org .rapid_framework .generator .Generator ;
910import cn .org .rapid_framework .generator .GeneratorFacade ;
1011import cn .org .rapid_framework .generator .GeneratorProperties ;
12+ import com .alibaba .fastjson .JSON ;
13+ import com .example .Application ;
1114import com .example .constant .Constant ;
1215import com .example .dao .GeneratorDao ;
1316import com .example .exception .SystemException ;
1720import org .apache .commons .io .FileUtils ;
1821import org .apache .commons .io .IOUtils ;
1922import org .apache .commons .lang3 .StringUtils ;
23+ import org .slf4j .Logger ;
24+ import org .slf4j .LoggerFactory ;
2025import org .springframework .beans .factory .annotation .Value ;
2126import org .springframework .web .bind .annotation .*;
2227import org .springframework .beans .factory .annotation .Autowired ;
3035import javax .servlet .http .HttpServletRequest ;
3136import javax .servlet .http .HttpServletResponse ;
3237import java .io .*;
33- import java .net .URL ;
34- import java .net .URLConnection ;
3538import java .util .*;
3639
3740/**
4447@ RequestMapping ("database" )
4548public class DataBaseController {
4649
50+ /**
51+ * logger
52+ */
53+ private static final Logger logger = LoggerFactory .getLogger (DataBaseController .class );
54+
4755 /**
4856 * 代码生成的文件输出路径
4957 */
@@ -182,9 +190,11 @@ public void genTableToZip(@PathVariable("tableName") String tableName,
182190 // 配置表名
183191 String [] tableNames = tableName .split ("-" );
184192 // 获得临时路径
185- String loadPath = request .getSession ().getServletContext ().getRealPath ("/" );
193+ String loadPath = request .getSession ().getServletContext ().getRealPath (File .separator );
194+ logger .info ("临时路径: {}" , loadPath );
186195 // 创建临时路径
187- File tempDir = new File (loadPath + "/code" );
196+ File tempDir = new File (loadPath + File .separator + "code" );
197+ logger .info ("完整临时路径: {}" , loadPath + File .separator + "code" );
188198 if (!tempDir .exists ()) {
189199 tempDir .mkdirs ();
190200 }
@@ -231,6 +241,7 @@ public ResponseBean open() {
231241 Runtime .getRuntime ().exec ("cmd.exe /c start " + System .getProperty ("user.dir" ) + outRoot );
232242 }
233243 } catch (IOException e ) {
244+ e .printStackTrace ();
234245 throw new SystemException ("操作失败" );
235246 }
236247 return new ResponseBean (HttpStatus .OK .value (), "操作成功" , null );
@@ -245,51 +256,49 @@ public ResponseBean open() {
245256 @ PutMapping ("/config" )
246257 public ResponseBean config (@ RequestBody Map <String , String > config ) {
247258 try {
248- final Enumeration urls = DataBaseController .class .getClassLoader ().getResources ("config/generator.properties" );
249- while (urls .hasMoreElements ()) {
250- final URL url = (URL ) urls .nextElement ();
251- InputStream input = null ;
252- OutputStream output = null ;
253- try {
254- final URLConnection con = url .openConnection ();
255- con .setUseCaches (false );
256- input = con .getInputStream ();
257- SafeProperties safeProperties = new SafeProperties ();
258- if (Boolean .parseBoolean (config .get ("isRead" ))) {
259- // 读
260- safeProperties .load (input );
261- config = (Map ) safeProperties ;
262- } else {
263- // 写
264- safeProperties .load (input );
265- // 遍历map写入
266- Iterator <Map .Entry <String , String >> entries = config .entrySet ().iterator ();
267- while (entries .hasNext ()) {
268- Map .Entry <String , String > entry = entries .next ();
269- if (Constant .TEMPLATE .equals (entry .getKey ())) {
270- // 查看配置路径下macro.includ文件是否存在
271- File file = new File (Constant .PROJECT_PATH + entry .getValue () + "/macro.include" );
272- if (!file .exists ()) {
273- // 模板代码位置不存在
274- return new ResponseBean (HttpStatus .BAD_REQUEST .value (), "当前填写的模板代码位置不存在" , null );
275- }
259+ InputStream input = null ;
260+ OutputStream output = null ;
261+ try {
262+ input = FileUtil .getInputStream (Constant .CONFIG_PATH_TEMP );
263+ SafeProperties safeProperties = new SafeProperties ();
264+ if (Boolean .parseBoolean (config .get ("isRead" ))) {
265+ // 读
266+ safeProperties .load (input );
267+ config = (Map ) safeProperties ;
268+ logger .info ("读取配置: {}" , JSON .toJSONString (config ));
269+ } else {
270+ // 写
271+ safeProperties .load (input );
272+ // 遍历map写入
273+ Iterator <Map .Entry <String , String >> entries = config .entrySet ().iterator ();
274+ while (entries .hasNext ()) {
275+ Map .Entry <String , String > entry = entries .next ();
276+ if (Constant .TEMPLATE .equals (entry .getKey ())) {
277+ // 查看配置路径下macro.includ文件是否存在
278+ File file = new File (Constant .PROJECT_PATH + entry .getValue () + "/macro.include" );
279+ if (!file .exists ()) {
280+ // 模板代码位置不存在
281+ return new ResponseBean (HttpStatus .BAD_REQUEST .value (), "当前填写的模板代码位置不存在" , null );
276282 }
277- safeProperties .setProperty (entry .getKey (), entry .getValue ());
278283 }
279- output = new FileOutputStream (url .getPath ());
280- // 更新,服务器热重启
281- safeProperties .store (output , null );
282- }
283- } finally {
284- if (input != null ) {
285- input .close ();
286- }
287- if (output != null ) {
288- output .close ();
284+ safeProperties .setProperty (entry .getKey (), entry .getValue ());
289285 }
286+ logger .info ("更新配置: {}, url: {}" , JSON .toJSONString (safeProperties ), Constant .CONFIG_PATH_TEMP );
287+ output = new FileOutputStream (Constant .CONFIG_PATH_TEMP );
288+ // 更新,服务器热重启
289+ safeProperties .store (output , null );
290+ Application .restart ();
291+ }
292+ } finally {
293+ if (input != null ) {
294+ input .close ();
295+ }
296+ if (output != null ) {
297+ output .close ();
290298 }
291299 }
292300 } catch (IOException e ) {
301+ e .printStackTrace ();
293302 throw new SystemException ("操作失败" );
294303 }
295304 return new ResponseBean (HttpStatus .OK .value (), "操作成功" , config );
@@ -308,10 +317,11 @@ public ResponseBean config(@RequestBody Map<String, String> config) {
308317 public boolean genCode (String [] tableNames , String outRoot ) throws IOException {
309318 GeneratorFacade generatorFacade = new CustomGeneratorFacade (outRoot );
310319 // 配置信息
311- GeneratorProperties .load (new String []{ "classpath:config/generator.properties" } );
320+ GeneratorProperties .load (Constant . CONFIG_PATH_TEMP );
312321 // 模板位置
313322 Generator generator = generatorFacade .getGenerator ();
314- generator .addTemplateRootDir (Constant .PROJECT_PATH + template );
323+ logger .info (Constant .PROJECT_PATH + File .separator + template );
324+ generator .addTemplateRootDir (Constant .PROJECT_PATH + File .separator + template );
315325 // 开始执行
316326 try {
317327 for (String tableName : tableNames ) {
0 commit comments