bug TDI-22580 fixed : Hbase connection wizard: No db type and version option

git-svn-id: http://talendforge.org/svn/tos/trunk@94194 f6f1c999-d317-4740-80b0-e6d1abc6f99e
This commit is contained in:
ycbai
2012-11-22 07:50:27 +00:00
parent ae97890240
commit 5972f0cec4
10 changed files with 371 additions and 48 deletions

View File

@@ -0,0 +1,146 @@
// ============================================================================
//
// Copyright (C) 2006-2012 Talend Inc. - www.talend.com
//
// This source code is available under agreement available at
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
//
// You should have received a copy of the agreement
// along with this program; if not, write to Talend SA
// 9 rue Pages 92150 Suresnes, France
//
// ============================================================================
package org.talend.core.classloader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.talend.commons.ui.runtime.exception.ExceptionHandler;
import org.talend.commons.utils.workbench.resources.ResourceUtils;
import org.talend.core.GlobalServiceRegister;
import org.talend.core.ILibraryManagerService;
import org.talend.core.model.general.Project;
import org.talend.repository.ProjectManager;
import org.talend.utils.io.FilesUtils;
/**
* DOC ycbai class global comment. Detailled comment
*/
public class ClassLoaderFactory {
private static Logger log = Logger.getLogger(ClassLoaderFactory.class);
private final static String EXTENSION_POINT_ID = "org.talend.core.runtime.classloader_provider"; //$NON-NLS-1$
private static IConfigurationElement[] configurationElements = null;
private final static String SEPARATOR = ";"; //$NON-NLS-1$
private final static String PATH_SEPARATOR = "/"; //$NON-NLS-1$
private static Map<String, DynamicClassLoader> classLoadersMap = null;
private static final String INDEX_ATTR = "index"; //$NON-NLS-1$
private static final String LIB_ATTR = "libraries"; //$NON-NLS-1$
static {
IExtensionRegistry registry = Platform.getExtensionRegistry();
configurationElements = registry.getConfigurationElementsFor(EXTENSION_POINT_ID);
}
public static ClassLoader getClassLoader(String index) {
if (classLoadersMap == null) {
initClassLoaders();
}
return classLoadersMap.get(index);
}
private static void initClassLoaders() {
File tmpFolder = getTmpFolder();
if (tmpFolder.exists()) {
FilesUtils.removeFolder(tmpFolder, true);
}
classLoadersMap = new HashMap<String, DynamicClassLoader>();
for (IConfigurationElement current : configurationElements) {
String index = current.getAttribute(INDEX_ATTR);
String libraries = current.getAttribute(LIB_ATTR);
if (StringUtils.isNotEmpty(index)) {
DynamicClassLoader classLoader = new DynamicClassLoader();
if (StringUtils.isNotEmpty(libraries)) {
String[] librariesArray = libraries.split(SEPARATOR);
List<String> jarPathList = retrieveJarPaths(librariesArray);
classLoader.addLibraries(jarPathList);
}
classLoadersMap.put(index, classLoader);
}
}
}
private static List<String> retrieveJarPaths(String[] driversArray) {
List<String> jarPathList = new ArrayList<String>();
if (driversArray == null || driversArray.length == 0) {
return jarPathList;
}
ILibraryManagerService librairesManagerService = (ILibraryManagerService) GlobalServiceRegister.getDefault().getService(
ILibraryManagerService.class);
String libPath = getLibPath();
for (String driverName : driversArray) {
String jarPath = libPath + PATH_SEPARATOR + driverName;
File jarFile = new File(jarPath);
if (!jarFile.exists()) {
boolean retrieved = librairesManagerService.retrieve(driverName, libPath, new NullProgressMonitor());
if (!retrieved) {
log.warn("Cannot retrieve lib: " + driverName);
}
}
jarPathList.add(jarFile.getAbsolutePath());
}
return jarPathList;
}
private static String getLibPath() {
File tmpFolder = getTmpFolder();
if (!tmpFolder.exists()) {
tmpFolder.mkdirs();
}
try {
tmpFolder = File.createTempFile("libs", null, tmpFolder); //$NON-NLS-1$
if (tmpFolder.exists() && tmpFolder.isFile()) {
tmpFolder.delete();
tmpFolder.mkdirs();
}
} catch (IOException e) {
}
return tmpFolder.getAbsolutePath();
}
private static File getTmpFolder() {
Project project = ProjectManager.getInstance().getCurrentProject();
IProject physProject;
String tmpFolderPath = System.getProperty("user.dir"); //$NON-NLS-1$
try {
physProject = ResourceUtils.getProject(project.getTechnicalLabel());
tmpFolderPath = physProject.getFolder("temp").getLocation().toPortableString(); //$NON-NLS-1$
} catch (Exception e) {
ExceptionHandler.process(e);
}
tmpFolderPath = tmpFolderPath + "/libraries"; //$NON-NLS-1$
return new File(tmpFolderPath);
}
}

View File

