📄 abstractbasemanager.java
字号:
package org.apache.torque.manager;/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Turbine" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache Turbine", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */import java.lang.ref.WeakReference;import java.util.Arrays;import java.util.List;import java.util.ArrayList;import java.util.Map;import java.util.HashMap;import java.util.Iterator;import java.io.Serializable;import java.io.IOException;import java.io.ObjectInputStream;import org.apache.commons.collections.FastArrayList;import org.apache.jcs.JCS;import org.apache.jcs.access.GroupCacheAccess;import org.apache.jcs.access.exception.CacheException;import org.apache.torque.Torque;import org.apache.torque.TorqueException;import org.apache.torque.om.ObjectKey;import org.apache.torque.om.Persistent;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * This class contains common functionality of a Manager for * instantiating OM's. * * @author <a href="mailto:jmcnally@collab.net">John McNally</a> * @version $Id: AbstractBaseManager.java,v 1.16 2003/06/20 08:13:47 dlr Exp $ */public abstract class AbstractBaseManager implements Serializable{ /** the log */ protected static Log log = LogFactory.getLog(AbstractBaseManager.class); /** used to cache the om objects. cache is set by the region property */ protected transient GroupCacheAccess cache; /** method results cache */ protected MethodResultCache mrCache; /** the class that the service will instantiate */ private Class omClass; private String className; private String region; private boolean isNew = true; protected Map validFields; protected Map listenersMap = new HashMap(); /** * Get the Class instance * * @return the om class */ protected Class getOMClass() { return omClass; } /** * Set the Class that will be instantiated by this manager * * @param omClass the om class */ protected void setOMClass(Class omClass) { this.omClass = omClass; } /** * Get a fresh instance of an om * * @return an instance of the om class * @throws InstantiationException * @throws IllegalAccessException */ protected Persistent getOMInstance() throws InstantiationException, IllegalAccessException { return (Persistent) omClass.newInstance(); } /** * Get the classname to instantiate for getInstance() * @return value of className. */ public String getClassName() { return className; } /** * Set the classname to instantiate for getInstance() * @param v Value to assign to className. * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ public void setClassName(String v) throws TorqueException { this.className = v; try { setOMClass(Class.forName(getClassName())); } catch (ClassNotFoundException cnfe) { throw new TorqueException("Could not load " + getClassName()); } } /** * Return an instance of an om based on the id * * @param id * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ protected Persistent getOMInstance(ObjectKey id) throws TorqueException { return getOMInstance(id, true); } /** * Return an instance of an om based on the id * * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ protected Persistent getOMInstance(ObjectKey key, boolean fromCache) throws TorqueException { Persistent om = null; if (fromCache) { om = cacheGet(key); } if (om == null) { om = retrieveStoredOM(key); if (fromCache) { putInstanceImpl(om); } } return om; } protected Persistent cacheGet(Serializable key) { Persistent om = null; if (cache != null) { synchronized (this) { om = (Persistent) cache.get(key); } } return om; } /** * * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ protected void clearImpl() throws TorqueException { if (cache != null) { try { cache.remove(); } catch (CacheException ce) { throw new TorqueException( "Could not clear cache due to internal JCS error.", ce); } } } /** * * @param key * @return * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ protected Persistent removeInstanceImpl(Serializable key) throws TorqueException { Persistent oldOm = null; if (cache != null) { try { synchronized (this) { oldOm = (Persistent) cache.get(key); cache.remove(key); } } catch (CacheException ce) { throw new TorqueException ("Could not remove from cache due to internal JCS error", ce); } } return oldOm; } /** * * @param om * @return * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ protected Persistent putInstanceImpl(Persistent om) throws TorqueException { ObjectKey key = om.getPrimaryKey(); return putInstanceImpl(key, om); } /** * * @param key * @param om * @return * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ protected Persistent putInstanceImpl(Serializable key, Persistent om) throws TorqueException { if (getOMClass() != null && !getOMClass().isInstance(om)) { throw new TorqueException(om + "; class=" + om.getClass().getName() + "; id=" + om.getPrimaryKey() + " cannot be cached with " + getOMClass().getName() + " objects"); } Persistent oldOm = null; if (cache != null) { try { synchronized (this) { oldOm = (Persistent) cache.get(key); cache.put(key, om); } } catch (CacheException ce) { throw new TorqueException ("Could not cache due to internal JCS error", ce); } } return oldOm; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -