下面列出了怎么用org.hibernate.service.spi.ServiceException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
@SuppressWarnings( {"unchecked"})
public PersisterFactory initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
final Object customImpl = configurationValues.get( IMPL_NAME );
if ( customImpl == null ) {
return new PersisterFactoryImpl();
}
if ( PersisterFactory.class.isInstance( customImpl ) ) {
return (PersisterFactory) customImpl;
}
final Class<? extends PersisterFactory> customImplClass = Class.class.isInstance( customImpl )
? ( Class<? extends PersisterFactory> ) customImpl
: locate( registry, customImpl.toString() );
try {
return customImplClass.newInstance();
}
catch (Exception e) {
throw new ServiceException( "Could not initialize custom PersisterFactory impl [" + customImplClass.getName() + "]", e );
}
}
@Override
@SuppressWarnings( {"unchecked"})
public PersisterClassResolver initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
final Object customImpl = configurationValues.get( IMPL_NAME );
if ( customImpl == null ) {
return new StandardPersisterClassResolver();
}
if ( PersisterClassResolver.class.isInstance( customImpl ) ) {
return (PersisterClassResolver) customImpl;
}
final Class<? extends PersisterClassResolver> customImplClass = Class.class.isInstance( customImpl )
? (Class<? extends PersisterClassResolver>) customImpl
: locate( registry, customImpl.toString() );
try {
return customImplClass.newInstance();
}
catch (Exception e) {
throw new ServiceException( "Could not initialize custom PersisterClassResolver impl [" + customImplClass.getName() + "]", e );
}
}
private Driver loadDriverIfPossible(String driverClassName) {
if ( driverClassName == null ) {
log.debug( "No driver class specified" );
return null;
}
if ( serviceRegistry != null ) {
final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
final Class<Driver> driverClass = classLoaderService.classForName( driverClassName );
try {
return driverClass.newInstance();
}
catch ( Exception e ) {
throw new ServiceException( "Specified JDBC Driver " + driverClassName + " could not be loaded", e );
}
}
try {
return (Driver) Class.forName( driverClassName ).newInstance();
}
catch ( Exception e1 ) {
throw new ServiceException( "Specified JDBC Driver " + driverClassName + " could not be loaded", e1 );
}
}
@Override
public BatchBuilder initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
final Object builder = configurationValues.get( BUILDER );
if ( builder == null ) {
return new BatchBuilderImpl(
ConfigurationHelper.getInt( Environment.STATEMENT_BATCH_SIZE, configurationValues, 1 )
);
}
if ( BatchBuilder.class.isInstance( builder ) ) {
return (BatchBuilder) builder;
}
final String builderClassName = builder.toString();
try {
return (BatchBuilder) registry.getService( ClassLoaderService.class ).classForName( builderClassName ).newInstance();
}
catch (Exception e) {
throw new ServiceException( "Could not build explicit BatchBuilder [" + builderClassName + "]", e );
}
}
/**
* 业务层需要自己声明异常的情况
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(ServiceException.class)
public String handleServiceException(ServiceException e, Model model) {
logger.error("业务逻辑异常", e);
String message = "【业务逻辑异常】" + e.getMessage();
model.addAttribute("message", message);
return viewName;
}
/**
* 业务层需要自己声明异常的情况
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(ServiceException.class)
public String handleServiceException(ServiceException e, Model model) {
logger.error("业务逻辑异常", e);
String message = "【业务逻辑异常】" + e.getMessage();
model.addAttribute("message", message);
return viewName;
}
/**
* 处理激活
*/
@ApiOperation(value = "处理激活", notes = "处理激活", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RequestMapping(value = "validateEmail", method = RequestMethod.POST)
public Result validateEmail(@RequestBody UserModel user
) throws ServiceException {
//数据访问层,通过email获取用户信息
UserModel userModel = service.findUserByEmail(user.getEmail());
if (userModel != null) {
return new Result(CodeConst.USER_REPEAT.getResultCode(), CodeConst.USER_REPEAT.getMessage());
}
//验证码是否过期
if (user.getRegisterTime() + TimeUtil.ONE_DAY_IN_MILLISECONDS < TimeUtil.getNowOfMills()) {
LOGGER.info("用户{}使用己过期的激活码{}激活邮箱失败!", user.getEmail(), user.getEmail());
return new Result(CodeConst.TIME_PASSED.getResultCode(), CodeConst.TIME_PASSED.getMessage());
}
//激活
String salt = RandomUtil.createSalt();
userModel = new UserModel();
userModel.setNickName(user.getNickName());
userModel.setEmail(user.getEmail());
userModel.setGender(GenderConst.SECRET);
userModel.setValidateCode(Md5Util.encode(user.getEmail(), salt));
userModel.setPhone(0L);
userModel.setSalt(salt);
userModel.setAddress("");
userModel.setPassword(Md5Util.encode(user.getPassword(), salt));
userModel = service.addUser(userModel);
LOGGER.info("用户{}使用激活码{}激活邮箱成功!", userModel.getEmail(), userModel.getValidateCode());
return new Result<>(userModel);
}
@Override
@SuppressWarnings( {"unchecked"})
public MultiTenantConnectionProvider initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
final MultiTenancyStrategy strategy = MultiTenancyStrategy.determineMultiTenancyStrategy( configurationValues );
if ( !strategy.requiresMultiTenantConnectionProvider() ) {
// nothing to do, but given the separate hierarchies have to handle this here.
return null;
}
final Object configValue = configurationValues.get( AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER );
if ( configValue == null ) {
// if they also specified the data source *name*, then lets assume they want
// DataSourceBasedMultiTenantConnectionProviderImpl
final Object dataSourceConfigValue = configurationValues.get( AvailableSettings.DATASOURCE );
if ( dataSourceConfigValue != null && String.class.isInstance( dataSourceConfigValue ) ) {
return new DataSourceBasedMultiTenantConnectionProviderImpl();
}
return null;
}
if ( MultiTenantConnectionProvider.class.isInstance( configValue ) ) {
return (MultiTenantConnectionProvider) configValue;
}
else {
final Class<MultiTenantConnectionProvider> implClass;
if ( Class.class.isInstance( configValue ) ) {
implClass = (Class) configValue;
}
else {
final String className = configValue.toString();
final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class );
try {
implClass = classLoaderService.classForName( className );
}
catch (ClassLoadingException cle) {
log.warn( "Unable to locate specified class [" + className + "]", cle );
throw new ServiceException( "Unable to locate specified multi-tenant connection provider [" + className + "]" );
}
}
try {
return implClass.newInstance();
}
catch (Exception e) {
log.warn( "Unable to instantiate specified class [" + implClass.getName() + "]", e );
throw new ServiceException( "Unable to instantiate specified multi-tenant connection provider [" + implClass.getName() + "]" );
}
}
}
@Override
public <R extends Service> R initiateService(ServiceInitiator<R> serviceInitiator) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public <R extends Service> void configureService(ServiceBinding<R> binding) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public <R extends Service> void injectDependencies(ServiceBinding<R> binding) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public <R extends Service> void startService(ServiceBinding<R> binding) {
throw new ServiceException( "Boot-strap registry should only contain provided services" );
}
@Override
public void setServiceHandler(IServiceHandler handler) {
throw new ServiceException("Unsupported operation");
}