/*
 * Copyright (c) 2016-2016, fastquery.org and/or its affiliates. All rights reserved.
 *
 * 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.
 * 
 * For more information, please see http://www.fastquery.org/.
 * 
 */

package org.fastquery.service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;

import org.apache.log4j.Logger;
import org.fastquery.asm.AsmRepository;
import org.fastquery.core.GenerateRepository;
import org.fastquery.core.Repository;
import org.fastquery.core.RepositoryException;
import org.fastquery.dsm.FastQueryJson;
import org.fastquery.util.LoadPrperties;

/**
 * 这个类在osgi环境中不能调用
 * @author xixifeng (fastquery@126.com)
 */
class GenerateRepositoryImpl implements GenerateRepository {

	private static final Logger LOG = Logger.getLogger(GenerateRepositoryImpl.class);
	
	private static GenerateRepositoryImpl instance;

	private GenerateRepositoryImpl() {
		LOG.debug("GenerateRepositoryImpl 已实例化.");
		
		// 装载配置文件
		Set<FastQueryJson> fqPropertie = LoadPrperties.load(new FQueryResourceImpl());
		
		// 批量生成 Repository 的实现类
		Set<String> basePackages = null;
		for (FastQueryJson fQueryPropertie : fqPropertie) {
			basePackages = fQueryPropertie.getBasePackages();
			for (String basePackage : basePackages) {
				try {
					@SuppressWarnings("unchecked")
					Class<? extends Repository> clazz = (Class<? extends Repository>) Class.forName(basePackage);
					generate(clazz);
				} catch (ClassNotFoundException e) {
					throw new RepositoryException(e.getMessage(),e);
				}
			}
		}
	}

	static GenerateRepositoryImpl getInstance() {
		if (instance == null) {
			// 如果在这儿, 可能有两个线程进入了
			synchronized (GenerateRepositoryImpl.class) {
				if (instance == null) { // 这步不能省!
					instance = new GenerateRepositoryImpl();
				}
			}
		}
		return instance;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T extends Repository> T generate(Class<T> repositoryClazz) {
		String name = repositoryClazz.getName()+ SUFFIX;
		if( FqClassLoader.findLoadedClassByName(name) == null ) {
			
			byte[] bytes = AsmRepository.generateBytes(repositoryClazz);
			// ---- 把字节存储到硬盘 ----------
			try {
				FileOutputStream fileOutputStream = new FileOutputStream("/tmp/tmp-root/" + repositoryClazz.getSimpleName()+SUFFIX +".class");
				fileOutputStream.write(bytes);
				fileOutputStream.flush();
				fileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			// ---- 把字节存储到硬盘 ---------- End
			return (T) new FqClassLoader(this.getClass().getClassLoader()).defineClassByName(name, bytes, 0, bytes.length);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T extends Repository> T getProxyRepository(Class<T> clazz) {
		return (T) FqClassLoader.findLoadedClassByName(clazz.getName() + SUFFIX);
	}
	
}

@@ -993,69 +993,8 @@
 e;%0A%0A
-import java.io.FileOutputStream;%0Aimport java.io.IOException;%0A
 impo
@@ -2800,367 +2800,8 @@
 z);%0A
-%09%09%09// ---- %E6%8A%8A%E5%AD%97%E8%8A%82%E5%AD%98%E5%82%A8%E5%88%B0%E7%A1%AC%E7%9B%98 ----------%0A%09%09%09try %7B%0A%09%09%09%09FileOutputStream fileOutputStream = new FileOutputStream(%22/tmp/tmp-root/%22 + repositoryClazz.getSimpleName()+SUFFIX +%22.class%22);%0A%09%09%09%09fileOutputStream.write(bytes);%0A%09%09%09%09fileOutputStream.flush();%0A%09%09%09%09fileOutputStream.close();%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%09// ---- %E6%8A%8A%E5%AD%97%E8%8A%82%E5%AD%98%E5%82%A8%E5%88%B0%E7%A1%AC%E7%9B%98 ---------- End
 %0A%09%09%09

update core