'use strict' ; import * as fse from "fs-extra" ; import * as _ from "lodash" ; import * as path from "path" ; import * as vscode from "vscode" ; import * as Constants from "../Constants" ; import { Utility } from "../Utility" ; import { TomcatServer } from "./TomcatServer" ; export class TomcatModel { private _serverList : TomcatServer [ ] = [ ] ; private _serversJsonFile : string ; constructor ( public defaultStoragePath ) { this . _serversJsonFile = path . join ( defaultStoragePath , 'servers.json' ) ; this . initServerListSync ( ) ; vscode . debug . onDidTerminateDebugSession ( ( session ) => { if ( session && session . name && session . name . startsWith ( Constants . DEBUG_SESSION_NAME ) ) { this . clearServerDebugInfo ( session . name . split ( '_' ) . pop ( ) ) ; } } ) ; } public getServerSet ( ) { return this . _serverList ; } public getTomcatServer ( serverName ) { return this . _serverList . find ( ( item ) => item . getName ( ) === serverName ) ; } public async saveServerList ( ) { try { await fse . outputJson ( this . _serversJsonFile , this . _serverList . map ( ( s ) => { return { _name : s . getName ( ) , _installPath : s . getInstallPath ( ) , _storagePath : s . getStoragePath ( ) } ; } ) ) ; vscode . commands . executeCommand ( 'tomcat.tree.refresh' ) ; } catch ( err ) { console . error ( err . toString ( ) ) ; } } public async updateJVMOptions ( serverName ) { const server = this . getTomcatServer ( serverName ) ; const installPath = server . getInstallPath ( ) ; const catalinaBase = server . getStoragePath ( ) ; const bootStrap = path . join ( installPath , 'bin' , 'bootstrap.jar' ) ; const tomcat = path . join ( installPath , 'bin' , 'tomcat-juli.jar' ) ; let result = [ `${ Constants . CLASS_PATH_KEY } "${ [ bootStrap , tomcat ] . join ( path . delimiter ) }"` , `${ Constants . CATALINA_BASE_KEY }="${ catalinaBase }"` , `${ Constants . CATALINA_HOME_KEY }="${ installPath }"` , `${ Constants . ENCODING }` ] ; if ( ! await fse . pathExists ( server . jvmOptionFile ) ) { server . jvmOptions = result . concat ( [ Constants . BOOTSTRAP_FILE , '"$@"' ] ) ; return ; } const filterFunction = ( para ) : boolean => { if ( ! para . startsWith ( '-' ) ) { return false ; } let valid = true ; Constants . JVM_DEFAULT_OPTIONS_KEYS . forEach ( ( key ) => { if ( para . startsWith ( key ) ) { valid = false ; return ; } } ) ; return valid ; } ; result = result . concat ( await Utility . readFileLineByLine ( server . jvmOptionFile , filterFunction ) ) ; const tmpDirConfiguration = result . find ( ( element ) => { return element . indexOf ( Constants . JAVA_IO_TEMP_DIR_KEY ) >= 0 ; } ) ; if ( ! tmpDirConfiguration ) { result = result . concat ( `${ Constants . JAVA_IO_TEMP_DIR_KEY }="${ path . join ( catalinaBase , 'temp' ) }"` ) ; } server . jvmOptions = result . concat ( [ Constants . BOOTSTRAP_FILE , '"$@"' ] ) ; } public deleteServer ( tomcatServer ) { const index = this . _serverList . findIndex ( ( item ) => item . getName ( ) === tomcatServer . getName ( ) ) ; if ( index > - 1 ) { const oldServer = this . _serverList . splice ( index , 1 ) ; if ( ! _ . isEmpty ( oldServer ) ) { fse . remove ( tomcatServer . getStoragePath ( ) ) ; this . saveServerList ( ) ; return true ; } } return false ; } public addServer ( tomcatServer ) { const index = this . _serverList . findIndex ( ( item ) => item . getName ( ) === tomcatServer . getName ( ) ) ; if ( index > - 1 ) { this . _serverList . splice ( index , 1 ) ; } this . _serverList . push ( tomcatServer ) ; this . saveServerList ( ) ; } public saveServerListSync ( ) { try { fse . outputJsonSync ( this . _serversJsonFile , this . _serverList . map ( ( s ) => { return { _name : s . getName ( ) , _installPath : s . getInstallPath ( ) , _storagePath : s . getStoragePath ( ) } ; } ) ) ; } catch ( err ) { console . error ( err . toString ( ) ) ; } } private initServerListSync ( ) { try { if ( fse . existsSync ( this . _serversJsonFile ) ) { const objArray = fse . readJsonSync ( this . _serversJsonFile ) ; if ( ! _ . isEmpty ( objArray ) ) { this . _serverList = this . _serverList . concat ( objArray . map ( ( obj ) => { return new TomcatServer ( obj . _name , obj . _installPath , obj . _storagePath ) ; } ) ) ; } } } catch ( err ) { console . error ( err ) ; } } private clearServerDebugInfo ( basePathName ) { const server = this . _serverList . find ( ( s ) => { return s . basePathName === basePathName ; } ) ; if ( server ) { server . clearDebugInfo ( ) ; } } }