您的位置:首页 > 其它

ofbiz中的factory模式运用

2015-06-19 13:08 267 查看
在ofbiz中大量使用了工厂模式,在使用工厂模式的同时使用了缓存模式,如如下生成数据操作工厂类
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you 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 org.ofbiz.entity.datasource;

import java.util.HashMap;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.config.model.Datasource;
import org.ofbiz.entity.config.EntityConfigUtil;

/**
* Generic Entity Helper Factory Class
* 数据存取帮助类工厂,此处会缓存会存放三个帮助类对象(定义在framework\entity\config\entityengine.xml)
* 1.<group-map group-name="org.ofbiz" datasource-name="localmysql"/>
* 2.<group-map group-name="org.ofbiz.olap" datasource-name="localmysqlolap"/>
* 3.<group-map group-name="org.ofbiz.tenant" datasource-name="localmysqltenant"/>
*/
public class GenericHelperFactory {

public static final String module = GenericHelperFactory.class.getName();

// protected static UtilCache helperCache = new UtilCache("entity.GenericHelpers", 0, 0);
//静态缓存对象
protected static Map<String, GenericHelper> helperCache = new HashMap<String, GenericHelper>();

public static GenericHelper getHelper(GenericHelperInfo helperInfo) {
GenericHelper helper = helperCache.get(helperInfo.getHelperFullName()); //查找帮助类是否存在于缓存中

if (helper == null) { // don't want to block here 同步块,防止重复定义
synchronized (GenericHelperFactory.class) {
// must check if null again as one of the blocked threads can still enter
helper = helperCache.get(helperInfo.getHelperFullName());
if (helper == null) {
try {
Datasource datasourceInfo = EntityConfigUtil.getDatasource(helperInfo.getHelperBaseName());//获取数据源定义

if (datasourceInfo == null) {
throw new IllegalStateException("Could not find datasource definition with name " + helperInfo.getHelperBaseName());
}
String helperClassName = datasourceInfo.getHelperClass();
Class<?> helperClass = null;

if (UtilValidate.isNotEmpty(helperClassName)) {
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
helperClass = loader.loadClass(helperClassName); //加载帮助类。此处是GenericHelperDAO
} catch (ClassNotFoundException e) {
Debug.logWarning(e, module);
throw new IllegalStateException("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage());
}
}

Class<?>[] paramTypes = new Class<?>[] {GenericHelperInfo.class};
Object[] params = new Object[] {helperInfo};

java.lang.reflect.Constructor<?> helperConstructor = null;

if (helperClass != null) {
try {
helperConstructor = helperClass.getConstructor(paramTypes);//构造方法
} catch (NoSuchMethodException e) {
Debug.logWarning(e, module);
throw new IllegalStateException("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage());
}
}
try {
helper = (GenericHelper) helperConstructor.newInstance(params);
} catch (IllegalAccessException e) {
Debug.logWarning(e, module);
throw new IllegalStateException("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage());
} catch (InstantiationException e) {
Debug.logWarning(e, module);
throw new IllegalStateException("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage());
} catch (java.lang.reflect.InvocationTargetException e) {
Debug.logWarning(e, module);
throw new IllegalStateException("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage());
}

if (helper != null)
helperCache.put(helperInfo.getHelperFullName(), helper);//放入缓存
} catch (SecurityException e) {
Debug.logError(e, module);
throw new IllegalStateException("Error loading GenericHelper class: " + e.toString());
}
}
}
}
return helper;
}
}


本文出自 “沧海龙腾” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: