001/* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016package org.opengion.hayabusa.report2; 017 018import java.io.File; 019import java.io.IOException; 020 021import org.opengion.fukurou.system.ThrowUtil; // 6.4.2.0 (2016/01/29) 022import org.opengion.fukurou.util.AbstractObjectPool; 023import org.opengion.fukurou.util.Cleanable; 024import org.opengion.fukurou.util.FileUtil; 025import org.opengion.hayabusa.common.HybsSystem; 026import org.opengion.hayabusa.common.SystemManager; 027 028/** 029 * Sofficeã®ï¾Œï¾Ÿï¾›ï½¾ï½½ã‚’管ç†ã™ã‚‹ï¾Œï½§ï½¸ï¾„リクラスã§ã™ï½¡ 030 * プロセスプールã®å®Ÿè£…ã¯ï½¤AbstractObjectPoolを継承ã—ã¦å®Ÿè£…ã•ã‚Œã¦ã„ã¾ã™ï½¡ 031 * 032 * プロセスã®åˆæœŸç”Ÿæˆæ•°ã¯0ã§ã™ï½¡æœ€å¤§ç”Ÿæˆæ•°ã¯ï½¤ï½¼ï½½ï¾ƒï¾‘リソースã®REPORT_MAX_PROCESS_COUNT㧠033 * 定義ã•ã‚Œã¾ã™ï½¡ã¾ãŸï½¤ç”Ÿå˜æ™‚é–“ã¯ï½¤REPORT_PROCESS_ALIVEã§å®šç¾©ã•ã‚Œã¦ã„ã¾ã™ï½¡ 034 * 035 * プロセスを全ã¦çµ‚了ã™ã‚‹ã«ã¯ï½¤clearメソッドを呼ã³å‡ºã—ã¾ã™ï½¡ 036 * clearメソッドã¯ï½¤Cleanableï½²ï¾ï¾€ï½°ï¾Œï½ªï½°ï½½ã®å®Ÿè£…ã¨ã—ã¦çµ„ã¿è¾¼ã¾ã‚Œï½¤SytemManagerã«ç™»éŒ²ã•ã‚Œã‚‹ãŸã‚、 037 * Tomcat終了時ã«ï½¤è‡ªå‹•çš„ã«ï¾Œï¾Ÿï¾›ï½¾ï½½ãŒçµ‚了ã•ã‚Œã¾ã™ï½¡ 038 * 但ã—、貸ã—出ã—ä¸(処ç†ä¸)ã®ï¾Œï¾Ÿï¾›ï½¾ï½½ã¯ï½¤AbstractObjecgPoolã®å®Ÿè£…ã‹ã‚‰ï½¤çµ‚了ã•ã‚Œãªã„ãŸã‚、別ã®æ–¹æ³•ã§ 039 * 明示的ã«killã™ã‚‹å¿…è¦ãŒã‚ã‚Šã¾ã™ 040 * 041 * @version 4.0 042 * @author Hiroki Nakamura 043 * @since JDK5.0, 044 */ 045public final class ProcessFactory { 046 047 /** 048 * プロセスプール 049 */ 050 private static ProcessPool pp = new ProcessPool() ; 051 052 /** Cleanable ï½²ï¾ï¾€ï½°ï¾Œï½ªï½°ï½½ã«ã‚ˆã‚‹åˆæœŸåŒ–å‡¦ç† */ 053 static { 054 final Cleanable clr = new Cleanable() { 055 public void clear() { 056 ProcessFactory.clear(); 057 } 058 }; 059 SystemManager.addCleanable( clr ); 060 061 // 5.2.2.0 (2010/11/01) 循環å‚照解消ã®ãŸã‚、SystemManager ã‹ã‚‰ç§»å‹• 062 final Cleanable clr2 = new Cleanable() { 063 /** 064 * åˆæœŸåŒ–(クリア)ã—ã¾ã™ï½¡ 065 * 主ã«ï½¤ï½·ï½¬ï½¯ï½¼ï½ï½¸ï¾˜ï½±ã§åˆ©ç”¨ã—ã¾ã™ï½¡ 066 */ 067 public void clear() { 068// ProcessFactory.kill(); 069// kill(); // 8.5.4.2 (2024/01/12) PMD 7.0.0 UnnecessaryFullyQualifiedName 070 } 071 }; 072 SystemManager.addCleanable( clr2 , true ); // コï¾ï¾ƒï½·ï½½ï¾„終了時ã®ã¿å‘¼ã³å‡ºã™ 073 } 074 075 /** 076 * デフォルトコï¾ï½½ï¾„ラクターをprivateã«ã—ã¦ã€ 077 * オブジェクトã®ç”Ÿæˆã‚’ã•ã›ãªã„よã†ã«ã™ã‚‹ã€‚ 078 */ 079 private ProcessFactory() {} 080 081 /** 082 * OpenOfficeã®ï¾Œï¾Ÿï¾›ï½¾ï½½ã‚’生æˆã—ã¾ã™ï½¡ 083 * 084 * @return sofficeã®ï¾Œï¾Ÿï¾›ï½¾ï½½ 085 */ 086 public static SOfficeProcess newInstance() { 087 return pp.newInstance(); 088 } 089 090 /** 091 * OpenOfficeã®ï¾Œï¾Ÿï¾›ï½¾ï½½ã‚’リリースã—ã¾ã™ï½¡ 092 * 093 * @param soffice SOfficeProcessオブジェクト 094 */ 095 public static void release( final SOfficeProcess soffice ) { 096 pp.release( soffice ); 097 } 098 099 /** 100 * OpenOfficeã®ï¾Œï¾Ÿï¾›ï½¾ï½½ã‚’クローズã—ã¾ã™ï½¡ 101 * 102 * @param soffice SOfficeProcessオブジェクト 103 */ 104 public static void remove( final SOfficeProcess soffice ) { 105 pp.remove( soffice ); 106 } 107 108 /** 109 * プールã•ã‚Œã¦ã„ã‚‹OpenOfficeã®ï¾Œï¾Ÿï¾›ï½¾ï½½ã‚’å…¨ã¦ï½¸ï¾›ï½°ï½½ï¾žã—ã¾ã™ï½¡ 110 */ 111 public static void clear() { 112 pp.clear(); 113 } 114 115 /** 116 * å…¨ã¦ã®soffice.binプロセスをKILLã—ã¾ã™ï½¡ 117 * アプリケーショï¾ã®çµ‚了処ç†ã§å®Ÿè¡Œã—ã¾ã™ï½¡ 118 * OSåãŒWindowsã‚’å«ã‚€å ´åˆã¯taskkill、ãれ以外ã®å ´åˆã¯killallã—ã¾ã™ï½¡ 119 * åˆï½¤ï¾Œï¾Ÿï¾›ï½¾ï½½çµ‚了後ã«ï½ºï¾‹ï¾Ÿï½°ã•ã‚ŒãŸè¨å®šï¾Œï½§ï½²ï¾™ã‚’削除ã—ã¾ã™ï½¡ 120 * 121 * @og.rev 4.3.0.0 (2008/07/18) è¿½åŠ 122 * @og.rev 4.3.0.0 (2008/07/22) è¨å®šï¾Œï½§ï½²ï¾™ã®å‰Šé™¤ã‚’è¿½åŠ 123 * @og.rev 4.3.5.0 (2009/02/01) Exception ã‚’ãã‚Œãžã‚Œã®Exceptionã«åˆ†ã‘ã¦æ•ã‚‰ãˆã‚‹ï½¡ 124 * @og.rev 6.4.2.0 (2016/01/29) ex.printStackTrace() を、ThrowUtil#ogStackTrace(Throwable) ã«ç½®ãæ›ãˆï½¡ 125 */ 126 public static void kill() { 127 try { 128 final String osName = HybsSystem.sys( "OS_INFO" ); //System.getProperty("os.name"); 129 if( osName.indexOf( "Windows" ) >= 0 ){ 130 // 4.3.0.0 (2008/07/18) Windoesã®taskkillを利用ã—ã¦soffice.binã®ï¾€ï½½ï½¸ã‚’強制終了ã—ã¾ã™ï½¡ 131 new ProcessBuilder( "cmd.exe","/c","taskkill","/F","/IM","soffice.bin" ).start().waitFor(); 132 } 133 else{ 134 // 4.3.0.0 (2008/07/24) Windowsã§ã¯ãªã„å ´åˆã¯killallコï¾ï¾ï¾„゙ 135 new ProcessBuilder( "killall","-9","-w","soffice.bin" ).start().waitFor(); 136 } 137 138 // 4.3.0.0 (2008/07/22) è¨å®šï¾Œï½§ï½²ï¾™(SOfficeProcessã§ï¾ƒï¾žï½¨ï¾šï½¸ï¾„リをè¨å®š)を全削除ã—ã¾ã™ï½¡ 139 FileUtil.deleteFiles( new File( SOfficeProcess.ENV_DIR ) ); 140 } 141 // 6.3.9.1 (2015/11/27) Exceptionã‚’ã¾ã¨ã‚ã¾ã™ï½¡ 142 catch( final IOException | InterruptedException | RuntimeException ex ) { 143 System.err.println( ThrowUtil.ogStackTrace( ex ) ); // 6.4.2.0 (2016/01/29) 144 } 145 } 146 147 /** 148 * ç¾åœ¨ã®çŠ¶æ…‹ã‚’æ–‡å—列ã§è¿”ã—ã¾ã™ï½¡ 149 * 150 * @return ç¾åœ¨ã®çŠ¶æ…‹ 151 * @og.rtnNotNull 152 */ 153 public static String information() { 154 return pp.toString(); 155 } 156 157 /** 158 * ProcessPool ã¯ï½¤AbstractObjectPool を継承ã—㟠オブジェクトプールã§ã™ï½¡ 159 * 160 * OpenOfficeã®ï¾Œï¾Ÿï¾›ï½¾ï½½ã‚’プールã—ã¾ã™ï½¡ 161 * 162 * @version 4.0 163 * @author Hiroki Nakamura 164 * @since JDK5.0, 165 */ 166 protected static final class ProcessPool extends AbstractObjectPool<SOfficeProcess> { 167 // 環境ファイル作æˆã®è˜åˆ¥ç”¨ 168 private int count ; 169 170 /** 171 * åˆæœŸå‡¦ç†ã‚’è¡Œã„ã¾ã™ï½¡ 172 */ 173 protected ProcessPool() { 174 super(); // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor 175 init( 0, HybsSystem.sysInt( "REPORT_MAX_PROCESS_COUNT") 176 , true, HybsSystem.sysInt( "REPORT_PROCESS_ALIVE" ) ); 177 } 178 179 /** 180 * soffieã®ï¾Œï¾Ÿï¾›ï½¾ï½½ï½µï¾Œï¾žï½¼ï¾žï½ªï½¸ï¾„を作æˆã—ã¾ã™ï½¡ 181 * 182 * @og.rev 4.3.5.0 (2009/02/01) Exception ã§ã¯ãªã、RuntimeException ã«å¤‰æ›´ 183 * @og.rev 5.1.7.0 (2010/06/01) TCP接続対応 184 * 185 * @return OpenOfficeã®ï¾Œï¾Ÿï¾›ï½¾ï½½ 186 */ 187 @Override 188 protected SOfficeProcess createInstance() { 189 SOfficeProcess soffice = null; 190 try { 191 // 5.1.7.0 (2010/06/01) TCP接続対応 192 if( "TCP".equalsIgnoreCase( HybsSystem.sys( "REPORT_OOO_CONN_TYPE" ) ) ) { 193 soffice = new SOfficeProcessTcp( "env_" + count, HybsSystem.sysInt( "REPORT_OOO_MIN_PORT" ) ); 194 } 195 else { 196 soffice = new SOfficeProcess( "env_" + count ); 197 } 198 soffice.bootstrap(); 199 200 count++; 201 } 202 catch( final RuntimeException ex ) { 203 System.out.println( "[ERROR]FACTORY:Failed to Connect Soffice! " + ex.getMessage() ); 204 } 205 return soffice; 206 } 207 208 /** 209 * オブジェクトプールã‹ã‚‰å‰Šé™¤ã™ã‚‹ã¨ãã«å‘¼ã°ã‚Œã¾ã™ï½¡ 210 * ã“ã®ï¾’ソッドã§å„オブジェクトã”ã¨ã®çµ‚了処ç†ã‚’è¡Œã„ã¾ã™ï½¡ 211 * 212 * @param soffice OpenOfficeã®ï¾Œï¾Ÿï¾›ï½¾ï½½ 213 */ 214 @Override 215 protected void objectFinal( final SOfficeProcess soffice ) { 216 soffice.close(); 217 } 218 } 219}