
/*******************************************************************************
 * Copyright (c) 2004, 2007 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/
package org.eclipse.birt.data.engine.olap.data.api.cube;

import java.io.IOException;
import java.util.Collection;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.birt.data.engine.olap.data.document.IDocumentManager;

/**
 * This class is used to maps data engine key to documentManager.
 */

public class DocManagerMap
{
	private static DocManagerMap instance = new DocManagerMap( );
	
	private Map map = new Hashtable( );
	
	protected static Logger logger = Logger.getLogger( DocManagerMap.class.getName( ) );
	/**
	 * 
	 * @return
	 */
	public static DocManagerMap getDocManagerMap( )
	{
		return instance;
	}
	
	/**
	 * 
	 * @param key
	 * @param manager
	 */
	public void set( String dataEngineKey, String key, IDocumentManager manager )
	{
		Map docManagerMap = null;
		if ( map.containsKey( dataEngineKey ) )
		{
			docManagerMap = (Map) map.get( dataEngineKey );
		}
		else
		{
			docManagerMap = new HashMap( );
			map.put( dataEngineKey, docManagerMap );
		}
		docManagerMap.put( key, manager );
	}
	
	/**
	 * 
	 * @param key
	 * @return
	 */
	public IDocumentManager get( String dataEngineKey, String key )
	{
		Map docManagerMap = null;
		if ( !map.containsKey( dataEngineKey ) )
		{
			return null;
		}
		docManagerMap = (Map) map.get( dataEngineKey );
		if ( !docManagerMap.containsKey( key ) )
		{
			return null;
		}
		return (IDocumentManager) docManagerMap.get( key );
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.birt.data.engine.api.IShutdownListener#dataEngineShutdown(java.lang.String)
	 */
	public void close( String dataEngineKey )
	{
		Map docManagerMap = null;
		if ( !map.containsKey( dataEngineKey ) )
			return;
		docManagerMap = (Map) map.get( dataEngineKey );
		Collection docManagers = docManagerMap.values( );
		if( docManagers == null || docManagers.size( ) == 0 )
			return;
		Iterator docManagerIterator = docManagers.iterator( );
		while( docManagerIterator.hasNext( ) )
		{
			try
			{
				((IDocumentManager)docManagerIterator.next( )).close( );
			}
			catch ( IOException e )
			{
				e.printStackTrace();
				logger.log( Level.WARNING, "IOException is thrown when document manage is closed!" );
			}
		}
		docManagerMap.clear( );
		map.remove( dataEngineKey );
	}

}

@@ -1070,34 +1070,127 @@
 ate 
-Map map = new Hashtable( )
+ThreadLocal%3CMap%3E tmap = new ThreadLocal%3CMap%3E( ) %7B%0A%0A%09%09protected Map initialValue( )%0A%09%09%7B%0A%09%09%09return new HashMap();%0A%09%09%7D%0A%0A%09%7D
 ;%0A%09%0A
@@ -1533,24 +1533,49 @@
 Map = null;%0A
+%09%09Map map = tmap.get( );%0A
 %09%09if ( map.c
@@ -1929,32 +1929,57 @@
 agerMap = null;%0A
+%09%09Map map = tmap.get( );%0A
 %09%09if ( !map.cont
@@ -2403,16 +2403,41 @@
 = null;%0A
+%09%09Map map = tmap.get( );%0A
 %09%09if ( !
@@ -3013,16 +3013,16 @@
 ear( );%0A
-
 %09%09map.re
@@ -3044,14 +3044,32 @@
 eKey );%0A
+%09%09tmap.remove( );%0A
 %09%7D%0A%0A%7D%0A

Fix concurrent modification of doc manager map. [44384]