The application of factory pattern in OFBiz

Source: Internet
Author: User

Factory mode is used extensively in OFBiz, while using the Factory mode, the cache mode is used, as follows to generate data Operation factory class

/******************************************************************************* * 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 *  data Access Help class factory, where the cache will hold threeHelp class object (defined in 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);     //static Cache Object     protected static  Map<String, GenericHelper> helperCache = new HashMap<String,  Generichelper> ();     public static generichelper gethelper ( Generichelperinfo&nbsP;helperinfo)  {        GenericHelper helper =  Helpercache.get (Helperinfo.gethelperfullname ());  //find help class exists in cache          if  (helper == null)  { // don ' t want to block  here  Sync block to prevent duplicate definitions             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 ());//Get Data source Definition                          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);  //loads the Help class. Here is 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) ;//Construction Method                              } 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);//Put Cache                      } catch  (securityexception e)  {                          debug.logerror (E, module);                         throw new  IllegalStateException ("error loading generichelper class: "  + e.toString ());                      }                }             }        }         return helper;    }}


This article from the "Sea Dragon" blog, declined reprint!

The application of factory pattern in OFBiz

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.