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.IOException;
019import java.net.InetSocketAddress;
020import java.net.Socket;
021
022import org.opengion.hayabusa.common.HybsSystemException;
023
024/**
025 * OpenOfficeのプロセスを表すクラスです。
026 *
027 * このクラスでは、TCPによりプロセスに接続を行います。
028 * 基本的には、パイプ名による接続({@link SOfficeProcess})を利用すべきですが、
029 * x64環境で、64Bit版のJavaを起動した場合、パイプ接続では、UnsatisfiedLinkErrorが発生します。
030 * このような場合では、TCP接続を利用することで、上記エラーを回避することができます。
031 *
032 * @version  4.0
033 * @author   Hiroki Nakamura
034 * @since    JDK5.0,
035 */
036public final class SOfficeProcessTcp extends SOfficeProcess {
037
038        private static final boolean[] ports    = new boolean[512];
039        private static final Object lock                = new Object();
040
041        private final int initPort;
042        private final int thisPort;
043
044        /**
045         * コンストラクタです。
046         *
047         * @param       id                      プロセスID
048         * @param       initPort        初期ポート
049         */
050        protected SOfficeProcessTcp( final String id, final int initPort ) {
051                super( id );
052
053                this.initPort = initPort;
054                this.thisPort = getThisPort();
055        }
056
057        /**
058         * TCP接続ポート番号を取得します。
059         *
060         * @return TCP接続ポート番号
061         */
062        private int getThisPort() {
063                try {
064                        Thread.sleep( 100 ); // 切断後すぐにopenされると、ポートチェックで引っかかるため100msWAIT
065                }
066                catch( InterruptedException ex ) {
067                        // ここの Exception は、無視します。
068                }
069
070                int port = -1;
071                synchronized( lock ) {
072                        for( int i=0; i<ports.length; i++ ) {
073                                // 6.0.0.1 (2014/04/25) These nested if statements could be combined
074                                if( !ports[i] && checkPort( initPort + i ) ) {
075                                        ports[i] = true;
076                                        port = initPort + i;
077                                        break;
078                                }
079                        }
080                }
081                if( port < 0 ) {
082                        throw new HybsSystemException( "TCP接続ポートを取得することができません" );
083                }
084
085                return port;
086        }
087
088        /**
089         * 引数のポートが使用中かどうかを調べます。
090         *
091         * @param port ポート番号
092         *
093         * @return      使用中かどうか
094         */
095        private boolean checkPort( final int port ) {
096                boolean flg = false;
097                Socket sk = null;
098                try {
099                        sk = new Socket();
100                        sk.connect( new InetSocketAddress( "localhost", port ) );
101                }
102                catch( IOException ex ) {
103                        flg = true;
104                }
105                finally {
106                        try {
107                                if( sk != null ) { sk.close(); }        // 5.5.2.6 (2012/05/25) findbugs対応
108                        }
109                        catch( IOException ex ) {
110                                ex.printStackTrace();
111                        }
112                }
113                return flg;
114        }
115
116        /**
117         * Pipe名をキーにOpenOfficeのプロセスに接続するための文字列を生成します。
118         * ※TCP接続の場合、キーのPipe名は無視され、内部的に管理されるポート番号一覧より
119         *   接続ポートを取得します。
120         *
121         * @param key Pipe名(無視されます)
122         *
123         * @return 接続文字列
124         */
125        @Override
126        protected String getConnParam( final String key ) {
127                System.out.println( "[INFO]OOo:TCP Connection Start,port=" + thisPort );
128                return "uno:socket,host=localhost,tcpNoDelay=1,port=" + thisPort + ";urp;StarOffice.ComponentContext";
129        }
130
131        /**
132         * Pipe名をキーにOpenOfficeのプロセスを生成するためのパラメーター文字列を生成します。
133         * ※TCP接続の場合、キーのPipe名は無視され、内部的に管理されるポート番号一覧より
134         *   接続ポートを取得します。
135         *
136         * @param key Pipe名(無視されます)
137         *
138         * @return プロセス生成パラメーター
139         */
140        @Override
141        protected String getProcParam( final String key ) {
142                return "-accept=socket,host=localhost,port=" + thisPort + ";urp;";
143        }
144
145        /**
146         * プロセスを終了します。
147         * また、同時に環境設定用のファイルも削除します。
148         * ここでは、プロセスを終了すると同時に、そのプロセスのポート番号を開放し、
149         * 次に起動されるプロセスで利用できるようにします。
150         */
151        @Override
152        public void close() {
153                super.close();
154                synchronized( lock ) {
155                        ports[thisPort-initPort] = false;
156                }
157                System.out.println( "[INFO]OOo:TCP Connection End(Release),port=" + thisPort );
158        }
159}
160