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.penguin.common;
017
018import java.lang.reflect.InvocationTargetException;                     // Ver7.0.0.0
019
020/**
021 * 共通的に使用されるメソッドを集約したクラスです。
022 *
023 * hayabusのcommon.HybsSystemと役割としてはほぼ同じです。
024 * パッケージ間の依存を切るためにこちらにも最小限の機能を持たせておきます。
025 *
026 * @og.group 初期化
027 *
028 * @version  4.0
029 * @author       Takahashi Masakazu
030 * @since    JDK5.0,
031 */
032public final class SystemUtil {
033
034        /** システム依存の改行記号をセットします。 */
035        public static final String CR = System.getProperty("line.separator");
036
037        /** HTMLでの改行記号( <br /> )をセットします。  */
038        public static final String BR = "<br />" + CR ;
039
040        /** システム依存のファイルセパレーター文字をセットします。 */
041        public static final char FS = System.getProperty("file.separator").charAt(0);
042
043        /**
044         * デフォルトコンストラクターをprivateにして、
045         * オブジェクトの生成をさせないようにする。
046         *
047         */
048        private SystemUtil() {}
049
050        /**
051         * 指定されたクラスローダを使って、識別id に応じた オブジェクトを作成します。
052         * 作成するには、デフォルトコンストラクターが必要です。
053         * initialize パラメータは true 相当(それまでに初期化されていない場合だけ初期化)です。
054         *
055         * @og.rev 6.8.2.3 (2017/11/10) java9対応(cls.newInstance() → cls.getDeclaredConstructor().newInstance())
056         *
057         * @param       cls     作成するクラスのフルネーム
058         *
059         * @return      オブジェクト
060         * @throws RuntimeException 何らかのエラーが発生した場合
061         */
062        public static Object newInstance( final String cls ) {
063                try {
064                        return Class.forName( cls ).getDeclaredConstructor().newInstance();                     // 6.8.2.3 (2017/11/10)
065                }
066                catch( final ClassNotFoundException ex1 ) {
067                        final String errMsg = "クラスが見つかりません。class=[" + cls + "]" + CR
068                                                + ex1.getMessage() ;
069                        throw new RuntimeException( errMsg,ex1 );
070                }
071                catch( final LinkageError ex2 ) {
072                        final String errMsg = "リンケージが失敗しました。class=[" + cls + "]" + CR
073                                                + ex2.getMessage();
074                        throw new RuntimeException( errMsg,ex2 );
075                }
076                catch( final InstantiationException ex3 ) {
077                        final String errMsg = "インスタンスの生成が失敗しました。class=[" + cls + "]" + CR
078                                                + ex3.getMessage() ;
079                        throw new RuntimeException( errMsg,ex3 );
080                }
081                catch( final IllegalAccessException ex4 ) {
082                        final String errMsg = "クラスまたは初期化子にアクセスできません。class=[" + cls + "]" + CR
083                                                + ex4.getMessage();
084                        throw new RuntimeException( errMsg,ex4 );
085                }
086                catch( final NoSuchMethodException | InvocationTargetException ex5 ) {                  // 6.8.2.3 (2017/11/10)
087                        final String errMsg = "指定のメソッド(コンストラクタ)が見つかりませんでした。class=[" + cls + "]" + CR
088                                                + ex5.getMessage();
089                        throw new RuntimeException( errMsg,ex5 );
090                }
091                catch( final RuntimeException ex6 ) {           // 3.6.0.0 (2004/09/17)
092                        final String errMsg = "予期せぬエラー class=[" + cls + "]" + CR
093                                                + ex6.getMessage() ;
094                        throw new RuntimeException( errMsg,ex6 );
095                }
096        }
097}