001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.transaction;
019:
020: import java.util.HashMap;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import org.apache.avalon.framework.container.ContainerUtil;
027: import org.apache.avalon.framework.logger.AbstractLogEnabled;
028: import org.apache.avalon.framework.logger.Logger;
029:
030: /**
031: * Identity map implementation.
032: *
033: * @version $Id: IdentityMapImpl.java 473861 2006-11-12 03:51:14Z gregor $
034: */
035: public final class IdentityMapImpl extends AbstractLogEnabled implements
036: IdentityMap {
037:
038: private Map maps = new HashMap();
039:
040: /**
041: * Ctor.
042: * @param logger The logger.
043: */
044: public IdentityMapImpl(Logger logger) {
045: ContainerUtil.enableLogging(this , logger);
046: }
047:
048: public Object get(IdentifiableFactory factory, String key) {
049: String type = factory.getType();
050: Map map = (Map) this .maps.get(type);
051: if (map == null) {
052: map = new HashMap();
053: this .maps.put(type, map);
054: }
055: Object object = map.get(key);
056:
057: if (getLogger().isDebugEnabled())
058: getLogger().debug(
059: "IdentityMapImpl::get() looked up type [" + type
060: + "], key [" + key
061: + "] in map, is it there ? "
062: + (object != null));
063:
064: if (object == null) {
065: try {
066: object = factory.build(this , key);
067: } catch (Exception e) {
068: throw new RuntimeException(e);
069: }
070: map.put(key, object);
071: }
072: return object;
073: }
074:
075: private UnitOfWork unitOfWork;
076:
077: /**
078: * @see org.apache.lenya.transaction.IdentityMap#getUnitOfWork()
079: */
080: public UnitOfWork getUnitOfWork() {
081: return this .unitOfWork;
082: }
083:
084: /**
085: * @see org.apache.lenya.transaction.IdentityMap#setUnitOfWork(org.apache.lenya.transaction.UnitOfWork)
086: */
087: public void setUnitOfWork(UnitOfWork unit) {
088: this .unitOfWork = unit;
089: }
090:
091: /**
092: * @see org.apache.lenya.transaction.IdentityMap#getObjects()
093: */
094: public Object[] getObjects() {
095: Set objects = new HashSet();
096: for (Iterator i = this .maps.values().iterator(); i.hasNext();) {
097: Map map = (Map) i.next();
098: for (Iterator j = map.values().iterator(); j.hasNext();) {
099: objects.add(j.next());
100: }
101: }
102: return (Object[]) objects.toArray(new Object[objects.size()]);
103: }
104:
105: }
|