
wee0.Wee0 Maven / Gradle / Ivy
/**
* Copyright (c) 2016-2022, wee0.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package wee0;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import wee0.config.IEnv;
/**
* Wee0框架核心操作类。
*
* @author baihw
* @date 2016年12月24日
**/
/**
*
* examples:
*
**/
public final class Wee0 {
/**
* API构建日期。
*/
public static final String BUILD_DATE = "20180627";
/**
* 调用getSingleInstance方法获取实现类唯一实例时统一使用的静态方法名。
*/
public static final String KEY_ME = "me";
/**
* 用户未指定实现类名称时使用的默认实现类名称。
*/
public static final String DEF_IMPL_NAME = "wee0.impl.DefaultWee0";
// 实现类实例。
private static IWee0 DEF_IMPL = null;
/**
* 获取框架实现类实例对象
*
* @return 框架实现类实例对象
*/
public static IWee0 impl() {
if( null == DEF_IMPL ) {
synchronized( Wee0.class ) {
if( null == DEF_IMPL )
DEF_IMPL = (IWee0)getSingleInstance( IEnv.KEY_WEE0_IMPL, DEF_IMPL_NAME );
}
}
return DEF_IMPL;
}
/**
* 获取指定类型对象的单一实例。
*
* @param key 配置关键字
* @param defImplName 默认的实现类完整名称
* @return 实现类的单一实例。
*/
public static final Object getSingleInstance( String key, String defImplName ) {
if( null == key || 0 == ( key = key.trim() ).length() )
return null;
String implName = System.getProperty( key, defImplName );
if( null == implName || 0 == ( implName = implName.trim() ).length() )
return null;
try {
Class> implCla = Class.forName( implName );
if( null == implCla )
return null;
Method meMethod = implCla.getDeclaredMethod( KEY_ME );
// 通过静态方法"me"获取实现类实例单例对象。方法签名为:public static T me(){} ;
return meMethod.invoke( null );
}catch( ClassNotFoundException | SecurityException | IllegalAccessException | IllegalArgumentException | NoSuchMethodException | InvocationTargetException e ) {
throw new RuntimeException( e );
}
}
} // end class