@@ -0,0 +1,54 @@
// ============================================================================
//
// Copyright (C) 2006-2012 Talend Inc. - www.talend.com
//
// This source code is available under agreement available at
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
//
// You should have received a copy of the agreement
// along with this program; if not, write to Talend SA
// 9 rue Pages 92150 Suresnes, France
//
// ============================================================================
package org.talend.core.classloader;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import org.talend.commons.ui.runtime.exception.ExceptionHandler;
/**
* DOC ycbai class global comment. Detailled comment
*/
public class DynamicClassLoader extends URLClassLoader {
/**
* DOC ycbai DynamicClassLoader constructor comment.
*/
public DynamicClassLoader() {
super(new URL[0], DynamicClassLoader.class.getClassLoader());
}
public void addLibraries(String lib) {
if (lib != null) {
File libFile = new File(lib);
try {
addURL(libFile.toURL());
} catch (MalformedURLException e) {
ExceptionHandler.process(e);
}
}
}
public void addLibraries(List<String> libs) {
if (libs != null && libs.size() > 0) {
for (String lib : libs) {
addLibraries(lib);
}
}
}
}

View File

@@ -0,0 +1,276 @@
// ============================================================================
//
// Copyright (C) 2006-2012 Talend Inc. - www.talend.com
//
// This source code is available under agreement available at
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
//
// You should have received a copy of the agreement
// along with this program; if not, write to Talend SA
// 9 rue Pages 92150 Suresnes, France
//
// ============================================================================
package org.talend.core.utils;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
/**
* DOC ycbai class global comment. Detailled comment
*
* General reflection utils.
*
*/
public class ReflectionUtils {
private static Logger log = Logger.getLogger(ReflectionUtils.class.getName());
/**
* DOC ycbai Comment method "getPublicField".
*
* Returns the value of a public field.
*
* @param owner
* @param fieldName
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws Exception
*/
public static Object getPublicField(Object owner, String fieldName) throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
Object fieldValue = null;
Class ownerClass = owner.getClass();
Field field = ownerClass.getField(fieldName);
fieldValue = field.get(owner);
return fieldValue;
}
/**
* DOC ycbai Comment method "getPrivateField".
*
* Returns the value of a private field.
*
* @param owner
* @param fieldName
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static Object getPrivateField(Object owner, String fieldName) throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
Object fieldValue = null;
Class ownerClass = owner.getClass();
Field f = ownerClass.getDeclaredField(fieldName);
f.setAccessible(true);
fieldValue = f.get(owner);
return fieldValue;
}
/**
* DOC ycbai Comment method "getStaticField".
*
* Returns the value of a static field.
*
* @param className
* @param loader
* @param fieldName
* @return
* @throws ClassNotFoundException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static Object getStaticField(String className, ClassLoader loader, String fieldName) throws ClassNotFoundException,
SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
Object fieldValue = null;
Class ownerClass = null;
if (loader != null) {
ownerClass = Class.forName(className, true, loader);
} else {
ownerClass = Class.forName(className);
}
Field field = ownerClass.getField(fieldName);
fieldValue = field.get(ownerClass);
return fieldValue;
}
/**
* To set the field using the given <code>value</code>. Added by Marvin Wang on Oct 19, 2012.
*/
public static void setStaticFieldValue(String className, ClassLoader loader, String fieldName, Object value) {
try {
Class ownerClass = null;
if (loader != null) {
ownerClass = Class.forName(className, true, loader);
} else {
ownerClass = Class.forName(className);
}
Field field = ownerClass.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(null, value);
} catch (Exception e) {
log.error("", e);
}
}
public static Object getStaticField(String className, String fieldName) throws SecurityException, IllegalArgumentException,
ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
return getStaticField(className, null, fieldName);
}
/**
* DOC ycbai Comment method "invokeMethod".
*
* Returns the value of a method.
*
* @param owner
* @param methodName
* @param args
* @return
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static Object invokeMethod(Object owner, String methodName, Object[] args) throws SecurityException,
NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Object returnValue = null;
Class ownerClass = owner.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
returnValue = method.invoke(owner, args);
return returnValue;
}
/**
* DOC ycbai Comment method "invokeStaticMethod".
*
* Returns the value of a static method.
*
* @param className
* @param loader
* @param methodName
* @param args
* @return
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static Object invokeStaticMethod(String className, ClassLoader loader, String methodName, Object[] args)
throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
Object returnValue = null;
Class ownerClass = null;
if (loader != null) {
ownerClass = Class.forName(className, true, loader);
} else {
ownerClass = Class.forName(className);
}
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
returnValue = method.invoke(null, args);
return returnValue;
}
public static Object invokeStaticMethod(String className, String methodName, Object[] args) throws SecurityException,
IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
return invokeStaticMethod(className, null, methodName, args);
}
/**
* DOC ycbai Comment method "newInstance".
*
* Create a new instance of a class.
*
* @param className
* @param initialize
* @param loader
* @param args
* @return
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws IllegalArgumentException
*/
public static Object newInstance(String className, ClassLoader loader, Object[] args) throws ClassNotFoundException,
SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException,
InvocationTargetException {
Object instance = null;
Class newClass = null;
if (loader != null) {
newClass = Class.forName(className, true, loader);
} else {
newClass = Class.forName(className);
}
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Constructor cons = newClass.getConstructor(argsClass);
instance = cons.newInstance(args);
return instance;
}
public static Object newInstance(String className, Object[] args) throws SecurityException, IllegalArgumentException,
ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException,
InvocationTargetException {
return newInstance(className, null, args);
}
/**
* DOC ycbai Comment method "isInstance".
*
* Whether or not the object is the instance of the class.
*
* @param obj
* @param cls
* @return
*/
public static boolean isInstance(Object obj, Class cls) {
return cls.isInstance(obj);
}
/**
* DOC ycbai Comment method "getByArray".
*
* Returns the value of a element of a array by the index.
*
* @param array
* @param index
* @return
*/
public static Object getByArray(Object array, int index) {
return Array.get(array, index);
}
}