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.fukurou.system;                                                            // 6.4.2.0 (2016/01/29) package変更 fukurou.util → fukurou.system
017
018import java.nio.charset.Charset;                                                                        // 5.5.2.6 (2012/05/25)
019
020import java.net.NetworkInterface;                                                                       // 8.0.0.0 (2021/07/01)
021import java.util.Enumeration;                                                                           // 8.0.0.0 (2021/07/01)
022import java.net.InetAddress;
023
024/**
025 * 共通的に使用される固定値を集約したクラスです。
026 *
027 * 全変数は、public static final 宣言されています。
028 *
029 * @og.rev 6.1.0.0 (2014/12/26) 固定値を集約したクラス対応
030 * @og.rev 6.4.2.0 (2016/01/29) package変更 fukurou.util → fukurou.system
031 *
032 * @og.group その他
033 *
034 * @version  6.0
035 * @author       Kazuhiko Hasegawa
036 * @since    JDK8.0,
037 */
038public final class HybsConst {
039
040        /** 8.0.0.0 (2021/07/01) 実行しているサーバーの名称 */
041        public static final String HOST_NAME ;
042        /** 8.0.0.0 (2021/07/01) 実行しているサーバーのIPアドレス */
043        public static final String HOST_ADRS ;
044
045        /**
046         * ホスト名と、IPアドレスを取得
047         *
048         * Java VM が実行しているホスト名と、IPアドレスを取得します。
049         * InetAddress.getLocalHost().getHostName() で求められる値は、Virtual アドレスなどの
050         * 複数考えられる為、出来るだけ直接設定されているIPアドレスに近い値を取得するようにします。
051         * でも、完全には特定できないと思われます。
052         *
053         * @og.rev 7.3.1.1 (2021/02/25) ホスト名と、IPアドレスを取得
054         * @og.rev 8.0.0.0 (2021/07/01) fukurou.system.HybsConst に移植
055         */
056        static {
057                String dmnHost = "Unknown" ;
058                String dmnAdrs = "Unknown" ;
059                try {
060                        boolean isNext = true;
061                        final Enumeration<NetworkInterface> enuIfs = NetworkInterface.getNetworkInterfaces();
062                        while( isNext && enuIfs.hasMoreElements() ) {
063                                final NetworkInterface ni = enuIfs.nextElement();
064
065                                final String displayName = ni.getDisplayName();
066                                if( displayName.contains("Virtual") ) { continue; }
067
068                                final Enumeration<InetAddress> enuIP = ni.getInetAddresses();
069                                while( isNext && enuIP.hasMoreElements() ) {
070                                        final InetAddress adrs = enuIP.nextElement();
071
072                                        if( adrs.isLinkLocalAddress() || adrs.isLoopbackAddress() ) { continue; }
073
074                                        dmnHost = adrs.getHostName() ;                  // adrs.getCanonicalHostName() はとりあえず使わないでおく。
075                                        dmnAdrs = adrs.getHostAddress() ;
076                                        isNext = false;
077//                                      break;                                                                  // Avoid using a branching statement as the last in a loop.
078                                }
079                        }
080                }
081                catch( final Throwable th ) {
082                        System.err.println( "HOST_NAME and HOST_ADRS Unknown!" );
083                }
084                HOST_NAME = dmnHost;
085                HOST_ADRS = dmnAdrs;
086        }
087
088        /** バッファの初期容量を設定する固定値(通常より若干多い目)。  {@value}  */
089        public static final int BUFFER_SMALL  = 100;
090
091        /** バッファの初期容量を設定する固定値(通常より多い目)。  {@value}  */
092        public static final int BUFFER_MIDDLE = 200;
093
094        /** バッファの初期容量を設定する固定値(通常より大幅に多い目)。  {@value} */
095        public static final int BUFFER_LARGE  = 500;
096
097        /** システム依存の改行記号(String)。        */
098        public static final String CR = System.getProperty("line.separator");
099
100        /** HTMLでの改行記号( &lt;br /&gt; )。 */
101        // 7.0.1.0 (2018/10/15) XHTML → HTML5 対応(空要素の、"/>" 止めを、">" に変更します)。
102//      public static final String BR = "<br />" + CR ;
103        public static final String BR = "<br>" + CR ;
104
105        /** システム依存のファイルセパレーター文字(char)。  */
106        public static final char FS = System.getProperty("file.separator").charAt(0);
107
108        /** タブ文字(char)。 */
109        public static final char TAB = '\t';
110
111        /**
112         * プラットフォーム依存のデフォルトの Charset です。
113         * プラットフォーム依存性を考慮する場合、エンコード指定で作成しておく事をお勧めします。
114         *
115         * @og.rev 5.5.2.6 (2012/05/25) findbugs対応
116         * @og.rev 6.4.2.0 (2016/01/29) fukurou.util.StringUtil → fukurou.system.HybsConst に変更
117         */
118        public static final Charset DEFAULT_CHARSET = Charset.defaultCharset() ;
119
120        /**
121         * ファイル等を読み取る場合のデフォルトエンコードを指定します。
122         * 通常は、UTF-8 にしておきます。
123         *
124         * @og.rev 6.4.5.1 (2016/04/28) ファイル等を読み取る場合のデフォルトエンコードを指定します。
125         */
126        public static final String UTF_8 = "UTF-8" ;
127
128        /**
129         * データ検索時のフェッチサイズ  {@value}
130         *
131         * ORACLEのデフォルトは、10です。
132         * 最適なフェッチ・サイズは、予想される結果サイズの半分または4分の1です。結果セットのサイズかどうかわからず、
133         * フェッチ・サイズの設定を大きくしすぎたり小さくしすぎたりすると、パフォーマンスが低下することに注意してください。
134         * openGionでは、検索時の最大件数を、1000にしていますが、オーバー判定のための+1件、余分に取得しています。
135         * よって、1/4の250件に、プラスして、251件を、初期値にしておきます。
136         *
137         * @og.rev 6.9.4.1 (2018/04/09) 共通の固定値として登録
138         */
139        public static final int DB_FETCH_SIZE = 251 ;
140
141        /**
142         * データ登録時のバッチサイズ  {@value}
143         *
144         * JDBC 2.0仕様では、これをバッチ更新(batch updates)と呼びます。
145         * 標準のバッチ更新は、手動の明示的なモデルです。バッチ値は設定されていません。
146         * 手動で操作をバッチに追加し、明示的にバッチ処理のタイミングを選択します。
147         * openGionでは、検索時のページ表示件数を、100にしていますので、初期値を、 100件にしておきます。
148         *
149         * @og.rev 6.9.4.1 (2018/04/09) 共通の固定値として登録
150         */
151        public static final int DB_BATCH_SIZE = 100 ;
152
153        /**
154         * プロパティ―をみて、なければ環境変数から値を取得します。
155         *
156         * 名称は、getenv ですが、処理としては、
157         * return System.getProperty( key , System.getenv( key ) );
158         * です。
159         *
160         * System.getProperty は、java起動時に、-Dxxx=yyy と渡す変数で、key=xxx , 値=yyy を受け取ります。
161         * 変数が未定義の場合は、System.getenv で、環境変数の値を取得します。
162         *
163         * @og.rev 7.2.3.1 (2020/04/17) プロパティ―をみて、なければ環境変数から取る(サービス化対応)
164         *
165         * @param key パラメータキー
166         * @return プロパティ―をみて、なければ環境変数から値を取得
167         */
168        public static String getenv( final String key ) {
169                return System.getProperty( key , System.getenv( key ) );
170        }
171
172        /**
173         * プロパティ―をみて、なければ環境変数から値を取得します。
174         *
175         * 名称は、getenv ですが、処理としては、
176         * System.getProperty( key , System.getenv( key ) ); で受け取った値が、
177         * null か、ゼロ文字列の場合は、defval で指定した文字列を返します。
178         *
179         * System.getProperty は、java起動時に、-Dxxx=yyy と渡す変数で、key=xxx , 値=yyy を受け取ります。
180         * 変数が未定義の場合は、System.getenv で、環境変数の値を取得します。
181         *
182         * @og.rev 7.2.3.1 (2020/04/17) プロパティ―をみて、なければ環境変数から取る(サービス化対応)
183         *
184         * @param key パラメータキー
185         * @param defval        null か、ゼロ文字列の場合の初期値
186         * @return プロパティ―をみて、なければ環境変数から値を取得
187         */
188        public static String getenv( final String key , final String defval ) {
189                final String rtn = System.getProperty( key , System.getenv( key ) );
190
191                return rtn == null || rtn.isEmpty() ? defval : rtn ;
192        }
193
194        /**
195         * デフォルトコンストラクターをprivateにして、
196         * オブジェクトの生成をさせないようにする。
197         */
198        private HybsConst() {}
199}