📄 abstractbasemanager.java
字号:
/** * * @param id * @return * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ protected abstract Persistent retrieveStoredOM(ObjectKey id) throws TorqueException; /** * Gets a list of om's based on id's. * * @param ids a <code>ObjectKey[]</code> value * @return a <code>List</code> value * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ protected List getOMs(ObjectKey[] ids) throws TorqueException { return getOMs(Arrays.asList(ids)); } /** * Gets a list of om's based on id's. * * @param ids a <code>List</code> of <code>ObjectKey</code>'s * @return a <code>List</code> value * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ protected List getOMs(List ids) throws TorqueException { return getOMs(ids, true); } /** * Gets a list of om's based on id's. * * @param ids a <code>List</code> of <code>ObjectKey</code>'s * @return a <code>List</code> value * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ protected List getOMs(List ids, boolean fromCache) throws TorqueException { List oms = null; if (ids != null && ids.size() > 0) { // start a new list where we will replace the id's with om's oms = new ArrayList(ids); List newIds = new ArrayList(ids.size()); for (int i = 0; i < ids.size(); i++) { ObjectKey key = (ObjectKey) ids.get(i); Persistent om = null; if (fromCache) { om = cacheGet(key); } if (om == null) { newIds.add(key); } else { oms.set(i, om); } } if (newIds.size() > 0) { List newOms = retrieveStoredOMs(newIds); for (int i = 0; i < oms.size(); i++) { if (oms.get(i) instanceof ObjectKey) { for (int j = newOms.size() - 1; j >= 0; j--) { Persistent om = (Persistent) newOms.get(j); if (om.getPrimaryKey().equals(oms.get(i))) { // replace the id with the om and add the om // to the cache oms.set(i, om); newOms.remove(j); if (fromCache) { putInstanceImpl(om); } break; } } } } } } return oms; } /** * * @param ids * @return * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ protected abstract List retrieveStoredOMs(List ids) throws TorqueException; /** * Get the value of region. * * @return value of region. */ public String getRegion() { return region; } /** * Set the value of region. * * @param v Value to assign to region. * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ public void setRegion(String v) throws TorqueException { this.region = v; try { if (Torque.getConfiguration().getBoolean(Torque.CACHE_KEY)) { cache = JCS.getInstance(getRegion()); mrCache = new MethodResultCache(cache); } else { mrCache = new NoOpMethodResultCache(cache); } } catch (Exception e) { throw new TorqueException("Cache could not be initialized", e); } if (cache == null) { log.info("Cache could not be initialized for region: " + v); } } /** * @return The cache instance. */ public MethodResultCache getMethodResultCache() { if (isNew) { synchronized (this) { if (isNew) { registerAsListener(); isNew = false; } } } return mrCache; } /** * NoOp version. Managers should override this method to notify other * managers that they are interested in CacheEvents. */ protected void registerAsListener() { } /** * * @param listener A new listener for cache events. */ public void addCacheListenerImpl(CacheListener listener) { List keys = listener.getInterestedFields(); Iterator i = keys.iterator(); while (i.hasNext()) { String key = (String) i.next(); // Peer.column names are the fields if (validFields != null && validFields.containsKey(key)) { List listeners = (List) listenersMap.get(key); if (listeners == null) { listeners = createSubsetList(key); } boolean isNew = true; Iterator j = listeners.iterator(); while (j.hasNext()) { Object listener2 = ((WeakReference) j.next()).get(); if (listener2 == null) { // do a little cleanup while checking for dupes // not thread-safe, not likely to be many nulls // but should revisit //j.remove(); } else if (listener2 == listener) { isNew = false; break; } } if (isNew) { listeners.add(new WeakReference(listener)); } } } } /** * * @param key * @return A subset of the list identified by <code>key</code>. */ private synchronized List createSubsetList(String key) { FastArrayList list = null; if (listenersMap.containsKey(key)) { list = (FastArrayList) listenersMap.get(key); } else { list = new FastArrayList(); list.setFast(true); listenersMap.put(key, list); } return list; } /** * * @param listeners * @param oldOm * @param om */ protected void notifyListeners(List listeners, Persistent oldOm, Persistent om) { if (listeners != null) { synchronized (listeners) { Iterator i = listeners.iterator(); while (i.hasNext()) { CacheListener listener = (CacheListener) ((WeakReference) i.next()).get(); if (listener == null) { // remove reference as its object was cleared i.remove(); } else { if (oldOm == null) { // object was added listener.addedObject(om); } else { // object was refreshed listener.refreshedObject(om); } } } } } } /** * helper methods for the Serializable interface * * @param out * @throws IOException */ private void writeObject(java.io.ObjectOutputStream out) throws IOException { out.defaultWriteObject(); } /** * Helper methods for the <code>Serializable</code> interface. * * @param in The stream to read a <code>Serializable</code> from. * @throws IOException * @throws ClassNotFoundException */ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); // initialize the cache try { if (region != null) { setRegion(region); } } catch (Exception e) { log.error("Cache could not be initialized for region '" + region + "' after deserialization"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -